'use client';

import { ReactNode } from 'react';
import { useAdmin } from '../../hooks/useAdmin';
import { Loader, Center } from '@mantine/core';

interface RequireAdminProps {
  fallback?: ReactNode;
  children: ReactNode;
  requireSuper?: boolean;
  loading?: ReactNode;
}

export function RequireAdmin({ 
  fallback = null, 
  children, 
  requireSuper = false,
  loading = <Center><Loader size="sm" /></Center>
}: RequireAdminProps) {
  const { isAdmin, isSuperAdmin, loading: isLoading } = useAdmin();

  if (isLoading) {
    return <>{loading}</>;
  }

  const hasAccess = requireSuper ? isSuperAdmin : isAdmin;

  if (!hasAccess) {
    return <>{fallback}</>;
  }

  return <>{children}</>;
}