import { Navigate, Outlet, useLocation } from 'react-router-dom';
import { useAuth } from '../../contexts/AuthContext';
import { canManageSystem } from '../../utils/permissions';

const AdminRoute = () => {
  const { user, isInitialized } = useAuth();
  const location = useLocation();

  console.log('AdminRoute - Current location:', location.pathname);
  console.log('AdminRoute - User:', user);

  if (!isInitialized) {
    return (
      <div className="min-h-screen flex items-center justify-center bg-campfire-dark">
         <div className="animate-spin rounded-full h-12 w-12 border-t-2 border-b-2 border-campfire-amber"></div>
      </div>
    );
  }

  if (!canManageSystem(user)) {
    console.log('AdminRoute - User does not have system management rights, redirecting to home');
    return <Navigate to="/" replace />;
  }

  console.log('AdminRoute - User has system management rights, rendering child routes');
  return <Outlet />;
};

export default AdminRoute;
