import type { Metadata } from "next";
import { notFound } from "next/navigation";
import { getChain, DEFAULT_CHAIN } from "@/lib/chains";
import { SitePage } from "../site-page";
import { copy, isLanguage, languagePath, languages, type Language } from "@/lib/i18n";

import { SITE_URL as siteUrl, SOCIAL_IMAGE } from "@/lib/site-config";

export function generateStaticParams() {
  return languages.filter((language) => language.code !== "en").map((language) => ({ lang: language.code }));
}

export async function generateMetadata({ params }: { params: Promise<{ lang: string }> }): Promise<Metadata> {
  const { lang } = await params;
  if (!isLanguage(lang)) return {};
  const t = copy[lang];
  const canonical = languagePath(lang);
  return {
    title: t.title,
    description: t.description,
    alternates: {
      canonical,
      languages: { en: "/", "zh-CN": "/zh", es: "/es", ja: "/ja", ko: "/ko", fr: "/fr", "pt-BR": "/pt", de: "/de" },
    },
    openGraph: { images: [SOCIAL_IMAGE], title: t.title, description: t.description, url: `${siteUrl}${canonical}`, locale: ({ zh: "zh_CN", es: "es_ES", ja: "ja_JP", ko: "ko_KR", fr: "fr_FR", pt: "pt_BR", de: "de_DE", en: "en_US" })[lang] },
    twitter: { card: "summary_large_image", images: [SOCIAL_IMAGE.url], title: t.title, description: t.description },
  };
}

export default async function LocalizedPage({ params, searchParams }: { params: Promise<{ lang: string }>; searchParams: Promise<{ chain?: string }> }) {
  const { lang } = await params;
  if (!isLanguage(lang) || lang === "en") notFound();
  const query = await searchParams;
  return <SitePage language={lang as Language} chain={getChain(query.chain ?? "")?.id ?? DEFAULT_CHAIN.id} />;
}
