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

import {
  RuleEngineCreate,
  RuleEngineDelete,
  RuleEngineUpdate,
  RuleEngineView,
} from '../../common/rule-engine-guards';

import { CreateTruckTypeDto } from './dto/create-truck-type.dto';
import { UpdateTruckTypeDto } from './dto/update-truck-type.dto';
import { TruckTypesService } from './truck-types.service';

@ApiTags('truck-types')
@Controller('truck-types')
@ApiBearerAuth()
export class TruckTypesController {
  constructor(private readonly truckTypesService: TruckTypesService) {}

  @Get()
  @RuleEngineView('truck-types')
  @ApiOperation({ summary: 'List truck types' })
  findAll(@Query() query: Record<string, string | undefined>) {
    return this.truckTypesService.findAll({
      isActive:
        query.isActive === 'all'
          ? undefined
          : query.isActive !== undefined
            ? query.isActive === 'true'
            : true,
      page: query.page ? parseInt(query.page, 10) : undefined,
      pageSize: query.pageSize ? parseInt(query.pageSize, 10) : undefined,
      sortBy: query.sortBy,
      sortOrder: query.sortOrder,
    });
  }

  @Get(':id')
  @RuleEngineView('truck-types')
  @ApiOperation({ summary: 'Get a truck type by ID' })
  findOne(@Param('id', ParseUUIDPipe) id: string) {
    return this.truckTypesService.findById(id);
  }

  @Post()
  @RuleEngineCreate('truck-types')
  @ApiOperation({ summary: 'Create a truck type' })
  create(@Body() dto: CreateTruckTypeDto) {
    return this.truckTypesService.create(dto);
  }

  @Patch(':id')
  @RuleEngineUpdate('truck-types')
  @ApiOperation({ summary: 'Update a truck type' })
  update(@Param('id', ParseUUIDPipe) id: string, @Body() dto: UpdateTruckTypeDto) {
    return this.truckTypesService.update(id, dto);
  }

  @Delete(':id')
  @RuleEngineDelete('truck-types')
  @HttpCode(HttpStatus.NO_CONTENT)
  @ApiOperation({ summary: 'Soft-delete a truck type' })
  remove(@Param('id', ParseUUIDPipe) id: string) {
    return this.truckTypesService.remove(id);
  }
}
