import {
  Body,
  Controller,
  Delete,
  Get,
  Param,
  Patch,
  Post,
  UseGuards,
} from '@nestjs/common';
import { OidcAuthGuard } from '../../../../libs/auth/src';
import type { AuthenticatedUser } from '../../../../libs/auth/src';
import {
  TravelersService,
  TripMembershipGuard,
} from '../../../../libs/trips/src';
import type {
  CreateTravelerDto,
  Traveler,
  UpdateTravelerDto,
} from '../../../../libs/trips/src';
import { CurrentUser } from '../auth/current-user.decorator';

@Controller('trips/:tripId/travelers')
@UseGuards(OidcAuthGuard, TripMembershipGuard)
export class TravelersController {
  constructor(private readonly travelersService: TravelersService) {}

  @Get()
  list(@Param('tripId') tripId: string): Promise<Traveler[]> {
    return this.travelersService.listTravelers(tripId);
  }

  @Post()
  create(
    @Param('tripId') tripId: string,
    @CurrentUser() currentUser: AuthenticatedUser,
    @Body() dto: CreateTravelerDto,
  ): Promise<Traveler> {
    return this.travelersService.createTraveler(tripId, dto, currentUser.id);
  }

  @Patch(':travelerId')
  update(
    @Param('tripId') tripId: string,
    @Param('travelerId') travelerId: string,
    @Body() dto: UpdateTravelerDto,
  ): Promise<Traveler> {
    return this.travelersService.updateTraveler(tripId, travelerId, dto);
  }

  @Delete(':travelerId')
  remove(
    @Param('tripId') tripId: string,
    @Param('travelerId') travelerId: string,
  ): Promise<void> {
    return this.travelersService.removeTraveler(tripId, travelerId);
  }
}
