import { useQuery } from "@tanstack/react-query";
import { toast } from "sonner";
import { getDashboardStats } from "@/super-admin/services/api/dashboardApi";
import { DashboardStatsDto } from "../types/dashboardStatsDto";

export const useDashboardStats = () => {
  const {
    data: stats,
    isLoading,
    isError,
    refetch,
  } = useQuery({
    queryKey: ["dashboardStats"],
    queryFn: async (): Promise<DashboardStatsDto> => {
      try {
        return await getDashboardStats();
      } catch (err) {
        toast("Failed to fetch dashboard stats. Using mock data.");
        return err instanceof Error
          ? Promise.reject(new Error(err.message))
          : Promise.reject(new Error("Failed to fetch dashboard stats."));
        // Fallback to mock data if API call fails
      }
    },
    staleTime: 5 * 60 * 1000,
  });

  return {
    stats,
    isLoading,
    isError,
    refetch,
  };
};
