// app/beach/page.tsx
import { Metadata } from 'next';
import { getConfig } from '../lib/config';
import BeachClientPage from './BeachClientPage';

export const dynamic = 'force-dynamic';

export async function generateMetadata(): Promise<Metadata> {
    const config = await getConfig();
    const matchDate = new Date(config.beachPage.matchStart);
    const year = matchDate.getFullYear();
    const dateFormatted = matchDate.toLocaleDateString('sv-SE', { day: 'numeric', month: 'long' });

    const isClosed = config.beachPage.isClosedOverride;
    const title = isClosed
        ? `Höganäs Beach Open ${year} (Fulltecknat)`
        : `Anmälan: Höganäs Beach Open ${year} - ${dateFormatted}`;

    const description = isClosed
        ? `Turneringen den ${dateFormatted} är nu fullbokad. Välkommen ner till Kvickbadet för att titta på beachvolleyboll i toppklass!`
        : `Välkommen till Höganäs Beach Open den ${dateFormatted}! Turneringsinfo, regler och anmälan för beachvolleyboll vid Kvickbadet.`;

    return {
        title: title,
        description: description,
        openGraph: {
            title: title,
            description: description,
            url: 'https://lerbergetsvolleyboll.se/beach',
            siteName: 'Lerbergets Volleybollsällskap',
            locale: 'sv_SE',
            type: 'website',
            images: [
                {
                    url: '/images/beach-tournament.webp',
                    width: 1200,
                    height: 630,
                    alt: `Höganäs Beach Open ${year}`,
                },
            ],
        },
        twitter: {
            card: 'summary_large_image',
            title: title,
            description: description,
            images: ['/images/beach-tournament.webp'],
        },
    };
}

export default async function BeachPage() {
    const config = await getConfig();
    const turnstileSiteKey = process.env.TURNSTILE_SITE_KEY || '';

    const jsonLd = {
        '@context': 'https://schema.org',
        '@type': 'Event',
        'name': 'Höganäs Beach Open',
        'startDate': config.beachPage.matchStart,
        'location': {
            '@type': 'Place',
            'name': 'Kvickbadet',
            'address': {
                '@type': 'PostalAddress',
                'addressLocality': 'Höganäs',
                'addressCountry': 'SE',
            },
        },
        'eventStatus': config.beachPage.isClosedOverride
            ? 'https://schema.org/EventScheduled'
            : 'https://schema.org/EventScheduled',
        'organizer': {
            '@type': 'Organization',
            'name': 'Lerbergets Volleybollsällskap',
            'url': 'https://lerbergetsvolleyboll.se'
        }
    };

    return (
        <>
            <script
                type="application/ld+json"
                dangerouslySetInnerHTML={{ __html: JSON.stringify(jsonLd) }}
            />
            <BeachClientPage config={config} turnstileSiteKey={turnstileSiteKey} />
        </>
    );
}