import {
  Controller,
  Get,
  Post,
  Put,
  Param,
  Body,
  Query,
  UseGuards,
} from '@nestjs/common';
import { ApiTags, ApiOperation, ApiBearerAuth } from '@nestjs/swagger';
import { WithdrawalService } from '@/modules/shared/finance/withdrawal.service';
import { JwtAuthGuard, RolesGuard } from '@/common';
import { Roles } from '@/common/decorators/roles.decorator';
import { CurrentUser } from '@/common/decorators/current-user.decorator';
import {
  CreatePlatformWithdrawalDto,
  QueryPlatformWithdrawalDto,
  QueryUserWithdrawalDto,
  QueryMerchantWithdrawalDto,
  RejectWithdrawalDto,
  ConfirmPaymentDto,
} from '@/modules/shared/finance/dto/withdrawal.dto';

@ApiTags('提现管理(管理员)')
@Controller('admin/finance/withdrawals')
@UseGuards(JwtAuthGuard, RolesGuard)
@Roles('admin')
@ApiBearerAuth()
export class WithdrawalAdminController {
  constructor(private readonly withdrawalService: WithdrawalService) {}

  // ==================== 平台提现 ====================

  @Post('platform')
  @ApiOperation({ summary: '平台申请提现' })
  async createPlatformWithdrawal(@Body() dto: CreatePlatformWithdrawalDto) {
    return this.withdrawalService.createPlatformWithdrawal(dto);
  }

  @Get('platform')
  @ApiOperation({ summary: '查询平台提现记录' })
  async getPlatformWithdrawals(@Query() dto: QueryPlatformWithdrawalDto) {
    return this.withdrawalService.getPlatformWithdrawals({
      status: dto.status,
      page: dto.page,
      pageSize: dto.pageSize,
    });
  }

  // ==================== 用户提现审核 ====================

  @Get('users')
  @ApiOperation({ summary: '查询用户提现列表' })
  async getUserWithdrawals(@Query() dto: QueryUserWithdrawalDto) {
    return this.withdrawalService.getUserWithdrawals({
      status: dto.status,
      page: dto.page,
      pageSize: dto.pageSize,
    });
  }

  // ==================== 商家提现审核 ====================

  @Get('merchants')
  @ApiOperation({ summary: '查询商家提现列表' })
  async getMerchantWithdrawals(@Query() dto: QueryMerchantWithdrawalDto) {
    return this.withdrawalService.getMerchantWithdrawals({
      status: dto.status,
      page: dto.page,
      pageSize: dto.pageSize,
    });
  }

  // ==================== 提现审核操作 ====================

  @Put(':id/approve')
  @ApiOperation({ summary: '审核通过提现' })
  async approveWithdrawal(
    @Param('id') id: number,
    @CurrentUser('sub') reviewerId: number,
    @Query('type') type: 'user' | 'merchant' | 'platform',
  ) {
    if (type === 'user') {
      return this.withdrawalService.approveUserWithdrawal(id, reviewerId);
    } else if (type === 'merchant') {
      return this.withdrawalService.approveMerchantWithdrawal(id, reviewerId);
    } else if (type === 'platform') {
      return this.withdrawalService.approvePlatformWithdrawal(id, reviewerId);
    }
    return { message: '无效的类型' };
  }

  @Put(':id/reject')
  @ApiOperation({ summary: '审核拒绝提现' })
  async rejectWithdrawal(
    @Param('id') id: number,
    @CurrentUser('sub') reviewerId: number,
    @Query('type') type: 'user' | 'merchant' | 'platform',
    @Body() dto: RejectWithdrawalDto,
  ) {
    if (type === 'user') {
      return this.withdrawalService.rejectUserWithdrawal(id, reviewerId, dto.rejectReason);
    } else if (type === 'merchant') {
      return this.withdrawalService.rejectMerchantWithdrawal(id, reviewerId, dto.rejectReason);
    } else if (type === 'platform') {
      return this.withdrawalService.rejectPlatformWithdrawal(id, reviewerId, dto.rejectReason);
    }
    return { message: '无效的类型' };
  }

  @Put(':id/confirm')
  @ApiOperation({ summary: '确认打款' })
  async confirmPayment(
    @Param('id') id: number,
    @Query('type') type: 'user' | 'merchant' | 'platform',
    @Body() dto: ConfirmPaymentDto,
  ) {
    if (type === 'user') {
      return this.withdrawalService.payUserWithdrawal(id, dto.paymentNo);
    } else if (type === 'merchant') {
      return this.withdrawalService.payMerchantWithdrawal(id, dto.paymentNo);
    } else if (type === 'platform') {
      return this.withdrawalService.payPlatformWithdrawal(id, dto.paymentNo);
    }
    return { message: '无效的类型' };
  }
}
