import { getTranslations } from 'next-intl/server'
import { Home, ChevronRight, CalendarDays, Clock, MessageCircle, ArrowLeft, ArrowUp } from 'lucide-react'
import { Link } from '@/i18n/navigation'
import Container from '@/components/frontend/Container'
import CmsTableOfContents from '@/components/frontend/cms/CmsTableOfContents'
import type { PageHeading } from '@/lib/cms/headingAnchors'
import '@/app/styles.css'

interface CmsPageLayoutProps {
  title: string
  updatedAt: string
  locale: string
  readMinutes: number
  headings: PageHeading[]
  // Already-processed page HTML (faqify + heading anchors), rendered with
  // the shared .blog-content typography from styles.css.
  html: string
}

// Table of contents only pays off on longer pages (policies, terms…).
const MIN_HEADINGS_FOR_TOC = 3

// Layout for every admin-created CMS page on /[slug] (Privacy, Terms, FAQ,
// Shipping/Return policies, About-style pages…). Same visual language as the
// blog: gradient hero, content card overlapping it, theme tokens only.
export default async function CmsPageLayout({ title, updatedAt, locale, readMinutes, headings, html }: CmsPageLayoutProps) {
  const t = await getTranslations('CmsPage')
  const showToc = headings.length >= MIN_HEADINGS_FOR_TOC
  const updated = new Date(updatedAt).toLocaleDateString(locale, { year: 'numeric', month: 'long', day: 'numeric' })

  return (
    <div id="top">
      {/* Hero */}
      <section className="relative overflow-hidden rounded-2xl">
        <div className="absolute inset-0 bg-gradient-primary opacity-90" />
        <div className="absolute -top-16 -end-16 h-72 w-72 rounded-full bg-white/10 blur-3xl" />
        <div className="absolute -bottom-24 -start-10 h-80 w-80 rounded-full bg-white/5 blur-3xl" />
        <div className="relative z-10 px-6 pt-10 pb-28 md:px-10 md:pt-14 md:pb-32">
          <div className="mx-auto max-w-4xl text-center">
            <nav aria-label="Breadcrumb" className="mb-6">
              <ol className="flex flex-wrap items-center justify-center gap-1.5 text-sm text-white/70">
                <li>
                  <Link href="/" className="inline-flex items-center gap-1 transition hover:text-white">
                    <Home size={14} /> <span>{t('home')}</span>
                  </Link>
                </li>
                <li aria-hidden="true"><ChevronRight size={14} className="text-white/40 rtl:rotate-180" /></li>
                <li className="max-w-xs truncate font-medium text-white" aria-current="page">{title}</li>
              </ol>
            </nav>
            <h1 className="mb-5 text-3xl font-bold leading-tight text-white md:text-4xl lg:text-5xl">{title}</h1>
            <div className="flex flex-wrap items-center justify-center gap-x-5 gap-y-2 text-sm text-white/85">
              <span className="inline-flex items-center gap-1.5">
                <CalendarDays size={15} />
                <time dateTime={updatedAt}>{t('updated', { date: updated })}</time>
              </span>
              <span className="h-1 w-1 rounded-full bg-white/50" aria-hidden />
              <span className="inline-flex items-center gap-1.5">
                <Clock size={15} /> {t('readTime', { minutes: readMinutes })}
              </span>
            </div>
          </div>
        </div>
      </section>

      <Container className="pb-16">
        <div
          className={`relative -mt-16 md:-mt-20 grid gap-8 ${
            showToc ? 'lg:grid-cols-[minmax(0,1fr)_280px]' : 'mx-auto max-w-4xl'
          }`}
        >
          <div className="min-w-0">
            {showToc && <CmsTableOfContents headings={headings} variant="inline" />}

            <article className="rounded-2xl border border-gray-100 bg-white p-6 shadow-xl shadow-black/5 md:p-10">
              <div className="blog-content" dangerouslySetInnerHTML={{ __html: html }} />
            </article>

            {/* Help */}
            <div className="mt-8 flex flex-col items-center justify-between gap-5 rounded-2xl border border-gray-100 bg-brand-wash p-6 text-center sm:flex-row sm:text-start md:p-8">
              <div className="flex flex-col items-center gap-4 sm:flex-row">
                <span className="flex h-12 w-12 shrink-0 items-center justify-center rounded-full bg-primary/10 text-primary">
                  <MessageCircle size={22} />
                </span>
                <div>
                  <h2 className="text-lg font-bold text-dark">{t('needHelpTitle')}</h2>
                  <p className="text-sm text-gray-custom">{t('needHelpDescription')}</p>
                </div>
              </div>
              <Link
                href="/contact"
                className="inline-flex shrink-0 items-center gap-2 rounded-lg bg-gradient-primary px-6 py-3 font-medium text-white shadow-md transition hover:shadow-lg"
              >
                {t('contactUs')}
              </Link>
            </div>

            <div className="mt-6 flex flex-wrap items-center justify-between gap-3 text-sm">
              <Link href="/" className="inline-flex items-center gap-1.5 font-medium text-gray-custom transition hover:text-primary">
                <ArrowLeft size={16} className="rtl:rotate-180" /> {t('backToHome')}
              </Link>
              <a href="#top" className="inline-flex items-center gap-1.5 font-medium text-gray-custom transition hover:text-primary">
                <ArrowUp size={16} /> {t('backToTop')}
              </a>
            </div>
          </div>

          {showToc && (
            <aside className="hidden lg:block">
              <div className="sticky top-28">
                <CmsTableOfContents headings={headings} variant="sidebar" />
              </div>
            </aside>
          )}
        </div>
      </Container>
    </div>
  )
}
