import type { Metadata } from "next";
import { notFound } from "next/navigation";
import { ServicePage } from "@/components/ServicePage";
import { brand, isLocale, locales, type Locale } from "@/content/site";
import { getServiceBySlug, serviceKeys, servicePath, services } from "@/content/services";

export function generateStaticParams() {
  return locales.flatMap((locale) =>
    serviceKeys.map((service) => ({ locale, service: services[service].slugs[locale] })),
  );
}

export async function generateMetadata({
  params,
}: {
  params: Promise<{ locale: string; service: string }>;
}): Promise<Metadata> {
  const values = await params;
  if (!isLocale(values.locale)) return {};
  const locale = values.locale;
  const match = getServiceBySlug(locale, decodeURIComponent(values.service));
  if (!match) return {};
  const copy = match.copy;
  const path = servicePath(locale, match.key);
  return {
    title: copy.metaTitle,
    description: copy.metaDescription,
    alternates: {
      canonical: path,
      languages: {
        fr: servicePath("fr", match.key),
        en: servicePath("en", match.key),
        ar: servicePath("ar", match.key),
        "x-default": servicePath("fr", match.key),
      },
    },
    openGraph: {
      title: copy.metaTitle,
      description: copy.metaDescription,
      url: path,
      type: "website",
      siteName: brand.name,
      images: [{ url: "/social/yashal-og.png", width: 1200, height: 630 }],
    },
    twitter: {
      card: "summary_large_image",
      title: copy.metaTitle,
      description: copy.metaDescription,
      images: ["/social/yashal-og.png"],
    },
  };
}

export default async function LocalizedServicePage({
  params,
}: {
  params: Promise<{ locale: string; service: string }>;
}) {
  const values = await params;
  if (!isLocale(values.locale)) notFound();
  const locale = values.locale as Locale;
  const match = getServiceBySlug(locale, decodeURIComponent(values.service));
  if (!match) notFound();
  return <ServicePage locale={locale} serviceKey={match.key} />;
}
