import { describe, expect, it } from "vitest";
import { renderMemorySummary, supportedMemorySummaryLanguages } from "@app/memory-client";
import svCommon from "../../mobile/src/locales/sv/common.json" with { type: "json" };
import enCommon from "../../mobile/src/locales/en/common.json" with { type: "json" };
import esCommon from "../../mobile/src/locales/es/common.json" with { type: "json" };
import itCommon from "../../mobile/src/locales/it/common.json" with { type: "json" };
import deCommon from "../../mobile/src/locales/de/common.json" with { type: "json" };
import frCommon from "../../mobile/src/locales/fr/common.json" with { type: "json" };
import daCommon from "../../mobile/src/locales/da/common.json" with { type: "json" };
import nbCommon from "../../mobile/src/locales/nb/common.json" with { type: "json" };
import fiCommon from "../../mobile/src/locales/fi/common.json" with { type: "json" };
import nlCommon from "../../mobile/src/locales/nl/common.json" with { type: "json" };
import plCommon from "../../mobile/src/locales/pl/common.json" with { type: "json" };
import ptCommon from "../../mobile/src/locales/pt/common.json" with { type: "json" };

const LOCALES: Record<string, Record<string, unknown>> = {
  sv: svCommon,
  en: enCommon,
  es: esCommon,
  it: itCommon,
  de: deCommon,
  fr: frCommon,
  da: daCommon,
  nb: nbCommon,
  fi: fiCommon,
  nl: nlCommon,
  pl: plCommon,
  pt: ptCommon,
};

const EXPECTED_LANGS = Object.keys(LOCALES);

function getTransparency(catalog: Record<string, unknown>): string | undefined {
  const flat = catalog["onboarding.memoryTransparency"];
  if (typeof flat === "string") return flat;
  const nested = catalog["onboarding"] as Record<string, unknown> | undefined;
  const nestedValue = nested?.["memoryTransparency"];
  return typeof nestedValue === "string" ? nestedValue : undefined;
}

describe("S6 minnes-i18n + transparens-paritet", () => {
  it("renderMemorySummary stödjer exakt 12 språk", () => {
    const supported = supportedMemorySummaryLanguages();
    expect(supported.length).toBe(12);
    expect(new Set(supported).size).toBe(12);
    for (const lang of EXPECTED_LANGS) {
      expect(supported).toContain(lang);
    }
  });

  it("alla S5-value-former renderas på alla 12 språk", () => {
    const cases = [
      { value: { favoriteCuisine: "italian" }, expectedSv: "Favoritkök: italian" },
      { value: { avoidIngredientId: "broccoli" }, expectedSv: "Undviker ingrediens: broccoli" },
      { value: { goal: "less_waste" }, expectedSv: "Mål: less_waste" },
      { value: { primaryGoal: "less_waste" }, expectedSv: "Primärt mål: less_waste" },
      { value: { allergen: "gluten" }, expectedSv: "Allergi: gluten" },
      { value: { spiceLevelMax: 2 }, expectedSv: "Max styrka: 2" },
    ];
    for (const { value, expectedSv } of cases) {
      expect(renderMemorySummary({ summarySv: "", value }, "sv-SE")).toBe(expectedSv);
      for (const lang of EXPECTED_LANGS) {
        const rendered = renderMemorySummary({ summarySv: "", value }, `${lang}-XX`);
        expect(rendered.length, `tom summary för ${lang}, ${JSON.stringify(value)}`).toBeGreaterThan(0);
        expect(rendered).not.toBe("");
      }
    }
  });

  it("transparens-strängen finns i alla 12 common.json", () => {
    for (const [lang, catalog] of Object.entries(LOCALES)) {
      const str = getTransparency(catalog);
      expect(str, `saknas i ${lang}`).toBeDefined();
      expect(str!.length, `tom i ${lang}`).toBeGreaterThan(0);
    }
  });

  it("inga dubblett-transparenssträngar över språk", () => {
    const seen = new Set<string>();
    for (const [lang, catalog] of Object.entries(LOCALES)) {
      const str = getTransparency(catalog) ?? "";
      seen.add(str);
    }
    expect(seen.size).toBeGreaterThanOrEqual(3);
  });
});
