"use client";

import { useState, useEffect, useCallback } from "react";
import SecretLayer from "@/components/SecretLayer";
import DDoSProtection from "@/components/DDoSProtection";
import OnionDisclosureBar from "@/components/OnionDisclosureBar";
import HubChatterPublicStrip from "@/components/HubChatterPublicStrip";
import AppProviders from "@/components/AppProviders";
import GlobalFakeSiteFooter from "@/components/GlobalFakeSiteFooter";

export default function RootLayoutClient({ children }: { children: React.ReactNode }) {
  const [isProtected, setIsProtected] = useState(true);

  useEffect(() => {
    try {
      const hasSeenDDoS = sessionStorage.getItem("ddos_verified");
      if (hasSeenDDoS) {
        setIsProtected(false);
      }
    } catch {
      // sessionStorage blocked (rare) — still allow progress overlay to complete
    }
  }, []);

  const handleComplete = useCallback(() => {
    try {
      sessionStorage.setItem("ddos_verified", "true");
    } catch {
      /* ignore */
    }
    setIsProtected(false);
  }, []);

  return (
    <>
      {isProtected ? (
        <DDoSProtection onComplete={handleComplete} />
      ) : (
        <>
          <AppProviders>
            <div className="flex min-h-screen flex-col">
              <div className="flex-1">{children}</div>
              <GlobalFakeSiteFooter />
            </div>
          </AppProviders>
          <HubChatterPublicStrip />
          <SecretLayer />
          <OnionDisclosureBar />
        </>
      )}
    </>
  );
}
