import {
  changeDates,
  diffChangeRequestLines,
  hotelChangeDates,
  travelsLabel,
  travelsWithinDays,
} from './change-request-lines';

describe('change-request-lines', () => {
  const before = {
    transport_items: [
      { id: 't1', date: '2026-11-18', pickup_time: '18:20:00', unit_price: '120.00', notes: null },
      { id: 't2', date: '2026-11-25', pickup_time: '09:00:00', unit_price: '80.00' },
    ],
    activity_items: [{ id: 'a1', date: '2026-11-18', quantity: 2, age_group: 'Adult' }],
    special_items: [{ id: 's1', date: '2026-11-19', service_name: 'Dinner' }],
  };

  describe('diffChangeRequestLines', () => {
    it('returns nothing when the payload re-sends the same lines', () => {
      const payload = {
        transport_items: [
          { id: 't1', date: '2026-11-18', pickup_time: '18:20', unit_price: 120, notes: '' },
          { id: 't2', date: '2026-11-25', pickup_time: '09:00', unit_price: 80, sort_order: 4 },
        ],
      };
      expect(diffChangeRequestLines(before, payload)).toEqual([]);
    });

    it('finds edited, added and removed lines per type', () => {
      const payload = {
        transport_items: [
          { id: 't1', date: '2026-11-18', pickup_time: '20:45' },
          { date: '2026-11-18', pickup_time: '10:00' },
        ],
        activity_items: [],
      };
      expect(diffChangeRequestLines(before, payload)).toEqual([
        { type: 'transport', id: 't1', dates: ['2026-11-18'], change: 'edited' },
        { type: 'transport', id: null, dates: ['2026-11-18'], change: 'added' },
        { type: 'transport', id: 't2', dates: ['2026-11-25'], change: 'removed' },
        { type: 'activity', id: 'a1', dates: ['2026-11-18'], change: 'removed' },
      ]);
    });

    it('keeps both dates when an edit moves the line', () => {
      const payload = { special_items: [{ id: 's1', date: '2026-11-22', service_name: 'Dinner' }] };
      expect(diffChangeRequestLines(before, payload)).toEqual([
        { type: 'special', id: 's1', dates: ['2026-11-22', '2026-11-19'], change: 'edited' },
      ]);
    });

    it('treats activity age-group rows as one line (real request shape)', () => {
      const snap = {
        activity_items: [
          {
            id: 'g-ad',
            activity_id: 'garden',
            date: '2026-11-06',
            age_group: 'adult',
            quantity: 10,
          },
          {
            id: 'g-ch',
            activity_id: 'garden',
            date: '2026-11-06',
            age_group: 'child',
            quantity: 0,
          },
          { id: 'f-ad', activity_id: 'frame', date: '2026-11-07', age_group: 'adult', quantity: 4 },
          { id: 'f-ch', activity_id: 'frame', date: '2026-11-07', age_group: 'child', quantity: 2 },
        ],
      };
      const payload = {
        activity_items: [
          // zero-qty child row left out — not a removal
          {
            id: 'g-ad',
            activity_id: 'garden',
            date: '2026-11-06',
            age_group: 'adult',
            quantity: 10,
          },
          // child age group dropped from a line that stays — an edit
          { id: 'f-ad', activity_id: 'frame', date: '2026-11-07', age_group: 'adult', quantity: 4 },
          // new activity saved as adult + child rows — one added line
          { activity_id: 'village', date: '2026-11-06', age_group: 'adult', quantity: 10 },
          { activity_id: 'village', date: '2026-11-06', age_group: 'child', quantity: 1 },
        ],
      };
      expect(diffChangeRequestLines(snap, payload)).toEqual([
        { type: 'activity', id: null, dates: ['2026-11-06'], change: 'added' },
        { type: 'activity', id: 'f-ch', dates: ['2026-11-07'], change: 'edited' },
      ]);
    });

    it('ignores types the payload leaves out and handles missing snapshots', () => {
      expect(diffChangeRequestLines(before, { hotel_items: [] })).toEqual([]);
      expect(diffChangeRequestLines(null, { transport_items: [] })).toEqual([]);
    });
  });

  describe('travelsWithinDays', () => {
    const line = (d: string) => ({
      type: 'transport' as const,
      id: 'x',
      dates: [d],
      change: 'edited' as const,
    });
    const today = '2026-11-17';

    it('returns days to the earliest change inside the window', () => {
      expect(
        travelsWithinDays([line('2026-11-18'), line('2026-11-17')], 'Asia/Dubai', 1, today),
      ).toBe(0);
      expect(travelsWithinDays([line('2026-11-18')], 'Asia/Dubai', 1, today)).toBe(1);
      expect(travelsWithinDays([line('2026-11-20')], 'Asia/Dubai', 3, today)).toBe(3);
    });

    it('returns null outside the window and for price-only requests', () => {
      expect(travelsWithinDays([line('2026-11-20')], 'Asia/Dubai', 2, today)).toBeNull();
      expect(travelsWithinDays([line('2026-11-18')], 'Asia/Dubai', 0, today)).toBeNull();
      expect(travelsWithinDays([], 'Asia/Dubai', 5, today)).toBeNull();
    });
  });

  it('labels and sorts change dates', () => {
    expect([0, 1, 3].map(travelsLabel)).toEqual(['today', 'tomorrow', 'in 3 days']);
    expect(
      changeDates([
        { type: 'transport', id: 'a', dates: ['2026-11-21', '2026-11-19'], change: 'edited' },
        { type: 'special', id: 'b', dates: ['2026-11-19'], change: 'removed' },
      ]),
    ).toEqual(['2026-11-19', '2026-11-21']);
  });

  it('finds hotel stays the request changes, by check-in date', () => {
    const snap = {
      hotel_items: [
        {
          id: 'h1',
          hotel_id: 'rove',
          check_in_date: '2026-11-20',
          check_out_date: '2026-11-23',
          nights: 3,
          subtotal: '900.00',
        },
        {
          id: 'h2',
          hotel_id: 'jw',
          check_in_date: '2026-11-23',
          check_out_date: '2026-11-25',
          nights: 2,
          subtotal: 700,
        },
      ],
    };
    const same = {
      hotel_items: snap.hotel_items.map((h) => ({ ...h, subtotal: Number(h.subtotal) })),
    };
    expect(hotelChangeDates(snap, same)).toEqual([]);
    expect(hotelChangeDates(snap, { markup_amount: 10 })).toEqual([]);
    const edited = {
      hotel_items: [{ ...snap.hotel_items[0], subtotal: 1100 }, snap.hotel_items[1]],
    };
    expect(hotelChangeDates(snap, edited)).toEqual(['2026-11-20']);
    expect(hotelChangeDates(snap, { hotel_items: [snap.hotel_items[0]] })).toEqual(['2026-11-23']);
  });
});
