/**
 * 法币提现管理服务
 * 用于管理后台审核和处理用户的法币提现申请
 */

import apiClient from '@/infrastructure/api/client';
import { API_ENDPOINTS } from '@/infrastructure/api/endpoints';
import type {
  FiatWithdrawalOrder,
  FiatWithdrawalListResponse,
  QueryFiatWithdrawalsParams,
  ReviewFiatWithdrawalRequest,
  StartPaymentRequest,
  CompletePaymentRequest,
  FiatWithdrawalStatus,
} from '@/types/withdrawal.types';

/**
 * 法币提现管理服务
 *
 * API 响应结构（经过 apiClient 拦截器解包后）:
 * { success: true, data: { code: "OK", message: "success", data: {...} } }
 *
 * 需要访问 .data.data 获取实际业务数据
 */
export const withdrawalService = {
  /**
   * 获取待审核的提现订单
   */
  async getPendingReview(params: QueryFiatWithdrawalsParams = {}): Promise<FiatWithdrawalListResponse> {
    const response = await apiClient.get(API_ENDPOINTS.FIAT_WITHDRAWALS.PENDING_REVIEW, { params });
    // eslint-disable-next-line @typescript-eslint/no-explicit-any
    const result = (response as any)?.data?.data;
    return result ?? { items: [], total: 0, page: 1, limit: 20 };
  },

  /**
   * 获取待打款的提现订单
   */
  async getPendingPayment(params: QueryFiatWithdrawalsParams = {}): Promise<FiatWithdrawalListResponse> {
    const response = await apiClient.get(API_ENDPOINTS.FIAT_WITHDRAWALS.PENDING_PAYMENT, { params });
    // eslint-disable-next-line @typescript-eslint/no-explicit-any
    const result = (response as any)?.data?.data;
    return result ?? { items: [], total: 0, page: 1, limit: 20 };
  },

  /**
   * 按状态获取提现订单
   */
  async getByStatus(status: FiatWithdrawalStatus, params: QueryFiatWithdrawalsParams = {}): Promise<FiatWithdrawalListResponse> {
    const response = await apiClient.get(API_ENDPOINTS.FIAT_WITHDRAWALS.BY_STATUS, {
      params: { ...params, status },
    });
    // eslint-disable-next-line @typescript-eslint/no-explicit-any
    const result = (response as any)?.data?.data;
    return result ?? { items: [], total: 0, page: 1, limit: 20 };
  },

  /**
   * 审核提现订单
   */
  async review(orderNo: string, data: ReviewFiatWithdrawalRequest): Promise<FiatWithdrawalOrder> {
    const response = await apiClient.post(API_ENDPOINTS.FIAT_WITHDRAWALS.REVIEW(orderNo), data);
    // eslint-disable-next-line @typescript-eslint/no-explicit-any
    return (response as any)?.data?.data;
  },

  /**
   * 开始打款
   */
  async startPayment(orderNo: string, data: StartPaymentRequest = {}): Promise<FiatWithdrawalOrder> {
    const response = await apiClient.post(API_ENDPOINTS.FIAT_WITHDRAWALS.START_PAYMENT(orderNo), data);
    // eslint-disable-next-line @typescript-eslint/no-explicit-any
    return (response as any)?.data?.data;
  },

  /**
   * 完成打款
   */
  async completePayment(orderNo: string, data: CompletePaymentRequest = {}): Promise<FiatWithdrawalOrder> {
    const response = await apiClient.post(API_ENDPOINTS.FIAT_WITHDRAWALS.COMPLETE_PAYMENT(orderNo), data);
    // eslint-disable-next-line @typescript-eslint/no-explicit-any
    return (response as any)?.data?.data;
  },
};

export default withdrawalService;
