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

export interface ProductCategory {
  id: number
  slug: string
  name: string
  description: string | null
  icon: string | null
  sort_order: number
  status: "active" | "hidden"
  is_featured: boolean
  featured_label: string
}

export interface ProductCategoryPayload {
  slug?: string
  name: string
  description?: string | null
  icon?: string | null
  sort_order?: number
  status?: "active" | "hidden"
  is_featured?: boolean
  featured_label?: string
}

export interface OsOption {
  label: string
  value: number
}

export interface OsOptionRow extends OsOption {
  index: number
}

export interface OsOptionsPage {
  items: OsOptionRow[]
  total: number
  offset: number
  limit: number
}

export interface ProductGroup {
  id: number
  category_id: number
  slug: string
  name: string
  headline: string | null
  description: string | null
  icon: string | null
  network_type: string | null
  sort_order: number
  requires_real_name: boolean
  area: number | null
  node: number | null
  os_options: OsOption[]
  created_at: string
  updated_at: string
}

export interface ProductGroupCreatePayload {
  category_id: number
  slug: string
  name: string
  headline?: string | null
  description?: string | null
  icon?: string | null
  network_type?: string | null
  sort_order?: number
  requires_real_name?: boolean
  area?: number | null
  node?: number | null
  os_options?: OsOption[]
}

export type ProductGroupUpdatePayload = Partial<Omit<ProductGroupCreatePayload, "slug">>

const BASE = "/v1/admin/product-catalog"

export async function listCategories() {
  const { data } = await adminClient.get<ApiResponse<ProductCategory[] | { items: ProductCategory[]; total: number }>>(`${BASE}/categories`)
  return { ...data, data: listResult<ProductCategory>(data.data) }
}

export async function createCategory(payload: ProductCategoryPayload & { slug: string }) {
  const { data } = await adminClient.post<ApiResponse<ProductCategory>>(
    `${BASE}/categories`,
    payload,
  )
  return data
}

export async function updateCategory(id: number, payload: ProductCategoryPayload) {
  const { data } = await adminClient.put<ApiResponse<ProductCategory>>(
    `${BASE}/categories/${id}`,
    payload,
  )
  return data
}

export async function deleteCategory(id: number) {
  const { data } = await adminClient.delete<ApiResponse<null>>(`${BASE}/categories/${id}`)
  return data
}

export async function listGroups(categoryId?: number, options?: { includeOsOptions?: boolean }) {
  const params = new URLSearchParams()
  if (categoryId) params.set("category_id", String(categoryId))
  if (options?.includeOsOptions === false) params.set("include_os_options", "false")
  const query = params.toString()
  const url = query ? `${BASE}/groups?${query}` : `${BASE}/groups`
  const { data } = await adminClient.get<ApiResponse<ProductGroup[] | { items: ProductGroup[]; total: number }>>(url)
  return { ...data, data: listResult<ProductGroup>(data.data) }
}

export async function createGroup(payload: ProductGroupCreatePayload) {
  const { data } = await adminClient.post<ApiResponse<ProductGroup>>(`${BASE}/groups`, payload)
  return data
}

export async function updateGroup(id: number, payload: ProductGroupUpdatePayload) {
  const { data } = await adminClient.put<ApiResponse<ProductGroup>>(
    `${BASE}/groups/${id}`,
    payload,
  )
  return data
}

export async function updateGroupVerificationRule(
  id: number,
  payload: { requires_real_name: boolean; apply_to_products?: boolean },
) {
  const { data } = await adminClient.put<ApiResponse<{ group: ProductGroup; updated_products: number }>>(
    `${BASE}/groups/${id}/verification-rule`,
    payload,
  )
  return data
}

export async function listGroupOsOptions(groupId: number, offset = 0, limit = 20) {
  const params = new URLSearchParams({ offset: String(offset), limit: String(limit) })
  const { data } = await adminClient.get<ApiResponse<OsOptionsPage>>(
    `${BASE}/groups/${groupId}/os-options?${params.toString()}`,
  )
  return data
}

export async function updateGroupOsOptionsPage(
  groupId: number,
  payload: { offset: number; limit: number; items: OsOption[] },
) {
  const { data } = await adminClient.put<ApiResponse<OsOptionsPage>>(
    `${BASE}/groups/${groupId}/os-options/page`,
    payload,
  )
  return data
}

export async function deleteGroup(id: number) {
  const { data } = await adminClient.delete<ApiResponse<null>>(`${BASE}/groups/${id}`)
  return data
}
