import { serverFetchJson } from "@/app/utils/fetchUtil";
import { BASE_API_URL } from "@/config/api";
import { cache } from "react";
import "server-only";
import { Task, TaskGroup } from "../tasks";
import { CreateProjectInputs } from "./actions";

export interface ProjectResult {
  id: number;
  code: string;
  name: string;
  category: string;
  team: string;
  teamCodeName: string;
  teamId: number;
  client: string;
  status: string;
  mainProject: string;
}

export interface MainProject {
  projectId: number;
  projectCode: string;
  projectName: string;
  projectCategoryId: number;
  projectDescription: string;
  projectLeadId: number;
  projectSubLeadId: number;
  projectStatus: string;
  isClpProject: boolean;
  serviceTypeId: number;
  fundingTypeId: number;
  contractTypeId: number;
  locationId: number;
  buildingTypeIds: number[];
  workNatureIds: number[];
  clientId: number;
  clientContactId: number;
  clientSubsidiaryId: number;
  expectedProjectFee: number;
  subContractFee: number;
}

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

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

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

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

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

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

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

export interface ProjectWithTasks {
  id: number;
  code: string;
  status?: string;
  actualEnd?: string;
  description?: string;
  name: string;
  tasks: Task[];
  milestones: {
    [taskGroupId: TaskGroup["id"]]: {
      startDate?: string;
      endDate?: string;
    };
  };
}

export interface AssignedProject extends ProjectWithTasks {
  // Manhour info
  hoursSpent: number;
  hoursSpentOther: number;
  currentStaffHoursSpent: number;
  currentStaffHoursSpentOther: number;
  hoursAllocated: number;
}

export const preloadProjects = () => {
  fetchProjectCategories();
  fetchProjects();
};

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

export const fetchMainProjects = cache(async () => {
  return serverFetchJson<MainProject[]>(`${BASE_API_URL}/projects/main`, {
    next: { tags: ["projects"] },
  });
});

export const fetchProjectCategories = cache(async () => {
  return serverFetchJson<ProjectCategory[]>(
    `${BASE_API_URL}/projects/categories`,
    {
      next: { tags: ["projectCategories"] },
    },
  );
});

export const fetchProjectServiceTypes = cache(async () => {
  return serverFetchJson<ServiceType[]>(
    `${BASE_API_URL}/projects/serviceTypes`,
    {
      next: { tags: ["projectServiceTypes"] },
    },
  );
});

export const fetchProjectFundingTypes = cache(async () => {
  return serverFetchJson<FundingType[]>(
    `${BASE_API_URL}/projects/fundingTypes`,
    {
      next: { tags: ["projectFundingTypes"] },
    },
  );
});

export const fetchProjectContractTypes = cache(async () => {
  return serverFetchJson<ContractType[]>(
    `${BASE_API_URL}/projects/contractTypes`,
    {
      next: { tags: ["projectContractTypes"] },
    },
  );
});

export const fetchProjectLocationTypes = cache(async () => {
  return serverFetchJson<LocationType[]>(
    `${BASE_API_URL}/projects/locationTypes`,
    {
      next: { tags: ["projectLocationTypes"] },
    },
  );
});

export const fetchProjectBuildingTypes = cache(async () => {
  return serverFetchJson<BuildingType[]>(
    `${BASE_API_URL}/projects/buildingTypes`,
    {
      next: { tags: ["projectBuildingTypes"] },
    },
  );
});

export const fetchProjectWorkNatures = cache(async () => {
  return serverFetchJson<WorkNature[]>(`${BASE_API_URL}/projects/workNatures`, {
    next: { tags: ["projectWorkNatures"] },
  });
});

export const fetchAssignedProjects = cache(async () => {
  return serverFetchJson<AssignedProject[]>(
    `${BASE_API_URL}/projects/assignedProjects`,
    {
      next: { tags: [`assignedProjects`] },
    },
  );
});

export const fetchProjectWithTasks = cache(async () => {
  return serverFetchJson<ProjectWithTasks[]>(
    `${BASE_API_URL}/projects/allProjectWithTasks`,
    {
      next: { tags: ["allProjectWithTasks"] },
    },
  );
});

export const fetchProjectDetails = cache(async (projectId: string) => {
  return serverFetchJson<CreateProjectInputs>(
    `${BASE_API_URL}/projects/projectDetails/${projectId}`,
    {
      next: { tags: [`projectDetails_${projectId}`] },
    },
  );
});
