import type { Metadata } from "next";
import {
  ArrowRight,
  BadgeCheck,
  Blocks,
  BriefcaseBusiness,
  ChartNoAxesCombined,
  Globe,
  Package2,
  Search,
} from "lucide-react";
import Link from "next/link";
import { getTranslations } from "next-intl/server";

import { Container } from "@/components/layout/container";
import { HomeHero } from "@/components/layout/home-hero";
import { StackedMarqueeSection, type MarqueeRow } from "@/components/layout/stacked-marquee-section";
import { MotionFade } from "@/components/motion-fade";
import { getMarqueeSettings, getSiteSettings, splitMarqueeRowItems } from "@/lib/app-config";
import { buildLocalizedMetadata } from "@/lib/metadata";
import { AppCard } from "@/components/ui/app-card";
import { Button } from "@/components/ui/button";
import { CardContent } from "@/components/ui/card";
import { getLocalizedPath, resolveLocale } from "@/lib/locale";

type HomePageProps = {
  params: Promise<{
    locale: string;
  }>;
};

const serviceIcons = [
  BriefcaseBusiness,
  Globe,
  Package2,
  Blocks,
  Search,
  ChartNoAxesCombined,
  BadgeCheck,
];

export const dynamic = "force-dynamic";

export async function generateMetadata({ params }: HomePageProps): Promise<Metadata> {
  const { locale } = await params;
  const localeKey = resolveLocale(locale);
  const [t, siteSettings] = await Promise.all([
    getTranslations({ locale: localeKey, namespace: "homepage" }),
    getSiteSettings(),
  ]);

  return await buildLocalizedMetadata({
    locale: localeKey,
    pathname: "/",
    title: siteSettings.locales[localeKey].siteName,
    description: t("heroText"),
    applyTitleTemplate: false,
  });
}

export default async function HomePage({ params }: HomePageProps) {
  const { locale } = await params;
  const localeKey = resolveLocale(locale);
  const [t, marqueeSettings] = await Promise.all([
    getTranslations({ locale: localeKey, namespace: "homepage" }),
    getMarqueeSettings(),
  ]);
  const serviceItems = [
    t("serviceBrand"),
    t("serviceWebsite"),
    t("serviceStore"),
    t("serviceWordPress"),
    t("serviceSeo"),
    t("serviceTracking"),
    t("serviceSystem"),
  ];
  const marqueeLocale = marqueeSettings.locales[localeKey];
  const marqueeRows: MarqueeRow[] = [
    { direction: "left", duration: 126, items: splitMarqueeRowItems(marqueeLocale.row1) },
    { direction: "right", duration: 220, items: splitMarqueeRowItems(marqueeLocale.row2) },
    { direction: "left", duration: 168, items: splitMarqueeRowItems(marqueeLocale.row3) },
    { direction: "right", duration: 144, items: splitMarqueeRowItems(marqueeLocale.row4) },
  ];

  return (
    <>
      <HomeHero
        locale={localeKey}
        kicker={t("heroKicker")}
        title={t("heroTitle")}
        description={t("heroText")}
      />

      <Container id="home-content" className="relative z-10 -mt-6 flex scroll-mt-28 flex-col gap-section pb-12 lg:-mt-8 lg:pb-16">
        <MotionFade delay={0.05}>
          <AppCard className="overflow-hidden">
            <CardContent className="p-6 lg:p-10">
              <div className="grid gap-8 lg:grid-cols-[minmax(0,1.05fr)_minmax(0,1fr)] lg:items-start">
                <div>
                  <p className="text-sm font-medium tracking-[0.08em] text-foreground/56">
                    {t("servicesEyebrow")}
                  </p>
                  <h2 className="mt-4 max-w-[12ch] text-balance text-4xl font-semibold leading-[0.92] tracking-[-0.06em] text-foreground sm:text-5xl lg:text-6xl">
                    {t("servicesTitle")}
                  </h2>
                  <p className="mt-5 max-w-[34rem] text-sm leading-7 text-muted-foreground sm:text-base">
                    {t("servicesText")}
                  </p>
                  <div className="mt-8 flex flex-wrap gap-3">
                    <Button asChild size="lg" className="min-w-[11rem]">
                      <Link href={getLocalizedPath(localeKey, "/contact")}>
                        {t("servicesPrimaryCta")}
                        <ArrowRight className="h-4 w-4" />
                      </Link>
                    </Button>
                    <Button
                      asChild
                      size="lg"
                      variant="outline"
                      className="min-w-[11rem] border-border/80 bg-background/70 hover:bg-background"
                    >
                      <Link href={getLocalizedPath(localeKey, "/portfolio")}>
                        {t("servicesSecondaryCta")}
                      </Link>
                    </Button>
                  </div>
                </div>

                <div className="grid gap-3 sm:grid-cols-2">
                  {serviceItems.map((item, index) => {
                    const Icon = serviceIcons[index];

                    return (
                      <AppCard
                        key={item}
                        layer="single"
                        interactive
                        className="h-full"
                      >
                        <div className="flex h-10 w-10 items-center justify-center rounded-full bg-[linear-gradient(135deg,hsl(var(--hero-left)/0.42),hsl(var(--hero-right)/0.26))]">
                          <Icon className="h-5 w-5 text-foreground/78" />
                        </div>
                        <p className="mt-4 text-sm font-medium leading-6 text-foreground/88 sm:text-base">
                          {item}
                        </p>
                      </AppCard>
                    );
                  })}
                </div>
              </div>
            </CardContent>
          </AppCard>
        </MotionFade>
      </Container>

      <StackedMarqueeSection rows={marqueeRows} className="relative z-10 mt-2 pb-10 sm:pb-12 lg:pb-14" />
    </>
  );
}
