import React from "react";
import GenerateLastModifiedReportLoading from "./GenerateLastModifiedReportLoading";
import GenerateLastModifiedReport from "./GenerateLastModifiedReport";
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 GenerateLastModifiedReportLoading;
}

const GenerateLastModifiedReportWrapper: 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 <GenerateLastModifiedReport projects={projects} companyHolidays={companyHolidaysList} />;
};

GenerateLastModifiedReportWrapper.Loading = GenerateLastModifiedReportLoading;

export default GenerateLastModifiedReportWrapper;
