import { TripInvitationsController } from './trip-invitations.controller';
import type { AuthenticatedUser } from '../../../../libs/auth/src';

describe('TripInvitationsController', () => {
  const currentUser: AuthenticatedUser = {
    id: 'owner-1',
    externalSubjectId: 'sub-1',
    displayName: 'Owner',
    email: 'owner@example.com',
  };

  it('POST /trips/:tripId/invitations creates an invitation and returns the raw token', async () => {
    const result = {
      invitation: { id: 'inv-1', tripId: 't1', email: 'friend@example.com' },
      rawToken: 'raw-token',
    };
    const invitationsService = {
      createInvitation: jest.fn().mockResolvedValue(result),
    };
    const controller = new TripInvitationsController(
      invitationsService as never,
    );

    await expect(
      controller.create('t1', currentUser, { email: 'friend@example.com' }),
    ).resolves.toEqual(result);
    expect(invitationsService.createInvitation).toHaveBeenCalledWith(
      't1',
      'owner-1',
      'friend@example.com',
    );
  });

  it('GET /trips/:tripId/invitations lists invitations', async () => {
    const invitationsService = {
      listInvitations: jest.fn().mockResolvedValue([]),
    };
    const controller = new TripInvitationsController(
      invitationsService as never,
    );

    await expect(controller.list('t1')).resolves.toEqual([]);
    expect(invitationsService.listInvitations).toHaveBeenCalledWith('t1');
  });

  it('DELETE /trips/:tripId/invitations/:invitationId removes an invitation', async () => {
    const invitationsService = {
      removeInvitation: jest.fn().mockResolvedValue(undefined),
    };
    const controller = new TripInvitationsController(
      invitationsService as never,
    );

    await controller.remove('t1', 'inv-1');
    expect(invitationsService.removeInvitation).toHaveBeenCalledWith(
      't1',
      'inv-1',
    );
  });

  it('POST /invitations/:token/accept accepts the invitation for the current user', async () => {
    const member = { id: 'm1', tripId: 't1', userId: 'owner-1' };
    const invitationsService = {
      acceptInvitation: jest.fn().mockResolvedValue(member),
    };
    const controller = new TripInvitationsController(
      invitationsService as never,
    );

    await expect(controller.accept('raw-token', currentUser)).resolves.toEqual(
      member,
    );
    expect(invitationsService.acceptInvitation).toHaveBeenCalledWith(
      'raw-token',
      'owner-1',
    );
  });
});
