// One typed client method per RFQ/Quotation endpoint (docs/11-BACKEND-PHASE1.md §3.2, FR-PROC-02).
//
// Note `vendorIds` is validated on create but not persisted — there is no RFQ↔vendor link
// in the model, so an Rfq comes back without them; quotations reference vendors directly.
import { apiRequest, buildQuery } from "@/lib/api-client"
import { PagedResponse } from "@/types/common"
import {
  CreateQuotationRequest,
  CreateRfqRequest,
  Quotation,
  Rfq,
  RfqComparison,
  RfqStatus,
  RfqSummary,
} from "@/types/procurement"

export interface ListRfqsParams {
  page?: number
  pageSize?: number
  q?: string
  status?: RfqStatus
  sort?: string
}

export const rfqsApi = {
  list(params: ListRfqsParams = {}): Promise<PagedResponse<RfqSummary>> {
    return apiRequest<PagedResponse<RfqSummary>>(`/rfqs${buildQuery(params)}`)
  },

  get(rfqId: number): Promise<Rfq> {
    return apiRequest<Rfq>(`/rfqs/${rfqId}`)
  },

  create(request: CreateRfqRequest): Promise<Rfq> {
    return apiRequest<Rfq>("/rfqs", { method: "POST", body: request })
  },

  /** One quotation per vendor per RFQ; a duplicate returns 409. */
  addQuotation(rfqId: number, request: CreateQuotationRequest): Promise<Quotation> {
    return apiRequest<Quotation>(`/rfqs/${rfqId}/quotations`, { method: "POST", body: request })
  },

  /** Server-computed vendor-by-line price matrix. */
  comparison(rfqId: number): Promise<RfqComparison> {
    return apiRequest<RfqComparison>(`/rfqs/${rfqId}/comparison`)
  },
}
