import { useQuery } from "@tanstack/react-query";
import { apiGet } from "../../../lib/api/methods";

export interface DashboardStatistics {
  totalUsers: number;
  activeUsers: number;
  inactiveUsers: number;
  totalRevenue: number;
  totalTransactions: number;
}

export interface DashboardRecentRegistration {
  uuid: string;
  firstName: string;
  lastName: string;
  email: string;
  createdAt: string;
}

export interface DashboardRecentTransaction {
  uuid: string;
  userName: string;
  packageName: string;
  amount: number;
  currency: string;
  provider: string;
  status: string;
  createdAt: string;
}

export interface DashboardData {
  statistics: DashboardStatistics;
  recentRegistrations: DashboardRecentRegistration[];
  recentTransactions: DashboardRecentTransaction[];
}

export default function useFetchDashboard() {
  const { data, isPending, isError, error } = useQuery({
    queryKey: ["admin-dashboard"],
    queryFn: async () => {
      const response = await apiGet<DashboardData>("/dashboard");
      return response.data;
    },
    staleTime: 60 * 1000,
    refetchOnWindowFocus: true,
    retry: false,
  });

  return {
    data,
    loading: isPending,
    isError,
    error,
  };
}
