'use client';

import { useEffect, useState } from 'react';
import { useRouter } from 'next/navigation';

export default function ProtectedRoute({ children }) {
  const router = useRouter();
  const [isChecking, setIsChecking] = useState(true);
  const [isAuthenticated, setIsAuthenticated] = useState(false);

  useEffect(() => {
    const hasToken = Boolean(window.localStorage.getItem('access'));
    setIsAuthenticated(hasToken);
    setIsChecking(false);

    if (!hasToken) {
      router.replace('/');
    }
  }, [router]);

  if (isChecking || !isAuthenticated) return null;

  return children;
}
