import {
  Body,
  Controller,
  Delete,
  Get,
  Param,
  Patch,
  UseGuards,
} from '@nestjs/common';
import { OidcAuthGuard } from '../../../../libs/auth/src';
import {
  TripMembersService,
  TripMembershipGuard,
  TripRoles,
} from '../../../../libs/trips/src';
import type {
  TripMember,
  TripMemberRole,
  TripMemberStatus,
} from '../../../../libs/trips/src';

interface UpdateTripMemberDto {
  role?: TripMemberRole;
  status?: TripMemberStatus;
}

@Controller('trips/:tripId/members')
@UseGuards(OidcAuthGuard, TripMembershipGuard)
export class TripMembersController {
  constructor(private readonly tripMembersService: TripMembersService) {}

  @Get()
  list(@Param('tripId') tripId: string): Promise<TripMember[]> {
    return this.tripMembersService.listMembers(tripId);
  }

  @Patch(':memberId')
  @TripRoles('OWNER')
  update(
    @Param('tripId') tripId: string,
    @Param('memberId') memberId: string,
    @Body() dto: UpdateTripMemberDto,
  ): Promise<TripMember> {
    return this.tripMembersService.updateMember(tripId, memberId, dto);
  }

  @Delete(':memberId')
  @TripRoles('OWNER')
  remove(
    @Param('tripId') tripId: string,
    @Param('memberId') memberId: string,
  ): Promise<void> {
    return this.tripMembersService.removeMember(tripId, memberId);
  }
}
