import { notFound } from "next/navigation";
import { getDictionary } from "@/get-dictionary";
import { i18n, type Locale } from "@/i18n-config";
import PrivacyDetailClient from "@/components/PrivacyDetailClient";
import { getAllPrivacyApps, getPrivacyAppBySlug } from "@/data/privacy";

export async function generateStaticParams() {
  const paths: Array<{ lang: Locale; slug: string }> = [];
  const apps = getAllPrivacyApps();

  for (const lang of i18n.locales) {
    for (const app of apps) {
      paths.push({ lang, slug: app.slug });
    }
  }

  return paths;
}

export async function generateMetadata({
  params,
}: {
  params: Promise<{ lang: Locale; slug: string }>;
}) {
  const { lang, slug } = await params;
  const app = getPrivacyAppBySlug(slug);

  if (!app) {
    return {
      title: lang === "tr" ? "Gizlilik Politikası Bulunamadı" : "Privacy Policy Not Found",
    };
  }

  const isTr = lang === "tr";

  return {
    title: isTr
      ? `${app.name} Gizlilik Politikası & Veri Güvenliği | Ayris Tech`
      : `${app.name} Privacy Policy & Data Security | Ayris Tech`,
    description: app.summary[lang],
    alternates: {
      canonical: `/${lang}/privacy/${app.slug}`,
    },
  };
}

export default async function PrivacyDetailPage({
  params,
}: {
  params: Promise<{ lang: Locale; slug: string }>;
}) {
  const { lang, slug } = await params;
  const dict = await getDictionary(lang);
  const app = getPrivacyAppBySlug(slug);

  if (!app) {
    notFound();
  }

  const allApps = getAllPrivacyApps();
  const otherApps = allApps.filter((a) => a.slug !== app.slug);

  return (
    <PrivacyDetailClient
      lang={lang}
      dict={dict}
      app={app}
      otherApps={otherApps}
    />
  );
}
