// One typed client method per Category / SubCategory endpoint
// (docs/11-BACKEND-PHASE1.md §2.3; FR-MD-04).
//
// The hierarchy is exactly two levels. Categories no longer self-nest — `parentId` and
// `GET /categories?tree=true` were removed on 2026-07-16 — so there is no tree() here.
// Subcategories are listed/created under their parent; updates address them by their own id.
import { apiRequest, apiRequestWithETag, buildQuery } from "@/lib/api-client"
import { ApiResult, EntityStatus, PagedResponse } from "@/types/common"
import {
  Category,
  CreateCategoryRequest,
  CreateSubCategoryRequest,
  SubCategory,
  UpdateCategoryRequest,
  UpdateSubCategoryRequest,
} from "@/types/master-data"

export interface ListCategoriesParams {
  page?: number
  pageSize?: number
  q?: string
  status?: EntityStatus
  sort?: string
}

export type ListSubCategoriesParams = ListCategoriesParams

export const categoriesApi = {
  list(params: ListCategoriesParams = {}): Promise<PagedResponse<Category>> {
    return apiRequest<PagedResponse<Category>>(`/categories${buildQuery(params)}`)
  },

  get(categoryId: number): Promise<ApiResult<Category>> {
    return apiRequestWithETag<Category>(`/categories/${categoryId}`)
  },

  create(request: CreateCategoryRequest): Promise<ApiResult<Category>> {
    return apiRequestWithETag<Category>("/categories", { method: "POST", body: request })
  },

  update(categoryId: number, request: UpdateCategoryRequest, ifMatch: string): Promise<ApiResult<Category>> {
    return apiRequestWithETag<Category>(`/categories/${categoryId}`, { method: "PUT", body: request, ifMatch })
  },

  /** Deactivate/reactivate — there is no DELETE (FR-MD-08). */
  updateStatus(categoryId: number, status: EntityStatus): Promise<void> {
    return apiRequest<void>(`/categories/${categoryId}/status`, { method: "PATCH", body: { status } })
  },

  /** 404s if the parent category does not exist. */
  listSubCategories(categoryId: number, params: ListSubCategoriesParams = {}): Promise<PagedResponse<SubCategory>> {
    return apiRequest<PagedResponse<SubCategory>>(`/categories/${categoryId}/subcategories${buildQuery(params)}`)
  },

  createSubCategory(categoryId: number, request: CreateSubCategoryRequest): Promise<ApiResult<SubCategory>> {
    return apiRequestWithETag<SubCategory>(`/categories/${categoryId}/subcategories`, {
      method: "POST",
      body: request,
    })
  },
}

export const subCategoriesApi = {
  get(subCategoryId: number): Promise<ApiResult<SubCategory>> {
    return apiRequestWithETag<SubCategory>(`/subcategories/${subCategoryId}`)
  },

  /** Name only — a subcategory cannot be moved to another category (docs/11 §2.3). */
  update(subCategoryId: number, request: UpdateSubCategoryRequest, ifMatch: string): Promise<ApiResult<SubCategory>> {
    return apiRequestWithETag<SubCategory>(`/subcategories/${subCategoryId}`, {
      method: "PUT",
      body: request,
      ifMatch,
    })
  },

  updateStatus(subCategoryId: number, status: EntityStatus): Promise<void> {
    return apiRequest<void>(`/subcategories/${subCategoryId}/status`, { method: "PATCH", body: { status } })
  },
}
