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

// Gate for /admin/* — redirects to the login screen when not authenticated.
export default function RequireAuth({ children }) {
  const { user, loading } = useAuth()
  const location = useLocation()

  if (loading) {
    return (
      <div style={{ minHeight: '100vh', display: 'grid', placeItems: 'center', background: 'var(--bg-deep)' }}>
        <span className="spin" />
      </div>
    )
  }
  if (!user) {
    return <Navigate to="/admin/login" state={{ from: location }} replace />
  }
  return children
}
