/** not used */
import { Navigate, useLocation } from "react-router-dom";
import { useAuth } from "@/context/AuthContext";
import { ReactNode } from "react";

interface ProtectedRouteProps {
    children: ReactNode;
}

/**
 * A wrapper component that protects routes requiring authentication
 * Redirects to login if user is not authenticated, preserving the intended destination
 */
const ProtectedRoute = ({ children }: ProtectedRouteProps) => {
    const auth = useAuth();
    const location = useLocation();

    if (!auth.isAuthenticated) {
        // Redirect to login page while saving the attempted location
        // This enables seamless redirection back after successful login
        return <Navigate to="/login" state={{ from: location }} replace />;
    }

    // If authenticated, render the protected content
    return <>{children}</>;
};

export default ProtectedRoute;
