import { useAuth } from '../contexts/AuthContext.jsx'
import { useSite } from '../contexts/SiteContext.jsx'
import Maintenance from '../routes/public/Maintenance.jsx'

// Wraps the public site. When the site is in maintenance, visitors see the
// coming-soon page; a logged-in admin sees the real site (live preview).
export default function MaintenanceGate({ children }) {
  const { mode, loading } = useSite()
  const { user, loading: authLoading } = useAuth()

  if (loading || authLoading) {
    return (
      <div style={{ minHeight: '100vh', display: 'grid', placeItems: 'center', background: 'var(--bg-deep)' }}>
        <span className="spin" />
      </div>
    )
  }
  if (mode === 'maintenance' && !user) {
    return <Maintenance />
  }
  return children
}
