import type { Freight } from "@edr/types";
import { client } from "../utils/api";

export type CreateBookingOrderPayload = Freight.CreateBookingOrderDto;

export const bookingOrdersService = {
  /** Orders placed against a general contract. */
  listByContract: async (
    contractBookingId: string,
  ): Promise<Freight.IBookingOrder[]> => {
    const { data } = await client.get("/api/booking-orders", {
      params: { contractBookingId },
    });
    return data.data ?? data;
  },

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

  /** Per-route contracted / ordered / remaining quantities (multi-route contracts). */
  routes: async (
    contractBookingId: string,
  ): Promise<Freight.ContractRouteLine[]> => {
    const { data } = await client.get(
      `/api/booking-orders/contract/${contractBookingId}/routes`,
    );
    return data.data ?? data;
  },

  /** Place a drawdown order against a contract. */
  create: async (
    payload: CreateBookingOrderPayload,
  ): Promise<Freight.IBookingOrder> => {
    const { data } = await client.post("/api/booking-orders", payload);
    return data.data ?? data;
  },
};
