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 { fetchUserAbilities, fetchUserStaff } from "@/app/utils/fetchUtil";
import {
  MAINTAIN_TIMESHEET_FAST_TIME_ENTRY,
  MAINTAIN_NORMAL_STAFF_WORKSPACE,
  MAINTAIN_MANAGEMENT_STAFF_WORKSPACE,
  SATURDAY_WORKERS,
} 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(),
    fetchUserAbilities(),
    fetchUserStaff(),
    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);

  const isSaturdayWorker = abilities.includes(SATURDAY_WORKERS)

  console.log(userStaff)

  return (
    <UserWorkspacePage
      joinDate={userStaff?.joinDate}
      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
      }
      isSaturdayWorker={isSaturdayWorker}
      userId={userStaff?.id}
    />
  );
};

export default UserWorkspaceWrapper;