import type { Metadata } from "next";
import { ArrowUpRight, CalendarDays, FolderKanban } from "lucide-react";
import Link from "next/link";
import { getTranslations } from "next-intl/server";

import { Container } from "@/components/layout/container";
import { MotionFade } from "@/components/motion-fade";
import { buildLocalizedMetadata } from "@/lib/metadata";
import { AppCard } from "@/components/ui/app-card";
import { CardContent } from "@/components/ui/card";
import { getLocalizedPath, resolveLocale } from "@/lib/locale";
import { pickText, portfolioItems } from "@/lib/site-data";

type PortfolioPageProps = {
  params: {
    locale: string;
  };
};

export async function generateMetadata({
  params: { locale },
}: PortfolioPageProps): Promise<Metadata> {
  const localeKey = resolveLocale(locale);
  const t = await getTranslations({ locale: localeKey, namespace: "portfolioPage" });

  return buildLocalizedMetadata({
    locale: localeKey,
    pathname: "/portfolio",
    title: t("title"),
    description: t("intro"),
  });
}

export default async function PortfolioPage({ params: { locale } }: PortfolioPageProps) {
  const localeKey = resolveLocale(locale);
  const t = await getTranslations({ locale: localeKey, namespace: "portfolioPage" });

  return (
    <Container className="flex flex-col gap-section py-10 lg:py-14">
      <MotionFade>
        <AppCard level={3}>
          <CardContent className="p-6 lg:p-10">
            <div className="flex items-center gap-3">
              <FolderKanban className="h-5 w-5 text-brand-primary" />
              <h1 className="text-3xl font-semibold text-foreground sm:text-4xl">
                {t("title")}
              </h1>
            </div>
            <p className="mt-4 max-w-3xl text-base text-muted-foreground sm:text-lg">
              {t("intro")}
            </p>
          </CardContent>
        </AppCard>
      </MotionFade>

      <section className="grid gap-4 md:grid-cols-2">
        {portfolioItems.map((item, index) => (
          <MotionFade key={item.slug} delay={index * 0.05}>
            <AppCard interactive>
              <CardContent className="p-5">
                <Link
                  href={getLocalizedPath(localeKey, `/portfolio/${item.slug}`)}
                  className="group block"
                >
                  <div className="flex items-center justify-between gap-3">
                    <p className="text-sm text-muted-foreground/80">
                      {pickText(item.category, localeKey)}
                    </p>
                    <p className="inline-flex items-center gap-1 text-xs text-muted-foreground/80">
                      <CalendarDays className="h-3.5 w-3.5" />
                      {item.year}
                    </p>
                  </div>
                  <h2 className="mt-3 text-xl font-semibold text-foreground">
                    {pickText(item.title, localeKey)}
                  </h2>
                  <p className="mt-2 text-sm text-muted-foreground">
                    {pickText(item.summary, localeKey)}
                  </p>
                  <p className="mt-4 inline-flex items-center gap-2 text-sm font-medium text-foreground/80 group-hover:text-foreground">
                    {t("open")}
                    <ArrowUpRight className="h-4 w-4" />
                  </p>
                </Link>
              </CardContent>
            </AppCard>
          </MotionFade>
        ))}
      </section>
    </Container>
  );
}
