// One typed client method per adjustment endpoint (docs/11-BACKEND-PHASE1.md §5.5).
// Auto-posts on create with a mandatory reason code (FR-STK-07).
import { apiRequest, buildQuery } from "@/lib/api-client"
import { PagedResponse } from "@/types/common"
import { CreateAdjustmentRequest, StockAdjustment, StockAdjustmentSummary } from "@/types/stock"

export interface ListAdjustmentsParams {
  page?: number
  pageSize?: number
  q?: string
  warehouseId?: number
  reasonCodeId?: number
  sort?: string
}

export const stockAdjustmentsApi = {
  list(params: ListAdjustmentsParams = {}): Promise<PagedResponse<StockAdjustmentSummary>> {
    return apiRequest<PagedResponse<StockAdjustmentSummary>>(`/stock-adjustments${buildQuery(params)}`)
  },

  get(adjustmentId: number): Promise<StockAdjustment> {
    return apiRequest<StockAdjustment>(`/stock-adjustments/${adjustmentId}`)
  },

  /**
   * 400 REASON_CODE_REQUIRED without a reason; 422 if it is not an Adjustment-context
   * reason or qtyDelta is 0; 409 STOCK_NEGATIVE_BLOCKED if a decrease exceeds available.
   */
  create(request: CreateAdjustmentRequest): Promise<StockAdjustment> {
    return apiRequest<StockAdjustment>("/stock-adjustments", { method: "POST", body: request })
  },
}
