import { api as client } from "../auth/http";
import { unwrap } from "@/utils/endpoint";
import type { Freight } from "@edr/types";

/**
 * Drawdown orders placed against a general contract (a booking with
 * bookingType = GENERAL_CONTRACT). Each order spawns a child ONE_TIME booking
 * that carries its own clearance/approval — managed on the child's detail page.
 */
export const bookingOrdersService = {
  /** Orders placed against a general contract, with their lines + child status. */
  listByContract: async (
    contractBookingId: string,
  ): Promise<Freight.IBookingOrder[]> => {
    const response = await client.get("/booking-orders", {
      params: { contractBookingId },
    });
    return unwrap(response.data) as Freight.IBookingOrder[];
  },

  /** Contracted / ordered / remaining quantities for a general contract. */
  pool: async (
    contractBookingId: string,
  ): Promise<Freight.ContractQuantityLine[]> => {
    const response = await client.get(
      `/booking-orders/contract/${contractBookingId}/pool`,
    );
    return unwrap(response.data) as Freight.ContractQuantityLine[];
  },
};
