import { type SharedData } from '@/types';
import { Head, Link, usePage } from '@inertiajs/react';
import { useEffect, useState } from 'react';

interface WelcomeProps extends SharedData {
    heroBackgroundUrl?: string | null;
    heroHeight?: number;
}

export default function Welcome() {
    const { auth, heroBackgroundUrl, heroHeight: _heroHeight } = usePage<WelcomeProps>().props;
    const [isScrolled, setIsScrolled] = useState(false);

    // Detect scroll to add blur effect
    useEffect(() => {
        const handleScroll = () => {
            setIsScrolled(window.scrollY > 10);
        };

        window.addEventListener('scroll', handleScroll);
        return () => window.removeEventListener('scroll', handleScroll);
    }, []);

    return (
        <>
            <Head title="Welcome">
                <link rel="preconnect" href="https://fonts.bunny.net" />
                <link href="https://fonts.bunny.net/css?family=instrument-sans:400,500,600" rel="stylesheet" />
            </Head>
            <div className="relative min-h-screen text-gray-800 dark:text-gray-100">
                {/* Hero Background */}
                {heroBackgroundUrl ? (
                    heroBackgroundUrl.endsWith('.mp4') ? (
                        <div className="absolute inset-0">
                            <video src={heroBackgroundUrl} className="h-full w-full object-cover" autoPlay loop muted playsInline />
                        </div>
                    ) : (
                        <div className="absolute inset-0 bg-cover bg-center bg-no-repeat" style={{ backgroundImage: `url(${heroBackgroundUrl})` }} />
                    )
                ) : (
                    <div className="absolute inset-0 bg-gradient-to-br from-blue-50 to-blue-100 dark:from-gray-900 dark:to-gray-800" />
                )}

                {/* Dark overlay for better text readability */}
                <div className="absolute inset-0 bg-black/40" />

                {/* Navbar */}
                <nav
                    className={`fixed top-0 right-0 left-0 z-50 px-6 py-4 transition-all duration-300 ${
                        isScrolled ? 'bg-white/70 shadow-md backdrop-blur-md dark:bg-gray-900/70' : 'bg-transparent'
                    }`}
                >
                    <div className="container mx-auto flex items-center justify-between">
                        <Link
                            href="/"
                            className={`text-2xl font-bold transition ${
                                isScrolled
                                    ? 'text-blue-600 hover:text-blue-700 dark:text-blue-400 dark:hover:text-blue-300'
                                    : 'text-white drop-shadow-md hover:text-white/90'
                            }`}
                        >
                            YourLogo
                        </Link>
                        <div className="hidden items-center space-x-6 md:flex">
                            <Link
                                href="#features"
                                className={`text-sm font-medium transition ${
                                    isScrolled
                                        ? 'text-gray-700 hover:text-blue-600 dark:text-gray-300 dark:hover:text-blue-400'
                                        : 'text-white/90 drop-shadow-sm hover:text-white'
                                }`}
                            >
                                Features
                            </Link>
                            <Link
                                href="#contact"
                                className={`text-sm font-medium transition ${
                                    isScrolled
                                        ? 'text-gray-700 hover:text-blue-600 dark:text-gray-300 dark:hover:text-blue-400'
                                        : 'text-white/90 drop-shadow-sm hover:text-white'
                                }`}
                            >
                                Contact
                            </Link>
                            {auth.user ? (
                                <Link
                                    href={route('dashboard')}
                                    className="inline-block rounded-lg bg-blue-600 px-4 py-2 text-sm font-medium text-white shadow-md transition hover:bg-blue-700 focus:ring-2 focus:ring-blue-500 focus:ring-offset-2 focus:outline-none"
                                >
                                    Dashboard
                                </Link>
                            ) : (
                                <>
                                    <Link
                                        href={route('login')}
                                        className={`relative inline-block text-sm font-medium transition duration-300 ease-in-out ${
                                            isScrolled
                                                ? 'text-blue-600 hover:text-blue-700 dark:text-blue-400 dark:hover:text-blue-300'
                                                : 'text-white drop-shadow-sm hover:text-white/90'
                                        }`}
                                    >
                                        Log in
                                    </Link>
                                    <Link
                                        href={route('register')}
                                        className="inline-block rounded-lg bg-blue-600 px-4 py-2 text-sm font-medium text-white shadow-md transition hover:bg-blue-700 focus:ring-2 focus:ring-blue-500 focus:ring-offset-2 focus:outline-none"
                                    >
                                        Sign up
                                    </Link>
                                </>
                            )}
                        </div>
                        {/* Mobile Menu */}
                        <div className="md:hidden">
                            <button
                                className={`focus:outline-none ${isScrolled ? 'text-gray-700 dark:text-gray-300' : 'text-white drop-shadow-sm'}`}
                                aria-label="Open Menu"
                            >
                                <svg
                                    xmlns="http://www.w3.org/2000/svg"
                                    fill="none"
                                    viewBox="0 0 24 24"
                                    strokeWidth="2"
                                    stroke="currentColor"
                                    className="h-6 w-6"
                                >
                                    <path strokeLinecap="round" strokeLinejoin="round" d="M4 6h16M4 12h16m-7 6h7" />
                                </svg>
                            </button>
                        </div>
                    </div>
                </nav>

                {/* Main Content */}
                <main className="relative z-10 flex min-h-screen flex-col items-center justify-center px-6 pt-20 text-center">
                    <header className="mb-10 w-full max-w-4xl">
                        <h1 className="text-4xl font-extrabold tracking-tight text-white drop-shadow-lg sm:text-5xl">Welcome to Our Platform</h1>
                        <p className="mt-4 text-lg text-white/90 drop-shadow-md">
                            Discover the best way to manage your tasks and collaborate with your team.
                        </p>
                    </header>

                    <nav className="flex items-center justify-center gap-6">
                        {auth.user ? (
                            <Link
                                href={route('dashboard')}
                                className="inline-block rounded-lg bg-blue-600 px-6 py-3 text-sm font-medium text-white shadow-lg transition hover:scale-105 hover:bg-blue-700 focus:ring-2 focus:ring-blue-500 focus:ring-offset-2 focus:outline-none"
                            >
                                Go to Dashboard
                            </Link>
                        ) : (
                            <>
                                <Link
                                    href={route('login')}
                                    className="inline-block rounded-lg border-2 border-white/80 bg-white/10 px-6 py-3 text-sm font-medium text-white shadow-lg backdrop-blur-sm transition hover:scale-105 hover:bg-white/20 focus:ring-2 focus:ring-white focus:ring-offset-2 focus:outline-none"
                                >
                                    Log in
                                </Link>
                                <Link
                                    href={route('register')}
                                    className="inline-block rounded-lg bg-blue-600 px-6 py-3 text-sm font-medium text-white shadow-lg transition hover:scale-105 hover:bg-blue-700 focus:ring-2 focus:ring-blue-500 focus:ring-offset-2 focus:outline-none"
                                >
                                    Sign up
                                </Link>
                            </>
                        )}
                    </nav>

                    <footer className="mt-12 text-center text-sm text-white/70">
                        <p>&copy; {new Date().getFullYear()} Your Company. All rights reserved.</p>
                    </footer>
                </main>
            </div>
        </>
    );
}
