import { describe, expect, it } from "vitest";

import { buildDefaultSiteSettings } from "../lib/site-settings";
import {
  applyTitleTemplateFn,
  buildAppMetadataFromConfig,
  buildLocaleAlternates,
  buildLocalizedMetadataFromConfig,
} from "../lib/metadata";

describe("metadata helpers", () => {
  it("applies the configured title template", () => {
    expect(applyTitleTemplateFn("About", "{pageTitle} | {siteName}", "Studio Moh")).toBe(
      "About | Studio Moh",
    );
    expect(applyTitleTemplateFn("About", "Studio Moh", "Studio Moh")).toBe("About | Studio Moh");
  });

  it("builds root metadata with dynamic icons and social preview", () => {
    const settings = buildDefaultSiteSettings("Studio Moh");
    settings.defaultLocale = "ar";
    settings.locales.ar.siteName = "Studio Moh AR";
    const metadata = buildAppMetadataFromConfig(settings, {
      siteLogoLight: null,
      siteLogoDark: null,
      favicon: {
        assetId: "fav",
        url: "/uploads/media/site-settings/favicon.svg",
        version: "v1",
      },
      defaultOgImage: {
        assetId: "og",
        url: "/uploads/media/site-settings/default-og.png",
        version: "v1",
      },
    });

    expect(metadata.title).toBe("Studio Moh AR");
    expect(metadata.icons).toEqual({
      icon: [{ url: "/favicon.ico?v=v1" }],
      shortcut: [{ url: "/favicon.ico?v=v1" }],
      apple: [{ url: "/apple-icon.png?v=v1" }],
    });
    expect(metadata.openGraph).toMatchObject({
      locale: "ar",
      url: "https://mohfarawati.de/",
    });
    expect(metadata.twitter).toMatchObject({
      card: "summary_large_image",
    });
  });

  it("falls back to localized site description and omits icons when unset", () => {
    const settings = buildDefaultSiteSettings("Studio Moh");
    settings.locales.en.siteDescription = "English fallback description";
    settings.locales.en.titleTemplate = "{pageTitle} | {siteName} | Freelancer";
    const metadata = buildLocalizedMetadataFromConfig({
      settings,
      bindings: {
        siteLogoLight: null,
        siteLogoDark: null,
        favicon: null,
        defaultOgImage: null,
      },
      locale: "en",
      pathname: "/about",
      title: "About",
    });

    expect(metadata.title).toBe("About | Studio Moh | Freelancer");
    expect(metadata.description).toBe("English fallback description");
    expect(metadata.openGraph?.siteName).toBe("Studio Moh");
    expect(metadata.twitter).toMatchObject({
      card: "summary",
    });
  });

  it("can skip the title template for the homepage", () => {
    const settings = buildDefaultSiteSettings("Studio Moh");
    settings.locales.ar.siteName = "اسم الموقع";
    settings.locales.ar.titleTemplate = "{pageTitle} | {siteName}";

    const metadata = buildLocalizedMetadataFromConfig({
      settings,
      bindings: {
        siteLogoLight: null,
        siteLogoDark: null,
        favicon: null,
        defaultOgImage: null,
      },
      locale: "ar",
      pathname: "/",
      title: "اسم الموقع",
      description: "وصف",
      applyTitleTemplate: false,
    });

    expect(metadata.title).toBe("اسم الموقع");
  });

  it("builds alternates and canonical from the runtime default locale", () => {
    const alternates = buildLocaleAlternates("/about", "ar");

    expect(alternates.canonical).toBe("https://mohfarawati.de/about");
    expect(alternates.languages.ar).toBe("https://mohfarawati.de/about");
    expect(alternates.languages.de).toBe("https://mohfarawati.de/de/about");
    expect(alternates.languages["x-default"]).toBe("https://mohfarawati.de/about");
  });

  it("builds localized metadata urls against the configured default locale", () => {
    const settings = buildDefaultSiteSettings("Studio Moh");
    settings.defaultLocale = "ar";

    const metadata = buildLocalizedMetadataFromConfig({
      settings,
      bindings: {
        siteLogoLight: null,
        siteLogoDark: null,
        favicon: null,
        defaultOgImage: null,
      },
      locale: "de",
      pathname: "/about",
      title: "About",
    });

    expect(metadata.alternates).toMatchObject({
      canonical: "https://mohfarawati.de/about",
      languages: {
        ar: "https://mohfarawati.de/about",
        de: "https://mohfarawati.de/de/about",
        en: "https://mohfarawati.de/en/about",
        "x-default": "https://mohfarawati.de/about",
      },
    });
    expect(metadata.openGraph).toMatchObject({
      url: "https://mohfarawati.de/de/about",
    });
  });
});
