import { Controller, Get, Param, Query, NotFoundException } from '@nestjs/common';
import { ApiTags, ApiOperation, ApiResponse, ApiParam, ApiQuery } from '@nestjs/swagger';
import { GetMiningAccountQuery } from '../../application/queries/get-mining-account.query';
import { GetMiningStatsQuery } from '../../application/queries/get-mining-stats.query';

@ApiTags('Mining')
@Controller('mining')
export class MiningController {
  constructor(
    private readonly getAccountQuery: GetMiningAccountQuery,
    private readonly getStatsQuery: GetMiningStatsQuery,
  ) {}

  @Get('stats')
  @ApiOperation({ summary: '获取挖矿统计数据' })
  @ApiResponse({ status: 200, description: '挖矿统计' })
  async getStats() {
    return this.getStatsQuery.execute();
  }

  @Get('ranking')
  @ApiOperation({ summary: '获取挖矿排行榜' })
  @ApiQuery({ name: 'limit', required: false, description: '返回数量限制', type: Number })
  @ApiResponse({ status: 200, description: '挖矿排行榜' })
  async getRanking(@Query('limit') limit?: number) {
    const ranking = await this.getStatsQuery.getRanking(limit ?? 100);
    return { data: ranking };
  }

  @Get('accounts/:accountSequence')
  @ApiOperation({ summary: '获取挖矿账户信息' })
  @ApiParam({ name: 'accountSequence', description: '账户序号' })
  @ApiResponse({ status: 200, description: '挖矿账户信息' })
  @ApiResponse({ status: 404, description: '账户不存在' })
  async getAccount(@Param('accountSequence') accountSequence: string) {
    const account = await this.getAccountQuery.execute(accountSequence);
    if (!account) {
      throw new NotFoundException(`Account ${accountSequence} not found`);
    }
    return account;
  }

  @Get('accounts/:accountSequence/records')
  @ApiOperation({ summary: '获取挖矿记录' })
  @ApiParam({ name: 'accountSequence', description: '账户序号' })
  @ApiQuery({ name: 'page', required: false, type: Number })
  @ApiQuery({ name: 'pageSize', required: false, type: Number })
  @ApiResponse({ status: 200, description: '挖矿记录列表' })
  async getRecords(
    @Param('accountSequence') accountSequence: string,
    @Query('page') page?: number,
    @Query('pageSize') pageSize?: number,
  ) {
    return this.getAccountQuery.getMiningRecords(
      accountSequence,
      page ?? 1,
      pageSize ?? 50,
    );
  }

  @Get('accounts/:accountSequence/transactions')
  @ApiOperation({ summary: '获取交易流水' })
  @ApiParam({ name: 'accountSequence', description: '账户序号' })
  @ApiQuery({ name: 'page', required: false, type: Number })
  @ApiQuery({ name: 'pageSize', required: false, type: Number })
  @ApiResponse({ status: 200, description: '交易流水列表' })
  async getTransactions(
    @Param('accountSequence') accountSequence: string,
    @Query('page') page?: number,
    @Query('pageSize') pageSize?: number,
  ) {
    return this.getAccountQuery.getTransactions(
      accountSequence,
      page ?? 1,
      pageSize ?? 50,
    );
  }
}
