// @vitest-environment happy-dom
import { afterEach, beforeEach, describe, expect, test, vi } from "vitest";
import { DEFAULT_INPUT_SHADOW } from "@formbricks/types/dark-palette";
import { type TSurveyStyling } from "@formbricks/types/surveys/types";
import { type TWorkspaceStyling } from "@formbricks/types/workspace";
import { AA_CONTRAST_RATIO, getContrastRatio } from "./color";
import { addCustomThemeToDom, addStylesToDom, getStyleNonce, setStyleNonce } from "./styles";

// Mock CSS module imports
vi.mock("@/styles/global.css?inline", () => ({ default: ".global {}" }));
vi.mock("@/styles/preflight.css?inline", () => ({ default: ".preflight {}" }));
vi.mock("../../../../apps/web/modules/ui/components/editor/styles-editor-frontend.css?inline", () => ({
  default: ".editor {}",
}));

// Mock color utility functions if they have complex dependencies or for controlled testing
// For now, we assume they work as expected or are simple enough not to need mocking here.
// If isLight or mixColor had external dependencies or complex logic, you might mock them:
// vi.mock('@/lib/color', () => ({
//   isLight: vi.fn(),
//   mixColor: vi.fn(),
// }));

// Helper to create a base TWorkspaceStyling object with all properties set to null or a default
const getBaseWorkspaceStyling = (overrides: Partial<TWorkspaceStyling> = {}): TWorkspaceStyling => {
  return {
    brandColor: null,
    cardBackgroundColor: null,
    cardBorderColor: null,

    inputBorderColor: null,
    roundness: null, // defaults to 8 in addCustomThemeToDom if null here
    hideProgressBar: null,
    allowStyleOverwrite: false,
    highlightBorderColor: null,
    isLogoHidden: null,
    ...overrides,
  };
};

describe("setStyleNonce and getStyleNonce", () => {
  beforeEach(() => {
    // Reset the DOM and nonce before each test
    document.head.innerHTML = "";
    document.body.innerHTML = "";
    setStyleNonce(undefined);
  });

  test("should set and get the nonce value", () => {
    const nonce = "test-nonce-123";
    setStyleNonce(nonce);
    expect(getStyleNonce()).toBe(nonce);
  });

  test("should allow clearing the nonce with undefined", () => {
    setStyleNonce("initial-nonce");
    expect(getStyleNonce()).toBe("initial-nonce");
    setStyleNonce(undefined);
    expect(getStyleNonce()).toBeUndefined();
  });

  test("should update existing formbricks__css element with nonce", () => {
    // Create an existing style element
    const existingElement = document.createElement("style");
    existingElement.id = "formbricks__css";
    document.head.appendChild(existingElement);

    const nonce = "test-nonce-456";
    setStyleNonce(nonce);

    expect(existingElement.getAttribute("nonce")).toBe(nonce);
  });

  test("should update existing formbricks__css__custom element with nonce", () => {
    // Create an existing custom style element
    const existingElement = document.createElement("style");
    existingElement.id = "formbricks__css__custom";
    document.head.appendChild(existingElement);

    const nonce = "test-nonce-789";
    setStyleNonce(nonce);

    expect(existingElement.getAttribute("nonce")).toBe(nonce);
  });

  test("should not update nonce on existing elements when nonce is undefined", () => {
    // Create existing style elements
    const mainElement = document.createElement("style");
    mainElement.id = "formbricks__css";
    mainElement.setAttribute("nonce", "existing-nonce");
    document.head.appendChild(mainElement);

    const customElement = document.createElement("style");
    customElement.id = "formbricks__css__custom";
    customElement.setAttribute("nonce", "existing-nonce");
    document.head.appendChild(customElement);

    setStyleNonce(undefined);

    // Elements should retain their existing nonce (or be cleared if implementation removes it)
    // The current implementation doesn't remove nonce when undefined, so we check it's not changed
    expect(mainElement.getAttribute("nonce")).toBe("existing-nonce");
    expect(customElement.getAttribute("nonce")).toBe("existing-nonce");
  });

  test("should handle setting nonce when elements don't exist", () => {
    const nonce = "test-nonce-no-elements";
    setStyleNonce(nonce);
    expect(getStyleNonce()).toBe(nonce);
    // Should not throw and should store the nonce for future use
  });
});

describe("addStylesToDom", () => {
  beforeEach(() => {
    // Reset the DOM before each test
    document.head.innerHTML = "";
    document.body.innerHTML = "";
    setStyleNonce(undefined);
  });

  afterEach(() => {
    const styleElement = document.getElementById("formbricks__css");
    if (styleElement) {
      styleElement.remove();
    }
    setStyleNonce(undefined);
  });

  test("should add a style element to the head with combined CSS", () => {
    addStylesToDom();
    const styleElement = document.getElementById("formbricks__css") as HTMLStyleElement;
    expect(styleElement).not.toBeNull();
    expect(styleElement.tagName).toBe("STYLE");
    expect(document.head.contains(styleElement)).toBe(true);

    const expectedCss = ".preflight {}.global {}.editor {}";
    expect(styleElement.innerHTML).toBe(expectedCss);
  });

  test("should not add a new style element if one already exists", () => {
    addStylesToDom(); // First call
    const firstStyleElement = document.getElementById("formbricks__css");
    const initialInnerHTML = firstStyleElement?.innerHTML;

    addStylesToDom(); // Second call
    const secondStyleElement = document.getElementById("formbricks__css");
    const allStyleElements = document.querySelectorAll("#formbricks__css");

    expect(allStyleElements).toHaveLength(1);
    expect(secondStyleElement).toBe(firstStyleElement);
    expect(secondStyleElement?.innerHTML).toBe(initialInnerHTML);
  });

  test("should apply nonce to new style element when nonce is set", () => {
    const nonce = "test-nonce-styles";
    setStyleNonce(nonce);
    addStylesToDom();

    const styleElement = document.getElementById("formbricks__css") as HTMLStyleElement;
    expect(styleElement).not.toBeNull();
    expect(styleElement.getAttribute("nonce")).toBe(nonce);
  });

  test("should not apply nonce when nonce is not set", () => {
    addStylesToDom();
    const styleElement = document.getElementById("formbricks__css") as HTMLStyleElement;
    expect(styleElement).not.toBeNull();
    expect(styleElement.getAttribute("nonce")).toBeNull();
  });

  test("should update nonce on existing style element if nonce is set after creation", () => {
    addStylesToDom(); // Create element without nonce
    const styleElement = document.getElementById("formbricks__css") as HTMLStyleElement;
    expect(styleElement.getAttribute("nonce")).toBeNull();

    const nonce = "test-nonce-update";
    setStyleNonce(nonce);
    addStylesToDom(); // Call again to trigger update logic

    expect(styleElement.getAttribute("nonce")).toBe(nonce);
  });

  test("should not overwrite existing nonce when updating via addStylesToDom", () => {
    const existingElement = document.createElement("style");
    existingElement.id = "formbricks__css";
    existingElement.setAttribute("nonce", "existing-nonce");
    document.head.appendChild(existingElement);

    // Don't call setStyleNonce - just verify addStylesToDom doesn't overwrite
    addStylesToDom(); // Should not overwrite since nonce already exists

    // The update logic in addStylesToDom only sets nonce if it doesn't exist
    expect(existingElement.getAttribute("nonce")).toBe("existing-nonce");
  });

  test("should overwrite existing nonce when setStyleNonce is called directly", () => {
    const existingElement = document.createElement("style");
    existingElement.id = "formbricks__css";
    existingElement.setAttribute("nonce", "existing-nonce");
    document.head.appendChild(existingElement);

    const newNonce = "new-nonce";
    setStyleNonce(newNonce); // setStyleNonce always updates existing elements

    // setStyleNonce directly updates the nonce attribute
    expect(existingElement.getAttribute("nonce")).toBe(newNonce);
  });
});

describe("addCustomThemeToDom", () => {
  beforeEach(() => {
    document.head.innerHTML = "";
    document.body.innerHTML = "";
    setStyleNonce(undefined);
  });

  afterEach(() => {
    const styleElement = document.getElementById("formbricks__css__custom");
    if (styleElement) {
      styleElement.remove();
    }
    setStyleNonce(undefined);
  });

  const getCssVariables = (styleElement: HTMLStyleElement | null): Record<string, string> => {
    if (!styleElement || !styleElement.innerHTML) return {};
    const cssText = styleElement.innerHTML;
    const rootMatch = cssText.match(/#fbjs\s*{\s*([^}]*?)\s*}/);
    if (!rootMatch || !rootMatch[1]) return {};

    const variables: Record<string, string> = {};
    const lines = rootMatch[1]
      .trim()
      .split(";")
      .filter((line) => line.trim() !== "");
    lines.forEach((line) => {
      const parts = line.split(":");
      if (parts.length === 2) {
        variables[parts[0].trim()] = parts[1].trim();
      }
    });
    return variables;
  };

  test("should add a custom theme style element to the head", () => {
    const styling = getBaseWorkspaceStyling({ brandColor: { light: "#FF0000" } });
    addCustomThemeToDom({ styling });
    const styleElement = document.getElementById("formbricks__css__custom") as HTMLStyleElement;
    expect(styleElement).not.toBeNull();
    expect(styleElement.tagName).toBe("STYLE");
    expect(document.head.contains(styleElement)).toBe(true);
  });

  // ENG-3864: Page font lets the survey take the page's font instead of the survey font.
  test("renders in the page's font when told to, and in the survey font otherwise", () => {
    const pageFontRule =
      /:root #fbjs \*,\s*:root #fbjs ::before,\s*:root #fbjs ::after\s*\{\s*font-family: inherit/;
    const styling = getBaseWorkspaceStyling({ pageFont: true });

    addCustomThemeToDom({ styling, pageFont: true });
    expect(document.getElementById("formbricks__css__custom")?.innerHTML).toMatch(pageFontRule);

    // Never from the styling alone: a WebView or a link survey has no page font to take.
    addCustomThemeToDom({ styling });
    expect(document.getElementById("formbricks__css__custom")?.innerHTML).not.toMatch(pageFontRule);
  });

  test("should reuse existing custom theme style element", () => {
    const styling1 = getBaseWorkspaceStyling({ brandColor: { light: "#FF0000" } });
    addCustomThemeToDom({ styling: styling1 });
    const firstElement = document.getElementById("formbricks__css__custom");

    const styling2 = getBaseWorkspaceStyling({ brandColor: { light: "#00FF00" } });
    addCustomThemeToDom({ styling: styling2 });
    const secondElement = document.getElementById("formbricks__css__custom");
    const allElements = document.querySelectorAll("#formbricks__css__custom");

    expect(allElements).toHaveLength(1);
    expect(secondElement).toBe(firstElement);
  });

  test("should apply minimal styling with brandColor and default roundness", () => {
    const styling = getBaseWorkspaceStyling({ brandColor: { light: "#0000FF" } }); // A dark color, roundness will use default
    addCustomThemeToDom({ styling });
    const styleElement = document.getElementById("formbricks__css__custom") as HTMLStyleElement;
    const variables = getCssVariables(styleElement);

    expect(variables["--fb-brand-color"]).toBe("#0000FF");
    expect(variables["--fb-focus-color"]).toBe("#0000FF");
    expect(variables["--fb-brand-text-color"]).toBe("white"); // isLight('#0000FF') is false
    expect(variables["--fb-border-radius"]).toBe("8px"); // Default roundness
  });

  test("should apply brand-text-color as black for light brandColor", () => {
    const styling = getBaseWorkspaceStyling({ brandColor: { light: "#FFFF00" } }); // A light color
    addCustomThemeToDom({ styling });
    const styleElement = document.getElementById("formbricks__css__custom") as HTMLStyleElement;
    const variables = getCssVariables(styleElement);
    expect(variables["--fb-brand-text-color"]).toBe("black"); // isLight('#FFFF00') is true
  });

  test("should default brand-text-color to white if brandColor is undefined", () => {
    const styling = getBaseWorkspaceStyling({ brandColor: null }); // Explicitly null brandColor
    addCustomThemeToDom({ styling });
    const styleElement = document.getElementById("formbricks__css__custom") as HTMLStyleElement;
    const variables = getCssVariables(styleElement);
    expect(variables["--fb-brand-text-color"]).toBe("#ffffff");
  });

  test("should apply all survey styling properties", () => {
    const styling: TSurveyStyling = {
      brandColor: { light: "#112233" },
      elementHeadlineColor: { light: "#AABBCC" },
      elementDescriptionColor: { light: "#AABBCC" },
      inputTextColor: { light: "#334455" },
      inputBorderColor: { light: "#DDDDDD" },
      cardBackgroundColor: { light: "#EEEEEE" },
      cardBorderColor: { light: "#CCCCCC" },

      inputBgColor: { light: "#F0F0F0" },
      roundness: 12,
      hideProgressBar: false,
      background: { bg: "#ABCDEF", bgType: "color", brightness: 100 },
      highlightBorderColor: { light: "#990000" },
      overwriteThemeStyling: false,
    };
    addCustomThemeToDom({ styling });
    const styleElement = document.getElementById("formbricks__css__custom") as HTMLStyleElement;
    const variables = getCssVariables(styleElement);

    expect(variables["--fb-brand-color"]).toBe("#112233");
    expect(variables["--fb-focus-color"]).toBe("#112233");
    expect(variables["--fb-brand-text-color"]).toBe("white");
    expect(variables["--fb-heading-color"]).toBe("#AABBCC");
    expect(variables["--fb-subheading-color"]).toBe("#AABBCC");
    expect(variables["--fb-placeholder-color"]).toBeDefined(); // Relies on mixColor
    expect(variables["--fb-border-color"]).toBe("#DDDDDD");
    expect(variables["--fb-border-color-highlight"]).toBeDefined(); // Relies on mixColor
    expect(variables["--fb-survey-background-color"]).toBe("#EEEEEE");
    expect(variables["--fb-survey-border-color"]).toBe("#CCCCCC");
    expect(variables["--fb-border-radius"]).toBe("12px");
    expect(variables["--fb-input-background-color"]).toBe("#F0F0F0");
    expect(variables["--fb-signature-text-color"]).toBeDefined(); // Relies on mixColor & isLight
    expect(variables["--fb-branding-text-color"]).toBeDefined(); // Relies on mixColor & isLight
    expect(variables["--fb-input-background-color-selected"]).toBeDefined(); // Relies on mixColor

    // Check accent colors derived from brandColor when not explicitly set
    expect(variables["--fb-accent-background-color"]).toBeDefined();
    expect(variables["--fb-accent-background-color-selected"]).toBeDefined();

    // calendar-tile-color depends on isLight(brandColor)
    expect(variables["--fb-calendar-tile-color"]).toBeUndefined(); // isLight('#112233') is false, so this should be undefined
  });

  test("should generate calendar-tile-color for light brandColor", () => {
    const styling = getBaseWorkspaceStyling({ brandColor: { light: "#ffffff" } });
    addCustomThemeToDom({ styling });
    const styleElement = document.getElementById("formbricks__css__custom") as HTMLStyleElement;
    const variables = getCssVariables(styleElement);

    expect(variables["--fb-calendar-tile-color"]).toBeDefined();
  });

  test("should apply advanced styling properties", () => {
    const styling: TSurveyStyling = {
      ...getBaseWorkspaceStyling(),
      // Buttons
      buttonBgColor: { light: "#AA1122" },
      buttonTextColor: { light: "#BB2233" },
      buttonBorderRadius: 4,
      buttonHeight: "40",
      buttonFontSize: 16,
      buttonFontWeight: "bold",
      buttonPaddingX: 20,
      buttonPaddingY: 10,
      // Inputs
      inputTextColor: { light: "#334455" },
      inputBorderRadius: 4,
      inputHeight: 40,
      inputFontSize: 14,
      inputPaddingX: 12,
      inputPaddingY: 8,
      inputPlaceholderOpacity: 0.5,
      inputShadow: "0 1px 2px 0 rgba(0, 0, 0, 0.05)",
      // Options
      optionBgColor: { light: "#CC4455" },
      optionLabelColor: { light: "#DD5566" },
      optionBorderColor: { light: "#EE6677" },
      optionBorderRadius: 4,
      optionPaddingX: 12,
      optionPaddingY: 8,
      optionFontSize: 14,
      // Element Headline & Description
      elementHeadlineFontSize: 24,
      elementHeadlineFontWeight: "bold",
      elementHeadlineColor: { light: "#223344" },
      elementDescriptionFontSize: 16,
      elementDescriptionColor: { light: "#445566" },
      // Progress Bar
      progressTrackHeight: 4,
      progressTrackBgColor: { light: "#667788" },
      progressIndicatorBgColor: { light: "#778899" },
    };

    addCustomThemeToDom({ styling });
    const styleElement = document.getElementById("formbricks__css__custom") as HTMLStyleElement;
    const variables = getCssVariables(styleElement);

    // Buttons
    expect(variables["--fb-button-bg-color"]).toBe("#AA1122");
    expect(variables["--fb-button-text-color"]).toBe("#BB2233");
    expect(variables["--fb-button-border-radius"]).toBe("4px");
    expect(variables["--fb-button-height"]).toBe("40px");
    expect(variables["--fb-button-font-size"]).toBe("16px");
    expect(variables["--fb-button-font-weight"]).toBe("bold");
    expect(variables["--fb-button-padding-x"]).toBe("20px");
    expect(variables["--fb-button-padding-y"]).toBe("10px");
    // Inputs
    expect(variables["--fb-input-text-color"]).toBe("#334455");
    expect(variables["--fb-input-border-radius"]).toBe("4px");
    expect(variables["--fb-input-height"]).toBe("40px");
    expect(variables["--fb-input-font-size"]).toBe("14px");
    expect(variables["--fb-input-padding-x"]).toBe("12px");
    expect(variables["--fb-input-padding-y"]).toBe("8px");
    expect(variables["--fb-input-placeholder-opacity"]).toBe("0.5");
    expect(variables["--fb-input-shadow"]).toBe("0 1px 2px 0 rgba(0, 0, 0, 0.05)");
    // Options
    expect(variables["--fb-option-bg-color"]).toBe("#CC4455");
    expect(variables["--fb-option-label-color"]).toBe("#DD5566");
    expect(variables["--fb-option-border-color"]).toBe("#EE6677");
    expect(variables["--fb-option-border-radius"]).toBe("4px");
    expect(variables["--fb-option-padding-x"]).toBe("12px");
    expect(variables["--fb-option-padding-y"]).toBe("8px");
    expect(variables["--fb-option-font-size"]).toBe("14px");
    // Element Headline & Description
    expect(variables["--fb-element-headline-font-size"]).toBe("24px");
    expect(variables["--fb-element-headline-font-weight"]).toBe("bold");
    expect(variables["--fb-element-headline-color"]).toBe("#223344");
    expect(variables["--fb-element-description-font-size"]).toBe("16px");
    expect(variables["--fb-element-description-color"]).toBe("#445566");
    // Progress Bar
    expect(variables["--fb-progress-track-height"]).toBe("4px");
    expect(variables["--fb-progress-track-bg-color"]).toBe("#667788");
    expect(variables["--fb-progress-indicator-bg-color"]).toBe("#778899");
  });

  test("keeps a dark button color as the back-button color (readable on the card)", () => {
    const styling = getBaseWorkspaceStyling({
      buttonBgColor: { light: "#1e40af" },
      cardBackgroundColor: { light: "#ffffff" },
    });
    addCustomThemeToDom({ styling });
    const styleElement = document.getElementById("formbricks__css__custom") as HTMLStyleElement;
    const variables = getCssVariables(styleElement);

    // Dark brand already clears AA on white, so it is used verbatim.
    expect(variables["--fb-back-button-color"]).toBe("#1e40af");
  });

  test("darkens a light button color for an AA-compliant back-button color", () => {
    const styling = getBaseWorkspaceStyling({
      buttonBgColor: { light: "#f9a8c0" }, // light pink — fails contrast as text on white
      cardBackgroundColor: { light: "#ffffff" },
    });
    addCustomThemeToDom({ styling });
    const styleElement = document.getElementById("formbricks__css__custom") as HTMLStyleElement;
    const variables = getCssVariables(styleElement);

    // The raw brand is unreadable on white, so it must be adjusted to a darker tone.
    expect(variables["--fb-back-button-color"]).toBeDefined();
    expect(variables["--fb-back-button-color"]).not.toBe("#f9a8c0");
    expect(getContrastRatio(variables["--fb-back-button-color"], "#ffffff")).toBeGreaterThanOrEqual(
      AA_CONTRAST_RATIO
    );
  });

  test("should format dimensions correctly", () => {
    const styling: TSurveyStyling = {
      ...getBaseWorkspaceStyling(),
      buttonBorderRadius: 10, // number -> px
      buttonHeight: "20", // numeric string -> px
      buttonFontSize: "1.5rem", // string -> string
    };

    addCustomThemeToDom({ styling });
    const styleElement = document.getElementById("formbricks__css__custom") as HTMLStyleElement;
    const variables = getCssVariables(styleElement);

    expect(variables["--fb-button-border-radius"]).toBe("10px");
    expect(variables["--fb-button-height"]).toBe("20px");
    expect(variables["--fb-button-font-size"]).toBe("1.5rem");
  });

  test("should derive input-placeholder-color from inputTextColor when set", () => {
    const styling: TSurveyStyling = {
      ...getBaseWorkspaceStyling(),
      inputTextColor: { light: "#112233" },
    };
    addCustomThemeToDom({ styling });
    const styleElement = document.getElementById("formbricks__css__custom") as HTMLStyleElement;
    const variables = getCssVariables(styleElement);

    expect(variables["--fb-input-placeholder-color"]).toBeDefined();
    expect(variables["--fb-placeholder-color"]).toBeDefined();
    expect(variables["--fb-input-placeholder-color"]).toBe(variables["--fb-placeholder-color"]);
  });

  test("formatted text in the headline and description hooks inherits the theme from the hook element", () => {
    // The hook element keeps the theme value; the text inside it inherits, so a Custom CSS rule on the
    // hook reaches formatted text too (ENG-3554).
    addCustomThemeToDom({
      styling: {
        allowStyleOverwrite: true,
        elementHeadlineFontSize: 24,
        elementHeadlineColor: { light: "#223344" },
        elementDescriptionFontWeight: 300,
      },
    });
    const css = document.getElementById("formbricks__css__custom")?.innerHTML ?? "";
    const ruleFor = (selector: string) => css.slice(css.indexOf(`${selector} {`)).split("}")[0];

    expect(ruleFor("#fbjs .label-headline,\n#fbjs .label-headline *")).toContain(
      "font-size: var(--fb-element-headline-font-size) !important;"
    );
    expect(ruleFor('#fbjs [data-fb-part="headline"] *')).toBe(
      '#fbjs [data-fb-part="headline"] * {\n  font-size: inherit !important;\n  color: inherit !important;\n'
    );
    expect(ruleFor('#fbjs [data-fb-part="description"] *')).toBe(
      '#fbjs [data-fb-part="description"] * {\n  font-weight: inherit !important;\n'
    );
    // Same specificity as the `.label-headline *` rule, so it has to come after it to win.
    expect(css.indexOf('[data-fb-part="headline"] *')).toBeGreaterThan(
      css.indexOf("#fbjs .label-headline *")
    );
  });

  test("without headline or description values, no hook rule overrides the stylesheet's own", () => {
    addCustomThemeToDom({ styling: { allowStyleOverwrite: true, brandColor: { light: "#1f5f8b" } } });
    expect(document.getElementById("formbricks__css__custom")?.innerHTML).not.toContain("data-fb-part");
  });

  test("should set signature and branding text colors for dark elementHeadlineColor", () => {
    const styling = getBaseWorkspaceStyling({
      elementHeadlineColor: { light: "#202020" }, // A dark color
      brandColor: { light: "#123456" },
    });
    addCustomThemeToDom({ styling });
    const styleElement = document.getElementById("formbricks__css__custom") as HTMLStyleElement;
    const variables = getCssVariables(styleElement);

    // For dark elementHeadlineColor ('#202020'), isLight is false, so mix with white.
    expect(variables["--fb-signature-text-color"]).toBeDefined();
    expect(variables["--fb-branding-text-color"]).toBeDefined();
  });

  test("should handle roundness 0 correctly", () => {
    const styling = getBaseWorkspaceStyling({ roundness: 0, brandColor: { light: "#123456" } });
    addCustomThemeToDom({ styling });
    const styleElement = document.getElementById("formbricks__css__custom") as HTMLStyleElement;
    const variables = getCssVariables(styleElement);
    expect(variables["--fb-border-radius"]).toBe("0px");
  });

  test("should set input-background-color-selected to slate-50 for white inputBgColor", () => {
    const whiteColors = ["#fff", "#ffffff", "white"];
    whiteColors.forEach((color) => {
      const styling = getBaseWorkspaceStyling({
        inputBgColor: { light: color },
        brandColor: { light: "#123" },
      });
      addCustomThemeToDom({ styling });
      const styleElement = document.getElementById("formbricks__css__custom") as HTMLStyleElement;
      const variables = getCssVariables(styleElement);
      expect(variables["--fb-input-background-color-selected"]).toBe("var(--slate-50)");
    });
  });

  test("should mix input-background-color-selected for non-white inputBgColor", () => {
    const styling = getBaseWorkspaceStyling({
      inputBgColor: { light: "#E0E0E0" },
      brandColor: { light: "#123" },
    }); // Not white
    addCustomThemeToDom({ styling });
    const styleElement = document.getElementById("formbricks__css__custom") as HTMLStyleElement;
    const variables = getCssVariables(styleElement);
    // We can't easily test the exact mixed color without duplicating mixColor logic or having access to its exact output for these inputs.
    // So, we just check that it's defined and not the slate-50 default.
    expect(variables["--fb-input-background-color-selected"]).toBeDefined();
    expect(variables["--fb-input-background-color-selected"]).not.toBe("var(--slate-50)");
  });

  test("should not set calendar-tile-color if brandColor is undefined", () => {
    const styling = getBaseWorkspaceStyling({ brandColor: null });
    addCustomThemeToDom({ styling });
    const styleElement = document.getElementById("formbricks__css__custom") as HTMLStyleElement;
    const variables = getCssVariables(styleElement);
    expect(variables["--fb-calendar-tile-color"]).toBeUndefined();
  });

  test("should not define variables for undefined styling properties", () => {
    const styling = getBaseWorkspaceStyling({ brandColor: { light: "#ABC" } }); // Only brandColor is defined
    addCustomThemeToDom({ styling });
    const styleElement = document.getElementById("formbricks__css__custom") as HTMLStyleElement;
    const variables = getCssVariables(styleElement);

    expect(variables["--fb-brand-color"]).toBe("#ABC");
    // Check a few that would not be set
    expect(variables["--fb-heading-color"]).toBeUndefined();
    expect(variables["--fb-survey-background-color"]).toBeUndefined();
    expect(variables["--fb-input-background-color"]).toBeUndefined();
  });

  test("should apply nonce to new custom theme style element when nonce is set", () => {
    const nonce = "test-nonce-custom";
    setStyleNonce(nonce);
    const styling = getBaseWorkspaceStyling({ brandColor: { light: "#FF0000" } });
    addCustomThemeToDom({ styling });

    const styleElement = document.getElementById("formbricks__css__custom") as HTMLStyleElement;
    expect(styleElement).not.toBeNull();
    expect(styleElement.getAttribute("nonce")).toBe(nonce);
  });

  test("should not apply nonce when nonce is not set", () => {
    const styling = getBaseWorkspaceStyling({ brandColor: { light: "#FF0000" } });
    addCustomThemeToDom({ styling });

    const styleElement = document.getElementById("formbricks__css__custom") as HTMLStyleElement;
    expect(styleElement).not.toBeNull();
    expect(styleElement.getAttribute("nonce")).toBeNull();
  });

  test("should update nonce on existing custom style element if nonce is set after creation", () => {
    const styling = getBaseWorkspaceStyling({ brandColor: { light: "#FF0000" } });
    addCustomThemeToDom({ styling }); // Create element without nonce
    const styleElement = document.getElementById("formbricks__css__custom") as HTMLStyleElement;
    expect(styleElement.getAttribute("nonce")).toBeNull();

    const nonce = "test-nonce-custom-update";
    setStyleNonce(nonce);
    addCustomThemeToDom({ styling }); // Call again to trigger update logic

    expect(styleElement.getAttribute("nonce")).toBe(nonce);
  });

  test("should not overwrite existing nonce when updating custom theme via addCustomThemeToDom", () => {
    const existingElement = document.createElement("style");
    existingElement.id = "formbricks__css__custom";
    existingElement.setAttribute("nonce", "existing-custom-nonce");
    document.head.appendChild(existingElement);

    // Don't call setStyleNonce - just verify addCustomThemeToDom doesn't overwrite
    const styling = getBaseWorkspaceStyling({ brandColor: { light: "#FF0000" } });
    addCustomThemeToDom({ styling }); // Should not overwrite since nonce already exists

    // The update logic in addCustomThemeToDom only sets nonce if it doesn't exist
    expect(existingElement.getAttribute("nonce")).toBe("existing-custom-nonce");
  });

  test("should overwrite existing nonce when setStyleNonce is called directly on custom theme", () => {
    const existingElement = document.createElement("style");
    existingElement.id = "formbricks__css__custom";
    existingElement.setAttribute("nonce", "existing-custom-nonce");
    document.head.appendChild(existingElement);

    const newNonce = "new-custom-nonce";
    setStyleNonce(newNonce); // setStyleNonce directly updates the nonce attribute

    // setStyleNonce directly updates the nonce attribute
    expect(existingElement.getAttribute("nonce")).toBe(newNonce);
  });
});

describe("getBaseWorkspaceStyling_Helper", () => {
  test("should return default values for all properties when no overrides are provided", () => {
    const baseStyling = getBaseWorkspaceStyling();
    expect(baseStyling.brandColor).toBeNull();
    expect(baseStyling.cardBackgroundColor).toBeNull();
    expect(baseStyling.cardBorderColor).toBeNull();

    expect(baseStyling.inputBorderColor).toBeNull();
    expect(baseStyling.roundness).toBeNull();
    expect(baseStyling.hideProgressBar).toBeNull();
    expect(baseStyling.allowStyleOverwrite).toBe(false);
    expect(baseStyling.highlightBorderColor).toBeNull();
    // End of user highlighted lines
    expect(baseStyling.isLogoHidden).toBeNull();
  });

  test("should correctly apply overrides to specified properties", () => {
    const overrides: Partial<TWorkspaceStyling> = {
      inputBgColor: { light: "#111" },
      inputBorderColor: { light: "#222" },
      roundness: 10,
      hideProgressBar: true,
      allowStyleOverwrite: true,
      highlightBorderColor: { light: "#333" },
      brandColor: { light: "#FFF" }, // Also test a non-highlighted property
    };
    const styled = getBaseWorkspaceStyling(overrides);

    expect(styled.inputBgColor).toEqual({ light: "#111" });
    expect(styled.inputBorderColor).toEqual({ light: "#222" });
    expect(styled.roundness).toBe(10);
    expect(styled.hideProgressBar).toBe(true);
    expect(styled.allowStyleOverwrite).toBe(true);
    expect(styled.highlightBorderColor).toEqual({ light: "#333" });
    expect(styled.brandColor).toEqual({ light: "#FFF" });

    // Check a property not in overrides retains its default
    expect(styled.cardBackgroundColor).toBeNull();
    expect(styled.isLogoHidden).toBeNull();
  });
});

describe("addCustomThemeToDom dark palette", () => {
  const getCss = () => document.getElementById("formbricks__css__custom")?.innerHTML ?? "";
  const getDarkBlock = () => getCss().split('#fbjs[data-appearance="dark"]')[1] ?? "";

  afterEach(() => {
    document.getElementById("formbricks__css__custom")?.remove();
  });

  test("keeps the light block unchanged and appends a separate dark block", () => {
    const styling = getBaseWorkspaceStyling({ brandColor: { light: "#1e40af" } });
    addCustomThemeToDom({ styling });
    const css = getCss();
    const [light] = css.split('#fbjs[data-appearance="dark"]');

    expect(light).toContain("--fb-brand-color: #1e40af;");
    expect(light).not.toContain("color-scheme");
    expect(getDarkBlock()).toContain("color-scheme: dark;");
  });

  test("derives dark surfaces and readable text from the brand color", () => {
    addCustomThemeToDom({ styling: getBaseWorkspaceStyling({ brandColor: { light: "#1e40af" } }) });
    const dark = getDarkBlock();

    expect(dark).toContain("--fb-survey-background-color: #0d1426;");
    expect(dark).toContain("--fb-element-headline-color: #dde2f3;");
    expect(getContrastRatio("#dde2f3", "#0d1426")).toBeGreaterThanOrEqual(AA_CONTRAST_RATIO);
  });

  test("an explicit dark value wins over the derived one", () => {
    addCustomThemeToDom({
      styling: getBaseWorkspaceStyling({
        brandColor: { light: "#1e40af" },
        cardBackgroundColor: { light: "#ffffff", dark: "#222222" },
      }),
    });
    expect(getDarkBlock()).toContain("--fb-survey-background-color: #222222;");
  });

  test("brand and button colors keep their light value in dark (D12)", () => {
    addCustomThemeToDom({
      styling: getBaseWorkspaceStyling({
        brandColor: { light: "#146a5d" },
        buttonBgColor: { light: "#146a5d" },
        buttonTextColor: { light: "#ffffff" },
      }),
    });
    const dark = getDarkBlock();
    expect(dark).toContain("--fb-brand-color: #146a5d;");
    expect(dark).toContain("--fb-button-bg-color: #146a5d;");
    expect(dark).toContain("--fb-button-text-color: #ffffff;");
  });

  test("dark hover and selected states lighten instead of darken", () => {
    addCustomThemeToDom({ styling: getBaseWorkspaceStyling({ brandColor: { light: "#1e40af" } }) });
    expect(getDarkBlock()).toContain("--fb-shade-color: white;");
  });

  test("brand text, focus ring and error text get readable variants while fills keep the brand", () => {
    addCustomThemeToDom({ styling: getBaseWorkspaceStyling({ brandColor: { light: "#1e40af" } }) });
    const dark = getDarkBlock();
    expect(dark).toContain("--fb-brand-color: #1e40af;");
    expect(dark).toContain("--fb-brand-readable-color: #788ccf;");
    expect(dark).toContain("--fb-focus-ring-outer-color: #4b66bf;");
    expect(dark).toContain("--destructive: #ee4d54;");
  });

  test("survey-ui base tokens follow a dark card override", () => {
    addCustomThemeToDom({
      styling: getBaseWorkspaceStyling({
        brandColor: { light: "#1e40af" },
        cardBackgroundColor: { light: "#ffffff", dark: "#222222" },
      }),
    });
    const dark = getDarkBlock();
    expect(dark).toContain("--background: #222222;");
    expect(dark).toContain("--card: #222222;");
    expect(dark).toMatch(/--popover: #[0-9a-f]{6};/);
  });

  test("keeps a customer's input shadow in dark and drops only the default", () => {
    addCustomThemeToDom({
      styling: getBaseWorkspaceStyling({
        brandColor: { light: "#1e40af" },
        inputShadow: DEFAULT_INPUT_SHADOW,
      }),
    });
    expect(getDarkBlock()).toContain("--fb-input-shadow: none;");
    addCustomThemeToDom({
      styling: getBaseWorkspaceStyling({
        brandColor: { light: "#1e40af" },
        inputShadow: "inset 0 0 0 1px #000000",
      }),
    });
    expect(getDarkBlock()).toContain("--fb-input-shadow: inset 0 0 0 1px #000000;");
  });

  test("the Back button is readable in dark without a button color (automatic palette)", () => {
    addCustomThemeToDom({ styling: getBaseWorkspaceStyling({}) });
    const back = /--fb-back-button-color: (#[0-9a-f]{6});/.exec(getDarkBlock())?.[1];
    expect(back).toBeDefined();
    expect(getContrastRatio(back ?? "", "#0d1426")).toBeGreaterThanOrEqual(AA_CONTRAST_RATIO);
  });

  test("an unparseable dark value does not throw", () => {
    const styling = getBaseWorkspaceStyling({
      brandColor: { light: "#146a5d" },
      buttonBgColor: { light: "#146a5d", dark: "not-a-color" },
    });
    expect(() => addCustomThemeToDom({ styling })).not.toThrow();
    expect(getDarkBlock()).toContain("--fb-button-bg-color: #146a5d;");
  });
});
