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

import { EasyFormaLogo } from '@/components/brand/logo';
import {
    dashboard,
    home,
    login,
    pricing,
    privacy,
    register,
    terms,
} from '@/routes';

const SECTIONS = {
    comeFunziona: 'come-funziona',
    assistente: 'assistente',
    conformita: 'conformita',
    piani: 'piani',
    faq: 'faq',
} as const;

/** Ancora sulla landing, raggiungibile anche dalle altre pagine pubbliche. */
function landingAnchor(id: string): string {
    return `${home.url()}#${id}`;
}

const NAV_LINKS = [
    { label: 'Come funziona', href: landingAnchor(SECTIONS.comeFunziona) },
    { label: 'Assistente', href: landingAnchor(SECTIONS.assistente) },
    { label: 'Conformità', href: landingAnchor(SECTIONS.conformita) },
    { label: 'Prezzi', href: pricing.url() },
    { label: 'FAQ', href: landingAnchor(SECTIONS.faq) },
];

const FOOTER_COLUMNS = [
    {
        title: 'Prodotto',
        links: [
            {
                label: 'Come funziona',
                href: landingAnchor(SECTIONS.comeFunziona),
            },
            { label: 'Piani', href: landingAnchor(SECTIONS.piani) },
            { label: 'Prezzi', href: pricing.url() },
        ],
    },
    {
        title: 'Risorse',
        links: [
            {
                label: 'Conformità FAD sincrona',
                href: landingAnchor(SECTIONS.conformita),
            },
            { label: 'Domande frequenti', href: landingAnchor(SECTIONS.faq) },
        ],
    },
    {
        title: 'Legale',
        links: [
            { label: 'Privacy', href: privacy.url() },
            { label: 'Termini di servizio', href: terms.url() },
        ],
    },
];

const focusRing =
    'rounded-marketing-sm outline-marketing-primary focus-visible:outline-2 focus-visible:outline-offset-2';

const primaryButton = `inline-flex items-center justify-center rounded-marketing-sm bg-marketing-primary px-5 py-3 text-[0.9375rem] leading-none font-medium text-marketing-primary-foreground transition-colors hover:bg-marketing-primary-hover ${focusRing}`;

const navLink = `text-[0.9375rem] font-medium text-marketing-ink/80 underline-offset-4 transition-colors hover:text-marketing-ink hover:underline ${focusRing}`;

type Props = {
    children: ReactNode;
    title?: string;
};

export default function MarketingLayout({ children, title }: Props) {
    const { auth, seo } = usePage().props;
    const [menuOpen, setMenuOpen] = useState(false);
    const isAuthenticated = Boolean(auth.user);

    return (
        <div className="marketing-scope bg-marketing-bg text-marketing-ink flex min-h-dvh flex-col font-sans antialiased">
            {/*
              Titolo completo risolto dal server (App\Support\Seo): contiene
              già il nome, e il template in `app.tsx` lo lascia intatto, così
              la scheda mostra lo stesso titolo che legge il crawler.
            */}
            <Head title={seo?.title ?? title} />

            <header className="border-marketing-border bg-marketing-surface sticky top-0 z-50 border-b">
                <div className="mx-auto flex h-16 w-full max-w-[72rem] items-center justify-between gap-6 px-6">
                    <Link
                        href={home()}
                        className={`text-marketing-ink flex items-center ${focusRing}`}
                        aria-label="EasyForma, home"
                    >
                        <EasyFormaLogo className="h-6 w-auto" title="" />
                    </Link>

                    <nav
                        aria-label="Navigazione principale"
                        className="hidden items-center gap-7 lg:flex"
                    >
                        {NAV_LINKS.map((item) => (
                            <a
                                key={item.label}
                                href={item.href}
                                className={navLink}
                            >
                                {item.label}
                            </a>
                        ))}
                    </nav>

                    <div className="hidden items-center gap-5 lg:flex">
                        {isAuthenticated ? (
                            <Link href={dashboard()} className={primaryButton}>
                                Vai alla dashboard
                            </Link>
                        ) : (
                            <>
                                <Link href={login()} className={navLink}>
                                    Accedi
                                </Link>
                                <Link
                                    href={register()}
                                    className={primaryButton}
                                >
                                    Crea account
                                </Link>
                            </>
                        )}
                    </div>

                    <button
                        type="button"
                        aria-expanded={menuOpen}
                        aria-controls="marketing-menu"
                        aria-label={
                            menuOpen ? 'Chiudi il menu' : 'Apri il menu'
                        }
                        onClick={() => setMenuOpen((open) => !open)}
                        className={`-mr-2 inline-flex size-10 items-center justify-center lg:hidden ${focusRing}`}
                    >
                        <span
                            aria-hidden="true"
                            className="relative block h-3.5 w-5"
                        >
                            <span className="bg-marketing-ink absolute inset-x-0 top-0 h-0.5" />
                            <span className="bg-marketing-ink absolute inset-x-0 top-1.5 h-0.5" />
                            <span className="bg-marketing-ink absolute inset-x-0 top-3 h-0.5" />
                        </span>
                    </button>
                </div>

                {menuOpen && (
                    <div
                        id="marketing-menu"
                        className="border-marketing-border bg-marketing-surface border-t lg:hidden"
                    >
                        <nav
                            aria-label="Navigazione principale"
                            className="mx-auto flex w-full max-w-[72rem] flex-col gap-1 px-6 py-4"
                        >
                            {NAV_LINKS.map((item) => (
                                <a
                                    key={item.label}
                                    href={item.href}
                                    onClick={() => setMenuOpen(false)}
                                    className={`py-2 ${navLink}`}
                                >
                                    {item.label}
                                </a>
                            ))}

                            {isAuthenticated ? (
                                <Link
                                    href={dashboard()}
                                    onClick={() => setMenuOpen(false)}
                                    className={`mt-3 ${primaryButton}`}
                                >
                                    Vai alla dashboard
                                </Link>
                            ) : (
                                <>
                                    <Link
                                        href={login()}
                                        onClick={() => setMenuOpen(false)}
                                        className={`py-2 ${navLink}`}
                                    >
                                        Accedi
                                    </Link>
                                    <Link
                                        href={register()}
                                        onClick={() => setMenuOpen(false)}
                                        className={`mt-3 ${primaryButton}`}
                                    >
                                        Crea account
                                    </Link>
                                </>
                            )}
                        </nav>
                    </div>
                )}
            </header>

            <main className="flex-1">{children}</main>

            <footer className="border-marketing-border bg-marketing-bg border-t">
                <div className="mx-auto w-full max-w-[72rem] px-6 py-14">
                    <EasyFormaLogo className="text-marketing-ink h-6 w-auto" />

                    <p className="text-marketing-ink/75 mt-5 max-w-[34rem] text-[0.9375rem] leading-relaxed">
                        EasyForma è la piattaforma per le aule virtuali dei
                        corsi FAD finanziati da Forma.Temp.
                    </p>

                    <div className="mt-10 grid gap-8 sm:grid-cols-3">
                        {FOOTER_COLUMNS.map((column) => (
                            <div key={column.title}>
                                <h2 className="font-display text-[0.9375rem] font-semibold">
                                    {column.title}
                                </h2>
                                <ul className="mt-3 flex flex-col gap-2">
                                    {column.links.map((item) => (
                                        <li key={item.label}>
                                            <a
                                                href={item.href}
                                                className={`text-marketing-ink/75 hover:text-marketing-ink text-sm underline-offset-4 hover:underline ${focusRing}`}
                                            >
                                                {item.label}
                                            </a>
                                        </li>
                                    ))}
                                </ul>
                            </div>
                        ))}
                    </div>

                    <p className="border-marketing-border text-marketing-ink/60 mt-12 border-t pt-6 text-sm">
                        © 2026 EasyForma — P.IVA{' '}
                        <span className="marketing-data">00000000000</span>
                    </p>
                </div>
            </footer>
        </div>
    );
}
