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 { CreateContainerTypeDto } from '../dto/create-container-type.dto';
import { MoveOrderDto } from '../dto/move-order.dto';
import { ReorderItemsDto } from '../dto/reorder-items.dto';
import { UpdateContainerTypeDto } from '../dto/update-container-type.dto';
import { ContainerTypesService } from '../services/container-types.service';

@ApiTags('container-types')
@Controller('container-types')
@ApiBearerAuth()
export class ContainerTypesController {
  constructor(private readonly service: ContainerTypesService) {}

  @Get()
  @RuleEngineView('container-types')
  @ApiOperation({ summary: 'List container types' })
  findAll(@Query() query: Record<string, string>) {
    return this.service.findAll({
      isActive: query['isActive'] !== undefined ? query['isActive'] === 'true' : undefined,
      page: query['page'] ? parseInt(query['page'], 10) : undefined,
      pageSize: query['pageSize'] ? parseInt(query['pageSize'], 10) : undefined,
    });
  }

  @Post('reorder')
  @RuleEngineManage('container-types')
  @HttpCode(HttpStatus.NO_CONTENT)
  @ApiOperation({ summary: 'Bulk reorder container types by ID list' })
  reorder(@Body() dto: ReorderItemsDto) {
    return this.service.reorder(dto);
  }

  @Post(':id/move-order')
  @RuleEngineManage('container-types')
  @HttpCode(HttpStatus.NO_CONTENT)
  @ApiOperation({ summary: 'Move a container type up or down in display order' })
  moveOrder(@Param('id', ParseUUIDPipe) id: string, @Body() dto: MoveOrderDto) {
    return this.service.moveOrder(id, dto.direction);
  }

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

  @Post()
  @RuleEngineManage('container-types')
  @ApiOperation({ summary: 'Create a container type' })
  create(@Body() dto: CreateContainerTypeDto) {
    return this.service.create(dto);
  }

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

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