import React from "react";
import { useSelector } from "react-redux";
import {
  useDashboardProjectsCardData,
  useDashboardTeamsCardData,
  useDashboardTasksCardData,
  useAttendanceOverviewData,
  useGetCollectionOverview,
} from "../../hooks/useDashboard_Data";

import Projects from "./Projects";
import Teams from "./Teams";
import TasksCard from "./Tasks";
import ProjectCompletionChart from "./ProjectCompletionChart";
import ProjectProgressChart from "./ProjectProgressChart";
import AttendanceOverview from "./AttendanceOverview";
import ExpenseAnalysis from "./ExpenseAnalysis";
import ExpenseStatus from "./ExpenseStatus";
import ExpenseByProject from "./ExpenseByProject";
import ProjectStatistics from "../Project/ProjectStatistics";
import ServiceJobs from "./ServiceJobs";
import { useHasUserPermission } from "../../hooks/useHasUserPermission";
import {
  REGULARIZE_ATTENDANCE,
  SELF_ATTENDANCE,
  TEAM_ATTENDANCE,
} from "../../utils/constants";
import CollectionOverview, { TopicBarChart } from "./CollectionOverview";
import { CollectionOverviewSkeleton } from "./CollectionOverviewSkeleton";
import ProjectWiseTeamCount from "./ProjectWiseTeamCount";


const Dashboard = () => {
  // Get the selected project ID from Redux store
  const projectId = useSelector((store) => store.localVariables.projectId);
  const isAllProjectsSelected = projectId === null;
  const canRegularize = useHasUserPermission(REGULARIZE_ATTENDANCE);
  const canTeamAttendance = useHasUserPermission(TEAM_ATTENDANCE);
  const canSelfAttendance = useHasUserPermission(SELF_ATTENDANCE);

  const { data, isLoading, isError } = useGetCollectionOverview();
  return (
    <div className="container-fluid mt-5">
      <div className="row gy-4">
        {isAllProjectsSelected && (
          <div className="col-sm-6 col-lg-6">
            <Projects />
          </div>
        )}

        <div
          className={`${!isAllProjectsSelected ? "col-sm-6 col-lg-6" : "col-sm-6 col-lg-6"
            }`}
        >
          <Teams />
        </div>

          {!isAllProjectsSelected && (   <div
          className={`${!isAllProjectsSelected ? "col-sm-6 col-lg-6" : "col-sm-6 col-lg-6"
            }`}
        >
          <TasksCard />
        </div>)}
        <div className="col-12 col-xl-4 col-md-6">
          <div className="card ">
            <ExpenseStatus />
          </div>
        </div>

        <div className="col-12 col-xl-8">
          <div className="card h-100">
            <ExpenseAnalysis />
          </div>
        </div>
        <div className="col-12 col-md-6">
          <ExpenseByProject />
        </div>

        {isAllProjectsSelected && (
          <div className="col-xxl-6 col-lg-6">
            <ProjectCompletionChart />
          </div>
        )}
        <div className="col-xxl-6 col-lg-6">
          <ProjectProgressChart />
        </div>
        {!isAllProjectsSelected &&
          (canRegularize || canTeamAttendance || canSelfAttendance) && (
            <div className="col-12 col-md-6 mb-sm-0 mb-4">
              <AttendanceOverview />
            </div>
          )}
             {isAllProjectsSelected && <div className="col-12 col-md-6 mb-sm-0 mb-4">
              <ProjectWiseTeamCount />
            </div>}

        {!isAllProjectsSelected && (
          <div className="col-xxl-4 col-lg-4">
            <div className="card h-100">
              <ProjectStatistics />
            </div>
          </div>
        )}
        {isAllProjectsSelected && (
          <div className="col-12 col-md-4 mb-sm-0 mb-4">
            <ServiceJobs />
          </div>
        )}
        <div className="col-md-8">
          {isLoading ? (
            <CollectionOverviewSkeleton />
          ) : (
            data && (
              <div className="card">
                <TopicBarChart data={data} />
              </div>
            )
          )}
        </div>
      </div>
    </div>
  );
};

export default Dashboard;
