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

import {
  buildDefaultSiteSettings,
  parseSiteSettingsValue,
} from "../lib/site-settings";

describe("site settings helpers", () => {
  it("builds defaults from the fallback site name", () => {
    const settings = buildDefaultSiteSettings("Studio Moh");

    expect(settings.defaultLocale).toBe("de");
    expect(settings.brand.primaryColor).toBe("#dc5a35");
    expect(settings.locales.ar.siteName).toBe("Studio Moh");
    expect(settings.locales.ar.titleTemplate).toBe("{pageTitle} | {siteName}");
    expect(settings.locales.en.siteDescription).toBe("Multilingual Next.js base project");
    expect(settings.locales.de.subhead).toBe("");
  });

  it("merges stored values with safe defaults", () => {
    const settings = parseSiteSettingsValue(
      JSON.stringify({
        defaultLocale: "ar",
        brand: {
          primaryColor: "#112233",
        },
        locales: {
          en: {
            siteName: "Brand EN",
            titleTemplate: "{pageTitle} - {siteName}",
            siteDescription: "English description",
          },
          de: {
            siteName: "Brand DE",
            titleTemplate: "{pageTitle} | {siteName} | Freelancer",
            subhead: "German subhead",
          },
        },
      }),
      "Fallback Name",
    );

    expect(settings.defaultLocale).toBe("ar");
    expect(settings.brand.primaryColor).toBe("#112233");
    expect(settings.locales.en.siteName).toBe("Brand EN");
    expect(settings.locales.en.titleTemplate).toBe("{pageTitle} - {siteName}");
    expect(settings.locales.en.siteDescription).toBe("English description");
    expect(settings.locales.en.subhead).toBe("");
    expect(settings.locales.ar.siteName).toBe("Fallback Name");
    expect(settings.locales.ar.titleTemplate).toBe("{pageTitle} | {siteName}");
    expect(settings.locales.de.siteDescription).toBe("Multilingual Next.js base project");
    expect(settings.locales.de.titleTemplate).toBe("{pageTitle} | {siteName} | Freelancer");
    expect(settings.locales.de.subhead).toBe("German subhead");
  });

  it("falls back when stored json is invalid", () => {
    const settings = parseSiteSettingsValue("{invalid-json", "Fallback Name");

    expect(settings).toEqual(buildDefaultSiteSettings("Fallback Name"));
  });

  it("ignores legacy top-level title template values", () => {
    const settings = parseSiteSettingsValue(
      JSON.stringify({
        titleTemplate: "{pageTitle} | moh-sass | Legacy",
        locales: {
          ar: {
            siteName: "اسم الموقع",
          },
        },
      }),
      "Fallback Name",
    );

    expect(settings.locales.ar.titleTemplate).toBe("{pageTitle} | {siteName}");
  });

  it("falls back for invalid default locale and invalid primary color", () => {
    const settings = parseSiteSettingsValue(
      JSON.stringify({
        defaultLocale: "fr",
        brand: {
          primaryColor: "red",
        },
      }),
      "Fallback Name",
    );

    expect(settings.defaultLocale).toBe("de");
    expect(settings.brand.primaryColor).toBe("#dc5a35");
  });
});
