import { cache } from "react";
import "server-only";
import { serverFetchJson } from "../../../utils/fetchUtil";
import { BASE_API_URL } from "../../../../config/api";

export type EquipmentDetailResult = {
  id: string | number;
  code: string;
  name: string;
  description: string | undefined;
  equipmentCode?: string;
  equipmentTypeId?: string | number | undefined;
  repairAndMaintenanceStatus?: boolean | number;
  latestRepairAndMaintenanceDate?: string | Date;
  lastRepairAndMaintenanceDate?: string | Date;
  repairAndMaintenanceRemarks?: string;
};

export const fetchAllEquipmentDetails = cache(async () => {
  return serverFetchJson<EquipmentDetailResult[]>(`${BASE_API_URL}/EquipmentDetail`, {
    next: { tags: ["equipmentDetails"] },
  });
});

export const fetchEquipmentDetail = cache(async (id: number) => {
  return serverFetchJson<EquipmentDetailResult>(
    `${BASE_API_URL}/EquipmentDetail/details/${id}`,
    {
      next: { tags: ["equipmentDetails"] },
    },
  );
});
