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 { MotionFade } from "@/components/motion-fade";
import { getSiteSettings } 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: {
    locale: string;
  };
};

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

export const dynamic = "force-dynamic";

export async function generateMetadata({
  params: { locale },
}: HomePageProps): Promise<Metadata> {
  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: { locale } }: HomePageProps) {
  const localeKey = resolveLocale(locale);
  const t = await getTranslations({ locale: localeKey, namespace: "homepage" });
  const serviceItems = [
    t("serviceBrand"),
    t("serviceWebsite"),
    t("serviceStore"),
    t("serviceWordPress"),
    t("serviceSeo"),
    t("serviceTracking"),
    t("serviceSystem"),
  ];

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

      <Container className="relative z-10 -mt-6 flex flex-col gap-section pb-12 lg:-mt-8 lg:pb-16">
        <MotionFade delay={0.05}>
          <AppCard
            level="inverse"
            className="overflow-hidden border border-white/6 bg-[linear-gradient(145deg,hsl(var(--foreground)),hsl(221_42%_10%))] text-primary-foreground"
          >
            <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-primary-foreground/62">
                    {t("servicesEyebrow")}
                  </p>
                  <h2 className="mt-4 max-w-[12ch] text-balance text-4xl font-semibold leading-[0.92] tracking-[-0.06em] text-primary-foreground sm:text-5xl lg:text-6xl">
                    {t("servicesTitle")}
                  </h2>
                  <p className="mt-5 max-w-[34rem] text-sm leading-7 text-primary-foreground/76 sm:text-base">
                    {t("servicesText")}
                  </p>
                  <div className="mt-7 flex flex-wrap gap-3">
                    <Button asChild size="lg" variant="secondary" 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-white/16 bg-white/6 text-primary-foreground hover:bg-white/10 hover:text-primary-foreground"
                    >
                      <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 (
                      <div
                        key={item}
                        className="rounded-nested border border-white/10 bg-white/6 p-4 shadow-[inset_0_1px_0_rgba(255,255,255,0.08)] backdrop-blur-sm transition-colors hover:bg-white/8"
                      >
                        <Icon className="h-5 w-5 text-primary-foreground/72" />
                        <p className="mt-4 text-sm font-medium leading-6 text-primary-foreground/88 sm:text-base">
                          {item}
                        </p>
                      </div>
                    );
                  })}
                </div>
              </div>
            </CardContent>
          </AppCard>
        </MotionFade>
      </Container>
    </>
  );
}
