import client from "./client"
import type { ApiResponse } from "../types"

export interface VerificationStatus {
  status: "unverified" | "pending" | "verified" | "rejected" | string
  is_verified: boolean
  real_name: string | null
  id_card_no: string | null
  verified_at: string | null
  reject_reason: string | null
  quota?: VerificationQuota | null
}

export interface VerificationOptions {
  enabled: boolean
  provider: string | null
  supported_modes: string[]
  default_mode: string
  material_retention_days: number
  quota?: Pick<VerificationQuota, "free_quota_per_user" | "purchase_enabled" | "price_cents"> | null
}

export interface VerificationQuota {
  free_quota_per_user: number
  purchase_enabled: boolean
  price_cents: number
  free_used: number
  free_remaining: number
  paid_remaining: number
  paid_purchased: number
  paid_used: number
  remaining_total: number
  balance_cents: number
  can_submit: boolean
  can_purchase: boolean
}

export interface VerificationSubmitResult {
  record_id: number
  passed: boolean
  status: string
  mode: string
  provider: string
  score?: number | null
  verified_at?: string | null
  reject_reason?: string | null
  reason?: string | null
  quota?: VerificationQuota | null
}

export async function getVerificationStatus() {
  const { data } = await client.get<ApiResponse<VerificationStatus>>("/v1/me/verification-status")
  return data
}

export async function getVerificationOptions() {
  const { data } = await client.get<ApiResponse<VerificationOptions>>("/v1/me/verification-options")
  return data
}

export async function submitTwoFactorVerification(payload: { real_name: string; id_card_no: string }) {
  const { data } = await client.post<ApiResponse<VerificationSubmitResult>>(
    "/v1/me/verification/two-factor",
    payload,
  )
  return data
}

export async function submitThreeFactorVerification(payload: { real_name: string; id_card_no: string; phone: string }) {
  const { data } = await client.post<ApiResponse<VerificationSubmitResult>>(
    "/v1/me/verification/three-factor",
    payload,
  )
  return data
}

export async function createRedirectVerificationSession(payload: {
  real_name: string
  id_card_no: string
  return_url: string
  notify_url?: string | null
}) {
  const { data } = await client.post<ApiResponse<{ record_id: number; url: string; qr_payload: string; external_id: string | null; expires_in: number | null }>>(
    "/v1/me/verification/redirect-session",
    payload,
  )
  return data
}

export async function queryRedirectVerificationResult(payload: {
  external_id?: string | null
  record_id?: number | null
}) {
  const { data } = await client.post<ApiResponse<VerificationSubmitResult>>(
    "/v1/me/verification/redirect-result",
    payload,
  )
  return data
}

export async function purchaseVerificationQuota(payload: { count: number }) {
  const { data } = await client.post<ApiResponse<{
    purchased_count: number
    amount_cents: number
    balance_cents: number
    quota: VerificationQuota
  }>>("/v1/me/verification/quota/purchase", payload)
  return data
}
