import { onlyNewOrChangedLines, SavedLines } from './changed-lines';

describe('onlyNewOrChangedLines', () => {
  const saved: SavedLines = {
    hotel_items: [{ id: 'h1', hotel_id: 'H', subtotal: '500.00' }],
    transport_items: [
      {
        id: 't1',
        transport_service_id: 'S',
        vehicle_type_id: 'V',
        is_shared: false,
        supplier_id: null,
        date: '2026-11-20',
        quantity: 1,
        unit_price: '80.00',
        subtotal: '80.00',
      },
    ],
    activity_items: [
      {
        id: 'a1',
        activity_id: 'TEAMLAB',
        ticket_id: null,
        age_group: 'child',
        date: '2026-11-20',
        quantity: 2,
        unit_price: '59.00',
        subtotal: '118.00',
      },
    ],
    special_items: [{ service_name: 'SIM card', date: '2026-11-20', total_price: '25.00' }],
  };

  const unchanged = {
    hotel_items: [{ id: 'h1', hotel_id: 'H', subtotal: 500 }],
    transport_items: [
      {
        id: 't1',
        transport_service_id: 'S',
        vehicle_type_id: 'V',
        is_shared: false,
        date: '2026-11-20',
        quantity: 1,
        unit_price: 80,
        subtotal: 80,
      },
    ],
    activity_items: [
      {
        id: 'a1',
        activity_id: 'TEAMLAB',
        age_group: 'child',
        date: '2026-11-20',
        quantity: 2,
        unit_price: 59,
        subtotal: 118,
        notes: 'any note',
      },
    ],
    special_items: [{ service_name: 'SIM card', date: '2026-11-20', total_price: 25 }],
  };

  it('drops every untouched line (old price below a new minimum is ignored)', () => {
    const r = onlyNewOrChangedLines(unchanged, saved);
    expect(r.hotel_items).toEqual([]);
    expect(r.transport_items).toEqual([]);
    expect(r.activity_items).toEqual([]);
    expect(r.special_items).toEqual([]);
  });

  it('keeps a new line (no id)', () => {
    const safari = {
      activity_id: 'SAFARI',
      age_group: 'adult',
      date: '2026-11-22',
      unit_price: 50,
    };
    const r = onlyNewOrChangedLines(
      { ...unchanged, activity_items: [...unchanged.activity_items, safari] },
      saved,
    );
    expect(r.activity_items).toEqual([safari]);
  });

  it.each([
    ['quantity', { quantity: 3 }],
    ['price', { unit_price: 55 }],
    ['date', { date: '2026-11-21' }],
    ['age group', { age_group: 'adult' }],
  ])('keeps an activity line whose %s changed', (_l, patch) => {
    const r = onlyNewOrChangedLines(
      { ...unchanged, activity_items: [{ ...unchanged.activity_items[0], ...patch }] },
      saved,
    );
    expect(r.activity_items).toHaveLength(1);
  });

  it('keeps a transport line whose supplier changed (below-cost check)', () => {
    const r = onlyNewOrChangedLines(
      {
        ...unchanged,
        transport_items: [{ ...unchanged.transport_items[0], supplier_id: 'SUP' }],
      },
      saved,
    );
    expect(r.transport_items).toHaveLength(1);
  });

  it('a line whose id is not on the booking counts as new', () => {
    const r = onlyNewOrChangedLines(
      { ...unchanged, activity_items: [{ ...unchanged.activity_items[0], id: 'other' }] },
      saved,
    );
    expect(r.activity_items).toHaveLength(1);
  });

  it('payload without ids → every line is checked (safe default)', () => {
    const { id: _id, ...noId } = unchanged.activity_items[0];
    void _id;
    const r = onlyNewOrChangedLines({ activity_items: [noId] }, saved);
    expect(r.activity_items).toHaveLength(1);
  });

  it('special items: same name/date/price is unchanged, a second copy is new', () => {
    const sim = unchanged.special_items[0];
    expect(onlyNewOrChangedLines({ special_items: [sim] }, saved).special_items).toEqual([]);
    expect(onlyNewOrChangedLines({ special_items: [sim, sim] }, saved).special_items).toHaveLength(
      1,
    );
    expect(
      onlyNewOrChangedLines({ special_items: [{ ...sim, total_price: 0 }] }, saved).special_items,
    ).toHaveLength(1);
  });

  it('leaves categories the payload does not send as undefined', () => {
    const r = onlyNewOrChangedLines({ notes: 'x' } as Record<string, unknown>, saved);
    expect(r.activity_items).toBeUndefined();
    expect(r.transport_items).toBeUndefined();
  });
});
