import type { ReactElement } from "react";
import { Navigate } from "react-router-dom";

import { useAuth } from "@/auth/useAuth";

/**
 * Root landing redirect for the vendored IAM pages. Sends super admins to the
 * user-management dashboard and everyone else to the freight overview. Minimal
 * replacement for the source app's @/routes/RootRedirect (not copied over).
 */
export function RootRedirect(): ReactElement {
  const { user } = useAuth();
  const roles = (user?.roles ?? []).map((r) => r.key).filter(Boolean);
  if (roles.includes("super_admin")) {
    return <Navigate to="/user-management/dashboard" replace />;
  }
  return <Navigate to="/dashboard/overview" replace />;
}

export default RootRedirect;
