import React from "react";
import { useSelectedProject } from "../../slices/apiDataManager";
import { useDashboardTasksCardData } from "../../hooks/useDashboard_Data";

const TasksCard = () => {
  const projectId = useSelectedProject();

  const {
    data: tasksCardData,
    isLoading,
    isError,
    error,
  } = useDashboardTasksCardData(projectId);

  return (
    <div className="card p-3 h-100 text-center d-flex justify-content-between">
      <div className="d-flex justify-content-start align-items-center mb-3">
        <h5 className="fw-bold mb-0 ms-2">
          <i className="bx bx-task text-success"></i> Tasks
        </h5>
      </div>

      {isLoading ? (
        // Loader while fetching
        <div className="d-flex justify-content-center align-items-center flex-grow-1">
          <div className="spinner-border text-primary" role="status">
            <span className="visually-hidden">Loading...</span>
          </div>
        </div>
      ) : isError ? (
        // Show error
        <div className="text-danger flex-grow-1 d-flex justify-content-center align-items-center">
          {error?.message || "Error loading data"}
        </div>
      ) : (
        // Show data
        <div className="d-flex justify-content-around align-items-start mt-n2">
          <div>
            <h4 className="mb-0 fw-bold">
              {tasksCardData?.totalTasks?.toLocaleString() ?? 0}
            </h4>
            <small className="text-muted">Total</small>
          </div>
          <div>
            <h4 className="mb-0 fw-bold">
              {tasksCardData?.completedTasks?.toLocaleString() ?? 0}
            </h4>
            <small className="text-muted">Completed</small>
          </div>
        </div>
      )}
    </div>
  );
};

export default TasksCard;
