import { Controller, Get, Put, Param, UseGuards } from '@nestjs/common';
import { AISelfImprovementService } from '../../core/ai/AISelfImprovementService';
import { RbacGuard } from '../../core/guards/rbac.guard';
import { ApiBearerAuth, ApiTags } from '@nestjs/swagger';

@ApiTags('AI Self-Improvement')
@ApiBearerAuth()
@Controller('api/ai/self-improvement')
export class AISelfImprovementController {
  constructor(private aiSelfImprovementService: AISelfImprovementService) {}

  /**
   * 生成改进建议
   */
  @Get('suggestions/generate')
  @UseGuards(RbacGuard)
  async generateSuggestions() {
    const suggestions = await this.aiSelfImprovementService.generateImprovementSuggestions();
    return { success: true, suggestions };
  }

  /**
   * 获取所有改进建议
   */
  @Get('suggestions')
  @UseGuards(RbacGuard)
  async getSuggestions() {
    const suggestions = await this.aiSelfImprovementService.getImprovementSuggestions();
    return { success: true, suggestions };
  }

  /**
   * 更新建议状态
   */
  @Put('suggestions/:id/status')
  @UseGuards(RbacGuard)
  async updateSuggestionStatus(@Param('id') id: string, @Param('status') status: 'implemented' | 'dismissed') {
    const result = await this.aiSelfImprovementService.updateSuggestionStatus(id, status);
    return { success: result };
  }

  /**
   * 自动应用改进建议
   */
  @Get('suggestions/apply')
  @UseGuards(RbacGuard)
  async applySuggestions() {
    const result = await this.aiSelfImprovementService.applyImprovementSuggestions();
    return { success: true, ...result };
  }

  /**
   * 执行定期优化
   */
  @Get('optimize')
  @UseGuards(RbacGuard)
  async performOptimization() {
    await this.aiSelfImprovementService.performRegularOptimization();
    return { success: true, message: '定期优化执行完成' };
  }
}
