import { Head, usePage } from '@inertiajs/react';
import { type ReactNode, useEffect } from 'react';
import { Toaster, toast } from 'sonner';
import type { PageProps } from '@/types';

interface Props {
    children: ReactNode;
    title?: string;
}

export default function GameLayout({ children, title }: Props) {
    const { flash } = usePage<PageProps>().props;

    useEffect(() => {
        // Force dark mode
        const root = window.document.documentElement;
        root.classList.add('dark');
        
        // Prevent scrolling on the body
        document.body.style.overflow = 'hidden';
        
        return () => {
            // Cleanup on unmount
            document.body.style.overflow = '';
        };
    }, []);

    useEffect(() => {
        if (flash?.success) {
            toast.success(flash.success, { id: 'success-' + Date.now() });
        }
        if (flash?.error) {
            toast.error(flash.error, { id: 'error-' + Date.now() });
        }
    }, [flash]);

    return (
        <>
            <Head title={title} />
            <div className="flex h-screen w-screen relative bg-[#0a0e1a] overflow-hidden font-sans">
                {/* Visual Theme: Neon Arcade */}
                <div className="fixed inset-0 z-0 pointer-events-none overflow-hidden">
                    
                    {/* Gradient Blobs */}
                    <div className="game-ambient-blob absolute top-[-20%] left-[-10%] w-[60%] h-[60%] rounded-full bg-[#ff2d78]/15 blur-[120px] animate-blob mix-blend-screen" />
                    <div className="game-ambient-blob absolute bottom-[-20%] right-[-10%] w-[60%] h-[60%] rounded-full bg-[#00e5ff]/15 blur-[120px] animate-blob animation-delay-2000 mix-blend-screen" />
                    <div className="game-ambient-blob absolute top-[30%] left-[30%] w-[40%] h-[40%] rounded-full bg-[#39ff14]/5 blur-[100px] animate-blob animation-delay-4000 mix-blend-screen" />

                    {/* Subtle Grid Pattern */}
                    <div 
                        className="absolute inset-0 opacity-[0.04]" 
                        style={{
                            backgroundImage: `
                                linear-gradient(to right, #00e5ff 1px, transparent 1px),
                                linear-gradient(to bottom, #ff2d78 1px, transparent 1px)
                            `,
                            backgroundSize: '60px 60px'
                        }} 
                    />

                    {/* Scanline Effect */}
                    <div className="absolute inset-0 opacity-[0.1] bg-[linear-gradient(rgba(18,16,16,0)_50%,rgba(0,0,0,0.25)_50%),linear-gradient(90deg,rgba(255,0,0,0.06),rgba(0,255,0,0.02),rgba(0,0,255,0.06))] bg-[length:100%_4px,3px_100%] z-50 pointer-events-none" />
                    
                    {/* Floating Particles */}
                    {Array.from({ length: 20 }).map((_, i) => (
                        <div 
                            key={i}
                            className="game-ambient-particle absolute rounded-full bg-white/30 animate-float shadow-[0_0_10px_rgba(255,255,255,0.8)]"
                            style={{
                                width: Math.random() * 3 + 1 + 'px',
                                height: Math.random() * 3 + 1 + 'px',
                                left: Math.random() * 100 + '%',
                                top: Math.random() * 100 + '%',
                                animationDuration: Math.random() * 15 + 10 + 's',
                                animationDelay: Math.random() * 5 + 's',
                            }}
                        />
                    ))}

                    <style dangerouslySetInnerHTML={{__html: `
                        @keyframes blob {
                            0% { transform: translate(0px, 0px) scale(1); }
                            33% { transform: translate(30px, -50px) scale(1.1); }
                            66% { transform: translate(-20px, 20px) scale(0.9); }
                            100% { transform: translate(0px, 0px) scale(1); }
                        }
                        .animate-blob {
                            animation: blob 20s infinite alternate ease-in-out;
                        }
                        .animation-delay-2000 {
                            animation-delay: 2s;
                        }
                        .animation-delay-4000 {
                            animation-delay: 4s;
                        }
                        @keyframes float {
                            0% { transform: translateY(0) translateX(0); opacity: 0; }
                            10% { opacity: 1; }
                            90% { opacity: 1; }
                            100% { transform: translateY(-100vh) translateX(30px); opacity: 0; }
                        }
                        .animate-float {
                            animation: float linear infinite;
                        }
                    `}} />
                </div>

                {/* Toaster for Flash Messages */}
                <Toaster richColors position="top-right" theme="dark" style={{ zIndex: 99999 }} />

                {/* Main Content */}
                <main className="flex-1 relative z-10 h-full w-full flex flex-col">
                    {children}
                </main>
            </div>
        </>
    );
}
