import { TravelersService } from './travelers.service';

describe('TripMember vs Traveler', () => {
  it('creating a traveler does not create or require a trip_members row', async () => {
    const travelersRepo = {
      create: jest.fn().mockResolvedValue({
        id: 'trav-1',
        tripId: 't1',
        linkedUserId: null,
        displayName: 'Mila',
        travelerType: 'CHILD',
        createdByUserId: 'u1',
      }),
    };
    const membersRepo = { create: jest.fn(), findByTripAndUser: jest.fn() };
    const service = new TravelersService(travelersRepo as never);

    const traveler = await service.createTraveler(
      't1',
      { displayName: 'Mila', travelerType: 'CHILD' },
      'u1',
    );

    expect(traveler.linkedUserId).toBeNull();
    expect(membersRepo.create).not.toHaveBeenCalled();
    expect(membersRepo.findByTripAndUser).not.toHaveBeenCalled();
  });

  it('a Traveler can be linked to a user who is independently a TripMember, without either row implying the other', async () => {
    const travelersRepo = {
      create: jest.fn().mockResolvedValue({
        id: 'trav-2',
        tripId: 't1',
        linkedUserId: 'u2',
        displayName: 'Alex',
        travelerType: 'ADULT',
        createdByUserId: 'u1',
      }),
    };
    const service = new TravelersService(travelersRepo as never);

    const traveler = await service.createTraveler(
      't1',
      { displayName: 'Alex', travelerType: 'ADULT', linkedUserId: 'u2' },
      'u1',
    );

    // linkedUserId is informational only; TripMembershipGuard never consults the travelers table.
    expect(traveler.linkedUserId).toBe('u2');
    expect(travelersRepo.create).toHaveBeenCalledWith(
      't1',
      expect.objectContaining({ linkedUserId: 'u2' }),
      'u1',
    );
  });
});
