import type { Metadata } from "next";
import { Suspense } from "react";
import { notFound } from "next/navigation";
import { getDictionary, getLocale, type Dictionary } from "@/i18n/get-dictionary";
import { projects } from "@/content/catalog";
import { fill } from "@/lib/format";
import { Footer } from "@/components/layout/footer/footer";
import { Header } from "@/components/layout/header/header";
import { PageContentPlaceholder } from "@/components/shared/page-content-placeholder";
import { DetailsHeroSection } from "@/features/details/hero/details-hero-section";

const findProject = (slug: string) => projects.find((project) => project.slug === slug);

export function generateStaticParams() {
  return projects.map((project) => ({ slug: project.slug }));
}

export async function generateMetadata({ params }: PageProps<"/[lang]/projects/[slug]">): Promise<Metadata> {
  const [{ slug }, dict] = await Promise.all([params, getDictionary()]);
  const project = findProject(slug);
  return { title: `${project ? dict.catalog.projects[project.id].details.title : dict.notFound.metaTitle} | ${dict.brand.name}` };
}

/**
 * Project details (Figma "Cars", "Media platform", "halqh", "Ai", "Aqarna", "MEDF", "Comparison").
 * The page frame (header, footer) renders right away; everything that depends on the link (`params`) waits inside
 * <Suspense>, as Next.js asks for instant navigation. Links here use `prefetch`, so it is usually ready before the click.
 */
export default async function ProjectPage({ params }: PageProps<"/[lang]/projects/[slug]">) {
  const [locale, dict] = await Promise.all([getLocale(), getDictionary()]);

  return (
    <>
      <Header locale={locale} nav={dict.nav} activeKey="projects" />
      <main>
        <Suspense fallback={<PageContentPlaceholder label={dict.nav.loading} />}>
          <ProjectDetails params={params} dict={dict} />
        </Suspense>
      </main>
      {/* Figma: the footer follows the 880px hero directly. */}
      <Footer locale={locale} t={dict.footer} brandName={dict.brand.name} className="mt-0 md:mt-0 lg:mt-0" />
    </>
  );
}

async function ProjectDetails({ params, dict }: { params: PageProps<"/[lang]/projects/[slug]">["params"]; dict: Dictionary }) {
  const { slug } = await params;
  const project = findProject(slug);
  if (!project) notFound();
  const text = dict.catalog.projects[project.id].details;

  return (
    <DetailsHeroSection
      {...text}
      featuresLabel={dict.details.features}
      accent={project.details.accent}
      background={project.details.background}
      mockup={project.details.mockup}
      mockupAlt={fill(dict.details.mockupAlt, { name: text.title })}
    />
  );
}
