// 对账相关API服务
import { apiClient } from './client'

export interface ReconciliationBatch {
  id: number
  batchNo: string
  reconDate: string
  status: string
  totalTransactions: number
  matchedCount: number
  mismatchedCount: number
  bankTotal: number
  ledgerTotal: number
  inTransitNet: number
  threeAccountBalanced: boolean
  difference: number
  createdAt: string
  completedAt?: string
}

export interface ReconciliationItem {
  id: number
  batchId: number
  transactionId?: number
  bankRefNo?: string
  txnType: string
  amount: number
  bankAmount?: number
  ledgerAmount?: number
  matchStatus: 'matched' | 'mismatched' | 'bank_only' | 'ledger_only'
  matchTime?: string
  remarks?: string
}

export interface ThreeAccountResult {
  bankBalance: number
  transitNet: number
  ledgerTotal: number
  isBalanced: boolean
  difference: number
}

export class ReconciliationAPI {
  // 获取对账批次列表
  static async getBatches(params?: {
    status?: string
    startDate?: string
    endDate?: string
    page?: number
    pageSize?: number
  }): Promise<{
    items: ReconciliationBatch[]
    total: number
  }> {
    return apiClient.get('/reconciliation/batches', params)
  }
  
  // 获取对账批次详情
  static async getBatch(id: number): Promise<ReconciliationBatch> {
    return apiClient.get(`/reconciliation/batches/${id}`)
  }
  
  // 创建对账批次
  static async createBatch(data: {
    reconDate: string
    accountIds?: number[]
  }): Promise<ReconciliationBatch> {
    return apiClient.post('/reconciliation/batches', data)
  }
  
  // 执行对账
  static async executeBatch(id: number): Promise<ReconciliationBatch> {
    return apiClient.post(`/reconciliation/batches/${id}/execute`)
  }
  
  // 获取对账明细
  static async getBatchItems(batchId: number, params?: {
    matchStatus?: string
    page?: number
    pageSize?: number
  }): Promise<{
    items: ReconciliationItem[]
    total: number
  }> {
    return apiClient.get(`/reconciliation/batches/${batchId}/items`, params)
  }
  
  // 三账校验
  static async verifyThreeAccounts(accountId: number): Promise<ThreeAccountResult> {
    return apiClient.get(`/reconciliation/three-account/${accountId}`)
  }
  
  // 手工调整
  static async createAdjustment(data: {
    batchId: number
    transactionId?: number
    adjustmentType: string
    amount: number
    reason: string
    operator: string
  }): Promise<ReconciliationItem> {
    return apiClient.post('/reconciliation/adjustments', data)
  }
  
  // 获取调整记录
  static async getAdjustments(batchId: number): Promise<ReconciliationItem[]> {
    return apiClient.get(`/reconciliation/batches/${batchId}/adjustments`)
  }
  
  // 对账统计
  static async getStats(params: {
    startDate?: string
    endDate?: string
    accountId?: number
  }): Promise<{
    totalBatches: number
    successRate: number
    totalDiscrepancy: number
    largestDiscrepancy: number
  }> {
    return apiClient.get('/reconciliation/stats', params)
  }
  
  // 导出对账报告
  static async exportReport(batchId: number, format: 'excel' | 'pdf' = 'excel'): Promise<Blob> {
    return apiClient.get(`/reconciliation/batches/${batchId}/export`, 
      { format }, 
      { responseType: 'blob' }
    )
  }
}
