import React from 'react';
import { Navigate } from 'react-router-dom';
import { useAuth } from '../../hooks/useAuth';

interface ProtectedRouteProps {
  children: React.ReactNode;
  allowedRoles?: string[];
}

export function ProtectedRoute({ children, allowedRoles = [] }: ProtectedRouteProps) {
  const { user, loading, userRole } = useAuth();

  if (loading) {
    return <div>Carregando...</div>;
  }

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

  if (allowedRoles.length > 0 && !allowedRoles.includes(userRole || '')) {
    // Redirecionar para a página inicial apropriada baseado no papel do usuário
    switch (userRole) {
      case 'student':
        return <Navigate to="/aluno" />;
      case 'teacher':
        return <Navigate to="/professor" />;
      case 'school':
        return <Navigate to="/dashboard" />;
      default:
        return <Navigate to="/" />;
    }
  }

  return <>{children}</>;
} 