import { Body, Controller, Get, Param, ParseUUIDPipe, Patch, Post, Query } from '@nestjs/common';
import { ApiBearerAuth, ApiOperation, ApiTags } from '@nestjs/swagger';

import { FleetManage, StaffReference } from '../../common/booking-guards';
import { FREIGHT_PERMS } from '../../seed/freight-permissions.registry';
import { CreateLocomotiveDto } from './dto/create-locomotive.dto';
import { FilterLocomotivesDto } from './dto/filter-locomotives.dto';
import { UpdateLocomotiveDto } from './dto/update-locomotive.dto';
import { LocomotivesService } from './locomotives.service';

@ApiTags('locomotives')
@ApiBearerAuth()
// No class-level guard: reads are login-only reference data (any staff can
// fetch a locomotive for a cross-flow view without the fleet:view that drives
// the Fleet sidebar). Every mutation carries its own @FleetManage().
@Controller('locomotives')
export class LocomotivesController {
  constructor(private readonly locomotivesService: LocomotivesService) {}

  @Get()
  @StaffReference()
  @ApiOperation({ summary: 'List locomotives' })
  findAll(@Query() filter: FilterLocomotivesDto) {
    return this.locomotivesService.findAll(filter);
  }

  @Get(':id')
  @StaffReference()
  @ApiOperation({ summary: 'Get a locomotive by ID' })
  findOne(@Param('id', ParseUUIDPipe) id: string) {
    return this.locomotivesService.findById(id);
  }

  @Post()
  @FleetManage(FREIGHT_PERMS.locomotives.create)
  @ApiOperation({ summary: 'Create a locomotive' })
  create(@Body() dto: CreateLocomotiveDto) {
    return this.locomotivesService.create(dto);
  }

  @Patch(':id')
  @FleetManage(FREIGHT_PERMS.locomotives.update)
  @ApiOperation({ summary: 'Update a locomotive' })
  update(@Param('id', ParseUUIDPipe) id: string, @Body() dto: UpdateLocomotiveDto) {
    return this.locomotivesService.update(id, dto);
  }

  @Post(':id/decommission')
  @FleetManage(FREIGHT_PERMS.locomotives.delete)
  @ApiOperation({ summary: 'Decommission a locomotive' })
  decommission(@Param('id', ParseUUIDPipe) id: string) {
    return this.locomotivesService.decommission(id);
  }
}
