import type { Metadata } from "next";
import { Suspense } from "react";
import { notFound } from "next/navigation";
import { getDictionary, getLocale, type Dictionary } from "@/i18n/get-dictionary";
import { findJob, jobParams } from "@/content/careers";
import { fill } from "@/lib/format";
import type { Locale } from "@/i18n/config";
import { routes } from "@/lib/routes";
import { Footer } from "@/components/layout/footer/footer";
import { Header } from "@/components/layout/header/header";
import { PageContentPlaceholder } from "@/components/shared/page-content-placeholder";
import { JobBodySection } from "@/features/career/details/job-body-section";
import { JobHeroSection } from "@/features/career/details/job-hero-section";

export function generateStaticParams() {
  return jobParams();
}

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

/**
 * Job details (Figma "Career" job page).
 * 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 JobPage({ params }: PageProps<"/[lang]/career/[jobId]">) {
  const [locale, dict] = await Promise.all([getLocale(), getDictionary()]);

  return (
    <>
      <Header locale={locale} nav={dict.nav} activeKey="career" />
      <main>
        <Suspense fallback={<PageContentPlaceholder label={dict.nav.loading} />}>
          <JobDetails params={params} locale={locale} dict={dict} />
        </Suspense>
      </main>
      {/* Figma: 25px between the apply card and the footer (desktop). */}
      <Footer locale={locale} t={dict.footer} brandName={dict.brand.name} className="lg:mt-6" />
    </>
  );
}

type JobDetailsProps = { params: PageProps<"/[lang]/career/[jobId]">["params"]; locale: Locale; dict: Dictionary };

async function JobDetails({ params, locale, dict }: JobDetailsProps) {
  const { jobId } = await params;
  const job = findJob(jobId);
  if (!job) notFound();
  const t = dict.career;
  const text = dict.catalog.jobs[job.id];

  return (
    <>
      <JobHeroSection
        title={text.title}
        description={t.hero.description}
        location={t.fullLocations[job.location]}
        type={t.types[job.type]}
        department={text.department}
        applyLabel={t.details.apply}
        applyHref={routes.applyForJob(locale, job.slug)}
        image={job.image}
        imageAlt={fill(t.details.imageAlt, { job: text.title })}
        backgroundAlt={t.hero.imageAlt}
      />
      <JobBodySection locale={locale} job={job} text={text} t={t} />
    </>
  );
}
