import { ProjectFormData } from "@/app/main/planning/projects/projectModal/zodProject";
import { baseApiRequest } from "../apiRequests/baseApiRequest";
import { Project } from "@/app/main/planning/projects/types/project";

const PROJECT_API = `${process.env.NEXT_PUBLIC_PROJECT_MANAGEMENT_API_URL}/api/v1/projects`;
const USER_API =
  `${process.env.NEXT_PUBLIC_IKON_API_URL}/platform/user/current`;

// CREATE
export const createProjectApi = async (project: ProjectFormData) => {
  return baseApiRequest(
    PROJECT_API,
    {
      method: "POST",
      body: JSON.stringify(project),
    },
    { isAccessTokenRequird: true },
  );
};

// READ
export const getProjectsApi = async () => {
  return baseApiRequest(
    PROJECT_API,
    {
      method: "GET",
      cache: "no-store",
    },
    { isAccessTokenRequird: true },
  );
};

export const getProjectByIdentifierApi = async (projectId: string) => {
  return baseApiRequest(
    `${PROJECT_API}/${projectId}`,
    {
      method: "GET",
      cache: "no-store",
    },
    { isAccessTokenRequird: true },
  );
};

// UPDATE
export const updateProjectApi = async (project: Project) => {
  return baseApiRequest(
    `${PROJECT_API}/${project.projectIdentifier}`,
    {
      method: "PUT",
      body: JSON.stringify(project),
    },
    { isAccessTokenRequird: true },
  );
};

export const getScheduleApi = async (projectId: string) => {
  return baseApiRequest(
    `${PROJECT_API}/${projectId}/schedules`,
    { 
      method: "GET",
      cache: "no-store" 
    },
    { isAccessTokenRequird: true },
  );
};

export const getAllSchedulesApi = async () => {
  return baseApiRequest(
    `${PROJECT_API}/schedules`,
    { 
      method: "GET",
      cache: "no-store" 
    },
    { isAccessTokenRequird: true },
  );
};

export const saveScheduleApi = async (
  projectId: string,
  scheduleData: { task: any[]; dependency: any[]; group?: Record<string, any> },
) => {
  return baseApiRequest(
    `${PROJECT_API}/${projectId}/schedules`,
    { method: "PUT", body: JSON.stringify(scheduleData) },
    { isAccessTokenRequird: true },
  );
};

export const getUsersApi = async () => {
  return baseApiRequest(
    USER_API,
    {
      method: "GET",
      cache: "no-store",
    },
    {
      isAccessTokenRequird: true,
    },
  );
};

export const getAllActiveProjectsTimelineApi = async () => {
  return baseApiRequest(
    `${PROJECT_API}/active-projects-timeline`,
    {
      method: "GET",
      cache: "no-store",
    },
    { isAccessTokenRequird: true },
  );
};
