import { request } from '@/utils/request';

// 钱包信息接口
export interface WalletInfo {
  balance: number;
  frozenBalance: number;
  availableBalance: number;
}

// 提现记录接口
export interface Withdrawal {
  id: number;
  userId: number;
  amount: number;
  accountType: 'alipay' | 'wechat';
  accountName: string;
  accountNumber: string;
  status: 'pending' | 'approved' | 'rejected' | 'paid';
  remark?: string;
  processedAt?: string;
  createdAt: string;
}

// 钱包API
export const walletApi = {
  // 获取钱包信息
  getWallet() {
    return request<WalletInfo>({
      url: '/api/app/finance/wallet',
      method: 'GET',
    });
  },

  // 申请提现
  withdraw(data: {
    amount: number;
    paymentChannel: 'wechat' | 'alipay';
  }) {
    return request({
      url: '/api/app/finance/withdraw',
      method: 'POST',
      data,
    });
  },

  // 获取提现记录
  getWithdrawals(params?: {
    status?: string;
    page?: number;
    pageSize?: number;
  }) {
    // 构建查询字符串
    const query = new URLSearchParams();
    if (params?.status) query.append('status', params.status);
    if (params?.page) query.append('page', params.page.toString());
    if (params?.pageSize) query.append('pageSize', params.pageSize.toString());

    const queryString = query.toString();
    const url = queryString ? `/api/app/finance/withdrawals?${queryString}` : '/api/app/finance/withdrawals';

    return request<{ list: Withdrawal[]; total: number }>({
      url,
      method: 'GET',
    });
  },
};
