import { describe, expect, it } from 'vitest';

import { buildTimelineEvents } from './PropertiesPane';
import type { Property } from '../../types';

function makeProperty(overrides: Partial<Property>): Property {
  return { lat: 51, lon: -0.1, ...overrides } as Property;
}

// Compact, discriminated summary of each event so assertions stay type-safe.
function summarize(property: Property): string[] {
  return buildTimelineEvents(property).map((event) => {
    switch (event.kind) {
      case 'tenure':
        return `tenure:${event.status}:${event.year}`;
      case 'sale':
        return `sale:${event.year}`;
      case 'reno':
        return `reno:${event.event}:${event.year}`;
      case 'built':
        return `built:${event.year}`;
    }
  });
}

describe('buildTimelineEvents', () => {
  it('renders tenure changes newest-first with their status', () => {
    expect(
      summarize(
        makeProperty({
          tenure_history: [
            { year: 2019, status: 'Rented (private)' },
            { year: 2023, status: 'Owner-occupied' },
          ],
        })
      )
    ).toEqual(['tenure:Owner-occupied:2023', 'tenure:Rented (private):2019']);
  });

  it('interleaves tenure changes with sales by year', () => {
    expect(
      summarize(
        makeProperty({
          historical_prices: [
            { year: 2015, month: 6, price: 200_000 },
            // Most recent sale is the card headline, so it is dropped here.
            { year: 2024, month: 1, price: 300_000 },
          ],
          tenure_history: [{ year: 2020, status: 'Rented (private)' }],
        })
      )
    ).toEqual(['tenure:Rented (private):2020', 'sale:2015']);
  });

  it('emits nothing when there is no tenure history', () => {
    expect(summarize(makeProperty({}))).toEqual([]);
  });
});
