import { useEffect } from 'react';
import { useNavigate } from 'react-router-dom';
import { useAuth } from '@/contexts/AuthContext';
import { Loader2 } from 'lucide-react';

export default function DashboardRedirect() {
  const { profile, loading } = useAuth();
  const navigate = useNavigate();

  useEffect(() => {
    if (loading) return;

    if (!profile) {
      // If profile not found, redirect to my-trips
      navigate('/my-trips');
      return;
    }

    if (profile.role === 'admin') {
      navigate('/admin');
    } else if (profile.role === 'provider') {
      navigate('/provider/dashboard');
    } else {
      navigate('/my-trips');
    }
  }, [profile, loading, navigate]);

  return (
    <div className="flex flex-col items-center justify-center min-h-[60vh] gap-4">
      <Loader2 className="w-10 h-10 animate-spin text-primary" />
      <p className="text-muted-foreground animate-pulse">Kontrol paneline yönlendiriliyorsunuz...</p>
    </div>
  );
}
