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

@ApiTags('Users')
@ApiBearerAuth()
@Controller('users')
export class UsersController {
  constructor(private readonly usersService: UsersService) {}

  @Get()
  @ApiOperation({ summary: '获取用户列表' })
  @ApiQuery({ name: 'page', required: false, type: Number })
  @ApiQuery({ name: 'pageSize', required: false, type: Number })
  @ApiQuery({ name: 'search', required: false, type: String, description: '手机号或账户序列号' })
  @ApiQuery({ name: 'status', required: false, type: String, description: 'ACTIVE, DISABLED, DELETED' })
  @ApiQuery({ name: 'kycStatus', required: false, type: String, description: 'PENDING, SUBMITTED, VERIFIED, REJECTED' })
  @ApiQuery({ name: 'hasAdopted', required: false, type: Boolean })
  async getUsers(
    @Query('page') page?: number,
    @Query('pageSize') pageSize?: number,
    @Query('search') search?: string,
    @Query('status') status?: string,
    @Query('kycStatus') kycStatus?: string,
    @Query('hasAdopted') hasAdopted?: boolean,
  ) {
    return this.usersService.getUsers({
      page: page ?? 1,
      pageSize: pageSize ?? 20,
      search,
      status,
      kycStatus,
      hasAdopted,
    });
  }

  @Get('p2p-transfers')
  @ApiOperation({ summary: '获取全部P2P转账记录（含手续费汇总）' })
  @ApiQuery({ name: 'page', required: false, type: Number })
  @ApiQuery({ name: 'pageSize', required: false, type: Number })
  @ApiQuery({ name: 'search', required: false, type: String, description: '搜索账号/手机号/转账单号' })
  async getP2pTransfers(
    @Query('page') page?: number,
    @Query('pageSize') pageSize?: number,
    @Query('search') search?: string,
  ) {
    return this.usersService.getP2pTransfers(
      page ?? 1,
      pageSize ?? 20,
      search,
    );
  }

  @Get(':accountSequence')
  @ApiOperation({ summary: '获取用户详情' })
  @ApiParam({ name: 'accountSequence', type: String })
  async getUserDetail(@Param('accountSequence') accountSequence: string) {
    return this.usersService.getUserDetail(accountSequence);
  }

  @Get(':accountSequence/contributions')
  @ApiOperation({ summary: '获取用户算力记录' })
  @ApiParam({ name: 'accountSequence', type: String })
  @ApiQuery({ name: 'page', required: false, type: Number })
  @ApiQuery({ name: 'pageSize', required: false, type: Number })
  @ApiQuery({ name: 'sortBy', required: false, type: String, description: '排序字段: sourceType, levelDepth, amount, createdAt' })
  @ApiQuery({ name: 'sortOrder', required: false, type: String, description: '排序方向: asc, desc' })
  async getUserContributions(
    @Param('accountSequence') accountSequence: string,
    @Query('page') page?: number,
    @Query('pageSize') pageSize?: number,
    @Query('sortBy') sortBy?: string,
    @Query('sortOrder') sortOrder?: string,
  ) {
    return this.usersService.getUserContributions(
      accountSequence,
      page ?? 1,
      pageSize ?? 20,
      sortBy,
      sortOrder as 'asc' | 'desc' | undefined,
    );
  }

  @Get(':accountSequence/mining-records')
  @ApiOperation({ summary: '获取用户挖矿记录' })
  @ApiParam({ name: 'accountSequence', type: String })
  @ApiQuery({ name: 'page', required: false, type: Number })
  @ApiQuery({ name: 'pageSize', required: false, type: Number })
  async getUserMiningRecords(
    @Param('accountSequence') accountSequence: string,
    @Query('page') page?: number,
    @Query('pageSize') pageSize?: number,
  ) {
    return this.usersService.getUserMiningRecords(
      accountSequence,
      page ?? 1,
      pageSize ?? 20,
    );
  }

  @Get(':accountSequence/orders')
  @ApiOperation({ summary: '获取用户交易订单' })
  @ApiParam({ name: 'accountSequence', type: String })
  @ApiQuery({ name: 'page', required: false, type: Number })
  @ApiQuery({ name: 'pageSize', required: false, type: Number })
  @ApiQuery({ name: 'type', required: false, type: String, description: 'BUY, SELL' })
  @ApiQuery({ name: 'status', required: false, type: String, description: 'PENDING, PARTIAL, FILLED, CANCELLED' })
  async getUserOrders(
    @Param('accountSequence') accountSequence: string,
    @Query('page') page?: number,
    @Query('pageSize') pageSize?: number,
    @Query('type') type?: string,
    @Query('status') status?: string,
  ) {
    return this.usersService.getUserOrders(accountSequence, {
      page: page ?? 1,
      pageSize: pageSize ?? 20,
      type,
      status,
    });
  }

  @Get(':accountSequence/referral-tree')
  @ApiOperation({ summary: '获取用户引荐关系树' })
  @ApiParam({ name: 'accountSequence', type: String })
  @ApiQuery({ name: 'direction', required: false, type: String, description: 'up, down, both' })
  @ApiQuery({ name: 'depth', required: false, type: Number })
  async getReferralTree(
    @Param('accountSequence') accountSequence: string,
    @Query('direction') direction?: string,
    @Query('depth') depth?: number,
  ) {
    return this.usersService.getReferralTree(accountSequence, direction || 'both', depth || 1);
  }

  @Get(':accountSequence/planting-ledger')
  @ApiOperation({ summary: '获取用户认种分类账' })
  @ApiParam({ name: 'accountSequence', type: String })
  @ApiQuery({ name: 'page', required: false, type: Number })
  @ApiQuery({ name: 'pageSize', required: false, type: Number })
  async getPlantingLedger(
    @Param('accountSequence') accountSequence: string,
    @Query('page') page?: number,
    @Query('pageSize') pageSize?: number,
  ) {
    return this.usersService.getPlantingLedger(accountSequence, page ?? 1, pageSize ?? 20);
  }

  @Get(':accountSequence/wallet-ledger')
  @ApiOperation({ summary: '获取用户钱包流水' })
  @ApiParam({ name: 'accountSequence', type: String })
  @ApiQuery({ name: 'page', required: false, type: Number })
  @ApiQuery({ name: 'pageSize', required: false, type: Number })
  async getWalletLedger(
    @Param('accountSequence') accountSequence: string,
    @Query('page') page?: number,
    @Query('pageSize') pageSize?: number,
  ) {
    return this.usersService.getWalletLedger(accountSequence, page ?? 1, pageSize ?? 20);
  }

  @Get(':accountSequence/batch-mining-records')
  @ApiOperation({ summary: '获取用户批量补发记录' })
  @ApiParam({ name: 'accountSequence', type: String })
  @ApiQuery({ name: 'page', required: false, type: Number })
  @ApiQuery({ name: 'pageSize', required: false, type: Number })
  async getBatchMiningRecords(
    @Param('accountSequence') accountSequence: string,
    @Query('page') page?: number,
    @Query('pageSize') pageSize?: number,
  ) {
    return this.usersService.getBatchMiningRecords(accountSequence, page ?? 1, pageSize ?? 20);
  }
}
