import { Navigate } from 'react-router-dom';
import { useAuth } from '@/lib/auth-context';

/**
 * / is a redirect to the first allowed module based on role.
 *   super_admin   → /companies
 *   tenant_admin  → /sources (their own company's sources)
 *   viewer        → /dlq (read-only)
 */
export function IndexRoute() {
  const { user } = useAuth();
  if (!user) return <Navigate to="/login" replace />;
  switch (user.role) {
    case 'super_admin':
      return <Navigate to="/companies" replace />;
    case 'tenant_admin':
      return <Navigate to="/sources" replace />;
    case 'viewer':
      return <Navigate to="/dlq" replace />;
    default:
      return <Navigate to="/login" replace />;
  }
}
