import type { Metadata } from "next";
import { getTranslations } from "next-intl/server";

// Shared builder for public marketing/legal page metadata (title, description,
// canonical + hreflang alternates, OpenGraph, Twitter). Keeps every public page
// consistent and avoids re-declaring the OG/Twitter block per file.

const SITE_URL = process.env.NEXT_PUBLIC_SITE_URL || "http://localhost:3001";
const BASE_PATH = process.env.NEXT_PUBLIC_BASE_PATH || "";

// Interim share card: the repo has no dedicated 1200x630 asset yet, so we reuse
// the primary brand hero (portrait). Swap this single path for a landscape card
// when design delivers one — LinkedIn then upgrades to a full banner.
const OG_IMAGE_PATH = "/images/hero-therapist-client.jpg";
const OG_IMAGE_WIDTH = 718;
const OG_IMAGE_HEIGHT = 808;

const OG_LOCALES: Record<string, string> = { en: "en_US", es: "es_ES" };

/**
 * Build Metadata for a public page.
 * @param locale  active locale ("en" | "es")
 * @param pageKey translation namespace under `metadata.` (e.g. "contact")
 * @param path    route path after the locale (e.g. "/contact"; "" for landing)
 */
export async function buildPublicPageMetadata(
  locale: string,
  pageKey: string,
  path: string
): Promise<Metadata> {
  const t = await getTranslations({ locale, namespace: `metadata.${pageKey}` });
  const tLanding = await getTranslations({ locale, namespace: "metadata.landing" });

  const canonicalUrl = `${SITE_URL}${BASE_PATH}/${locale}${path}`;
  const ogImageUrl = `${SITE_URL}${BASE_PATH}${OG_IMAGE_PATH}`;
  const title = t("title");
  const description = t("description");

  return {
    metadataBase: new URL(SITE_URL),
    title,
    description,
    alternates: {
      canonical: canonicalUrl,
      languages: {
        en: `${SITE_URL}${BASE_PATH}/en${path}`,
        es: `${SITE_URL}${BASE_PATH}/es${path}`,
      },
    },
    openGraph: {
      type: "website",
      siteName: "MBHS",
      title,
      description,
      url: canonicalUrl,
      locale: OG_LOCALES[locale] ?? OG_LOCALES.en,
      images: [
        {
          url: ogImageUrl,
          width: OG_IMAGE_WIDTH,
          height: OG_IMAGE_HEIGHT,
          alt: tLanding("imageAlt"),
        },
      ],
    },
    twitter: {
      card: "summary_large_image",
      title,
      description,
      images: [ogImageUrl],
    },
  };
}

/**
 * Organization + WebSite JSON-LD for the landing page, so search engines can
 * render rich results (name, logo, description). Returns a plain object to be
 * JSON-stringified into a <script type="application/ld+json"> tag.
 */
export async function buildLandingJsonLd(locale: string) {
  const t = await getTranslations({ locale, namespace: "metadata.landing" });
  const homeUrl = `${SITE_URL}${BASE_PATH}/${locale}`;

  return {
    "@context": "https://schema.org",
    "@graph": [
      {
        "@type": "Organization",
        name: "MBHS",
        alternateName: "Multidimensional Behavioral Health Screen",
        url: homeUrl,
        logo: `${SITE_URL}${BASE_PATH}/images/mbhs-logo.png`,
        description: t("description"),
      },
      {
        "@type": "WebSite",
        name: "MBHS",
        url: homeUrl,
        inLanguage: locale,
      },
    ],
  };
}
