import { fetchAllTasks, fetchTaskTemplates } from "@/app/api/tasks";
import CreateProject from "./CreateProject";
import {
  fetchProjectBuildingTypes,
  fetchProjectCategories,
  fetchProjectContractTypes,
  fetchProjectDetails,
  fetchProjectFundingTypes,
  fetchProjectLocationTypes,
  fetchProjectServiceTypes,
  fetchProjectWorkNatures,
} from "@/app/api/projects";
import { fetchStaff, fetchTeamLeads } from "@/app/api/staff";
import { fetchAllCustomers, fetchAllSubsidiaries } from "@/app/api/customer";
import { fetchGrades } from "@/app/api/grades";

type CreateProjectProps = { isEditMode: false };
interface EditProjectProps {
  isEditMode: true;
  projectId?: string;
}

type Props = CreateProjectProps | EditProjectProps;

const CreateProjectWrapper: React.FC<Props> = async (props) => {
  const [
    tasks,
    taskTemplates,
    projectCategories,
    teamLeads,
    allCustomers,
    allSubsidiaries,
    contractTypes,
    fundingTypes,
    locationTypes,
    serviceTypes,
    buildingTypes,
    workNatures,
    allStaffs,
    grades,
  ] = await Promise.all([
    fetchAllTasks(),
    fetchTaskTemplates(),
    fetchProjectCategories(),
    fetchTeamLeads(),
    fetchAllCustomers(),
    fetchAllSubsidiaries(),
    fetchProjectContractTypes(),
    fetchProjectFundingTypes(),
    fetchProjectLocationTypes(),
    fetchProjectServiceTypes(),
    fetchProjectBuildingTypes(),
    fetchProjectWorkNatures(),
    fetchStaff(),
    fetchGrades(),
  ]);

  const projectInfo = props.isEditMode
    ? await fetchProjectDetails(props.projectId!!)
    : undefined;

  return (
    <CreateProject
      isEditMode={props.isEditMode}
      defaultInputs={projectInfo}
      allTasks={tasks}
      projectCategories={projectCategories}
      taskTemplates={taskTemplates}
      teamLeads={teamLeads}
      allSubsidiaries={allSubsidiaries}
      allCustomers={allCustomers}
      contractTypes={contractTypes}
      fundingTypes={fundingTypes}
      locationTypes={locationTypes}
      serviceTypes={serviceTypes}
      buildingTypes={buildingTypes}
      workNatures={workNatures}
      allStaffs={allStaffs}
      grades={grades}
    />
  );
};

export default CreateProjectWrapper;
