import { Navigate, Outlet, useLocation } from 'react-router-dom';
import type { ReactNode } from 'react';
import { useAuthToken } from '../hooks/useAuthToken';

interface ProtectedRouteProps {
  children?: ReactNode;
  loginPath?: string;
}

export function ProtectedRoute({ children, loginPath = '/' }: ProtectedRouteProps) {
  const location = useLocation();
  const token = useAuthToken();

  if (!token) {
    return <Navigate to={loginPath} state={{ from: location }} replace />;
  }

  return children ? <>{children}</> : <Outlet />;
}
