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

export interface DepartmentResult {
    id: number;
    code: string;
    name: string;
    description: string;
}

export const preloadDepartments = () => {
    fetchDepartments();
};

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

export const fetchDepartmentDetails = cache(async (departmentId: string) => {
    return serverFetchJson<CreateDepartmentInputs>(
      `${BASE_API_URL}/departments/departmentDetails/${departmentId}`,
      {
        next: { tags: [`departmentDetail${departmentId}`] },
      },
    );
});