// 账户相关API服务
import { apiClient } from './client'
import type { AccountBalance, AccountSummary } from '@/types/account'

export interface PhysicalAccount {
  id: number
  account_no: string
  account_name: string
  bank_code: string
  bank_name: string
  status: 'active' | 'frozen' | 'closed'
  personal_balance: number
  labor_balance: number
  frozen_balance: number
  bank_balance: number
  transit_amount: number
  available_balance: number
  version: number
  created_at: string
  updated_at: string
}

export interface SubAccount {
  id: number
  physical_account_id: number
  sub_account_no: string
  sub_account_name: string
  sub_account_type: 'settlement' | 'management' | 'temporary'
  status: 'active' | 'frozen' | 'closed'
  balance: number
  frozen_amount: number
  version: number
  created_at: string
}

export class AccountAPI {
  // 获取实体账户列表
  static async getPhysicalAccounts(params?: {
    status?: string
    page?: number
    page_size?: number
  }): Promise<{
    items: PhysicalAccount[]
    total: number
    page: number
    page_size: number
  }> {
    return apiClient.get('/physical-accounts', params)
  }
  
  // 获取实体账户详情
  static async getPhysicalAccount(id: number): Promise<PhysicalAccount> {
    return apiClient.get(`/physical-accounts/${id}`)
  }
  
  // 创建实体账户
  static async createPhysicalAccount(data: {
    account_no?: string
    account_name: string
    bank_code?: string
    bank_name?: string
    initial_balance?: number
  }): Promise<PhysicalAccount> {
    return apiClient.post('/physical-accounts', data)
  }
  
  // 冻结实体账户
  static async freezePhysicalAccount(id: number, amount: number): Promise<PhysicalAccount> {
    return apiClient.post(`/physical-accounts/${id}/freeze`, { amount })
  }
  
  // 解冻实体账户
  static async unfreezePhysicalAccount(id: number, amount: number): Promise<PhysicalAccount> {
    return apiClient.post(`/physical-accounts/${id}/unfreeze`, { amount })
  }
  
  // 获取子账户列表
  static async getSubAccounts(physicalAccountId: number): Promise<{
    items: SubAccount[]
    total: number
  }> {
    return apiClient.get(`/physical-accounts/${physicalAccountId}/sub-accounts`)
  }
  
  // 获取子账户详情
  static async getSubAccount(id: number): Promise<SubAccount> {
    return apiClient.get(`/sub-accounts/${id}`)
  }
  
  // 获取子账户余额
  static async getSubAccountBalance(id: number): Promise<AccountBalance> {
    return apiClient.get(`/sub-accounts/${id}/balance`)
  }
  
  // 创建子账户
  static async createSubAccount(data: {
    physical_account_id: number
    sub_account_name: string
    sub_account_type: 'settlement' | 'management' | 'temporary'
  }): Promise<SubAccount> {
    return apiClient.post('/sub-accounts', data)
  }
  
  // 冻结子账户
  static async freezeSubAccount(id: number, amount: number): Promise<void> {
    return apiClient.post(`/sub-accounts/${id}/freeze`, { amount })
  }
  
  // 解冻子账户
  static async unfreezeSubAccount(id: number, amount: number): Promise<void> {
    return apiClient.post(`/sub-accounts/${id}/unfreeze`, { amount })
  }
  
  // 关闭子账户
  static async closeSubAccount(id: number): Promise<void> {
    return apiClient.post(`/sub-accounts/${id}/close`)
  }
  
  // 校验不变量（三账校验）
  static async validateInvariant(accountId: number): Promise<{
    physical_account_id: number
    bank_balance: number
    transit_net: number
    ledger_total: number
    expected_total: number
    difference: number
    is_balanced: boolean
    verified_at: string
  }> {
    return apiClient.get(`/reconciliation/three-account/${accountId}`)
  }
  
  // 获取账户余额详情
  static async getAccountBalance(id: number): Promise<AccountBalance> {
    return apiClient.get(`/physical-accounts/${id}/balance`)
  }
}
