import Link from "next/link"

import NewsStrip from "@/components/home/NewsStrip"
import SubscribeForm from "@/components/newsletter/SubscribeForm"
import { getAllReports } from "@/lib/mdx"

const regions = [
  {
    name: "Global Politics",
    href: "/directory?region=Global+Politics",
  },
  { name: "World Economy", href: "/directory?region=World+Economy" },
  {
    name: "Tech & Military",
    href: "/directory?region=Tech+%26+Military",
  },
  { name: "Energy", href: "/directory?region=Energy" },
  { name: "Middle East", href: "/directory?region=Middle+East" },
  { name: "Europe", href: "/directory?region=Europe" },
  { name: "Asia", href: "/directory?region=Asia" },
  {
    name: "Africa & Americas",
    href: "/directory?region=Africa+%26+Americas",
  },
]

function formatDate(date: string) {
  return new Date(date).toLocaleDateString("en-GB", {
    day: "2-digit",
    month: "short",
    year: "numeric",
  })
}

export default function Home() {
  const reports = getAllReports().slice(0, 6)

  return (
    <>
      <section
        className="relative flex min-h-[80vh] w-full items-center justify-center bg-[var(--color-bg)] bg-cover bg-center px-6 text-center"
        style={{
          backgroundImage:
            "linear-gradient(rgba(10, 15, 30, 0.55), rgba(10, 15, 30, 0.55)), url('https://images.unsplash.com/photo-1451187580459-43490279c0fa?w=1600')",
        }}
      >
        <div className="relative z-10 mx-auto max-w-3xl">
          <p className="text-[11px] font-semibold uppercase tracking-[0.25em] text-[var(--color-accent)]">
            GEOPOLITICAL INTELLIGENCE
          </p>
          <h1 className="mt-5 font-serif text-[clamp(2.5rem,6vw,4.5rem)] font-normal leading-[1.15] text-white">
            The world explained.
          </h1>
          <p className="mx-auto mt-5 max-w-[520px] text-lg leading-8 text-[var(--color-text-secondary)]">
            In-depth reports on geopolitics, economics, and global power.
          </p>
          <div className="mt-8 flex flex-col items-center justify-center gap-3 sm:flex-row">
            <Link
              href="/directory"
              className="rounded bg-[var(--color-accent)] px-6 py-3 text-[15px] font-medium text-white transition hover:bg-[var(--color-accent-hover)]"
            >
              Explore Reports
            </Link>
            <Link
              href="/about"
              className="rounded border border-[var(--color-border)] px-6 py-3 text-[15px] font-medium text-[var(--color-text-primary)] transition hover:border-[var(--color-accent)]"
            >
              What is Parallax?
            </Link>
          </div>
        </div>
      </section>

      <section className="bg-[var(--color-bg)] px-6 py-20">
        <div className="mx-auto max-w-[1200px]">
          <p className="text-[11px] font-semibold uppercase tracking-widest text-[var(--color-accent)]">
            LATEST REPORTS
          </p>
          <h2 className="mt-3 font-serif text-3xl text-[var(--color-text-primary)]">
            Recent Analysis
          </h2>

          <div className="mt-8 grid gap-6 md:grid-cols-2 lg:grid-cols-3">
            {reports.map((report) => (
              <Link
                key={report.slug}
                href={`/${report.slug}`}
                className="overflow-hidden rounded-lg border border-[var(--color-border)] bg-[var(--color-surface)] transition duration-200 hover:scale-[1.01] hover:border-[var(--color-accent)]"
              >
                {report.coverImage ? (
                  <img
                    src={report.coverImage}
                    alt={report.title}
                    className="aspect-video w-full object-cover"
                  />
                ) : (
                  <div className="aspect-video w-full bg-[var(--color-border)]" />
                )}

                <div className="p-4">
                  <div className="flex flex-wrap gap-2">
                    <span className="rounded bg-[var(--color-tag-bg)] px-2 py-0.5 text-[11px] uppercase tracking-widest text-[var(--color-accent)]">
                      {report.region}
                    </span>
                    {report.isPro ? (
                      <span className="rounded bg-[#7c3aed] px-2 py-0.5 text-[11px] uppercase tracking-widest text-white">
                        PRO
                      </span>
                    ) : null}
                  </div>

                  <h3 className="mt-2 line-clamp-2 font-serif text-[17px] leading-[1.4] text-[var(--color-text-primary)]">
                    {report.title}
                  </h3>
                  <p className="mt-1 line-clamp-2 text-[13px] text-[var(--color-text-secondary)]">
                    {report.subtitle}
                  </p>

                  <div className="mt-3 flex items-center justify-between text-xs text-[var(--color-text-secondary)]">
                    <span>{formatDate(report.publishedAt)}</span>
                    <span>{report.readTime} min read</span>
                  </div>
                </div>
              </Link>
            ))}
          </div>
        </div>
      </section>

      <NewsStrip />

      <section className="bg-[var(--color-surface)] px-6 py-20">
        <div className="mx-auto max-w-[1200px]">
          <p className="text-[11px] font-semibold uppercase tracking-widest text-[var(--color-accent)]">
            EXPLORE
          </p>
          <h2 className="mt-3 font-serif text-3xl text-[var(--color-text-primary)]">
            Browse by Region
          </h2>

          <div className="mt-8 grid gap-4 md:grid-cols-2 lg:grid-cols-4">
            {regions.map((region) => (
              <Link
                key={region.name}
                href={region.href}
                className="rounded-lg border border-[var(--color-border)] bg-[var(--color-bg)] p-5 transition-all duration-200 hover:border-[var(--color-accent)] hover:bg-[var(--color-surface)]"
              >
                <div className="flex items-center justify-between gap-4">
                  <h3 className="font-serif text-[17px] text-[var(--color-text-primary)]">
                    {region.name}
                  </h3>
                  <span className="text-[var(--color-accent)]">&rarr;</span>
                </div>
              </Link>
            ))}
          </div>
        </div>
      </section>

      <section className="bg-[var(--color-bg)] px-6 py-20">
        <div className="mx-auto max-w-[640px] text-center">
          <p className="text-[11px] font-semibold uppercase tracking-widest text-[var(--color-accent)]">
            STAY INFORMED
          </p>
          <h2 className="mt-3 font-serif text-3xl text-white">
            Weekly geopolitical analysis.
          </h2>
          <p className="mt-2 text-base leading-7 text-[var(--color-text-secondary)]">
            No noise. No opinion. Just the facts and the forces behind them.
          </p>

          <div className="mx-auto mt-6 max-w-[420px]">
            <SubscribeForm variant="inline" />
          </div>
          <p className="mt-2 text-xs text-[var(--color-text-secondary)]">
            No spam. Unsubscribe anytime.
          </p>
        </div>
      </section>
    </>
  )
}
