import { describe, it, expect } from "vitest";
import type { AppearanceConfig } from "@/lib/api-types";
import {
  isAllowedLocale,
  isAllowedTheme,
  isConsentLocale,
  resolveEffectiveLocale,
  resolveEffectiveTheme,
  filterLanguageOptions,
  isDarkModeEnabled,
} from "@/lib/settings/validators";

describe("isAllowedLocale", () => {
  it("returns true for a locale in the config languages", () => {
    expect(isAllowedLocale("uz", ["uz", "ru", "en"])).toBe(true);
    expect(isAllowedLocale("en", ["uz", "ru", "en"])).toBe(true);
  });

  it("returns false for a locale not in the config languages", () => {
    expect(isAllowedLocale("fr", ["uz", "ru", "en"])).toBe(false);
  });

  it("returns false for a locale that is a valid AppLocale but not in config", () => {
    expect(isAllowedLocale("en", ["uz", "ru"])).toBe(false);
  });

  it("returns false for an invalid AppLocale", () => {
    expect(isAllowedLocale("xx", ["xx"])).toBe(false);
  });
});

describe("isAllowedTheme", () => {
  it("returns true for themes in the allowed list", () => {
    expect(isAllowedTheme("light", ["light", "dark", "system"])).toBe(true);
    expect(isAllowedTheme("dark", ["light", "dark", "system"])).toBe(true);
    expect(isAllowedTheme("system", ["light", "dark", "system"])).toBe(true);
  });

  it("returns false for themes not in the allowed list", () => {
    expect(isAllowedTheme("dark", ["light", "system"])).toBe(false);
    expect(isAllowedTheme("auto", ["light", "dark", "system"])).toBe(false);
  });
});

describe("isConsentLocale", () => {
  it("accepts uz and ru", () => {
    expect(isConsentLocale("uz")).toBe(true);
    expect(isConsentLocale("ru")).toBe(true);
  });

  it("rejects en and other strings", () => {
    expect(isConsentLocale("en")).toBe(false);
    expect(isConsentLocale("fr")).toBe(false);
  });
});

describe("resolveEffectiveLocale", () => {
  const languages = ["uz", "ru", "en"];

  it("prefers profile language when valid", () => {
    expect(resolveEffectiveLocale("ru", "en", languages)).toBe("ru");
  });

  it("falls back to stored locale when profile is null", () => {
    expect(resolveEffectiveLocale(null, "en", languages)).toBe("en");
  });

  it("falls back to config default when both are null", () => {
    expect(resolveEffectiveLocale(null, null, languages)).toBe("uz");
  });

  it("falls back to DEFAULT_LOCALE when config is empty", () => {
    expect(resolveEffectiveLocale(null, null, [])).toBe("uz");
  });

  it("skips invalid profile language", () => {
    expect(resolveEffectiveLocale("fr", "ru", languages)).toBe("ru");
  });
});

describe("resolveEffectiveTheme", () => {
  const appearance: AppearanceConfig = {
    allowedThemes: ["light", "dark", "system"],
    defaultTheme: "system",
    darkModeEnabled: true,
  };

  it("uses stored theme when valid", () => {
    expect(resolveEffectiveTheme("dark", appearance)).toBe("dark");
  });

  it("falls back to default when stored is invalid", () => {
    expect(resolveEffectiveTheme("auto", appearance)).toBe("system");
  });

  it("falls back to default when stored is null", () => {
    expect(resolveEffectiveTheme(null, appearance)).toBe("system");
  });

  it("handles null appearance config", () => {
    expect(resolveEffectiveTheme("light", null)).toBe("light");
    expect(resolveEffectiveTheme(null, null)).toBe("system");
  });
});

describe("filterLanguageOptions", () => {
  it("filters to valid AppLocales", () => {
    expect(filterLanguageOptions(["uz", "ru", "en"])).toEqual(["uz", "ru", "en"]);
  });

  it("removes invalid locales", () => {
    expect(filterLanguageOptions(["uz", "fr", "en"])).toEqual(["uz", "en"]);
  });
});

describe("isDarkModeEnabled", () => {
  it("returns true when enabled", () => {
    expect(
      isDarkModeEnabled({
        countryCode: "UZ",
        languages: ["uz"],
        consentVersion: "2026.1",
        featureFlags: {},
        appearance: { allowedThemes: ["light", "dark", "system"], defaultTheme: "system", darkModeEnabled: true },
      }),
    ).toBe(true);
  });

  it("returns false when disabled", () => {
    expect(
      isDarkModeEnabled({
        countryCode: "UZ",
        languages: ["uz"],
        consentVersion: "2026.1",
        featureFlags: {},
        appearance: { allowedThemes: ["light", "system"], defaultTheme: "light", darkModeEnabled: false },
      }),
    ).toBe(false);
  });

  it("returns true when config is null", () => {
    expect(isDarkModeEnabled(null)).toBe(true);
  });
});
