"use client";

import Image from "next/image";
import { AnimatePresence, motion } from "framer-motion";
import { useEffect, useState } from "react";

export function SiteEntryAnimation({ children }: { children: React.ReactNode }) {
  const [showIntro, setShowIntro] = useState(true);

  useEffect(() => {
    const seenIntro = window.sessionStorage.getItem("sg-intro-seen");

    if (seenIntro === "1") {
      setShowIntro(false);
      return;
    }

    const timer = window.setTimeout(() => {
      setShowIntro(false);
      window.sessionStorage.setItem("sg-intro-seen", "1");
    }, 2400);

    return () => window.clearTimeout(timer);
  }, []);

  return (
    <>
      <motion.div
        initial={{ opacity: 0, scale: 0.97 }}
        animate={{ 
          opacity: showIntro ? 0.2 : 1, 
          scale: showIntro ? 0.97 : 1 
        }}
        transition={{ duration: 1.2, ease: [0.16, 1, 0.3, 1] }}
      >
        {children}
      </motion.div>

      <AnimatePresence>
        {showIntro && (
          <motion.div
            key="site-intro"
            className="fixed inset-0 z-[200] flex items-center justify-center overflow-hidden"
            style={{ background: "#05040f" }}
            initial={{ opacity: 1 }}
            exit={{ opacity: 0, transition: { duration: 0.7, ease: [0.43, 0.13, 0.23, 0.96] } }}
          >
            {/* Animated background orbs */}
            <motion.div
              className="absolute inset-0"
              initial={{ opacity: 0 }}
              animate={{ opacity: 1 }}
              transition={{ duration: 1.2 }}
              style={{
                background:
                  "radial-gradient(ellipse 80% 60% at 30% 40%, rgba(124,58,237,0.25) 0%, transparent 55%), radial-gradient(ellipse 60% 50% at 70% 60%, rgba(201,162,80,0.15) 0%, transparent 50%), radial-gradient(ellipse 100% 80% at 50% 100%, rgba(100,20,55,0.20) 0%, transparent 60%)"
              }}
            />

            {/* Particle ring */}
            <motion.div
              className="absolute rounded-full"
              initial={{ scale: 0.5, opacity: 0 }}
              animate={{ scale: [0.5, 1.8, 2.4], opacity: [0, 0.25, 0] }}
              transition={{ duration: 2, ease: "easeOut", delay: 0.3 }}
              style={{
                width: 320,
                height: 320,
                border: "1px solid rgba(201,162,80,0.35)"
              }}
            />
            <motion.div
              className="absolute rounded-full"
              initial={{ scale: 0.4, opacity: 0 }}
              animate={{ scale: [0.4, 1.4, 2.0], opacity: [0, 0.2, 0] }}
              transition={{ duration: 2, ease: "easeOut", delay: 0.6 }}
              style={{
                width: 220,
                height: 220,
                border: "1px solid rgba(124,58,237,0.25)"
              }}
            />

            {/* Center content */}
            <motion.div
              className="relative flex flex-col items-center gap-5"
              initial={{ scale: 0.7, opacity: 0 }}
              animate={{ scale: 1, opacity: 1 }}
              transition={{ duration: 0.85, ease: [0.2, 0.9, 0.2, 1] }}
            >
              {/* Logo circle */}
              <motion.div
                className="relative flex h-24 w-24 items-center justify-center rounded-full"
                animate={{ scale: [1, 1.06, 1] }}
                transition={{ duration: 2, repeat: Infinity, ease: "easeInOut" }}
                style={{
                  background: "rgba(201,162,80,0.08)",
                  border: "1px solid rgba(201,162,80,0.30)",
                  boxShadow: "0 0 48px rgba(201,162,80,0.28), 0 0 80px rgba(124,58,237,0.12)"
                }}
              >
                {/* Rotating outer ring */}
                <motion.div
                  className="absolute inset-[-6px] rounded-full"
                  animate={{ rotate: 360 }}
                  transition={{ duration: 6, repeat: Infinity, ease: "linear" }}
                  style={{
                    background:
                      "conic-gradient(from 0deg, transparent 70%, rgba(201,162,80,0.6) 90%, transparent 100%)",
                    borderRadius: "50%"
                  }}
                />
                <Image
                  src="/logo.png.png"
                  alt="Sunna Ghar logo"
                  fill
                  className="object-contain p-3"
                  priority
                />
              </motion.div>

              {/* Brand name */}
              <motion.p
                initial={{ opacity: 0, y: 10 }}
                animate={{ opacity: 1, y: 0 }}
                transition={{ delay: 0.35, duration: 0.55 }}
                className="text-xl font-extrabold tracking-[0.28em] uppercase"
                style={{
                  background: "linear-gradient(90deg, #c9a250 0%, #f0d080 40%, #faedb0 50%, #f0d080 60%, #c9a250 100%)",
                  backgroundSize: "200% auto",
                  WebkitBackgroundClip: "text",
                  backgroundClip: "text",
                  WebkitTextFillColor: "transparent",
                  animation: "shimmer-text 3s linear infinite"
                }}
              >
                Sunna Ghar
              </motion.p>

              {/* Tagline */}
              <motion.p
                initial={{ opacity: 0 }}
                animate={{ opacity: 1 }}
                transition={{ delay: 0.6 }}
                className="text-xs font-medium tracking-[0.4em] text-white/35 uppercase"
              >
                Premium Fashion
              </motion.p>

              {/* Loading bar */}
              <motion.div
                className="relative h-px w-40 overflow-hidden rounded-full"
                style={{ background: "rgba(255,255,255,0.08)" }}
              >
                <motion.div
                  className="absolute inset-y-0 left-0 rounded-full"
                  initial={{ width: "0%" }}
                  animate={{ width: "100%" }}
                  transition={{ duration: 1.8, ease: "easeInOut", delay: 0.4 }}
                  style={{
                    background: "linear-gradient(90deg, #c9a250, #f0d080, #c9a250)"
                  }}
                />
              </motion.div>
            </motion.div>
          </motion.div>
        )}
      </AnimatePresence>
    </>
  );
}

