"use server";

import { BASE_API_URL } from "@/config/api";
import { convertObjToURLSearchParams } from "@/app/utils/commonUtil";
import { serverFetchJson } from "@/app/utils/fetchUtil";
import { ItemLotTraceParams, ItemLotTraceResponse } from ".";
import { parseItemLotTraceResponse } from "./schema";

export const fetchItemLotTrace = async (
  params: ItemLotTraceParams,
): Promise<ItemLotTraceResponse> => {
  const query = convertObjToURLSearchParams(params as Record<string, unknown>);
  const json = await serverFetchJson<unknown>(
    `${BASE_API_URL}/inventoryLotLine/trace?${query}`,
    { method: "GET" },
  );
  return parseItemLotTraceResponse(json) as unknown as ItemLotTraceResponse;
};

/**
 * Backend also exposes GET /inventoryLotLine/trace/location/{inventoryLotId} for
 * lazy location blocks. The main /trace payload already includes locationBlocks,
 * so the UI uses that eager path and does not call a separate location fetch.
 */
