/**
 * @ai-summary React hook for admin access verification
 * @ai-context Calls /api/admin/verify and caches result
 */

import { useQuery } from '@tanstack/react-query';
import { useAuth0 } from '@auth0/auth0-react';
import { adminApi } from '../../features/admin/api/admin.api';

export const useAdminAccess = () => {
  const { isAuthenticated, isLoading: authLoading } = useAuth0();

  const query = useQuery({
    queryKey: ['adminAccess'],
    queryFn: () => adminApi.verifyAccess(),
    enabled: isAuthenticated && !authLoading,
    staleTime: 5 * 60 * 1000, // 5 minutes - admin status doesn't change often
    gcTime: 10 * 60 * 1000, // 10 minutes cache time
    retry: 1, // Only retry once for admin checks
    refetchOnWindowFocus: false,
    refetchOnMount: false,
  });

  return {
    isAdmin: query.data?.isAdmin ?? false,
    adminRecord: query.data?.adminRecord ?? null,
    loading: query.isLoading,
    error: query.error,
  };
};
