// 交易相关API服务
import { apiClient } from './client'
import type { 
  SystemTransaction, 
  BankTransaction, 
  TransactionQuery, 
  TransactionListResponse,
  CreateTransactionRequest,
  TransferRequest,
  TransferResponse 
} from '@/types/transaction'

export class TransactionAPI {
  // 获取交易详情
  static async getTransaction(id: number): Promise<SystemTransaction> {
    return apiClient.get(`/transactions/${id}`)
  }
  
  // 根据交易号获取交易
  static async getTransactionByNo(txnNo: string): Promise<SystemTransaction> {
    return apiClient.get(`/transactions/by-no/${txnNo}`)
  }
  
  // 获取交易列表
  static async getTransactionList(query: TransactionQuery = {}): Promise<TransactionListResponse> {
    return apiClient.get('/transactions', query)
  }
  
  // 创建交易
  static async createTransaction(data: CreateTransactionRequest): Promise<SystemTransaction> {
    return apiClient.post('/transactions', data)
  }
  
  // 提交交易到银行
  static async submitToBank(id: number): Promise<{ bankRefNo: string }> {
    return apiClient.post(`/transactions/${id}/submit`)
  }
  
  // 取消交易
  static async cancelTransaction(id: number): Promise<void> {
    return apiClient.post(`/transactions/${id}/cancel`)
  }
  
  // 重试交易
  static async retryTransaction(id: number): Promise<SystemTransaction> {
    return apiClient.post(`/transactions/${id}/retry`)
  }
  
  // 获取交易状态
  static async getTransactionStatus(id: number): Promise<{ status: string, bankRefNo?: string }> {
    return apiClient.get(`/transactions/${id}/status`)
  }
  
  // 转账操作
  static async transfer(data: TransferRequest): Promise<TransferResponse> {
    return apiClient.post('/transfer', data)
  }
  
  // 查询银行流水
  static async getBankStatements(params: {
    accountNo: string
    startDate: string
    endDate: string
    page?: number
    pageSize?: number
  }): Promise<{
    items: BankTransaction[]
    total: number
  }> {
    return apiClient.get('/bank/statements', params)
  }
  
  // 查询交易统计
  static async getTransactionStats(params: {
    accountId?: number
    startDate?: string
    endDate?: string
  }): Promise<{
    totalCount: number
    totalAmount: number
    successCount: number
    failedCount: number
    pendingCount: number
  }> {
    return apiClient.get('/transactions/stats', params)
  }
  
  // 获取交易历史（分页）
  static async getTransactionHistory(params: {
    accountId: number
    page?: number
    pageSize?: number
    startDate?: string
    endDate?: string
  }): Promise<{
    items: SystemTransaction[]
    total: number
    page: number
    pageSize: number
  }> {
    return apiClient.get(`/accounts/${params.accountId}/transactions`, params)
  }
}
