import {
  BadRequestException,
  Body, Controller, Delete, Get, HttpCode, HttpStatus,
  Param, ParseUUIDPipe, Patch, Post, Query,
} from '@nestjs/common';
import { RuleEngineCreate, RuleEngineDelete, RuleEngineUpdate, RuleEngineView } from '../../../common/rule-engine-guards';
import { ApiBearerAuth, ApiOperation, ApiTags } from '@nestjs/swagger';
import { CreatePriorityConfigDto } from '../dto/create-priority-config.dto';
import { ListPriorityConfigsQueryDto } from '../dto/list-rule-engine-query.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: ListPriorityConfigsQueryDto) {
    return this.service.findAll(query);
  }

  // Static route — must stay above `:id` (Express matches in declaration order).
  @Get('next-range')
  @RuleEngineView('priority-configs')
  @ApiOperation({
    summary: 'Where the next contiguous range for a type (and currency) must start',
  })
  nextRange(
    @Query('type') type: 'WAGON' | 'CURRENCY' | 'CUSTOMS',
    @Query('currency') currency?: string,
  ) {
    if (!['WAGON', 'CURRENCY', 'CUSTOMS'].includes(type)) {
      throw new BadRequestException('type must be WAGON, CURRENCY, or CUSTOMS');
    }
    return this.service.nextRange(type, currency ?? null);
  }

  @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()
  @RuleEngineCreate('priority-configs')
  @ApiOperation({ summary: 'Create a priority config' })
  create(@Body() dto: CreatePriorityConfigDto) {
    return this.service.create(dto);
  }

  @Post('reorder')
  @RuleEngineUpdate('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')
  @RuleEngineUpdate('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')
  @RuleEngineUpdate('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')
  @RuleEngineDelete('priority-configs')
  @HttpCode(HttpStatus.NO_CONTENT)
  @ApiOperation({ summary: 'Soft-delete a priority config' })
  remove(@Param('id', ParseUUIDPipe) id: string) {
    return this.service.remove(id);
  }
}
