import { Navigate, Outlet } from 'react-router';
import { useAuth } from './useAuth';

export function ProtectedRoute() {
  const { user, loading } = useAuth();

  if (loading) {
    return (
      <div className="min-h-screen flex items-center justify-center bg-bg-primary">
        <div className="animate-spin h-8 w-8 border-4 border-accent border-t-transparent rounded-full" />
      </div>
    );
  }

  if (!user) {
    return <Navigate to="/login" replace />;
  }

  if (user.status === 'PENDING') {
    return <Navigate to="/pending" replace />;
  }

  if (user.status === 'REJECTED' || user.status === 'DISABLED') {
    return <Navigate to="/denied" replace />;
  }

  return <Outlet />;
}
