// One typed client method per warehouse/bin endpoint (docs/11-BACKEND-PHASE1.md §2.5,
// FR-MD-07/FR-WH-01).
//
// Warehouses are create-and-list only: there is no PUT, no status, and no ETag on them.
import { apiRequest, buildQuery } from "@/lib/api-client"
import { PagedResponse } from "@/types/common"
import { Bin, CreateBinRequest, CreateWarehouseRequest, Warehouse } from "@/types/master-data"

export type { CreateBinRequest, CreateWarehouseRequest }

export interface ListWarehousesParams {
  page?: number
  pageSize?: number
  q?: string
  sort?: string
}

export const warehousesApi = {
  list(params: ListWarehousesParams = {}): Promise<PagedResponse<Warehouse>> {
    return apiRequest<PagedResponse<Warehouse>>(`/warehouses${buildQuery(params)}`)
  },

  get(warehouseId: number): Promise<Warehouse> {
    return apiRequest<Warehouse>(`/warehouses/${warehouseId}`)
  },

  create(request: CreateWarehouseRequest): Promise<Warehouse> {
    return apiRequest<Warehouse>("/warehouses", { method: "POST", body: request })
  },

  /** Returns a bare array, not a paged envelope — the server does not page bins. */
  listBins(warehouseId: number): Promise<Bin[]> {
    return apiRequest<Bin[]>(`/warehouses/${warehouseId}/bins`)
  },

  createBin(warehouseId: number, request: CreateBinRequest): Promise<Bin> {
    return apiRequest<Bin>(`/warehouses/${warehouseId}/bins`, { method: "POST", body: request })
  },
}
