﻿import type { Metadata } from "next";
import { notFound } from "next/navigation";
import { loadDictionary } from "@allai/i18n/server";
import { locales, resolveLocale } from "@/config/i18n";
import { FaqPage, getFaqContent } from "@/features/marketing/FaqPage";
import { buildCanonical, buildLocaleAlternates, buildOpenGraph, buildTwitterCard } from "@/seo/seoUtils";

const PATH = "/faq" as const;

type PageProps = {
  params: { locale: string };
};

export async function generateMetadata({ params }: PageProps): Promise<Metadata> {
  const locale = resolveLocale(params.locale);

  if (!locales.includes(locale)) {
    return {
      title: "FAQs"
    };
  }

  const dictionary = await loadDictionary(locale);
  const title = `FAQs | ${dictionary.common.brandLong ?? dictionary.common.brandShort}`;
  const description =
    "Answers to common questions about AllAI Studio models, collaboration features, export formats, and API access.";

  return {
    title,
    description,
    alternates: {
      canonical: buildCanonical(locale, PATH),
      languages: buildLocaleAlternates(PATH)
    },
    openGraph: buildOpenGraph({
      locale,
      title,
      description,
      path: PATH
    }),
    twitter: buildTwitterCard({
      title,
      description
    })
  };
}

export default async function FaqRoute({ params }: PageProps) {
  const locale = resolveLocale(params.locale);

  if (!locales.includes(locale)) {
    notFound();
  }

  const dictionary = await loadDictionary(locale);

  const structuredData = {
    "@context": "https://schema.org",
    "@type": "FAQPage",
    mainEntity: getFaqContent(locale).entries.map((item) => ({
      "@type": "Question",
      name: item.question,
      acceptedAnswer: {
        "@type": "Answer",
        text: item.answer
      }
    }))
  };

  return (
    <>
      <script
        type="application/ld+json"
        suppressHydrationWarning
        dangerouslySetInnerHTML={{ __html: JSON.stringify(structuredData) }}
      />
      <FaqPage locale={locale} />
    </>
  );
}
