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

import { RuleEngineManage, RuleEngineView } from '../../common/rule-engine-guards';

import { CreateWagonTypeDto } from './dto/create-wagon-type.dto';
import { UpdateWagonTypeDto } from './dto/update-wagon-type.dto';
import { WagonTypesService } from './wagon-types.service';

@ApiTags('wagon-types')
@Controller('wagon-types')
@ApiBearerAuth()
export class WagonTypesController {
  constructor(private readonly wagonTypesService: WagonTypesService) {}

  @Get()
  @RuleEngineView('wagon-types')
  @ApiOperation({ summary: 'List wagon types' })
  findAll(@Query() query: Record<string, string | undefined>) {
    return this.wagonTypesService.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('wagon-types')
  @ApiOperation({ summary: 'Get a wagon type by ID' })
  findOne(@Param('id', ParseUUIDPipe) id: string) {
    return this.wagonTypesService.findById(id);
  }

  @Post()
  @RuleEngineManage('wagon-types')
  @ApiOperation({ summary: 'Create a wagon type' })
  create(@Body() dto: CreateWagonTypeDto) {
    return this.wagonTypesService.create(dto);
  }

  @Patch(':id')
  @RuleEngineManage('wagon-types')
  @ApiOperation({ summary: 'Update a wagon type' })
  update(@Param('id', ParseUUIDPipe) id: string, @Body() dto: UpdateWagonTypeDto) {
    return this.wagonTypesService.update(id, dto);
  }

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