import {
  fetchAssignedProjects,
  fetchProjectWithTasks,
} from "@/app/api/projects";
import UserWorkspacePage from "./UserWorkspacePage";
import {
  fetchLeaveTypes,
  fetchLeaves,
  fetchTeamMemberLeaves,
  fetchTeamMemberTimesheets,
  fetchTimesheets,
} from "@/app/api/timesheets";
import { fetchHolidays } from "@/app/api/holidays";
import { getUserAbilities, getUserStaff } from "@/app/utils/commonUtil";
import {
  MAINTAIN_TIMESHEET_FAST_TIME_ENTRY,
  MAINTAIN_NORMAL_STAFF_WORKSPACE,
  MAINTAIN_MANAGEMENT_STAFF_WORKSPACE,
} from "@/middleware";
import { fetchAllTasks } from "@/app/api/tasks";

const UserWorkspaceWrapper: React.FC = async () => {
  const [
    teamLeaves,
    teamTimesheets,
    assignedProjects,
    allProjects,
    timesheets,
    leaves,
    leaveTypes,
    holidays,
    abilities,
    userStaff,
    allTasks,
  ] = await Promise.all([
    fetchTeamMemberLeaves(),
    fetchTeamMemberTimesheets(),
    fetchAssignedProjects(),
    fetchProjectWithTasks(),
    fetchTimesheets(),
    fetchLeaves(),
    fetchLeaveTypes(),
    fetchHolidays(),
    getUserAbilities(),
    getUserStaff(),
    fetchAllTasks(),
  ]);

  const fastEntryEnabled = abilities.includes(
    MAINTAIN_TIMESHEET_FAST_TIME_ENTRY,
  );
  const maintainNormalStaffWorkspaceAbility = abilities.includes(
    MAINTAIN_NORMAL_STAFF_WORKSPACE,
  );
  const maintainManagementStaffWorkspaceAbility = abilities.includes(
    MAINTAIN_MANAGEMENT_STAFF_WORKSPACE,
  );
  const isFullTime = userStaff?.employType === "FT";

  const miscTasks = allTasks.filter((t) => t.taskGroup.id === 5);

  return (
    <UserWorkspacePage
      isFullTime={isFullTime}
      teamLeaves={teamLeaves}
      teamTimesheets={teamTimesheets}
      allProjects={allProjects}
      assignedProjects={assignedProjects}
      defaultTimesheets={timesheets}
      defaultLeaveRecords={leaves}
      leaveTypes={leaveTypes}
      holidays={holidays}
      miscTasks={miscTasks}
      // Change to access check
      fastEntryEnabled={fastEntryEnabled}
      maintainNormalStaffWorkspaceAbility={maintainNormalStaffWorkspaceAbility}
      maintainManagementStaffWorkspaceAbility={
        maintainManagementStaffWorkspaceAbility
      }
    />
  );
};

export default UserWorkspaceWrapper;
