import { Head, Link, usePage } from '@inertiajs/react';
import {
    ArrowLeft,
    CalendarClock,
    ClipboardList,
    Eye,
    FileText,
    Mail,
    Phone,
    Sparkles,
    Sun,
} from 'lucide-react';
import AppLogoIcon from '@/components/app-logo-icon';
import type { Branding, ComingSoonPageProps } from '@/types';

/**
 * The page a site wears before it is public.
 *
 * One quiet screen: the company's name and mark, one sentence about what is
 * coming, and how to reach the office in the meantime. No countdown timer, no
 * email capture, no carousel — a holding page has one job, and anything else on
 * it makes the site look busier than it is.
 *
 * The date, when one has been set, is printed as a month rather than a day: a
 * business that says "March" and opens on the 18th has kept its word, and one
 * that says "the 18th" and opens on the 19th has not.
 */
export default function ComingSoon({
    preview = false,
    ...page
}: ComingSoonPageProps) {
    const { branding } = usePage<{ branding: Branding }>().props;
    const year = new Date().getFullYear();
    const countdown = countdownLabel(page.days_away);

    return (
        <>
            <Head title={page.eyebrow}>
                {/* A page that is not public yet has nothing to offer a
                    search engine but a wrong first impression. */}
                <meta name="robots" content="noindex,nofollow" />
            </Head>

            {preview ? (
                <div className="relative z-20 flex flex-wrap items-center justify-center gap-x-3 gap-y-1 bg-amber-400 px-4 py-2 text-center text-[13px] font-medium text-amber-950">
                    <span className="inline-flex items-center gap-1.5">
                        <Eye className="size-4" />
                        Preview — this is the page visitors see while Coming
                        soon is on.
                    </span>
                    <Link
                        href="/settings/coming-soon"
                        className="inline-flex items-center gap-1 underline underline-offset-4"
                    >
                        <ArrowLeft className="size-3.5" />
                        Back to settings
                    </Link>
                </div>
            ) : null}

            <header className="mx-auto flex w-full max-w-6xl items-center justify-between gap-4 px-6 py-6 sm:px-8">
                <div className="flex min-w-0 items-center gap-3">
                    {branding.logo_url ? (
                        // The uploaded logo is the wordmark: repeating the
                        // company name beside it would say it twice. It sits on
                        // a faint plate so a dark logo is not lost here.
                        <img
                            src={branding.logo_url}
                            alt={branding.name}
                            className="h-10 w-auto max-w-[11rem] rounded-lg bg-white/10 object-contain object-left p-1 ring-1 ring-white/15"
                        />
                    ) : (
                        <>
                            <span className="flex size-9 items-center justify-center rounded-xl bg-white/10 ring-1 ring-white/20 backdrop-blur-sm">
                                <AppLogoIcon className="size-4 fill-white" />
                            </span>
                            <span className="min-w-0 truncate text-sm font-semibold tracking-tight">
                                {branding.name}
                            </span>
                        </>
                    )}
                </div>

                <Link
                    href="/login"
                    className="shrink-0 text-[11px] font-medium tracking-[0.12em] text-white/45 uppercase underline-offset-4 transition-colors hover:text-white/80 hover:underline"
                >
                    Team sign in
                </Link>
            </header>

            <main className="mx-auto flex w-full max-w-6xl flex-1 flex-col justify-center px-6 py-14 sm:px-8 sm:py-20">
                <div className="grid items-center gap-12 lg:grid-cols-[minmax(0,1.2fr)_minmax(0,20rem)] lg:gap-20">
                    <div className="max-w-2xl">
                    <p className="inline-flex items-center gap-2 rounded-full border border-white/15 bg-white/[0.07] px-3 py-1 text-[11px] font-medium tracking-[0.12em] text-white/75 uppercase backdrop-blur-sm">
                        <span className="relative flex size-1.5">
                            <span className="absolute inline-flex size-full animate-ping rounded-full bg-white/70" />
                            <span className="relative inline-flex size-1.5 rounded-full bg-white" />
                        </span>
                        {page.eyebrow}
                    </p>

                    <h1 className="mt-6 text-4xl font-semibold tracking-tight text-balance sm:text-5xl lg:text-[3.4rem] lg:leading-[1.05]">
                        {page.headline}
                    </h1>

                    <p className="mt-5 max-w-xl text-[15px] leading-relaxed text-white/65 sm:text-base">
                        {page.message}
                    </p>

                    <div className="mt-9 flex flex-wrap items-center gap-3">
                        {page.expected_label ? (
                            <span className="inline-flex items-center gap-2 rounded-full border border-white/15 bg-white/[0.06] px-4 py-2 text-sm text-white/85 backdrop-blur-sm">
                                <CalendarClock className="size-4 text-white/55" />
                                Launching {page.expected_label}
                                {countdown ? (
                                    <span className="text-white/45">
                                        · {countdown}
                                    </span>
                                ) : null}
                            </span>
                        ) : null}

                        {phoneNumber(page.contact_phone) ? (
                            <a
                                href={`tel:${phoneNumber(page.contact_phone)?.replace(/\s+/g, '')}`}
                                className="inline-flex items-center gap-2 rounded-full border border-white/15 bg-white/[0.06] px-4 py-2 text-sm text-white/85 backdrop-blur-sm transition-colors hover:border-white/25 hover:text-white"
                            >
                                <Phone className="size-4 text-white/55" />
                                {page.contact_phone}
                            </a>
                        ) : null}

                        {page.contact_email ? (
                            <a
                                href={`mailto:${page.contact_email}`}
                                className="inline-flex items-center gap-2 rounded-full border border-white/15 bg-white/[0.06] px-4 py-2 text-sm text-white/85 backdrop-blur-sm transition-colors hover:border-white/25 hover:text-white"
                            >
                                <Mail className="size-4 text-white/55" />
                                {page.contact_email}
                            </a>
                        ) : null}
                    </div>

                        {!preview &&
                        (page.contact_email || phoneNumber(page.contact_phone)) ? (
                            <p className="mt-10 flex max-w-md items-start gap-2 text-xs leading-relaxed text-white/40">
                                <Sparkles className="mt-0.5 size-3.5 shrink-0" />
                                Existing customers can still reach the office
                                above. Everything else opens on launch day.
                            </p>
                        ) : null}
                    </div>

                    {/* What the portal will be for. Kept to three lines and
                        hidden on a phone, where the message is already the
                        whole page. */}
                    <aside className="hidden lg:block">
                        <div className="rounded-2xl border border-white/10 bg-white/[0.04] p-5 backdrop-blur-sm">
                            <p className="text-[10px] font-medium tracking-[0.16em] text-white/40 uppercase">
                                On launch day
                            </p>
                            <ul className="mt-4 space-y-4">
                                {launchItems.map((item) => (
                                    <li
                                        key={item.title}
                                        className="flex items-start gap-3"
                                    >
                                        <span className="mt-0.5 flex size-7 shrink-0 items-center justify-center rounded-lg bg-white/[0.08] ring-1 ring-white/10">
                                            <item.icon className="size-3.5 text-white/70" />
                                        </span>
                                        <span className="min-w-0">
                                            <span className="block text-[13px] font-medium">
                                                {item.title}
                                            </span>
                                            <span className="mt-0.5 block text-[11px] leading-relaxed text-white/50">
                                                {item.body}
                                            </span>
                                        </span>
                                    </li>
                                ))}
                            </ul>
                        </div>
                    </aside>
                </div>
            </main>

            <footer className="mx-auto w-full max-w-6xl px-6 pb-7 sm:px-8">
                <div className="flex flex-col gap-1 border-t border-white/10 pt-5 text-[11px] text-white/40 sm:flex-row sm:items-center sm:justify-between">
                    <span>
                        {branding.legal_name || branding.name}
                        {branding.abn_label ? ` · ${branding.abn_label}` : ''}
                    </span>
                    <span className="flex flex-wrap items-center gap-x-3 gap-y-1">
                        {branding.address_lines.length > 0 ? (
                            <span>{branding.address_lines.join(', ')}</span>
                        ) : null}
                        <span>
                            © {year} {branding.name}
                        </span>
                    </span>
                </div>
            </footer>
        </>
    );
}

/**
 * The three things the portal will be for, in the words a customer uses rather
 * than the words the CRM uses.
 */
const launchItems = [
    {
        title: 'Your system',
        icon: Sun,
        body: 'Every panel, inverter and certificate for your install, in one place.',
    },
    {
        title: 'Your requests',
        icon: ClipboardList,
        body: 'Follow an open fault without ringing the office to ask where it is up to.',
    },
    {
        title: 'Your documents',
        icon: FileText,
        body: 'Invoices, warranties and compliance paperwork you can find years later.',
    },
];

/**
 * A phone number, or nothing.
 *
 * The number comes from the company profile, where a field can end up holding
 * something that is not a number at all. A line that reads "Dingley Village"
 * under a telephone icon is worse than no line, so anything without digits is
 * left off the page.
 */
function phoneNumber(value: string | null): string | null {
    return value && /\d{6,}/.test(value.replace(/\s+/g, '')) ? value : null;
}

/**
 * "in 34 days", "tomorrow", "today" — or nothing at all once the date has
 * passed, because a countdown that has run out is worse than no countdown.
 */
function countdownLabel(days: number | null): string | null {
    if (days === null || days < 0) {
        return null;
    }

    if (days === 0) {
        return 'today';
    }

    if (days === 1) {
        return 'tomorrow';
    }

    return `in ${days} days`;
}
