// One typed client method per Brand endpoint (docs/11-BACKEND-PHASE1.md §2.6; FR-MD-09).
import { apiRequest, apiRequestWithETag, buildQuery } from "@/lib/api-client"
import { ApiResult, EntityStatus, PagedResponse } from "@/types/common"
import { Brand, CreateBrandRequest, UpdateBrandRequest } from "@/types/master-data"

export interface ListBrandsParams {
  page?: number
  pageSize?: number
  q?: string
  status?: EntityStatus
  /** `name` / `-name` etc. (docs/11 §1.5). */
  sort?: string
}

export const brandsApi = {
  list(params: ListBrandsParams = {}): Promise<PagedResponse<Brand>> {
    return apiRequest<PagedResponse<Brand>>(`/brands${buildQuery(params)}`)
  },

  get(brandId: number): Promise<ApiResult<Brand>> {
    return apiRequestWithETag<Brand>(`/brands/${brandId}`)
  },

  create(request: CreateBrandRequest): Promise<ApiResult<Brand>> {
    return apiRequestWithETag<Brand>("/brands", { method: "POST", body: request })
  },

  update(brandId: number, request: UpdateBrandRequest, ifMatch: string): Promise<ApiResult<Brand>> {
    return apiRequestWithETag<Brand>(`/brands/${brandId}`, { method: "PUT", body: request, ifMatch })
  },

  /**
   * Deactivate/reactivate. There is no DELETE anywhere in the API: masters referenced by
   * transactions are deactivated, never removed (FR-MD-08).
   */
  updateStatus(brandId: number, status: EntityStatus): Promise<void> {
    return apiRequest<void>(`/brands/${brandId}/status`, { method: "PATCH", body: { status } })
  },
}
