import { apiClient } from '@/shared/api/client'

import type { ApiResponse, PaginatedResult } from '@/shared/types/types'

// 收款账号类型
export type AccountType = 'alipay' | 'wechat' | 'bank'

// 收款账号接口
export interface PaymentAccount {
  id: number
  user_id: number
  account_type: AccountType
  account_name: string
  account_no: string // 脱敏显示
  bank_name: string
  bank_branch: string
  certificate_urls: string[]
  is_default: boolean
  status: string
  created_at: string
  updated_at: string
}

// 创建收款账号请求
export interface CreatePaymentAccountRequest {
  account_type: AccountType
  account_name: string
  account_no: string
  bank_name?: string
  bank_branch?: string
  certificate_urls?: string[]
}

// 更新收款账号请求
export interface UpdatePaymentAccountRequest {
  bank_branch?: string
  certificate_urls?: string[]
  is_default?: boolean
}

// 提现状态
export type WithdrawalStatus = 'pending' | 'processing' | 'completed' | 'rejected' | 'cancelled'

// 提现申请接口
export interface WithdrawalRequest {
  id: number
  withdraw_no: string
  user_id: number
  amount_cent: number
  fee_cent: number
  actual_amount_cent: number
  account_type: AccountType
  account_name: string
  account_no: string
  bank_name: string
  status: WithdrawalStatus
  review_remark: string
  created_at: string
  updated_at: string
  reviewed_at: string | null
  paid_at: string | null
}

// 创建提现申请请求
export interface CreateWithdrawalRequest {
  payment_account_id: number
  amount_cent: number
}

// ========== 收款账号管理 API ==========

export async function fetchPaymentAccounts(page = 1, pageSize = 20) {
  const { data } = await apiClient.get<ApiResponse<PaginatedResult<PaymentAccount>>>(
    '/payment-accounts',
    {
      params: { page, page_size: pageSize },
    }
  )
  return data.data
}

export async function fetchPaymentAccount(id: number) {
  const { data } = await apiClient.get<ApiResponse<PaymentAccount>>(`/payment-accounts/${id}`)
  return data.data
}

export async function createPaymentAccount(req: CreatePaymentAccountRequest) {
  const { data } = await apiClient.post<ApiResponse<PaymentAccount>>('/payment-accounts', req)
  return data.data
}

export async function updatePaymentAccount(id: number, req: UpdatePaymentAccountRequest) {
  const { data } = await apiClient.put<ApiResponse<PaymentAccount>>(`/payment-accounts/${id}`, req)
  return data.data
}

export async function deletePaymentAccount(id: number) {
  const { data } = await apiClient.delete<ApiResponse<{ deleted: boolean }>>(
    `/payment-accounts/${id}`
  )
  return data.data
}

export async function setDefaultPaymentAccount(id: number) {
  const { data } = await apiClient.post<ApiResponse<{ updated: boolean }>>(
    `/payment-accounts/${id}/set-default`
  )
  return data.data
}

// ========== 提现申请 API ==========

export async function createWithdrawal(req: CreateWithdrawalRequest) {
  const { data } = await apiClient.post<ApiResponse<WithdrawalRequest>>('/withdrawals', req)
  return data.data
}

export async function fetchWithdrawals(page = 1, pageSize = 20) {
  const { data } = await apiClient.get<ApiResponse<PaginatedResult<WithdrawalRequest>>>(
    '/withdrawals',
    {
      params: { page, page_size: pageSize },
    }
  )
  return data.data
}

export async function fetchWithdrawal(id: number) {
  const { data } = await apiClient.get<ApiResponse<WithdrawalRequest>>(`/withdrawals/${id}`)
  return data.data
}

export async function cancelWithdrawal(id: number) {
  const { data } = await apiClient.post<ApiResponse<{ cancelled: boolean }>>(
    `/withdrawals/${id}/cancel`
  )
  return data.data
}
