// 个性域名绑定（对齐 PC /admin/domain-bindings，基址走 websopy-api）
import { get, post } from '@/utils/request'
import { WebsopyBaseUrl } from '@/config/app'

const BASE = '/app/domain'

export interface AppDomain {
  id?: number
  tenantId?: number
  productId?: number
  domain?: string
  targetIp?: string
  recordType?: string
  status?: number // 0待解析 1校验中 2已生效 3已拒绝 4已停用
  dnsStatus?: number
  dnsIp?: string
  proxyConfigured?: number
  rejectReason?: string
  tenantName?: string
  createTime?: string
}

/** 管理端列表 */
export async function pageAppDomain(params: any) {
  const res = await get<any>(`${BASE}/admin/list`, params, { baseUrl: WebsopyBaseUrl })
  if (res?.code === 0) return res.data
  return Promise.reject(new Error(res?.message || '加载失败'))
}

export async function verifyDomain(id: number) {
  const res = await post<any>(`${BASE}/verify/${id}`, {}, { baseUrl: WebsopyBaseUrl })
  if (res?.code === 0) return res.data
  return Promise.reject(new Error(res?.message || '校验失败'))
}

export async function setProxy(id: number) {
  const res = await post<any>(`${BASE}/proxy/${id}`, {}, { baseUrl: WebsopyBaseUrl })
  if (res?.code === 0) return res.data
  return Promise.reject(new Error(res?.message || '操作失败'))
}

export async function activeDomain(id: number) {
  const res = await post<any>(`${BASE}/active/${id}`, {}, { baseUrl: WebsopyBaseUrl })
  if (res?.code === 0) return res.data
  return Promise.reject(new Error(res?.message || '操作失败'))
}

export async function rejectDomain(id: number, reason: string) {
  const res = await post<any>(`${BASE}/reject/${id}`, { reason }, { baseUrl: WebsopyBaseUrl })
  if (res?.code === 0) return res.message
  return Promise.reject(new Error(res?.message || '操作失败'))
}

export async function disableDomain(id: number) {
  const res = await post<any>(`${BASE}/disable/${id}`, {}, { baseUrl: WebsopyBaseUrl })
  if (res?.code === 0) return res.message
  return Promise.reject(new Error(res?.message || '操作失败'))
}
