import {
  Body, Controller, Delete, Get, HttpCode, HttpStatus,
  Param, ParseUUIDPipe, Patch, Post, Query,
} from '@nestjs/common';
import { RuleEngineManage, RuleEngineView } from '../../../common/rule-engine-guards';
import { ApiBearerAuth, ApiOperation, ApiTags } from '@nestjs/swagger';
import { CreatePriorityConfigDto } from '../dto/create-priority-config.dto';
import { UpdatePriorityConfigDto } from '../dto/update-priority-config.dto';
import { MoveOrderDto } from '../dto/move-order.dto';
import { ReorderItemsDto } from '../dto/reorder-items.dto';
import { PriorityConfigsService } from '../services/priority-configs.service';

@ApiTags('priority-configs')
@Controller('priority-configs')
@ApiBearerAuth()
export class PriorityConfigsController {
  constructor(private readonly service: PriorityConfigsService) {}

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

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

  @Post()
  @RuleEngineManage('priority-configs')
  @ApiOperation({ summary: 'Create a priority config' })
  create(@Body() dto: CreatePriorityConfigDto) {
    return this.service.create(dto);
  }

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

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

  @Patch(':id')
  @RuleEngineManage('priority-configs')
  @ApiOperation({ summary: 'Update a priority config' })
  update(@Param('id', ParseUUIDPipe) id: string, @Body() dto: UpdatePriorityConfigDto) {
    return this.service.update(id, dto);
  }

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