import Game from '@/components/Game';
import NewsSection from '@/components/NewsSection';
import { getOrCreateSpecialPuzzle } from '@/lib/dailyPuzzle';
import Link from 'next/link';
import { PrismaClient } from '@prisma/client';

export const dynamic = 'force-dynamic';

const prisma = new PrismaClient();

interface PageProps {
    params: Promise<{ name: string }>;
}

export default async function SpecialPage({ params }: PageProps) {
    const { name } = await params;
    const decodedName = decodeURIComponent(name);

    const currentSpecial = await prisma.special.findUnique({
        where: { name: decodedName }
    });

    const now = new Date();
    const isStarted = currentSpecial && (!currentSpecial.launchDate || currentSpecial.launchDate <= now);
    const isEnded = currentSpecial && (currentSpecial.endDate && currentSpecial.endDate < now);

    if (!currentSpecial || !isStarted) {
        return (
            <div style={{ textAlign: 'center', padding: '2rem' }}>
                <h1>Special Not Available</h1>
                <p>This special has not launched yet or does not exist.</p>
                <Link href="/">Go Home</Link>
            </div>
        );
    }

    if (isEnded) {
        return (
            <div style={{ textAlign: 'center', padding: '2rem' }}>
                <h1>Special Ended</h1>
                <p>This special event has ended.</p>
                <Link href="/">Go Home</Link>
            </div>
        );
    }

    const dailyPuzzle = await getOrCreateSpecialPuzzle(decodedName);
    const genres = await prisma.genre.findMany({
        where: { active: true },
        orderBy: { name: 'asc' }
    });
    const specials = await prisma.special.findMany({ orderBy: { name: 'asc' } });

    const activeSpecials = specials.filter(s => {
        const sStarted = !s.launchDate || s.launchDate <= now;
        const sEnded = s.endDate && s.endDate < now;
        return sStarted && !sEnded;
    });

    return (
        <>
            <div style={{ textAlign: 'center', padding: '1rem', background: '#fce7f3' }}>
                <div style={{ display: 'flex', justifyContent: 'center', gap: '1rem', flexWrap: 'wrap', alignItems: 'center' }}>
                    <Link href="/" style={{ color: '#4b5563', textDecoration: 'none' }}>Global</Link>

                    {/* Genres */}
                    {genres.map(g => (
                        <Link
                            key={g.id}
                            href={`/${g.name}`}
                            style={{
                                color: '#4b5563',
                                textDecoration: 'none'
                            }}
                        >
                            {g.name}
                        </Link>
                    ))}

                    {/* Separator if both exist */}
                    {genres.length > 0 && activeSpecials.length > 0 && (
                        <span style={{ color: '#d1d5db' }}>|</span>
                    )}

                    {/* Specials */}
                    {activeSpecials.map(s => (
                        <Link
                            key={s.id}
                            href={`/special/${s.name}`}
                            style={{
                                fontWeight: s.name === decodedName ? 'bold' : 'normal',
                                textDecoration: s.name === decodedName ? 'underline' : 'none',
                                color: s.name === decodedName ? '#9d174d' : '#be185d'
                            }}
                        >
                            ★ {s.name}
                        </Link>
                    ))}
                </div>
            </div>
            <NewsSection />
            <Game
                dailyPuzzle={dailyPuzzle}
                genre={decodedName}
                isSpecial={true}
                maxAttempts={dailyPuzzle?.maxAttempts}
                unlockSteps={dailyPuzzle?.unlockSteps}
            />
        </>
    );
}
