import React from "react";
import GenerateProjectPandLReportLoading from "./GenerateProjectPandLReportLoading";
import { fetchProjects } from "@/app/api/projects";
import GenerateProjectPandLReport from "./GenerateProjectPandLReport";
import { fetchUserStaff } from "@/app/utils/fetchUtil";

interface SubComponents {
  Loading: typeof GenerateProjectPandLReportLoading;
}

const GenerateProjectPandLReportWrapper: React.FC & SubComponents = async () => {
  const [projects, userStaff] = await Promise.all([fetchProjects(), fetchUserStaff()]);

  // console.log(userStaff?.teamId)
  // Skip HO Team (id = 7)
  const filteredProjects = userStaff?.teamId && userStaff?.teamId !== 7 ? 
    projects.filter(project => project.teamId === userStaff.teamId) 
    : projects

  return <GenerateProjectPandLReport projects={filteredProjects}/>;
};

GenerateProjectPandLReportWrapper.Loading = GenerateProjectPandLReportLoading;

export default GenerateProjectPandLReportWrapper;