import { Controller, Get, Param, Query } from '@nestjs/common';
import { ApiTags, ApiOperation, ApiBearerAuth, ApiParam, ApiQuery } from '@nestjs/swagger';
import { SystemAccountsService } from '../../application/services/system-accounts.service';

@ApiTags('System Accounts')
@ApiBearerAuth()
@Controller('system-accounts')
export class SystemAccountsController {
  constructor(private readonly systemAccountsService: SystemAccountsService) {}

  @Get()
  @ApiOperation({ summary: '获取系统账户列表' })
  async getSystemAccounts() {
    return this.systemAccountsService.getSystemAccounts();
  }

  @Get('summary')
  @ApiOperation({ summary: '获取系统账户汇总' })
  async getSystemAccountsSummary() {
    return this.systemAccountsService.getSystemAccountsSummary();
  }

  @Get(':accountType/records')
  @ApiOperation({ summary: '获取系统账户挖矿记录' })
  @ApiParam({ name: 'accountType', type: String, description: '系统账户类型（OPERATION/PROVINCE/CITY/HEADQUARTERS）' })
  @ApiQuery({ name: 'regionCode', required: false, type: String, description: '区域代码（省/市代码）' })
  @ApiQuery({ name: 'page', required: false, type: Number })
  @ApiQuery({ name: 'pageSize', required: false, type: Number })
  async getSystemAccountMiningRecords(
    @Param('accountType') accountType: string,
    @Query('regionCode') regionCode?: string,
    @Query('page') page?: number,
    @Query('pageSize') pageSize?: number,
  ) {
    return this.systemAccountsService.getSystemAccountMiningRecords(
      accountType,
      regionCode || null,
      page ?? 1,
      pageSize ?? 20,
    );
  }

  @Get(':accountType/transactions')
  @ApiOperation({ summary: '获取系统账户交易记录' })
  @ApiParam({ name: 'accountType', type: String, description: '系统账户类型（OPERATION/PROVINCE/CITY/HEADQUARTERS）' })
  @ApiQuery({ name: 'regionCode', required: false, type: String, description: '区域代码（省/市代码）' })
  @ApiQuery({ name: 'referenceType', required: false, type: String, description: '关联类型筛选（如 BATCH_MINING）' })
  @ApiQuery({ name: 'page', required: false, type: Number })
  @ApiQuery({ name: 'pageSize', required: false, type: Number })
  async getSystemAccountTransactions(
    @Param('accountType') accountType: string,
    @Query('regionCode') regionCode?: string,
    @Query('referenceType') referenceType?: string,
    @Query('page') page?: number,
    @Query('pageSize') pageSize?: number,
  ) {
    return this.systemAccountsService.getSystemAccountTransactions(
      accountType,
      regionCode || null,
      page ?? 1,
      pageSize ?? 20,
      referenceType || null,
    );
  }

  @Get(':accountType/contributions')
  @ApiOperation({
    summary: '获取系统账户算力来源明细',
    description: '显示该账户的每笔算力来自哪个认种订单',
  })
  @ApiParam({
    name: 'accountType',
    type: String,
    description: '系统账户类型（OPERATION/PROVINCE/CITY/HEADQUARTERS）',
  })
  @ApiQuery({ name: 'regionCode', required: false, type: String, description: '区域代码（省/市代码）' })
  @ApiQuery({ name: 'page', required: false, type: Number, description: '页码，默认1' })
  @ApiQuery({ name: 'pageSize', required: false, type: Number, description: '每页数量，默认20' })
  async getSystemAccountContributionRecords(
    @Param('accountType') accountType: string,
    @Query('regionCode') regionCode?: string,
    @Query('page') page?: number,
    @Query('pageSize') pageSize?: number,
  ) {
    return this.systemAccountsService.getSystemAccountContributionRecords(
      accountType,
      regionCode || null,
      page ?? 1,
      pageSize ?? 20,
    );
  }

  @Get(':accountType/contribution-stats')
  @ApiOperation({
    summary: '获取系统账户算力明细统计',
    description: '显示算力来源的汇总信息，包括记录数、来源认种订单数、来源用户数等',
  })
  @ApiParam({
    name: 'accountType',
    type: String,
    description: '系统账户类型（OPERATION/PROVINCE/CITY/HEADQUARTERS）',
  })
  @ApiQuery({ name: 'regionCode', required: false, type: String, description: '区域代码（省/市代码）' })
  async getSystemAccountContributionStats(
    @Param('accountType') accountType: string,
    @Query('regionCode') regionCode?: string,
  ) {
    return this.systemAccountsService.getSystemAccountContributionStats(accountType, regionCode || null);
  }
}
