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

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

// 交易流水接口
export interface Transaction {
  id: number;
  accountId: number;
  direction: 'in' | 'out';
  transactionType: string;
  amount: number;
  balanceBefore: number;
  balanceAfter: number;
  description: string;
  relatedId?: number;
  relatedType?: string;
  createdAt: string;
}

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

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

  // 获取交易流水
  getTransactions(params?: {
    direction?: 'in' | 'out';
    transactionType?: string;
    startDate?: string;
    endDate?: string;
    page?: number;
    pageSize?: number;
  }) {
    return request<{ items: Transaction[]; total: number }>({
      url: '/api/app/finance/transactions',
      method: 'GET',
      data: params,
    });
  },

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

  // 获取提现记录
  getWithdrawals(params?: {
    status?: string;
    page?: number;
    pageSize?: number;
  }) {
    return request<{ items: Withdrawal[]; total: number }>({
      url: '/api/app/finance/withdrawals',
      method: 'GET',
      data: params,
    });
  },
};
