import { Controller, Get, Put, Body, UseGuards } from '@nestjs/common';
import { ApiTags, ApiOperation, ApiBearerAuth } from '@nestjs/swagger';
import { ConfigService } from '@/modules/shared/config/config.service';
import { JwtAuthGuard, RolesGuard, Roles } from '@/common';
import { UploadService } from '@/modules/shared/upload/upload.service';

@ApiTags('管理端-系统配置')
@ApiBearerAuth()
@UseGuards(JwtAuthGuard, RolesGuard)
@Roles('admin')
@Controller('admin/config')
export class AdminConfigController {
  constructor(
    private readonly configService: ConfigService,
    private readonly uploadService: UploadService,
  ) {}

  @Get('service-fee')
  @ApiOperation({ summary: '获取服务费配置' })
  async getServiceFeeConfig() {
    return this.configService.getServiceFeeConfig();
  }

  @Put('service-fee')
  @ApiOperation({ summary: '更新服务费配置' })
  async updateServiceFeeConfig(@Body() body: { rate: number }) {
    await this.configService.updateServiceFeeRate(body.rate);
    return this.configService.getServiceFeeConfig();
  }

  @Get('storage')
  @ApiOperation({ summary: '获取存储配置' })
  async getStorageConfig() {
    return this.uploadService.getStorageConfig();
  }

  @Put('storage')
  @ApiOperation({ summary: '更新存储配置' })
  async updateStorageConfig(@Body() body: Record<string, string>) {
    await this.uploadService.updateStorageConfig(body);
    return this.uploadService.getStorageConfig();
  }

  @Get('withdraw')
  @ApiOperation({ summary: '获取提现配置' })
  async getWithdrawConfig() {
    const [merchantMin, platformMin] = await Promise.all([
      this.configService.getMerchantMinWithdrawAmount(),
      this.configService.getPlatformMinWithdrawAmount(),
    ]);
    return {
      merchantMinWithdrawAmount: merchantMin,
      platformMinWithdrawAmount: platformMin,
    };
  }

  @Put('withdraw/merchant-min')
  @ApiOperation({ summary: '设置商家提现最低金额' })
  async setMerchantMinWithdrawAmount(@Body() body: { amount: number }) {
    await this.configService.setMerchantMinWithdrawAmount(body.amount);
    return { amount: await this.configService.getMerchantMinWithdrawAmount() };
  }

  @Put('withdraw/platform-min')
  @ApiOperation({ summary: '设置平台提现最低金额' })
  async setPlatformMinWithdrawAmount(@Body() body: { amount: number }) {
    await this.configService.setPlatformMinWithdrawAmount(body.amount);
    return { amount: await this.configService.getPlatformMinWithdrawAmount() };
  }
}
