"use server";
import {
  ServerFetchError,
  serverFetchJson,
  serverFetchWithNoContent,
} from "@/app/utils/fetchUtil";
import { revalidateTag, revalidatePath } from "next/cache";
import { BASE_API_URL } from "@/config/api";
import { CreateItemResponse, RecordsRes } from "../../utils";
import { ItemQc, ItemsResult } from ".";
import { QcChecksInputs } from "../qcCheck/actions";
import { cache } from "react";


// export type TypeInputs = {
//   id: number;
//   name: string
// }
// export type UomInputs = {
//   uom: string
// }
// export type WeightUnitInputs = {
//   weightUnit: string
//   conversion: number
// }

export type CreateItemInputs = {
  id?: string | number;
  code: string;
  name: string;
  description?: string | undefined;
  remarks?: string | undefined;
  shelfLife?: number | undefined;
  countryOfOrigin?: string | undefined;
  maxQty: number;
  type: string;
  qcChecks: QcChecksInputs[];
  qcChecks_active: number[];
  qcCategoryId: number | undefined;
  store_id?: string | undefined;
  warehouse?: string | undefined;
  area?: string | undefined;
  slot?: string | undefined;
  LocationCode?: string | undefined;
  isEgg?: boolean | undefined;
  isFee?: boolean | undefined;
  isBag?: boolean | undefined;
  qcType?: string | undefined;
};

export const saveItem = async (data: CreateItemInputs) => {
  const item = await serverFetchJson<CreateItemResponse<CreateItemInputs>>(
    `${BASE_API_URL}/items/new`,
    {
      method: "POST",
      body: JSON.stringify(data),
      headers: { "Content-Type": "application/json" },
    },
  );
  revalidateTag("items");
  return item;
};

export const deleteItem = async (id: number) => {
  const response = await serverFetchJson<ItemsResult>(
    `${BASE_API_URL}/items/${id}`,
    {
      method: "DELETE",
      headers: { "Content-Type": "application/json" },
    },
  );

  revalidateTag("items");
  revalidatePath("/(main)/settings/items");

  return response;
};

export interface ItemCombo {
  id: number,
  label: string,
  uomId: number,
  uom: string,
  uomDesc: string,
  group?: string,
  currentStockBalance?: number,
}
export interface ItemWithDetails {
  id: number;
  code: string;
  name: string;
  description?: string;
  uomId: number;
  uom: string;
  uomDesc: string;
  currentStockBalance: number;
}
export const fetchItemsWithDetails = cache(async (searchParams?: Record<string, any>) => {
  if (searchParams) {
    const queryString = new URLSearchParams(searchParams).toString();
    return serverFetchJson<RecordsRes<ItemWithDetails>>(
      `${BASE_API_URL}/items/itemsWithDetails?${queryString}`,
      {
        next: { tags: ["items"] },
      }
    );
  } else {
    return serverFetchJson<RecordsRes<ItemWithDetails>>(
      `${BASE_API_URL}/items/itemsWithDetails`,
      {
        next: { tags: ["items"] },
      }
    );
  }
});

export const fetchAllItemsInClient = cache(async () => {
  return serverFetchJson<ItemCombo[]>(`${BASE_API_URL}/items/consumables`, {
    next: { tags: ["items"] },
  });
});
export const fetchPickOrderItemsByPageClient = cache(
  async (queryParams?: Record<string, any>) => {
    if (queryParams) {
      const queryString = new URLSearchParams(queryParams).toString();
      return serverFetchJson<RecordsRes<any>>(
        `${BASE_API_URL}/items/pickOrderItems?${queryString}`,
        {
          method: "GET",
          next: { tags: ["pickorder"] },
        },
      );
    } else {
      return serverFetchJson<RecordsRes<any>>(
        `${BASE_API_URL}/items/pickOrderItems`,
        {
          method: "GET",
          next: { tags: ["pickorder"] },
        },
      );
    }
  },
);