import {
  Body,
  Controller,
  Delete,
  Get,
  Param,
  ParseUUIDPipe,
  Patch,
  Post,
  Query,
} from '@nestjs/common';
import { ApiOperation, ApiTags } from '@nestjs/swagger';
import { FleetManage, FleetView } from '../../common/booking-guards';
import { CreateContainerDto } from './dto/create-container.dto';
import { UpdateContainerDto } from './dto/update-container.dto';
import { AssignContainerToWagonDto } from './dto/assign-container-to-wagon.dto';
import { ContainersService } from './containers.service';

@ApiTags('containers')
@Controller('containers')
@FleetView()
export class ContainersController {
  constructor(private readonly containersService: ContainersService) {}

  @Post()
  @FleetManage()
  @ApiOperation({ summary: 'Create a new container' })
  create(@Body() dto: CreateContainerDto) {
    return this.containersService.create(dto);
  }

  @Get()
  @ApiOperation({ summary: 'List all containers' })
  findAll(@Query() query: Record<string, string | undefined>) {
    return this.containersService.findAll(query);
  }

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

  @Patch(':id')
  @FleetManage()
  @ApiOperation({ summary: 'Update a container' })
  update(@Param('id', ParseUUIDPipe) id: string, @Body() dto: UpdateContainerDto) {
    return this.containersService.update(id, dto);
  }

  @Delete(':id')
  @FleetManage()
  @ApiOperation({ summary: 'Delete a container' })
  remove(@Param('id', ParseUUIDPipe) id: string) {
    return this.containersService.remove(id);
  }

  @Post(':id/assign-wagon')
  @FleetManage()
  @ApiOperation({ summary: 'Assign container to a wagon' })
  assignToWagon(@Param('id', ParseUUIDPipe) id: string, @Body() dto: AssignContainerToWagonDto) {
    return this.containersService.assignToWagon(id, dto);
  }

  @Post(':id/unassign-wagon')
  @FleetManage()
  @ApiOperation({ summary: 'Unassign container from wagon' })
  unassignFromWagon(@Param('id', ParseUUIDPipe) id: string) {
    return this.containersService.unassignFromWagon(id);
  }
}
