import { Navigate, useLocation } from 'react-router-dom';
import { useAuth } from '../store/AuthContext';

// Token yoksa /login'e yönlendirir
export default function Protected({ children }) {
  const { user, loading } = useAuth();
  const location = useLocation();

  if (loading) {
    return (
      <div className="page container">
        <div className="empty">
          <span className="empty-icon">◈</span>
          Yükleniyor…
        </div>
      </div>
    );
  }

  if (!user) {
    return <Navigate to="/login" replace state={{ from: location.pathname }} />;
  }

  return children;
}
