import Head from "next/head";
import Image from "next/image";
import Link from "next/link";
import { useRouter } from "next/router";
import { useTranslation } from "next-i18next";
import { serverSideTranslations } from "next-i18next/serverSideTranslations";
import type { GetServerSideProps } from "next";
import i18nConfig from "../../../next-i18next.config";
import {
  Calendar,
  MessageCircle,
  Gift,
  Lock,
  Mail,
  Facebook,
  ArrowRight,
} from "lucide-react";
import { MessageTextIcon } from "@/components/expat/icons";
import FxCalculatorCard from "@/components/calculator";
import { type ExpatStory, type UpcomingEvent } from "@/lib/expatContent";
import { getUpcomingEvents, getExpatStories } from "@/lib/webflowEvents";
import { gmailComposeUrl } from "@/lib/gmail";

// yyyy-mm-dd -> yyyy.mm.dd
const fmtDate = (iso: string) => iso.replaceAll("-", ".");

type Props = { upcomingEvents: UpcomingEvent[]; expatStories: ExpatStory[] };

export default function ExpatPage({ upcomingEvents, expatStories }: Props) {
  const router = useRouter();
  const { t, i18n } = useTranslation("expat");
  const lang = i18n.language || router.locale || "en";
  const isEn = lang === "en";

  const nextEvent = upcomingEvents[0];
  const latestStory = expatStories[0];

  return (
    <div className="relative min-h-screen overflow-x-hidden bg-white">
      <Head>
        <title>{t("index.meta.title")}</title>
        <meta name="description" content={t("index.meta.description")} />
      </Head>

      {/* Hero */}
      <section className="overflow-hidden bg-gradient-to-br from-[#051973] via-[#0b3e98] to-[#00B6FF] px-4 py-36 sm:px-6 lg:px-8">
        <div
          aria-hidden
          className="pointer-events-none relative rounded-full bg-white/10 blur-3xl"
        />
        <div className="relative mx-auto grid max-w-6xl grid-cols-1 items-center gap-10 lg:grid-cols-[1.2fr_1fr]">
          <div>
            <span className="inline-block rounded-full border border-white/30 bg-white/10 px-3 py-1 text-xs font-semibold text-white backdrop-blur">
              {t("index.hero.badge")}
            </span>
            <h1 className="mt-4 text-3xl font-extrabold leading-tight text-white sm:text-4xl md:text-5xl">
              {t("index.hero.title")}
            </h1>
            <p className="mt-3 text-xs font-semibold tracking-[0.2em] text-white/70 sm:text-sm">
              {t("index.hero.tagline")}
            </p>
          </div>

          <div className="rounded-2xl border border-white/20 bg-white/10 p-6 text-center backdrop-blur-md sm:p-12">
            <p className="text-4xl font-extrabold text-white sm:text-5xl">
              7,000<span className="text-[#19D2FF]">+</span>
            </p>
            <p className="mt-2 text-sm text-white/80 sm:text-base">
              {t("index.hero.statLabel")}
            </p>
            {/* <p className="mt-1 text-[11px] text-white/50">
              {t("index.hero.asOf")} {asOf ?? "—"}
            </p> */}
          </div>
        </div>
      </section>

      {/* Explore */}
      <section className="bg-[#f4faff]">
        <div className="mx-auto max-w-7xl px-4 py-16 sm:px-6 lg:px-8">
          <div className="text-center">
            <span className="inline-block rounded-full border border-border bg-[#f0fcfc] px-3 py-1 text-xs font-bold text-main">
              {t("index.explore.badge")}
            </span>
            <h2 className="mt-3 text-2xl font-extrabold text-black sm:text-4xl">
              {t("index.explore.title")}
            </h2>
            <p className="mx-auto mt-3 max-w-2xl text-sm text-gray-600 sm:text-base">
              {t("index.explore.subtitle")}
            </p>
          </div>

          <div className="mt-10 grid grid-cols-1 gap-6 md:grid-cols-3 md:items-stretch">
            {/* Events & Community */}
            <div className="flex flex-col rounded-2xl border-t-2 border-[#19D2FF] bg-white shadow-sm overflow-hidden">
              <div className="flex flex-1 flex-col p-6">
                <div className="grid h-11 w-11 place-items-center rounded-xl bg-sky-50 text-sky-600">
                  <Calendar className="h-6 w-6" />
                </div>
                <h3 className="mt-4 min-h-[56px] text-lg font-bold text-main">
                  {t("index.explore.events.title")}
                </h3>
                <p className="mt-2 min-h-[92px] text-sm text-gray-600">
                  {t("index.explore.events.desc")}
                </p>

                <div className="mt-4 border-t border-border pt-4 mb-4">
                  <Link
                    href="/expat/events"
                    className="relative block min-h-[152px] rounded-xl border border-sky-100 bg-sky-50 p-3 text-left transition hover:border-sky-300"
                  >
                    <span className="inline-block rounded-full bg-white px-2 py-0.5 text-[10px] font-semibold text-sky-600">
                      {t("index.explore.events.badge")}
                    </span>
                    <span className="float-right text-[11px] text-gray-400">
                      {fmtDate(nextEvent.date)}
                    </span>
                    <p className="mt-2 line-clamp-2 text-sm font-semibold text-[#051973] clear-both">
                      {isEn ? nextEvent.title_en : nextEvent.title_mn}
                    </p>
                    <span className="absolute bottom-3 left-3 inline-flex items-center gap-1 text-xs font-medium text-sky-600">
                      {t("index.explore.events.details")}
                      <ArrowRight className="h-3.5 w-3.5" />
                    </span>
                  </Link>
                </div>

                <Link
                  href="/expat/events"
                  className="mt-auto inline-flex items-center justify-center gap-2 rounded-xl bg-[#051973] px-4 py-2.5 text-sm font-semibold text-white transition hover:brightness-110"
                >
                  {t("index.explore.events.cta")}
                  <ArrowRight className="h-4 w-4" />
                </Link>
              </div>
            </div>

            {/* Expat Stories */}
            <div className="flex flex-col rounded-2xl border-t-2 border-[#FF8C42] bg-white shadow-sm overflow-hidden">
              <div className="flex flex-1 flex-col p-6">
                <div className="grid h-11 w-11 place-items-center rounded-xl bg-orange-50 text-orange-500">
                  <MessageTextIcon className="h-6 w-6" />
                </div>
                <h3 className="mt-4 min-h-[56px] text-lg font-bold text-main">
                  {t("index.explore.stories.title")}
                </h3>
                <p className="mt-2 min-h-[92px] text-sm text-gray-600">
                  {t("index.explore.stories.desc")}
                </p>

                <div className="mt-4 border-t border-border pt-4 mb-4">
                  <Link
                    href={`/expat/stories/${latestStory.slug}`}
                    className="relative block min-h-[152px] rounded-xl border border-orange-100 bg-orange-50 p-3 text-left transition hover:border-orange-300"
                  >
                    <span className="inline-block rounded-full bg-white px-2 py-0.5 text-[10px] font-semibold text-orange-500">
                      {t("index.explore.stories.badge")}
                    </span>
                    <span className="float-right text-[11px] text-gray-400">
                      {fmtDate(latestStory.date)}
                    </span>
                    <p className="mt-2 line-clamp-2 text-sm font-semibold text-[#051973] clear-both">
                      {isEn ? latestStory.title_en : latestStory.title_mn}
                    </p>
                    <span className="absolute bottom-3 left-3 inline-flex items-center gap-1 text-xs font-medium text-orange-500">
                      {t("index.explore.stories.details")}
                      <ArrowRight className="h-3.5 w-3.5" />
                    </span>
                  </Link>
                </div>

                <Link
                  href="/expat/stories"
                  className="mt-auto inline-flex items-center justify-center gap-2 rounded-xl bg-orange-500 px-4 py-2.5 text-sm font-semibold text-white transition hover:brightness-110"
                >
                  {t("index.explore.stories.cta")}
                  <ArrowRight className="h-4 w-4" />
                </Link>
              </div>
            </div>

            {/* Expat Benefits */}
            <div className="flex flex-col rounded-2xl border-t-2 border-[#7C6CF0] bg-white shadow-sm overflow-hidden">
              <div className="flex flex-1 flex-col p-6">
                <div className="grid h-11 w-11 place-items-center rounded-xl bg-violet-50 text-violet-500">
                  <Gift className="h-6 w-6" />
                </div>
                <h3 className="mt-4 min-h-[56px] text-lg font-bold text-main">
                  {t("index.explore.benefits.title")}
                </h3>
                <p className="mt-2 min-h-[92px] text-sm text-gray-600">
                  {t("index.explore.benefits.desc")}
                </p>

                <div className="mt-4 border-t border-border pt-4 mb-4">
                  <div className="flex min-h-[152px] items-center justify-center gap-2 rounded-xl border border-violet-100 bg-violet-50 px-4 py-2.5 text-sm font-medium text-violet-400">
                    <Lock className="h-3.5 w-3.5" />
                    {t("index.explore.benefits.comingSoon")}
                  </div>
                </div>

                <div className="mt-auto flex items-center justify-center gap-2 rounded-xl bg-violet-50 px-4 py-2.5 text-sm font-medium text-violet-400">
                  <Lock className="h-3.5 w-3.5" />
                  {t("index.explore.benefits.comingSoon")}
                </div>
              </div>
            </div>
          </div>
        </div>
      </section>

      {/* Send Money Home */}
      <section className="bg-gradient-to-b from-[#f4faff] to-white">
        <div className="mx-auto grid max-w-6xl grid-cols-1 items-center gap-10 px-4 py-16 sm:px-6 lg:grid-cols-2 lg:px-8">
          <div>
            <h2 className="text-3xl font-extrabold leading-tight text-black sm:text-4xl">
              {t("index.sendMoney.title")}
            </h2>
            <p className="mt-3 max-w-md text-sm text-gray-600 sm:text-base">
              {t("index.sendMoney.desc")}
            </p>
            <Link
              href="/services/remit"
              className="mt-6 inline-flex items-center justify-center rounded-xl bg-[#19D2FF] px-5 py-2.5 text-sm font-semibold text-main shadow hover:brightness-95"
            >
              {t("index.sendMoney.cta")}
            </Link>
          </div>

          <div className="relative">
            <div className="pointer-events-none select-none block absolute -right-2 md:-right-6 lg:-right-0 bottom-0 lg:-top-20 opacity-70">
              <Image
                width={4000}
                height={4000}
                src="/images/Polygon.png"
                alt=""
                className="w-full h-auto"
                priority
              />
            </div>
            <div className="mx-auto md:mx-0 w-full max-w-sm sm:max-w-md">
              <FxCalculatorCard code="US" />
            </div>
          </div>
        </div>
      </section>

      {/* Contact Us */}
      <section className="bg-gradient-to-b from-[#f4faff] to-white pt-16">
        <div className="mx-auto max-w-5xl text-center">
          <h2 className="text-2xl font-extrabold text-black sm:text-4xl">
            {t("index.contact.title")}
          </h2>
          <p className="mx-auto mt-3 max-w-2xl text-sm text-gray-600 sm:text-base">
            {t("index.contact.subtitle")}
          </p>

          <div className="mt-10 grid grid-cols-1 gap-6 sm:grid-cols-3">
            <div className="flex h-full min-h-[250px] flex-col rounded-2xl border border-gray bg-white p-6">
              <div className="mx-auto grid h-11 w-11 place-items-center rounded-full bg-sky-50 text-sky-600">
                <MessageCircle className="h-5 w-5" />
              </div>
              <p className="mt-4 text-sm font-medium text-main">
                {t("index.contact.chatDesc")}
              </p>
              <Link
                href="https://www.facebook.com/SendMN.MoneyTransfer"
                target="_blank"
                className="mt-auto w-fit self-center inline-flex items-center justify-center rounded-xl bg-[#00D2FF] px-5 py-3 text-xs font-semibold text-[#051973] hover:brightness-95"
              >
                {t("index.contact.chatCta")}
              </Link>
            </div>

            <div className="flex h-full min-h-[250px] flex-col rounded-2xl border border-gray bg-white p-6">
              <div className="mx-auto grid h-11 w-11 place-items-center rounded-full bg-sky-50 text-sky-600">
                <Mail className="h-5 w-5" />
              </div>
              <p className="mt-4 text-sm font-medium text-main">
                {t("index.contact.emailDesc")}
              </p>
              <Link
                href={gmailComposeUrl({ to: "expat@send.mn" })}
                target="_blank"
                rel="noopener noreferrer"
                className="mt-auto w-fit self-center inline-flex items-center justify-center rounded-xl bg-[#00D2FF] px-5 py-3 text-xs font-semibold text-[#051973] hover:brightness-95"
              >
                {t("index.contact.emailCta")}
              </Link>
            </div>

            <div className="flex h-full min-h-[250px] flex-col rounded-2xl border border-gray bg-white p-6">
              <div className="mx-auto grid h-11 w-11 place-items-center rounded-full bg-sky-50 text-sky-600">
                <Facebook className="h-5 w-5" />
              </div>
              <p className="mt-4 text-sm font-medium text-main">
                {t("index.contact.fbDesc")}
              </p>
              <Link
                href="https://www.facebook.com/groups/2718122861788256"
                target="_blank"
                rel="noopener noreferrer"
                className="mt-auto w-fit self-center inline-flex items-center justify-center rounded-xl bg-[#00D2FF] px-5 py-3 text-xs font-semibold text-[#051973] hover:brightness-95"
              >
                {t("index.contact.fbCta")}
              </Link>
            </div>
          </div>
        </div>
      </section>
    </div>
  );
}

export const getServerSideProps: GetServerSideProps<Props> = async ({
  locale,
}) => {
  const [upcomingEvents, expatStories] = await Promise.all([
    getUpcomingEvents(),
    getExpatStories(),
  ]);

  return {
    props: {
      upcomingEvents,
      expatStories,
      ...(await serverSideTranslations(
        locale ?? "en",
        ["common", "expat"],
        i18nConfig,
      )),
    },
  };
};
