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(':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 })
  async getUserContributions(
    @Param('accountSequence') accountSequence: string,
    @Query('page') page?: number,
    @Query('pageSize') pageSize?: number,
  ) {
    return this.usersService.getUserContributions(
      accountSequence,
      page ?? 1,
      pageSize ?? 20,
    );
  }

  @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,
    });
  }
}
