import adminClient from "./client"
import type { ApiResponse } from "../../types"
import { listResult } from "./response"

export type BusinessSettingValue = number | string | boolean

export interface BusinessSetting {
  key: string
  value: BusinessSettingValue
  label: string
  description: string | null
  value_type: "integer" | "boolean" | "text" | "url" | "password" | "email" | string
  min_value: number | null
  max_value: number | null
  unit: string
  group: "platform" | "refund" | "lifecycle" | "upstream" | "finance" | "promotion" | "notification_email" | "notification_sms" | string
}

export interface FeaturePluginField {
  key: string
  name: string
  label: string
  type: string
  required: boolean
  secret: boolean
  default?: BusinessSettingValue
  placeholder?: string
  help?: string
  validation?: Record<string, unknown>
}

export interface FeaturePlugin {
  schema_version: number
  id: string
  type: "email" | "sms" | string
  name: string
  version: string
  status: string
  description: string
  provider: string
  configurable: boolean
  config_prefix: string
  test_supported: boolean
  capabilities: string[]
  config_fields: FeaturePluginField[]
  docs?: string
  manifest_path: string
}

export async function getBusinessRules() {
  const { data } = await adminClient.get<ApiResponse<BusinessSetting[] | { items: BusinessSetting[] }>>("/v1/admin/settings/business-rules")
  return { ...data, data: listResult<BusinessSetting>(data.data) }
}

export async function updateBusinessRules(values: Record<string, BusinessSettingValue>) {
  const { data } = await adminClient.put<ApiResponse<BusinessSetting[] | { items: BusinessSetting[] }>>("/v1/admin/settings/business-rules", {
    values,
  })
  return { ...data, data: listResult<BusinessSetting>(data.data) }
}

export interface SiteBranding {
  site_name: string
  logo_url: string
  hero_video_url: string
  hero_title: string
  hero_description: string
  hero_slides: unknown[]
  auth_background_type: "image" | "video"
  auth_background_url: string
}

export async function uploadBrandLogo(file: File) {
  const form = new FormData()
  form.append("file", file)
  const { data } = await adminClient.post<ApiResponse<SiteBranding>>("/v1/admin/settings/branding/logo", form, {
    headers: { "Content-Type": "multipart/form-data" },
  })
  return data
}

export async function uploadFrontendAsset(file: File) {
  const form = new FormData()
  form.append("file", file)
  const { data } = await adminClient.post<ApiResponse<{ url: string; kind: string }>>(
    "/v1/admin/settings/branding/assets",
    form,
    { headers: { "Content-Type": "multipart/form-data" }, timeout: 60_000 },
  )
  return data
}

export async function getFrontendConfig() { const { data } = await adminClient.get<ApiResponse<SiteBranding>>("/v1/admin/settings/frontend-config"); return data }
export async function updateFrontendConfig(values: Partial<SiteBranding>) { const { data } = await adminClient.put<ApiResponse<SiteBranding>>("/v1/admin/settings/frontend-config", values); return data }
