import type { Dictionary } from "@/app/[lang]/dictionaries";
import type { DocsSearchRecord } from "@/lib/docs/search-index";

export type { DocsSearchRecord };

/**
 * Curated search records for the marketing site (services, key landing
 * pages, docs entry point). Hrefs are locale-less — SiteSearch prefixes
 * `/${lang}` when navigating, mirroring the docs search behaviour.
 *
 * Pure data (no fs / server-only deps): safe to call from a server layout
 * and merge with `buildDocsSearchIndex()`.
 */
export function buildSiteIndex(
  dict: Dictionary,
  lang: string,
): DocsSearchRecord[] {
  const nav = dict.nav;
  const services = nav.servicesItems;

  // Curated records are locale-specific (built from the active dictionary), so
  // each intermediate record carries `lang` and is typed as DocsSearchRecord —
  // making the locale type-enforced end-to-end rather than tacked on at the end.
  const serviceRecords: DocsSearchRecord[] = [
    { item: services.seoCode, href: "/services/seo-code-optimization" },
    { item: services.wpManagement, href: "/services/wordpress-management" },
    { item: services.wpSeo, href: "/services/wordpress-seo" },
    { item: services.cmsOptimization, href: "/services/cms-optimization" },
  ].map(({ item, href }) => ({
    pageTitle: item.name,
    title: item.name,
    href,
    content: `${item.name} ${item.description}`,
    lang,
  }));

  const pages: DocsSearchRecord[] = [
    {
      pageTitle: lang === "fr" ? "Accueil" : "Home",
      title: lang === "fr" ? "Accueil" : "Home",
      href: "/",
      content: lang === "fr" ? "Accueil page d'accueil" : "Home landing page",
      lang,
    },
    {
      pageTitle: nav.features,
      title: nav.features,
      href: "/#modules",
      content: nav.features,
      lang,
    },
    {
      pageTitle: nav.modules,
      title: nav.modules,
      href: "/#product",
      content: nav.modules,
      lang,
    },
    {
      pageTitle: nav.pricing,
      title: nav.pricing,
      href: "/pricing",
      content: nav.pricing,
      lang,
    },
    {
      pageTitle: nav.faqs,
      title: nav.faqs,
      href: "/#faqs",
      content: nav.faqs,
      lang,
    },
    {
      pageTitle: "Documentation",
      title: "Documentation",
      href: "/docs",
      content: "Documentation docs API",
      lang,
    },
  ];

  return [...serviceRecords, ...pages];
}
