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

export interface TaskGroup {
  id: number;
  name: string;
}

export interface Task {
  id: number;
  name: string;
  description: string | null;
  taskGroup: TaskGroup;
}

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

export const preloadTaskTemplates = () => {
  fetchTaskTemplates();
};

export const fetchTaskTemplates = cache(async () => {
  return serverFetchJson<TaskTemplate[]>(`${BASE_API_URL}/tasks/templates`, {
    next: { tags: ["taskTemplates"] },
  });
});

export const preloadAllTasks = () => {
  fetchAllTasks();
};

export const fetchAllTasks = cache(async () => {
  return serverFetchJson<Task[]>(`${BASE_API_URL}/tasks`);
});
