import React from "react";
import GenerateProjectMonthlyReportLoading from "./GenerateProjectMonthlyReportLoading";
import GenerateProjectMonthlyReport from "./GenerateProjectMonthlyReport";
import { fetchStaff } from "@/app/api/staff";
import { getServerSession } from "next-auth";
import { authOptions } from "@/config/authConfig";
import { TEAM_LEAD } from "@/middleware";
import { fetchHolidays } from "@/app/api/holidays";
import { convertDateArrayToString } from "@/app/utils/formatUtil";
import { fetchProjects } from "@/app/api/projects";
interface SubComponents {
  Loading: typeof GenerateProjectMonthlyReportLoading;
}

const GenerateProjectMonthlyReportWrapper: React.FC &
  SubComponents = async () => {
  const session: any = await getServerSession(authOptions);
  const teamId = session.staff?.teamId;
  const role = session.role;

  const companyHolidays = await fetchHolidays()
  let companyHolidaysList: String[] = []
  if (companyHolidays.length > 0) {
    companyHolidaysList = companyHolidays.map(item => convertDateArrayToString(item.date, "DD/MM/YYYY")) as String[]
  }
  console.log(companyHolidaysList)
  let projects = await fetchProjects();

  if (role.includes(TEAM_LEAD)) {
    projects = projects.filter((project) => project.teamId === teamId);
  }

  return <GenerateProjectMonthlyReport projects={projects} companyHolidays={companyHolidaysList} />;
};

GenerateProjectMonthlyReportWrapper.Loading = GenerateProjectMonthlyReportLoading;

export default GenerateProjectMonthlyReportWrapper;
