import { BadRequestException } from '@nestjs/common';
import { assertRowPaxWithinParty, partyFromQuery, rowPaxViolations } from './row-pax';

describe('row-pax', () => {
  const party = partyFromQuery({ adults: 3, children_ages: [6] });

  it('derives the party from the query', () => {
    expect(party).toEqual({ adults: 3, children: 1 });
  });

  it('accepts rows within the party and null (= full party)', () => {
    expect(
      rowPaxViolations(
        party,
        [
          { adults: 2, children: 0 },
          { adults: null, children: null },
        ],
        [
          { activity_id: 'a', date: '2026-10-20', age_group: 'adult', quantity: 2 },
          { activity_id: 'a', date: '2026-10-20', age_group: 'senior', quantity: 1 },
          { activity_id: 'a', date: '2026-10-20', age_group: 'child', quantity: 1 },
        ],
      ),
    ).toEqual([]);
  });

  it('flags a transport row above the party', () => {
    expect(
      rowPaxViolations(party, [{ date: '2026-10-20', adults: 4, children: 2 }], []),
    ).toHaveLength(2);
  });

  it('counts seniors against adults per activity line', () => {
    const v = rowPaxViolations(
      party,
      [],
      [
        { activity_id: 'a', age_group: 'adult', quantity: 3 },
        { activity_id: 'a', age_group: 'senior', quantity: 1 },
      ],
    );
    expect(v).toHaveLength(1);
    expect(v[0]).toContain('4 adults/seniors > 3');
  });

  it('keeps separate activity lines separate', () => {
    expect(
      rowPaxViolations(
        party,
        [],
        [
          { activity_id: 'a', date: 'd1', age_group: 'adult', quantity: 3 },
          { activity_id: 'a', date: 'd2', age_group: 'adult', quantity: 3 },
        ],
      ),
    ).toEqual([]);
  });

  it('throws 400 with the reasons', () => {
    expect(() => assertRowPaxWithinParty(party, [{ adults: 9 }], [])).toThrow(BadRequestException);
  });
});
