import { Controller, Get, Put, Body, UseGuards } from '@nestjs/common';
import { ApiTags, ApiOperation, ApiResponse, ApiBearerAuth } from '@nestjs/swagger';
import { LeaderboardApplicationService } from '../../application/services/leaderboard-application.service';
import { JwtAuthGuard } from '../guards/jwt-auth.guard';
import { AdminGuard } from '../guards/admin.guard';
import { CurrentUser, CurrentUserPayload } from '../decorators/current-user.decorator';
import {
  LeaderboardConfigResponseDto,
  UpdateLeaderboardSwitchDto,
  UpdateVirtualRankingDto,
  UpdateDisplaySettingsDto,
  UpdateRefreshIntervalDto,
} from '../dto/leaderboard-config.dto';

@ApiTags('龙虎榜配置')
@Controller('leaderboard/config')
@UseGuards(JwtAuthGuard, AdminGuard)
@ApiBearerAuth()
export class LeaderboardConfigController {
  constructor(
    private readonly leaderboardService: LeaderboardApplicationService,
  ) {}

  @Get()
  @ApiOperation({ summary: '获取榜单配置' })
  @ApiResponse({
    status: 200,
    description: '榜单配置',
    type: LeaderboardConfigResponseDto,
  })
  async getConfig() {
    const config = await this.leaderboardService.getConfig();

    return {
      code: 0,
      message: 'success',
      data: {
        id: config.id?.toString(),
        configKey: config.configKey,
        dailyEnabled: config.dailyEnabled,
        weeklyEnabled: config.weeklyEnabled,
        monthlyEnabled: config.monthlyEnabled,
        virtualRankingEnabled: config.virtualRankingEnabled,
        virtualAccountCount: config.virtualAccountCount,
        displayLimit: config.displayLimit,
        refreshIntervalMinutes: config.refreshIntervalMinutes,
      },
    };
  }

  @Put('switch')
  @ApiOperation({ summary: '更新榜单开关' })
  @ApiResponse({ status: 200, description: '更新成功' })
  async updateSwitch(
    @Body() dto: UpdateLeaderboardSwitchDto,
    @CurrentUser() user: CurrentUserPayload,
  ) {
    const config = await this.leaderboardService.getConfig();
    config.updateLeaderboardSwitch(dto.type, dto.enabled, user.userId);
    await this.leaderboardService.updateConfig(config);

    return {
      code: 0,
      message: '榜单开关更新成功',
    };
  }

  @Put('virtual')
  @ApiOperation({ summary: '更新虚拟排名设置' })
  @ApiResponse({ status: 200, description: '更新成功' })
  async updateVirtualRanking(
    @Body() dto: UpdateVirtualRankingDto,
    @CurrentUser() user: CurrentUserPayload,
  ) {
    const config = await this.leaderboardService.getConfig();
    config.updateVirtualRankingSettings(dto.enabled, dto.accountCount, user.userId);
    await this.leaderboardService.updateConfig(config);

    return {
      code: 0,
      message: '虚拟排名设置更新成功',
    };
  }

  @Put('display')
  @ApiOperation({ summary: '更新显示设置' })
  @ApiResponse({ status: 200, description: '更新成功' })
  async updateDisplaySettings(
    @Body() dto: UpdateDisplaySettingsDto,
    @CurrentUser() user: CurrentUserPayload,
  ) {
    const config = await this.leaderboardService.getConfig();
    config.updateDisplayLimit(dto.displayLimit, user.userId);
    await this.leaderboardService.updateConfig(config);

    return {
      code: 0,
      message: '显示设置更新成功',
    };
  }

  @Put('refresh-interval')
  @ApiOperation({ summary: '更新刷新间隔' })
  @ApiResponse({ status: 200, description: '更新成功' })
  async updateRefreshInterval(
    @Body() dto: UpdateRefreshIntervalDto,
    @CurrentUser() user: CurrentUserPayload,
  ) {
    const config = await this.leaderboardService.getConfig();
    config.updateRefreshInterval(dto.minutes, user.userId);
    await this.leaderboardService.updateConfig(config);

    return {
      code: 0,
      message: '刷新间隔更新成功',
    };
  }
}
