import React from "react";
import { fetchAllCustomers } from "@/app/api/customer";
import { fetchAllSubsidiaries } from "@/app/api/subsidiary";
import { fetchIndivTeam, fetchTeam } from "@/app/api/team";
import CostAndExpenseReport from "./CostAndExpenseReport";
import CostAndExpenseReportLoading from "./CostAndExpenseReportLoading";
import { headers, cookies } from 'next/headers';
import { getServerSession } from "next-auth";
import { authOptions } from "@/config/authConfig";
import { TEAM_LEAD } from "@/middleware";
interface SubComponents {
  Loading: typeof CostAndExpenseReportLoading;
}

const CostAndExpenseReportWrapper: React.FC & SubComponents = async () => {
    const session: any = await getServerSession(authOptions)
    const teamId = session.staff?.teamId
    const role = session.role
    let customers = await fetchAllCustomers()
    let subsidiaries = await fetchAllSubsidiaries()
    let teams = await fetchTeam()
    let needAll = true
    
    if (role.includes(TEAM_LEAD)) {
      needAll = false
      teams = teams.filter((team) => team.id === teamId);
    } 

    return <CostAndExpenseReport team={teams} customer={customers} subsidiary={subsidiaries} needAll={needAll} />
};

CostAndExpenseReportWrapper.Loading = CostAndExpenseReportLoading;

export default CostAndExpenseReportWrapper;