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

import { TrainSchedulingManage, TrainSchedulingView } from '../../common/booking-guards';

import { CreateFirstMileDto } from './dto/create-first-mile.dto';
import { UpdateFirstMileDto } from './dto/update-first-mile.dto';
import { FirstMileStatus } from './entities/first-mile.entity';
import { FirstMileService } from './first-mile.service';

@ApiTags('first-mile')
@ApiBearerAuth()
@Controller('first-mile')
@TrainSchedulingView()
export class FirstMileController {
  constructor(private readonly firstMileService: FirstMileService) {}

  @Get()
  @ApiOperation({ summary: 'List first-mile legs' })
  findAll(
    @Query('status') status?: string,
    @Query('bookingId') bookingId?: string,
    @Query('vehicleId') vehicleId?: string,
    @Query('page') page?: string,
    @Query('pageSize') pageSize?: string,
    @Query('sortBy') sortBy?: string,
    @Query('sortOrder') sortOrder?: 'ASC' | 'DESC',
  ) {
    return this.firstMileService.findAll({
      status: status as FirstMileStatus | undefined,
      bookingId,
      vehicleId,
      page: page ? parseInt(page, 10) : undefined,
      pageSize: pageSize ? parseInt(pageSize, 10) : undefined,
      sortBy,
      sortOrder,
    });
  }

  @Get(':id')
  @ApiOperation({ summary: 'Get a first-mile leg by ID' })
  findOne(@Param('id', ParseUUIDPipe) id: string) {
    return this.firstMileService.findById(id);
  }

  @Post('accept/:reference')
  @TrainSchedulingManage()
  @ApiOperation({ summary: 'Accept a paid booking and create a first-mile leg' })
  acceptBooking(@Param('reference') reference: string) {
    return this.firstMileService.acceptBooking(reference);
  }

  @Post()
  @TrainSchedulingManage()
  @ApiOperation({ summary: 'Create a first-mile leg' })
  create(@Body() dto: CreateFirstMileDto) {
    return this.firstMileService.create(dto);
  }

  @Patch(':id')
  @TrainSchedulingManage()
  @ApiOperation({ summary: 'Update a first-mile leg' })
  update(@Param('id', ParseUUIDPipe) id: string, @Body() dto: UpdateFirstMileDto) {
    return this.firstMileService.update(id, dto);
  }

  @Delete(':id')
  @TrainSchedulingManage()
  @HttpCode(HttpStatus.NO_CONTENT)
  @ApiOperation({ summary: 'Soft-delete a first-mile leg' })
  remove(@Param('id', ParseUUIDPipe) id: string) {
    return this.firstMileService.remove(id);
  }
}
