import React, {useEffect} from 'react';
import { useNavigate } from 'react-router-dom';
import { useSelector } from 'react-redux';
import { getDashboardPath } from '../../services/firestoreService'
import type { RootState } from '../../store/store';
/**
 * RoleDashboardRedirect Component
 * Dynamically redirects users to their appropriate dashboard based on their role
 */
export const RoleDashboardRedirect: React.FC = () => {
  const { isAuthenticated, user } = useSelector((state: RootState) => state.auth);
  const navigate = useNavigate();

  useEffect(() => {
    if (!isAuthenticated) {
      navigate('/login', { replace: true });
      return;
    }

    if (user?.userRole) {
      const dashboardPath = getDashboardPath(user.userRole);
      navigate(dashboardPath, { replace: true });
    }
  }, [isAuthenticated, user?.userRole, navigate]);

  return null;
};