import { Controller, Get, NotFoundException, Param, Post, UseGuards } from '@nestjs/common';
import { CommandBus, QueryBus } from '@nestjs/cqrs';
import {
  ApiBearerAuth,
  ApiOperation,
  ApiParam,
  ApiResponse,
  ApiTags,
} from '@nestjs/swagger';
import {
  type JwtPayload,
  CurrentUser,
  JwtAuthGuard,
  RolesGuard,
  Roles,
} from '@modules/auth';
import { RecalculateQualityScoreCommand } from '../../application/commands/recalculate-quality-score/recalculate-quality-score.command';
import { GetAgentDashboardQuery } from '../../application/queries/get-agent-dashboard/get-agent-dashboard.query';
import { GetAgentPublicProfileQuery } from '../../application/queries/get-agent-public-profile/get-agent-public-profile.query';
import { AgentDashboardData, AgentPublicProfileData } from '../../domain/repositories/agent.repository';

@ApiTags('agents')
@Controller('agents')
export class AgentsController {
  constructor(
    private readonly commandBus: CommandBus,
    private readonly queryBus: QueryBus,
  ) {}

  @ApiBearerAuth('JWT')
  @ApiOperation({ summary: 'Get agent dashboard stats' })
  @ApiResponse({ status: 200, description: 'Agent dashboard data' })
  @ApiResponse({ status: 401, description: 'Unauthorized' })
  @ApiResponse({ status: 403, description: 'Forbidden — only agents' })
  @ApiResponse({ status: 404, description: 'Agent profile not found' })
  @UseGuards(JwtAuthGuard, RolesGuard)
  @Roles('AGENT')
  @Get('me/dashboard')
  async getDashboard(
    @CurrentUser() user: JwtPayload,
  ): Promise<AgentDashboardData> {
    return this.queryBus.execute(new GetAgentDashboardQuery(user.sub));
  }

  @ApiOperation({ summary: 'Get public agent profile' })
  @ApiParam({ name: 'agentId', description: 'Agent ID' })
  @ApiResponse({ status: 200, description: 'Agent public profile data' })
  @ApiResponse({ status: 404, description: 'Agent not found' })
  @Get(':agentId/profile')
  async getPublicProfile(
    @Param('agentId') agentId: string,
  ): Promise<AgentPublicProfileData> {
    const profile = await this.queryBus.execute(
      new GetAgentPublicProfileQuery(agentId),
    );
    if (!profile) {
      throw new NotFoundException('Không tìm thấy môi giới');
    }
    return profile;
  }

  @ApiBearerAuth('JWT')
  @ApiOperation({ summary: 'Recalculate quality score (admin/system)' })
  @ApiParam({ name: 'agentId', description: 'Agent ID' })
  @ApiResponse({ status: 201, description: 'Quality score recalculated' })
  @ApiResponse({ status: 401, description: 'Unauthorized' })
  @ApiResponse({ status: 403, description: 'Forbidden — only admins' })
  @UseGuards(JwtAuthGuard, RolesGuard)
  @Roles('ADMIN')
  @Post(':agentId/recalculate-score')
  async recalculateScore(
    @Param('agentId') agentId: string,
  ): Promise<{ message: string }> {
    await this.commandBus.execute(
      new RecalculateQualityScoreCommand(agentId),
    );
    return { message: 'Quality score recalculated' };
  }
}
