// Product configuration (docs/11-BACKEND-PHASE1.md §2.8; FR-MD-11). Singleton — no id.
//
// `subcategoriesEnabled`/`brandsEnabled` are enforced server-side: an item write carrying
// a gated field while its flag is off returns 422 CONFIG_DISABLED. `itemTypesEnabled` is
// ADVISORY — items hold no item-type reference, so there is nothing for the server to
// reject; this frontend is what honours it by hiding the builder's type section.
import { apiRequest, apiRequestWithETag } from "@/lib/api-client"
import { ApiResult } from "@/types/common"
import { ProductConfig, UpdateProductConfigRequest } from "@/types/master-data"

export const productConfigApi = {
  get(): Promise<ApiResult<ProductConfig>> {
    return apiRequestWithETag<ProductConfig>("/product-config")
  },

  /** All three flags are required; a partial body is a 400, never a silent disable. */
  update(request: UpdateProductConfigRequest, ifMatch: string): Promise<ApiResult<ProductConfig>> {
    return apiRequestWithETag<ProductConfig>("/product-config", { method: "PUT", body: request, ifMatch })
  },
}

/** Read-only helper for screens that only need the flags. */
export function productConfig(): Promise<ProductConfig> {
  return apiRequest<ProductConfig>("/product-config")
}
