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

export interface StoreCategory {
  id: number
  slug: string
  name: string
  description: string | null
  icon: string | null
  sort_order: number
  status: string
  is_featured: boolean
  featured_label: string
}

export interface StoreGroup {
  id: number
  category_id: number
  parent_id: number | null
  slug: string
  name: string
  headline: string | null
  description: string | null
  icon: string | null
  network_type: string | null
  sort_order: number
  area: number | null
  node: number | null
  os_options: { label: string; value: number }[]
}

export interface StorePricing {
  id: number
  cycle: string
  price: number
  original_price: number | null
  setup_fee: number
  is_enabled: boolean
  sort_order: number
}

export interface StoreConfigField {
  def: string
  type: "fixed" | "user_select" | "input" | "slider"
  value?: string | number | null
  options?: { def: string }[]
  validation?: string
  hostname_prefix?: string
  hostname_length?: string
  slider_min?: number
  slider_max?: number
  slider_step?: number
  slider_unit_price?: number
  slider_unit?: string
}

export interface StoreProduct {
  id: number
  group_id: number
  sku: string
  name: string
  type: string
  api_type?: string
  api_identifier?: string | null
  vcpu: number | null
  cpu_model: string | null
  cpu_base_mhz: number | null
  cpu_turbo_mhz: number | null
  memory: string | null
  disk: string | null
  bandwidth: string | null
  features: string | null
  badges: string | null
  base_price: number
  base_setup_fee: number
  stock_policy: string
  stock: number
  min_quantity: number
  max_quantity: number
  sort_order: number
  trial_enabled: boolean
  trial_days: number
  is_featured: boolean
  provision_config: StoreConfigField[] | null
  provision_plugin?: StoreProvisionPlugin | null
  product_page?: StoreProvisionProductPage | null
  pricings: StorePricing[]
}

export interface StoreProvisionProductPageTab {
  key: string
  label: string
  html: string
}

export interface StoreProvisionProductPage {
  layout?: string
  summary_html?: string
  empty_state_html?: string
  tabs?: StoreProvisionProductPageTab[]
  purchase_fields?: StoreProvisionPurchaseField[]
}

export interface StoreProvisionPurchaseFieldOption {
  label: string
  value: string
}

export interface StoreProvisionPurchaseField {
  key: string
  label: string
  type?: "text" | "textarea" | "select" | "number" | "checkbox" | string
  required?: boolean
  default?: string | number | boolean | null
  placeholder?: string
  help?: string
  options?: StoreProvisionPurchaseFieldOption[]
}

export interface StoreProvisionPlugin {
  id: string
  name: string
  version: string
  description?: string
  capabilities?: string[]
  product_page?: StoreProvisionProductPage
}

const BASE = "/v1/store"

export async function fetchCategories() {
  const { data } = await client.get<ApiResponse<StoreCategory[]>>(`${BASE}/categories`)
  return data
}

export async function fetchGroups(categorySlug: string) {
  const { data } = await client.get<ApiResponse<StoreGroup[]>>(
    `${BASE}/categories/${categorySlug}/groups`
  )
  return data
}

export async function fetchProducts(categorySlug: string, groupSlug: string) {
  const { data } = await client.get<ApiResponse<StoreProduct[]>>(
    `${BASE}/categories/${categorySlug}/groups/${groupSlug}/products`
  )
  return data
}
