// One typed client method per Requisition endpoint (docs/11-BACKEND-PHASE1.md §3.1, FR-PROC-01).
import { apiRequest, buildQuery } from "@/lib/api-client"
import { PagedResponse } from "@/types/common"
import { CreateRequisitionRequest, Requisition, RequisitionStatus, RequisitionSummary } from "@/types/procurement"

export interface ListRequisitionsParams {
  page?: number
  pageSize?: number
  q?: string
  status?: RequisitionStatus
  sort?: string
}

export const requisitionsApi = {
  list(params: ListRequisitionsParams = {}): Promise<PagedResponse<RequisitionSummary>> {
    return apiRequest<PagedResponse<RequisitionSummary>>(`/requisitions${buildQuery(params)}`)
  },

  get(requisitionId: number): Promise<Requisition> {
    return apiRequest<Requisition>(`/requisitions/${requisitionId}`)
  },

  /** `requestedBy` is stamped from the session, never posted. */
  create(request: CreateRequisitionRequest): Promise<Requisition> {
    return apiRequest<Requisition>("/requisitions", { method: "POST", body: request })
  },

  submit(requisitionId: number): Promise<Requisition> {
    return apiRequest<Requisition>(`/requisitions/${requisitionId}/submit`, { method: "POST" })
  },
}
