import { Controller, Get, Post, Param, Query, Body, UseGuards } from '@nestjs/common';
import { ApiTags, ApiOperation, ApiBearerAuth, ApiQuery } from '@nestjs/swagger';
import { JwtAuthGuard, RolesGuard, Roles, UserRole } from '@genex/common';
import { AdminAgentService } from '../../../application/services/admin-agent.service';

@ApiTags('Admin - AI Agent')
@Controller('ai/admin/agent')
@UseGuards(JwtAuthGuard, RolesGuard)
@Roles(UserRole.ADMIN)
@ApiBearerAuth()
export class AdminAgentController {
  constructor(private readonly adminAgentService: AdminAgentService) {}

  @Get('stats')
  @ApiOperation({ summary: 'AI agent session stats (sessions today, avg response time, satisfaction)' })
  async getStats() {
    return { code: 0, data: await this.adminAgentService.getStats() };
  }

  @Get('top-questions')
  @ApiOperation({ summary: 'Most commonly asked questions' })
  @ApiQuery({ name: 'limit', required: false, type: Number })
  async getTopQuestions(@Query('limit') limit = '10') {
    return { code: 0, data: await this.adminAgentService.getTopQuestions(+limit) };
  }

  @Get('modules')
  @ApiOperation({ summary: 'AI module status and accuracy' })
  async getModules() {
    return { code: 0, data: await this.adminAgentService.getModules() };
  }

  @Post('modules/:id/config')
  @ApiOperation({ summary: 'Configure an AI module' })
  async configureModule(
    @Param('id') moduleId: string,
    @Body() config: Record<string, any>,
  ) {
    return { code: 0, data: await this.adminAgentService.configureModule(moduleId, config) };
  }

  @Get('sessions')
  @ApiOperation({ summary: 'Recent AI chat sessions' })
  @ApiQuery({ name: 'page', required: false, type: Number })
  @ApiQuery({ name: 'limit', required: false, type: Number })
  async getSessions(
    @Query('page') page = '1',
    @Query('limit') limit = '20',
  ) {
    return { code: 0, data: await this.adminAgentService.getSessions(+page, +limit) };
  }

  @Get('satisfaction')
  @ApiOperation({ summary: 'AI satisfaction metrics' })
  async getSatisfaction() {
    return { code: 0, data: await this.adminAgentService.getSatisfactionMetrics() };
  }
}
