import { serverFetchJson } from "@/app/utils/fetchUtil";
import { BASE_API_URL } from "@/config/api";
import { cache } from "react";
import "server-only";
import { CreateCompanyInputs, EditCompanyInputs } from "./actions";

export interface CompanyResult {
    id: number;
    companyCode: string;
    name: string;
    brNo: string;
    contactName: string;
    phone: string
    email: string;
}

export const preloadCompanys = () => {
    fetchCompanys();
};

export const fetchCompanys = cache(async () => {
    return serverFetchJson<CompanyResult[]>(`${BASE_API_URL}/companys`, {
      next: { tags: ["companys"] },
    });
});

export const fetchCompanyDetails = cache(async (companyId: string) => {
    return serverFetchJson<EditCompanyInputs>(
      `${BASE_API_URL}/companys/companyDetails/${companyId}`,
      {
        next: { tags: [`departmentDetail`] },
      },
    );
});