import { Controller, Get, Post, Delete, Body, Param, Query, Req } from '@nestjs/common';
import { ApiTags, ApiOperation, ApiBearerAuth, ApiQuery, ApiParam } from '@nestjs/swagger';
import { ConfigManagementService } from '../../application/services/config.service';

class SetConfigDto { category: string; key: string; value: string; description?: string; }

@ApiTags('Config')
@ApiBearerAuth()
@Controller('configs')
export class ConfigController {
  constructor(private readonly configService: ConfigManagementService) {}

  @Get()
  @ApiOperation({ summary: '获取配置列表' })
  @ApiQuery({ name: 'category', required: false })
  async getConfigs(@Query('category') category?: string) {
    return this.configService.getConfigs(category);
  }

  @Get(':category/:key')
  @ApiOperation({ summary: '获取单个配置' })
  @ApiParam({ name: 'category' })
  @ApiParam({ name: 'key' })
  async getConfig(@Param('category') category: string, @Param('key') key: string) {
    return this.configService.getConfig(category, key);
  }

  @Post()
  @ApiOperation({ summary: '设置配置' })
  async setConfig(@Body() dto: SetConfigDto, @Req() req: any) {
    await this.configService.setConfig(req.admin.id, dto.category, dto.key, dto.value, dto.description);
    return { success: true };
  }

  @Delete(':category/:key')
  @ApiOperation({ summary: '删除配置' })
  async deleteConfig(@Param('category') category: string, @Param('key') key: string, @Req() req: any) {
    await this.configService.deleteConfig(req.admin.id, category, key);
    return { success: true };
  }
}
