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 { CreatePriorityRuleDto } from '../dto/create-priority-rule.dto';
import { UpdatePriorityRuleDto } from '../dto/update-priority-rule.dto';
import { PriorityRulesService } from '../services/priority-rules.service';

@ApiTags('priority-rules')
@Controller('priority-rules')
@ApiBearerAuth()
export class PriorityRulesController {
  constructor(private readonly service: PriorityRulesService) {}

  @Get()
  @RuleEngineView('priority-rules')
  @ApiOperation({ summary: 'List priority rules' })
  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,
    });
  }

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

  @Post()
  @RuleEngineManage('priority-rules')
  @ApiOperation({ summary: 'Create a priority rule' })
  create(@Body() dto: CreatePriorityRuleDto) {
    return this.service.create(dto);
  }

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

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