import { convertObjToURLSearchParams } from "@/app/utils/commonUtil";
import { serverFetchJson } from "@/app/utils/fetchUtil";
import { BASE_API_URL } from "@/config/api";
import { cache } from "react";
import "server-only";

export interface InventoryResult {
  id: number;
  itemId: number;
  itemCode: string;
  itemName: string;
  itemType: string;
  onHandQty: number;
  onHoldQty: number;
  unavailableQty: number;
  availableQty: number;
  uomCode: string;
  uomUdfudesc: string;
  uomShortDesc: string;
  // germPerSmallestUnit: number;
  qtyPerSmallestUnit: number;
  baseUom: string;
  // smallestUnit: string;
  price: number;
  currencyName: string;
  status: string;
  latestMarketUnitPrice?: number;
  latestMupUpdatedDate?: string;
}

export interface InventoryLotLineResult {
  id: number;
  lotNo: string;
  item: InventoryLotLineItem;
  warehouse: InventoryLotLineWarehouse;
  inQty: number;
  outQty: number;
  holdQty: number;
  expiryDate: number[];
  status: string;
  availableQty: number;
  uom: string;
  qtyPerSmallestUnit: number;
  baseUom: string;
  stockInLineId: number
}

export interface InventoryLotLineItem {
  id: number;
  code: string;
  name: string;
  type: string;
}

export interface InventoryLotLineWarehouse {
  id: number;
  code: string;
  name: string;
}

export const preloadInventory = () => {
  fetchInventories();
};

export const fetchInventories = cache(async () => {
  return serverFetchJson<InventoryResult[]>(`${BASE_API_URL}/inventory/list`, {
    next: { tags: ["inventories"] },
  });
});
