import { DashboardService } from './dashboard.service';

type Row = Record<string, unknown>;

function build(rows: Row[], users: Array<{ id: string; name: string }> = []) {
  const query = jest.fn().mockResolvedValueOnce(rows).mockResolvedValueOnce(users);
  const bookingRepo = { manager: { query } };
  const svc = new DashboardService(
    {} as never,
    bookingRepo as never,
    {} as never,
    {} as never,
    {} as never,
    {} as never,
    {} as never,
    {} as never,
  );
  return (
    svc as unknown as {
      getSpecialActivityBookings(t: string, d?: string): Promise<Array<Record<string, unknown>>>;
    }
  ).getSpecialActivityBookings('tenant-1');
}

const base: Row = {
  booking_id: 'b1',
  booking_number: 'MT-1',
  booking_status: 'upcoming',
  payment_status: 'UNPAID',
  guest_name: 'KT',
  agent_name: 'Mellow tours 2',
  activity_name: '30 Days Single Entry Visa',
  activity_date: '2026-10-29',
  ticket_status: 'Pending',
  item_id: 'item-1',
  adults: '2',
  children: '4',
  children_ages: '[3,2,4,8]',
  sales_team: ['u1'],
  age_config: JSON.stringify([{ label: 'Child', min_age: 0, max_age: 3, enabled: true }]),
};

describe('getSpecialActivityBookings', () => {
  it('moves children over the activity max child age into adults and keeps the ages', async () => {
    const [r] = await build([base], [{ id: 'u1', name: 'Asha' }]);
    expect(r).toMatchObject({
      adults: 4,
      children: 2,
      children_ages: [3, 2, 4, 8],
      agent_name: 'Mellow tours 2',
      sales_person: 'Asha',
      booking_status: 'upcoming',
      payment_status: 'UNPAID',
      item_id: 'item-1',
    });
  });

  it('leaves the stored split when the activity has no usable Child band', async () => {
    const [r] = await build([{ ...base, age_config: null }]);
    expect(r).toMatchObject({ adults: 2, children: 4 });
  });

  it('does not reclassify a partial line (children count != booked ages)', async () => {
    const [r] = await build([{ ...base, children: '2' }]);
    expect(r).toMatchObject({ adults: 2, children: 2 });
  });

  it('returns null sales person when none resolve', async () => {
    const [r] = await build([{ ...base, sales_team: [] }]);
    expect(r.sales_person).toBeNull();
  });
});
