import React from 'react';

const HUB_URL = import.meta.env.VITE_HUB_URL || 'https://workspace.aduanasoft.com';

function isAuthenticated() {
  return !!localStorage.getItem('access');
}

export default function RequireAuth({ children }) {
  if (!isAuthenticated()) {
    // Sesión expirada o no iniciada → redirigir al Hub con return_to
    const returnTo = encodeURIComponent(`${window.location.origin}/auth/sso`);
    window.location.href = `${HUB_URL}/login?return_to=${returnTo}`;
    return null;
  }

  return children;
}
