import { Controller, Get, Param, Query, Req } from '@nestjs/common';
import { ApiTags, ApiOperation, ApiParam, ApiQuery, ApiBearerAuth } from '@nestjs/swagger';
import { AssetService } from '../../application/services/asset.service';
import { TradingSellRestrictionService } from '../../application/services/sell-restriction.service';
import { RequireCapability } from '../../shared/decorators/require-capability.decorator';
import { Public } from '../../shared/guards/jwt-auth.guard';

@ApiTags('Asset')
@ApiBearerAuth()
@Controller('asset')
export class AssetController {
  constructor(
    private readonly assetService: AssetService,
    private readonly sellRestrictionService: TradingSellRestrictionService,
  ) {}

  @Get('my')
  @RequireCapability('VIEW_ASSET')
  @ApiOperation({ summary: '获取我的资产显示' })
  @ApiQuery({ name: 'dailyAllocation', required: false, type: String, description: '每日分配量（可选）' })
  async getMyAsset(@Req() req: any, @Query('dailyAllocation') dailyAllocation?: string) {
    const accountSequence = req.user?.accountSequence;
    if (!accountSequence) {
      throw new Error('Unauthorized');
    }

    const asset = await this.assetService.getAssetDisplay(accountSequence, dailyAllocation);
    if (!asset) {
      throw new Error('Account not found');
    }

    return asset;
  }

  @Get('account/:accountSequence')
  @Public()
  @ApiOperation({ summary: '获取指定账户资产显示' })
  @ApiParam({ name: 'accountSequence', description: '账户序号' })
  @ApiQuery({ name: 'dailyAllocation', required: false, type: String, description: '每日分配量（可选）' })
  async getAccountAsset(
    @Param('accountSequence') accountSequence: string,
    @Query('dailyAllocation') dailyAllocation?: string,
  ) {
    const asset = await this.assetService.getAssetDisplay(accountSequence, dailyAllocation);
    if (!asset) {
      return { message: 'Account not found' };
    }
    return asset;
  }

  @Get('estimate-sell')
  @Public()
  @ApiOperation({ summary: '预估卖出收益' })
  @ApiQuery({ name: 'quantity', required: true, type: String, description: '卖出数量' })
  async estimateSellProceeds(@Query('quantity') quantity: string) {
    return this.assetService.estimateSellProceeds(quantity);
  }

  @Get('market')
  @Public()
  @ApiOperation({ summary: '获取市场概览' })
  async getMarketOverview() {
    return this.assetService.getMarketOverview();
  }

  @Get('growth-per-second')
  @Public()
  @ApiOperation({ summary: '计算资产每秒增长量' })
  @ApiQuery({ name: 'dailyAllocation', required: true, type: String, description: '每日分配量' })
  async calculateGrowthPerSecond(@Query('dailyAllocation') dailyAllocation: string) {
    const perSecond = this.assetService.calculateAssetGrowthPerSecond(dailyAllocation);
    return { dailyAllocation, assetGrowthPerSecond: perSecond };
  }

  // [2026-03-04] 预种卖出限制状态查询（供 mobile app 前端判断是否禁用卖出按钮）
  @Get('sell-restriction')
  @ApiOperation({ summary: '查询当前用户预种卖出限制状态' })
  async getSellRestriction(@Req() req: any) {
    const accountSequence = req.user?.accountSequence;
    if (!accountSequence) throw new Error('Unauthorized');
    const isRestricted = await this.sellRestrictionService.isRestricted(accountSequence);
    return { isRestricted };
  }
}
