import { requestClient } from '#/api/request'

// 支付配置接口
export interface PaymentConfig {
  // 支付宝配置
  alipay?: {
    alipay_app_id?: string
    alipay_gateway_url?: string
    alipay_private_key?: string
    alipay_public_key?: string
    alipay_sign_type?: string
    alipay_charset?: string
    alipay_notify_url?: string
    alipay_return_url?: string
    alipay_enabled?: boolean
  }
  // 微信支付配置
  wechat?: {
    wechat_app_id?: string
    wechat_mch_id?: string
    wechat_key?: string
    wechat_secret?: string
    wechat_cert_path?: string
    wechat_key_path?: string
    wechat_notify_url?: string
    wechat_trade_type?: string
    wechat_enabled?: boolean
  }
  // 通用配置
  general?: {
    default_method?: string
    timeout?: number
    min_amount?: number
    max_amount?: number
    success_url?: string
    fail_url?: string
    balance_enabled?: boolean
    points_enabled?: boolean
    points_ratio?: number
  }
}

// 支付统计接口
export interface PaymentStats {
  total_amount: number
  total_count: number
  success_count: number
  fail_count: number
  pending_count: number
  today_amount: number
  today_count: number
  alipay_amount: number
  alipay_count: number
  wechat_amount: number
  wechat_count: number
  balance_amount: number
  balance_count: number
}

// 支付记录接口
export interface PaymentRecord {
  id: number
  order_id: string
  user_id: number
  method: string
  amount: number
  status: string
  trade_no?: string
  transaction_id?: string
  subject: string
  body?: string
  notify_data?: any
  created_at: string
  updated_at: string
  user?: {
    id: number
    username: string
    nickname?: string
  }
}

// 退款记录接口
export interface RefundRecord {
  id: number
  payment_id: number
  refund_no: string
  amount: number
  reason: string
  status: string
  refund_data?: any
  created_at: string
  updated_at: string
  payment?: PaymentRecord
}

// 测试支付参数
export interface TestPaymentParams {
  method: string
  amount: number
  subject: string
  body?: string
}

// 测试支付结果
export interface TestPaymentResult {
  order_id: string
  payUrl?: string
  qrCode?: string
  message: string
}

// 获取支付配置
export function getPaymentConfigApi() {
  return requestClient.get<PaymentConfig>('/admin/settings/payment')
}

// 更新支付配置
export function updatePaymentConfigApi(data: {
  type: 'alipay' | 'wechat' | 'general'
  config: any
}) {
  return requestClient.put('/admin/settings/payment', data)
}

// 测试支付
export function testPaymentApi(data: TestPaymentParams) {
  return requestClient.post<TestPaymentResult>('/admin/payment/test', data)
}

// 重置支付配置
export function resetPaymentConfigApi(type: 'alipay' | 'wechat' | 'general') {
  return requestClient.delete(`/admin/settings/payment/${type}`)
}

// 获取支付统计
export function getPaymentStatsApi(params?: {
  start_date?: string
  end_date?: string
  method?: string
}) {
  return requestClient.get<PaymentStats>('/admin/payment/stats', { params })
}

// 获取支付记录
export function getPaymentRecordsApi(params?: {
  page?: number
  limit?: number
  method?: string
  status?: string
  user_id?: number
  order_id?: string
  start_date?: string
  end_date?: string
}) {
  return requestClient.get<{
    data: PaymentRecord[]
    total: number
    page: number
    limit: number
  }>('/admin/payment/records', { params })
}

// 获取退款记录
export function getRefundRecordsApi(params?: {
  page?: number
  limit?: number
  status?: string
  payment_id?: number
  start_date?: string
  end_date?: string
}) {
  return requestClient.get<{
    data: RefundRecord[]
    total: number
    page: number
    limit: number
  }>('/admin/payment/refunds', { params })
}

// 处理退款
export function processRefundApi(data: {
  payment_id: number
  amount: number
  reason: string
}) {
  return requestClient.post('/admin/payment/refund', data)
}

// 查询支付状态
export function queryPaymentStatusApi(orderId: string) {
  return requestClient.get(`/admin/payment/query/${orderId}`)
}

// 同步支付状态
export function syncPaymentStatusApi(paymentId: number) {
  return requestClient.post(`/admin/payment/sync/${paymentId}`)
}

// 批量同步支付状态
export function batchSyncPaymentStatusApi(paymentIds: number[]) {
  return requestClient.post('/admin/payment/batch-sync', { payment_ids: paymentIds })
}

// 关闭支付订单
export function closePaymentOrderApi(orderId: string) {
  return requestClient.post(`/admin/payment/close/${orderId}`)
}

// 验证支付配置
export function validatePaymentConfigApi(data: {
  type: 'alipay' | 'wechat'
  config: any
}) {
  return requestClient.post('/admin/payment/validate', data)
}

// 获取支付方式模板
export function getPaymentMethodTemplateApi(method: 'alipay' | 'wechat') {
  return requestClient.get(`/admin/payment/template/${method}`)
}