/**
 * 系统账户报表控制器
 * [2026-01-04] 新增：提供系统账户报表相关API
 * 回滚方式：删除此文件，并从 api.module.ts 中移除注册
 */
import { Controller, Get, Query, Logger } from '@nestjs/common';
import { ApiTags, ApiOperation, ApiResponse, ApiQuery } from '@nestjs/swagger';
import { SystemAccountReportApplicationService } from '../../application/services/system-account-report-application.service';

@ApiTags('System Account Reports')
@Controller('system-account-reports')
export class SystemAccountReportController {
  private readonly logger = new Logger(SystemAccountReportController.name);

  constructor(
    private readonly systemAccountReportService: SystemAccountReportApplicationService,
  ) {}

  @Get()
  @ApiOperation({ summary: '获取完整系统账户报表' })
  @ApiQuery({ name: 'startDate', required: false, description: '开始日期 (YYYY-MM-DD)' })
  @ApiQuery({ name: 'endDate', required: false, description: '结束日期 (YYYY-MM-DD)' })
  @ApiResponse({ status: 200, description: '系统账户报表数据' })
  async getFullReport(
    @Query('startDate') startDate?: string,
    @Query('endDate') endDate?: string,
  ) {
    this.logger.log(`[getFullReport] 请求系统账户报表, startDate=${startDate}, endDate=${endDate}`);
    return this.systemAccountReportService.getFullReport({ startDate, endDate });
  }

  @Get('fixed-accounts')
  @ApiOperation({ summary: '获取固定系统账户列表（带余额）' })
  @ApiResponse({ status: 200, description: '固定系统账户列表' })
  async getFixedAccounts() {
    this.logger.log('[getFixedAccounts] 请求固定系统账户列表');
    return this.systemAccountReportService.getFixedAccountsWithBalances();
  }

  @Get('province-summary')
  @ApiOperation({ summary: '获取省区域账户汇总' })
  @ApiResponse({ status: 200, description: '省区域账户汇总' })
  async getProvinceSummary() {
    this.logger.log('[getProvinceSummary] 请求省区域账户汇总');
    return this.systemAccountReportService.getProvinceAccountsSummary();
  }

  @Get('city-summary')
  @ApiOperation({ summary: '获取市区域账户汇总' })
  @ApiResponse({ status: 200, description: '市区域账户汇总' })
  async getCitySummary() {
    this.logger.log('[getCitySummary] 请求市区域账户汇总');
    return this.systemAccountReportService.getCityAccountsSummary();
  }

  @Get('offline-settlement')
  @ApiOperation({ summary: '获取面对面结算统计' })
  @ApiQuery({ name: 'startDate', required: false, description: '开始日期 (YYYY-MM-DD)' })
  @ApiQuery({ name: 'endDate', required: false, description: '结束日期 (YYYY-MM-DD)' })
  @ApiResponse({ status: 200, description: '面对面结算统计' })
  async getOfflineSettlement(
    @Query('startDate') startDate?: string,
    @Query('endDate') endDate?: string,
  ) {
    this.logger.log(`[getOfflineSettlement] 请求面对面结算统计, startDate=${startDate}, endDate=${endDate}`);
    return this.systemAccountReportService.getOfflineSettlementSummary({ startDate, endDate });
  }

  @Get('expired-rewards')
  @ApiOperation({ summary: '获取过期收益统计' })
  @ApiQuery({ name: 'startDate', required: false, description: '开始日期 (YYYY-MM-DD)' })
  @ApiQuery({ name: 'endDate', required: false, description: '结束日期 (YYYY-MM-DD)' })
  @ApiResponse({ status: 200, description: '过期收益统计' })
  async getExpiredRewards(
    @Query('startDate') startDate?: string,
    @Query('endDate') endDate?: string,
  ) {
    this.logger.log(`[getExpiredRewards] 请求过期收益统计, startDate=${startDate}, endDate=${endDate}`);
    return this.systemAccountReportService.getExpiredRewardsSummary({ startDate, endDate });
  }

  // [2026-01-07] 新增：过期收益明细列表
  // 用于系统账户报表中展示过期收益的具体记录
  // 回滚方式：删除此方法
  @Get('expired-rewards-entries')
  @ApiOperation({ summary: '获取过期收益明细列表' })
  @ApiQuery({ name: 'page', required: false, description: '页码，默认1' })
  @ApiQuery({ name: 'pageSize', required: false, description: '每页条数，默认50' })
  @ApiQuery({ name: 'rightType', required: false, description: '权益类型筛选（可选）' })
  @ApiResponse({ status: 200, description: '过期收益明细列表' })
  async getExpiredRewardsEntries(
    @Query('page') page?: string,
    @Query('pageSize') pageSize?: string,
    @Query('rightType') rightType?: string,
  ) {
    this.logger.log(`[getExpiredRewardsEntries] 请求过期收益明细, page=${page}, pageSize=${pageSize}, rightType=${rightType || 'all'}`);
    return this.systemAccountReportService.getExpiredRewardsEntries({
      page: page ? parseInt(page, 10) : undefined,
      pageSize: pageSize ? parseInt(pageSize, 10) : undefined,
      rightType,
    });
  }

  // [2026-01-07] 新增：面对面结算明细列表
  // 回滚方式：删除此方法
  @Get('offline-settlement-entries')
  @ApiOperation({ summary: '获取面对面结算明细列表' })
  @ApiQuery({ name: 'page', required: false, description: '页码，默认1' })
  @ApiQuery({ name: 'pageSize', required: false, description: '每页条数，默认50' })
  @ApiQuery({ name: 'startDate', required: false, description: '开始日期 (YYYY-MM-DD)' })
  @ApiQuery({ name: 'endDate', required: false, description: '结束日期 (YYYY-MM-DD)' })
  @ApiResponse({ status: 200, description: '面对面结算明细列表' })
  async getOfflineSettlementEntries(
    @Query('page') page?: string,
    @Query('pageSize') pageSize?: string,
    @Query('startDate') startDate?: string,
    @Query('endDate') endDate?: string,
  ) {
    this.logger.log(`[getOfflineSettlementEntries] 请求面对面结算明细, page=${page}, pageSize=${pageSize}`);
    return this.systemAccountReportService.getOfflineSettlementEntries({
      page: page ? parseInt(page, 10) : undefined,
      pageSize: pageSize ? parseInt(pageSize, 10) : undefined,
      startDate,
      endDate,
    });
  }

  // [2026-01-05] 新增：所有系统账户分类账明细
  @Get('all-ledger')
  @ApiOperation({ summary: '获取所有系统账户的分类账明细' })
  @ApiQuery({ name: 'pageSize', required: false, description: '每个账户显示的条数，默认50' })
  @ApiQuery({ name: 'startDate', required: false, description: '开始日期 (YYYY-MM-DD)' })
  @ApiQuery({ name: 'endDate', required: false, description: '结束日期 (YYYY-MM-DD)' })
  @ApiResponse({ status: 200, description: '所有系统账户的分类账明细' })
  async getAllSystemAccountsLedger(
    @Query('pageSize') pageSize?: string,
    @Query('startDate') startDate?: string,
    @Query('endDate') endDate?: string,
  ) {
    this.logger.log(`[getAllSystemAccountsLedger] 请求所有系统账户分类账, pageSize=${pageSize}, startDate=${startDate}, endDate=${endDate}`);
    return this.systemAccountReportService.getAllSystemAccountsLedger({
      pageSize: pageSize ? parseInt(pageSize, 10) : undefined,
      startDate,
      endDate,
    });
  }

  // [2026-01-06] 新增：收益类型汇总统计接口
  // 用于系统账户报表中的：手续费汇总、省团队收益汇总、市团队收益汇总、分享引荐收益汇总、社区收益汇总
  // 回滚方式：删除以下 API 方法即可
  @Get('reward-type-summaries')
  @ApiOperation({ summary: '获取所有收益类型汇总统计' })
  @ApiResponse({ status: 200, description: '所有收益类型汇总' })
  async getAllRewardTypeSummaries() {
    this.logger.log(`[getAllRewardTypeSummaries] 请求所有收益类型汇总统计`);
    return this.systemAccountReportService.getAllRewardTypeSummaries();
  }

  // [2026-01-06] 新增：收益类型详细记录列表接口
  // 用于系统账户报表中展示各收益类型的明细列表
  // 回滚方式：删除以下 API 方法即可
  @Get('reward-entries-by-type')
  @ApiOperation({ summary: '获取指定收益类型的详细记录列表' })
  @ApiQuery({ name: 'rightType', required: true, description: '收益类型' })
  @ApiQuery({ name: 'page', required: false, description: '页码，默认1' })
  @ApiQuery({ name: 'pageSize', required: false, description: '每页条数，默认50' })
  @ApiResponse({ status: 200, description: '收益类型详细记录列表' })
  async getRewardEntriesByType(
    @Query('rightType') rightType: string,
    @Query('page') page?: string,
    @Query('pageSize') pageSize?: string,
  ) {
    this.logger.log(`[getRewardEntriesByType] 请求收益类型详细记录, rightType=${rightType}, page=${page}, pageSize=${pageSize}`);
    return this.systemAccountReportService.getRewardEntriesByType({
      rightType,
      page: page ? parseInt(page, 10) : undefined,
      pageSize: pageSize ? parseInt(pageSize, 10) : undefined,
    });
  }

  @Get('fee-entries-detailed')
  @ApiOperation({ summary: '获取手续费类型的详细记录列表（系统费用，非提现手续费）' })
  @ApiQuery({ name: 'page', required: false, description: '页码，默认1' })
  @ApiQuery({ name: 'pageSize', required: false, description: '每页条数，默认50' })
  @ApiResponse({ status: 200, description: '手续费类型详细记录列表' })
  async getFeeEntriesDetailed(
    @Query('page') page?: string,
    @Query('pageSize') pageSize?: string,
  ) {
    this.logger.log(`[getFeeEntriesDetailed] 请求手续费类型详细记录, page=${page}, pageSize=${pageSize}`);
    return this.systemAccountReportService.getFeeEntriesDetailed({
      page: page ? parseInt(page, 10) : undefined,
      pageSize: pageSize ? parseInt(pageSize, 10) : undefined,
    });
  }

  // [2026-01-06] 新增：手续费归集账户统计接口（真正的提现手续费）
  // 来源：wallet-service 中 S0000000006 账户的 FEE_COLLECTION 类型流水
  // 回滚方式：删除以下 API 方法即可

  @Get('fee-collection-summary')
  @ApiOperation({ summary: '获取手续费归集账户汇总统计（提现手续费）' })
  @ApiResponse({ status: 200, description: '手续费归集账户汇总统计' })
  async getFeeCollectionSummary() {
    this.logger.log(`[getFeeCollectionSummary] 请求手续费归集账户汇总统计`);
    return this.systemAccountReportService.getFeeCollectionSummary();
  }

  @Get('fee-collection-entries')
  @ApiOperation({ summary: '获取手续费归集账户流水明细（提现手续费）' })
  @ApiQuery({ name: 'page', required: false, description: '页码，默认1' })
  @ApiQuery({ name: 'pageSize', required: false, description: '每页条数，默认50' })
  @ApiQuery({ name: 'feeType', required: false, description: '手续费类型筛选 (WITHDRAWAL_FEE/FIAT_WITHDRAWAL_FEE)' })
  @ApiQuery({ name: 'startDate', required: false, description: '开始日期' })
  @ApiQuery({ name: 'endDate', required: false, description: '结束日期' })
  @ApiResponse({ status: 200, description: '手续费归集流水明细列表' })
  async getFeeCollectionEntries(
    @Query('page') page?: string,
    @Query('pageSize') pageSize?: string,
    @Query('feeType') feeType?: string,
    @Query('startDate') startDate?: string,
    @Query('endDate') endDate?: string,
  ) {
    this.logger.log(`[getFeeCollectionEntries] 请求手续费归集流水明细, page=${page}, pageSize=${pageSize}, feeType=${feeType}`);
    return this.systemAccountReportService.getFeeCollectionEntries({
      page: page ? parseInt(page, 10) : undefined,
      pageSize: pageSize ? parseInt(pageSize, 10) : undefined,
      feeType,
      startDate,
      endDate,
    });
  }
}
