import React from "react";
import { Navigate } from "react-router-dom";

import { useAuth } from "../contexts/AuthContext";
import { useDemoMode } from "../contexts/EngineConfigContext";

interface ProtectedRouteProps {
  children: React.ReactNode;
  adminOnly?: boolean;
}

export const ProtectedRoute: React.FC<ProtectedRouteProps> = ({ children, adminOnly = false }) => {
  const { isAuthenticated, isLoading, isTenant } = useAuth();
  const { demoMode } = useDemoMode();

  if (isLoading) {
    return (
      <div className="min-h-screen flex items-center justify-center">
        <div className="animate-spin rounded-full h-8 w-8 border-b-2 border-blue-600"></div>
      </div>
    );
  }

  if (!isAuthenticated) {
    return <Navigate to={demoMode ? "/welcome" : "/login"} replace />;
  }

  if (adminOnly && isTenant) {
    return <Navigate to="/" replace />;
  }

  return <>{children}</>;
};
