import {
  Controller,
  Get,
  Post,
  Put,
  Delete,
  Param,
  Body,
  Query,
  ParseIntPipe,
  UseGuards,
} from '@nestjs/common';
import { ApiTags, ApiOperation, ApiBearerAuth } from '@nestjs/swagger';
import { JwtAuthGuard } from '@/common/guards/jwt-auth.guard';
import { RolesGuard } from '@/common/guards/roles.guard';
import { Roles } from '@/common/decorators/roles.decorator';
import { AdminRole } from '@/entities/admin.entity';
import { SecretService } from '@/modules/shared/secret/secret.service';
import { CreateSecretDto, UpdateSecretDto, QuerySecretDto } from './dto/secret.dto';

@ApiTags('管理端-系统密钥')
@ApiBearerAuth()
@UseGuards(JwtAuthGuard, RolesGuard)
@Roles(AdminRole.SUPER_ADMIN)
@Controller('admin/secrets')
export class AdminSecretController {
  constructor(private readonly secretService: SecretService) {}

  @Get('group/list')
  @ApiOperation({ summary: '获取密钥分组列表' })
  getGroups() {
    return this.secretService.getGroups();
  }

  @Get()
  @ApiOperation({ summary: '获取密钥列表（值脱敏）' })
  findAll(@Query() query: QuerySecretDto) {
    return this.secretService.findAll(query);
  }

  @Get(':id')
  @ApiOperation({ summary: '获取密钥详情（值脱敏）' })
  findOne(@Param('id', ParseIntPipe) id: number) {
    return this.secretService.findOne(id);
  }

  @Post()
  @ApiOperation({ summary: '新增密钥' })
  create(@Body() dto: CreateSecretDto) {
    return this.secretService.create(dto);
  }

  @Put(':id')
  @ApiOperation({ summary: '更新密钥' })
  update(@Param('id', ParseIntPipe) id: number, @Body() dto: UpdateSecretDto) {
    return this.secretService.update(id, dto);
  }

  @Delete(':id')
  @ApiOperation({ summary: '删除密钥' })
  remove(@Param('id', ParseIntPipe) id: number) {
    return this.secretService.remove(id);
  }
}
