import { Body, Controller, Delete, Get, Param, Patch, Post, Query } from '@nestjs/common';
import { ApiTags, ApiOperation, ApiBearerAuth, ApiParam, ApiResponse, ApiBody } from '@nestjs/swagger';
import { IsPublic } from '@tria-plc/api-common/modules/auth/decorators/public.decorator';
import { SeatClassesService } from './seat-classes.service';
import { CreateSeatClassDto, UpdateSeatClassDto } from './seat-classes.dto';
import { PassengerAdmin, PassengerStaff } from '../../common/passenger-guards';
import { PASSENGER_PERMS } from '../../seed/passenger-permissions.registry';

@ApiTags('Seat Classes')
@Controller('seat-classes')
export class SeatClassesController {
  constructor(private service: SeatClassesService) {}

  @Get()
  @IsPublic()
  @ApiOperation({ summary: 'List all seat classes' })
  @ApiResponse({ status: 200, description: 'Returns all seat classes with their coaches' })
  listSeatClasses() { return this.service.listSeatClasses(); }

  @Get(':id')
  @IsPublic()
  @ApiOperation({ summary: 'Get a seat class by ID' })
  @ApiParam({ name: 'id', description: 'Seat class UUID' })
  @ApiResponse({ status: 200, description: 'Returns seat class with its coaches' })
  @ApiResponse({ status: 404, description: 'Seat class not found' })
  getSeatClass(@Param('id') id: string) { return this.service.getSeatClass(id); }

  @Post()
  @PassengerStaff([PASSENGER_PERMS.tariffRates.manage, PASSENGER_PERMS.admin]) @ApiBearerAuth('IAM-auth')
  @ApiOperation({ summary: 'Create a seat class' })
  @ApiBody({ type: CreateSeatClassDto })
  @ApiResponse({ status: 201, description: 'Seat class created' })
  @ApiResponse({ status: 409, description: 'Seat class name already exists' })
  createSeatClass(@Body() dto: CreateSeatClassDto) { return this.service.createSeatClass(dto); }

  @Patch(':id')
  @PassengerStaff([PASSENGER_PERMS.tariffRates.manage, PASSENGER_PERMS.admin]) @ApiBearerAuth('IAM-auth')
  @ApiOperation({ summary: 'Update a seat class' })
  @ApiParam({ name: 'id', description: 'Seat class UUID' })
  @ApiBody({ type: UpdateSeatClassDto })
  @ApiResponse({ status: 200, description: 'Seat class updated' })
  @ApiResponse({ status: 404, description: 'Seat class not found' })
  updateSeatClass(@Param('id') id: string, @Body() dto: UpdateSeatClassDto) { return this.service.updateSeatClass(id, dto); }

  @Delete(':id')
  @PassengerAdmin()
  @ApiBearerAuth('IAM-auth')
  @ApiOperation({ summary: 'Delete a seat class' })
  @ApiParam({ name: 'id', description: 'Seat class UUID' })
  @ApiResponse({ status: 200, description: 'Seat class deleted' })
  @ApiResponse({ status: 404, description: 'Seat class not found' })
  deleteSeatClass(@Param('id') id: string, @Query('cascade') cascade?: string) {
    return this.service.deleteSeatClass(id, cascade === 'true');
  }
}
