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

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

// 管理员端提现详情接口
export interface WithdrawalDetail {
  id: number
  withdraw_no: string
  user_id: number
  user_nickname: string
  user_phone: string
  amount_cent: number
  fee_cent: number
  actual_amount_cent: number
  payment_account_id: number | null
  account_type: string
  account_name: string
  account_no: string // 管理员可见完整账号
  bank_name: string
  bank_branch: string
  certificate_urls: string[] // 收款二维码图片
  status: string
  reviewed_by: number | null
  reviewed_by_name: string
  reviewed_at: string | null
  review_remark: string
  paid_by: number | null
  paid_by_name: string
  paid_at: string | null
  payment_proof_url: string
  payment_remark: string
  created_at: string
  updated_at: string
}

// 审核请求
export interface ReviewWithdrawalRequest {
  approved: boolean
  remark: string
}

// 确认打款请求
export interface ConfirmPaymentRequest {
  payment_proof_url?: string
  remark: string
}

// ========== 管理员端提现管理 API ==========

export async function fetchAdminWithdrawals(params: {
  status?: string
  user_id?: number
  page?: number
  page_size?: number
}) {
  const { data } = await apiClient.get<ApiResponse<PaginatedResult<WithdrawalDetail>>>(
    '/admin/withdrawals',
    {
      params,
    }
  )
  return data.data
}

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

export async function reviewWithdrawal(id: number, req: ReviewWithdrawalRequest) {
  const { data } = await apiClient.post<ApiResponse<WithdrawalDetail>>(
    `/admin/withdrawals/${id}/review`,
    req
  )
  return data.data
}

export async function confirmPayment(id: number, req: ConfirmPaymentRequest) {
  const { data } = await apiClient.post<ApiResponse<WithdrawalDetail>>(
    `/admin/withdrawals/${id}/confirm-payment`,
    req
  )
  return data.data
}
