/**
 * 佣金管理相关 API
 */

import { BaseService } from '../BaseService'
import type { BaseResponse } from '@/types/api'
import type {
  WithdrawalRequestQueryParams,
  WithdrawalRequestPageResult,
  ApproveWithdrawalParams,
  RejectWithdrawalParams,
  WithdrawalSettingListResult,
  WithdrawalSettingItem,
  CreateWithdrawalSettingParams,
  CommissionRecordQueryParams,
  SubmitWithdrawalParams,
  ShopCommissionRecordPageResult,
  ShopFundSummaryQueryParams,
  ShopFundSummaryPageResult,
  ShopCommissionStatsQueryParams,
  CommissionStatsResponse,
  DailyCommissionStatsQueryParams,
  DailyCommissionStatsItem,
  ResolveCommissionParams,
  MainWalletTransactionQueryParams,
  MainWalletTransactionPageResult
} from '@/types/api/commission'

export class CommissionService extends BaseService {
  // ==================== 提现申请管理 ====================

  /**
   * 获取提现申请列表
   * GET /api/admin/commission/withdrawal-requests
   */
  static getWithdrawalRequests(
    params?: WithdrawalRequestQueryParams
  ): Promise<BaseResponse<WithdrawalRequestPageResult>> {
    return this.get<BaseResponse<WithdrawalRequestPageResult>>(
      '/api/admin/commission/withdrawal-requests',
      params
    )
  }

  /**
   * 审批通过提现申请
   * POST /api/admin/commission/withdrawal-requests/{id}/approve
   */
  static approveWithdrawal(id: number, params?: ApproveWithdrawalParams): Promise<BaseResponse> {
    return this.post<BaseResponse>(
      `/api/admin/commission/withdrawal-requests/${id}/approve`,
      params
    )
  }

  /**
   * 拒绝提现申请
   * POST /api/admin/commission/withdrawal-requests/{id}/reject
   */
  static rejectWithdrawal(id: number, params: RejectWithdrawalParams): Promise<BaseResponse> {
    return this.post<BaseResponse>(`/api/admin/commission/withdrawal-requests/${id}/reject`, params)
  }

  // ==================== 提现配置管理 ====================

  /**
   * 获取提现配置列表
   * GET /api/admin/commission/withdrawal-settings
   */
  static getWithdrawalSettings(): Promise<BaseResponse<WithdrawalSettingListResult>> {
    return this.get<BaseResponse<WithdrawalSettingListResult>>(
      '/api/admin/commission/withdrawal-settings'
    )
  }

  /**
   * 新增提现配置
   * POST /api/admin/commission/withdrawal-settings
   */
  static createWithdrawalSetting(params: CreateWithdrawalSettingParams): Promise<BaseResponse> {
    return this.post<BaseResponse>('/api/admin/commission/withdrawal-settings', params)
  }

  /**
   * 获取当前生效的提现配置
   * GET /api/admin/commission/withdrawal-settings/current
   */
  static getCurrentWithdrawalSetting(): Promise<BaseResponse<WithdrawalSettingItem>> {
    return this.get<BaseResponse<WithdrawalSettingItem>>(
      '/api/admin/commission/withdrawal-settings/current'
    )
  }

  // ==================== 代理商资金概况 ====================

  /**
   * 获取代理商资金明细
   * GET /api/admin/shops/{shop_id}/commission-records
   */
  static getShopCommissionRecords(
    shopId: number,
    params?: CommissionRecordQueryParams
  ): Promise<BaseResponse<ShopCommissionRecordPageResult>> {
    return this.get<BaseResponse<ShopCommissionRecordPageResult>>(
      `/api/admin/shops/${shopId}/commission-records`,
      params
    )
  }

  /**
   * 获取代理商提现记录
   * GET /api/admin/shops/{shop_id}/withdrawal-requests
   */
  static getShopWithdrawalRequests(
    shopId: number,
    params?: WithdrawalRequestQueryParams
  ): Promise<BaseResponse<WithdrawalRequestPageResult>> {
    return this.get<BaseResponse<WithdrawalRequestPageResult>>(
      `/api/admin/shops/${shopId}/withdrawal-requests`,
      params
    )
  }

  /**
   * 获取代理商资金汇总列表（含预充值余额 + 佣金）
   * GET /api/admin/shops/fund-summary
   */
  static getShopFundSummary(
    params?: ShopFundSummaryQueryParams
  ): Promise<BaseResponse<ShopFundSummaryPageResult>> {
    return this.get<BaseResponse<ShopFundSummaryPageResult>>(
      '/api/admin/shops/fund-summary',
      params
    )
  }

  /**
   * 获取代理商资金统计
   * GET /api/admin/shops/{shop_id}/commission-stats
   */
  static getShopCommissionStats(
    shopId: number,
    params?: ShopCommissionStatsQueryParams
  ): Promise<BaseResponse<CommissionStatsResponse>> {
    return this.get<BaseResponse<CommissionStatsResponse>>(
      `/api/admin/shops/${shopId}/commission-stats`,
      params
    )
  }

  /**
   * 获取代理商每日佣金统计
   * GET /api/admin/shops/{shop_id}/commission-daily-stats
   */
  static getShopDailyCommissionStats(
    shopId: number,
    params?: DailyCommissionStatsQueryParams
  ): Promise<BaseResponse<DailyCommissionStatsItem[]>> {
    return this.get<BaseResponse<DailyCommissionStatsItem[]>>(
      `/api/admin/shops/${shopId}/commission-daily-stats`,
      params
    )
  }

  /**
   * 发起代理商提现申请
   * POST /api/admin/shops/{shop_id}/withdrawal-requests
   */
  static submitShopWithdrawalRequest(
    shopId: number,
    params: SubmitWithdrawalParams
  ): Promise<BaseResponse> {
    return this.post<BaseResponse>(`/api/admin/shops/${shopId}/withdrawal-requests`, params)
  }

  /**
   * 获取预充值钱包流水
   * GET /api/admin/shops/{shop_id}/main-wallet/transactions
   */
  static getMainWalletTransactions(
    shopId: number,
    params?: MainWalletTransactionQueryParams
  ): Promise<BaseResponse<MainWalletTransactionPageResult>> {
    return this.get<BaseResponse<MainWalletTransactionPageResult>>(
      `/api/admin/shops/${shopId}/main-wallet/transactions`,
      params
    )
  }

  // ==================== 佣金记录修正 ====================

  /**
   * 修正待审佣金记录
   * POST /api/admin/commission-records/{id}/resolve
   */
  static resolveCommissionRecord(
    id: number,
    params: ResolveCommissionParams
  ): Promise<BaseResponse> {
    return this.post<BaseResponse>(`/api/admin/commission-records/${id}/resolve`, params)
  }
}
