// One typed client method per vendor (supplier) endpoint (docs/11-BACKEND-PHASE1.md
// §2.4, FR-MD-06). ETag/If-Match on update, PATCH status for deactivate (masters are
// deactivated, not hard-deleted, FR-MD-08).
import { apiRequest, apiRequestWithETag, buildQuery } from "@/lib/api-client"
import { ApiResult, EntityStatus, PagedResponse } from "@/types/common"
import { CreateVendorRequest, UpdateVendorRequest, Vendor } from "@/types/master-data"

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

export const vendorsApi = {
  list(params: ListVendorsParams = {}): Promise<PagedResponse<Vendor>> {
    return apiRequest<PagedResponse<Vendor>>(`/vendors${buildQuery(params)}`)
  },

  get(vendorId: number): Promise<ApiResult<Vendor>> {
    return apiRequestWithETag<Vendor>(`/vendors/${vendorId}`)
  },

  create(request: CreateVendorRequest): Promise<ApiResult<Vendor>> {
    return apiRequestWithETag<Vendor>("/vendors", { method: "POST", body: request })
  },

  update(vendorId: number, request: UpdateVendorRequest, ifMatch: string): Promise<ApiResult<Vendor>> {
    return apiRequestWithETag<Vendor>(`/vendors/${vendorId}`, { method: "PUT", body: request, ifMatch })
  },

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