import {
  Controller,
  Get,
  Query,
} from '@nestjs/common';
import { AnalyticsService } from '../services/analytics.service';
import { CurrentUser } from '@common/decorators/current-user.decorator';
import { RequirePermissions } from '@common/decorators/permissions.decorator';
import { PERFORMANCE_PERMISSIONS } from '../constants/permissions';
import { BusinessException } from '@common/exceptions/business.exception';
import { PERFORMANCE_ERROR_CODES } from '../constants/error-codes';

/**
 * 统计分析控制器
 * 路由前缀: /api/v1/performance/reports
 */
@Controller('performance/reports')
export class AnalyticsController {
  constructor(private readonly analyticsService: AnalyticsService) {}

  /**
   * 获取个人绩效概览
   * GET /api/v1/performance/reports/personal
   */
  @Get('personal')
  @RequirePermissions(PERFORMANCE_PERMISSIONS.RESULT_VIEW_OWN)
  async getPersonalOverview(
    @CurrentUser('userId') userId: string,
    @Query('cycleId') cycleId?: string,
  ) {
    return this.analyticsService.getPersonalOverview(userId, cycleId);
  }

  /**
   * 获取个人历史绩效趋势
   * GET /api/v1/performance/reports/personal/trend
   */
  @Get('personal/trend')
  @RequirePermissions(PERFORMANCE_PERMISSIONS.RESULT_VIEW_OWN)
  async getPersonalTrend(
    @CurrentUser('userId') userId: string,
    @Query('limit') limit?: number,
  ) {
    const items = await this.analyticsService.getPersonalTrend(userId, limit);
    return { items };
  }

  /**
   * 获取团队绩效统计（仅 Manager）
   * GET /api/v1/performance/reports/team
   */
  @Get('team')
  @RequirePermissions(PERFORMANCE_PERMISSIONS.RESULT_VIEW_ALL)
  async getTeamStatistics(
    @CurrentUser('userId') managerId: string,
    @Query('cycleId') cycleId?: string,
  ) {
    return this.analyticsService.getTeamStatistics(managerId, cycleId);
  }

  /**
   * 获取部门绩效报表
   * GET /api/v1/performance/reports/department
   */
  @Get('department')
  @RequirePermissions(PERFORMANCE_PERMISSIONS.RESULT_VIEW_ALL)
  async getDepartmentReport(
    @Query('departmentId') departmentId?: string,
    @Query('cycleId') cycleId?: string,
  ) {
    if (!departmentId || !cycleId) {
      throw new BusinessException(
        PERFORMANCE_ERROR_CODES.REPORT_MISSING_PARAMS.message,
        PERFORMANCE_ERROR_CODES.REPORT_MISSING_PARAMS.code,
        PERFORMANCE_ERROR_CODES.REPORT_MISSING_PARAMS.httpStatus,
      );
    }
    return this.analyticsService.getDepartmentReport(departmentId, cycleId);
  }

  /**
   * 获取公司绩效报表
   * GET /api/v1/performance/reports/company
   */
  @Get('company')
  @RequirePermissions(PERFORMANCE_PERMISSIONS.RESULT_VIEW_ALL)
  async getCompanyReport(@Query('cycleId') cycleId?: string) {
    if (!cycleId) {
      throw new BusinessException(
        PERFORMANCE_ERROR_CODES.REPORT_MISSING_PARAMS.message,
        PERFORMANCE_ERROR_CODES.REPORT_MISSING_PARAMS.code,
        PERFORMANCE_ERROR_CODES.REPORT_MISSING_PARAMS.httpStatus,
      );
    }
    return this.analyticsService.getCompanyReport(cycleId);
  }

  /**
   * 获取等级分布报表
   * GET /api/v1/performance/reports/grade-distribution
   */
  @Get('grade-distribution')
  @RequirePermissions(PERFORMANCE_PERMISSIONS.RESULT_VIEW_ALL)
  async getGradeDistributionReport(
    @Query('cycleId') cycleId?: string,
    @Query('departmentId') departmentId?: string,
  ) {
    if (!cycleId) {
      throw new BusinessException(
        PERFORMANCE_ERROR_CODES.REPORT_MISSING_PARAMS.message,
        PERFORMANCE_ERROR_CODES.REPORT_MISSING_PARAMS.code,
        PERFORMANCE_ERROR_CODES.REPORT_MISSING_PARAMS.httpStatus,
      );
    }
    return this.analyticsService.getGradeDistributionReport(cycleId, departmentId);
  }

  /**
   * 获取周期对比数据
   * GET /api/v1/performance/reports/cycle-comparison
   */
  @Get('cycle-comparison')
  @RequirePermissions(PERFORMANCE_PERMISSIONS.RESULT_VIEW_ALL)
  async getCycleComparison(@Query('cycleIds') cycleIds: string) {
    if (!cycleIds) {
      throw new BusinessException(
        PERFORMANCE_ERROR_CODES.REPORT_MISSING_PARAMS.message,
        PERFORMANCE_ERROR_CODES.REPORT_MISSING_PARAMS.code,
        PERFORMANCE_ERROR_CODES.REPORT_MISSING_PARAMS.httpStatus,
      );
    }
    // 解析逗号分隔的周期 ID 列表
    const ids = cycleIds.split(',').map((id) => id.trim());
    const items = await this.analyticsService.getCycleComparison(ids);
    return { items };
  }
}
