import type { Freight } from "@edr/types";

import { URL_CONSTANTS } from "@/constants/URLS";
import { client } from "../utils/api";

const B = URL_CONSTANTS.BOOKINGS;

/**
 * Multi-truck self-haul assignment for a booking (no EDR first/last mile).
 * Each truck carries 1–2 of the booking's containers and tracks its own arrival.
 */
export const customerTrucksService = {
  list: async (bookingId: string): Promise<Freight.ICustomerTruck[]> => {
    const { data } = await client.get(B.CUSTOMER_TRUCKS(bookingId));
    return data.data ?? data;
  },

  add: async (
    bookingId: string,
    payload: Freight.AddCustomerTruckPayload,
  ): Promise<Freight.ICustomerTruck[]> => {
    const { data } = await client.post(B.CUSTOMER_TRUCKS(bookingId), payload);
    return data.data ?? data;
  },

  update: async (
    bookingId: string,
    assignmentId: string,
    payload: Freight.AddCustomerTruckPayload,
  ): Promise<Freight.ICustomerTruck[]> => {
    const { data } = await client.patch(B.CUSTOMER_TRUCK(bookingId, assignmentId), payload);
    return data.data ?? data;
  },

  remove: async (
    bookingId: string,
    assignmentId: string,
  ): Promise<Freight.ICustomerTruck[]> => {
    const { data } = await client.delete(B.CUSTOMER_TRUCK(bookingId, assignmentId));
    return data.data ?? data;
  },
};
