import { Controller, Get, Query, UseGuards, Logger } from '@nestjs/common';
import {
  ApiTags,
  ApiOperation,
  ApiResponse,
  ApiBearerAuth,
} from '@nestjs/swagger';
import { DashboardApplicationService } from '../../application/services/dashboard-application.service';
import {
  DashboardTrendQueryDto,
  DashboardActivitiesQueryDto,
} from '../dto/request/dashboard-query.dto';
import { DashboardStatsResponseDto } from '../dto/response/dashboard-stats.dto';
import { DashboardTrendResponseDto } from '../dto/response/dashboard-trend.dto';
import { DashboardActivitiesResponseDto } from '../dto/response/dashboard-activity.dto';
import { DashboardRegionResponseDto } from '../dto/response/dashboard-region.dto';
import { JwtAuthGuard } from '../../shared/guards/jwt-auth.guard';
import { DashboardPeriod } from '../../domain/value-objects';
import { HotWalletBalanceCacheService, HotWalletBalanceResponse } from '../../infrastructure/redis/hot-wallet-balance-cache.service';

@ApiTags('Dashboard')
@Controller('dashboard')
@UseGuards(JwtAuthGuard)
@ApiBearerAuth()
export class DashboardController {
  private readonly logger = new Logger(DashboardController.name);

  constructor(
    private readonly dashboardService: DashboardApplicationService,
    private readonly hotWalletBalanceCache: HotWalletBalanceCacheService,
  ) {}

  @Get('stats')
  @ApiOperation({ summary: '获取仪表板统计卡片数据' })
  @ApiResponse({
    status: 200,
    description: '统计数据',
    type: DashboardStatsResponseDto,
  })
  async getStats(): Promise<DashboardStatsResponseDto> {
    return this.dashboardService.getStats();
  }

  @Get('charts')
  @ApiOperation({ summary: '获取趋势图表数据' })
  @ApiResponse({
    status: 200,
    description: '趋势数据',
    type: DashboardTrendResponseDto,
  })
  async getTrendData(
    @Query() query: DashboardTrendQueryDto,
  ): Promise<DashboardTrendResponseDto> {
    return this.dashboardService.getTrendData(
      query.period ?? DashboardPeriod.SEVEN_DAYS,
    );
  }

  @Get('activities')
  @ApiOperation({ summary: '获取最近活动列表' })
  @ApiResponse({
    status: 200,
    description: '活动列表',
    type: DashboardActivitiesResponseDto,
  })
  async getActivities(
    @Query() query: DashboardActivitiesQueryDto,
  ): Promise<DashboardActivitiesResponseDto> {
    return this.dashboardService.getActivities(query.limit ?? 5);
  }

  @Get('region')
  @ApiOperation({ summary: '获取区域分布数据' })
  @ApiResponse({
    status: 200,
    description: '区域分布数据',
    type: DashboardRegionResponseDto,
  })
  async getRegionDistribution(): Promise<DashboardRegionResponseDto> {
    return this.dashboardService.getRegionDistribution();
  }

  // [2026-01-07] 新增：热钱包余额查询接口
  // 用于仪表板显示公共账户(dUSDT)和因子(KAVA)余额
  // 回滚方式：删除此方法
  @Get('hot-wallet-balance')
  @ApiOperation({ summary: '获取热钱包余额（公共账户和因子）' })
  @ApiResponse({
    status: 200,
    description: '热钱包余额数据',
  })
  async getHotWalletBalance(): Promise<HotWalletBalanceResponse> {
    this.logger.log('[getHotWalletBalance] 请求热钱包余额');
    return this.hotWalletBalanceCache.getHotWalletBalance('KAVA');
  }
}
