import { type Page, expect } from "@playwright/test";
import { test } from "./lib/fixtures";
import { clickUntilExpanded } from "./utils/click-until";
import { createSurveyFromScratch, publishSurvey } from "./utils/helper";

test.describe("Survey Styling", async () => {
  // Shared Helpers
  // Opens a styling section by its expanded state (clicked until it says it is open, see `clickUntil`).
  const openAccordion = (page: Page, name: string) =>
    clickUntilExpanded(
      page.locator("[aria-expanded]").filter({ hasText: name }).locator("visible=true").last()
    );

  const setColor = async (page: Page, label: string, hex: string) => {
    const labelEl = page.locator("label").filter({ hasText: label }).locator("visible=true").last();
    const container = labelEl.locator("..");
    await container.getByRole("textbox").fill(hex.replace("#", ""));
    await container.getByRole("textbox").blur();
  };

  const setDimension = async (page: Page, label: string, value: string) => {
    const labelEl = page.locator("label").filter({ hasText: label }).locator("visible=true").last();
    const container = labelEl.locator("..");
    // Try finding number input, fallback to simple fill if it's text
    const input = container.locator('input[type="number"], input[type="text"]').first();
    await input.fill(value);
    await input.blur();
  };

  test("Global Theme Styling (Workspace Level)", async ({ page, users }) => {
    const user = await users.create();
    await user.login();

    // Navigate to Appearance settings
    await page.waitForURL(/\/workspaces\/[^/]+\/surveys/);
    const workspaceId = /\/workspaces\/([^/]+)\//.exec(page.url())?.[1];
    await page.goto(`/workspaces/${workspaceId}/settings/workspace/look`);
    await page.waitForURL(/\/workspaces\/[^/]+\/settings\/workspace\/look/);

    // Toggle "Allow survey-level styling"
    const addCustomStyles = page.getByLabel("Allow survey-level styling");
    if (!(await addCustomStyles.isChecked())) {
      await addCustomStyles.click();
    }

    // --- Survey styling ---
    await openAccordion(page, "Survey styling");

    // 1. Headlines & Descriptions
    await openAccordion(page, "Headlines & Descriptions");
    await setColor(page, "Headline Color", "aa0000"); // Red-ish
    await setColor(page, "Description Color", "00aa00"); // Green-ish
    await setDimension(page, "Headline Font Size", "24");
    await setDimension(page, "Description Font Size", "18");
    await setDimension(page, "Headline Font Weight", "700");
    await setColor(page, "Label Color", "0000aa"); // Blue-ish
    await setDimension(page, "Label Font Size", "14");
    await setDimension(page, "Label Font Weight", "600");

    // Verify Typography Variables
    await page.waitForTimeout(1000);
    let css = await page.evaluate(() => document.getElementById("formbricks__css__custom")?.innerHTML);
    expect(css).toContain("--fb-element-headline-color: #aa0000");
    expect(css).toContain("--fb-element-description-color: #00aa00");
    expect(css).toContain("--fb-element-headline-font-size: 24px");
    expect(css).toContain("--fb-element-description-font-size: 18px");
    expect(css).toContain("--fb-element-headline-font-weight: 700");
    expect(css).toContain("--fb-element-upper-label-color: #0000aa");
    expect(css).toContain("--fb-element-upper-label-font-size: 14px");
    expect(css).toContain("--fb-element-upper-label-font-weight: 600");

    // 2. Inputs
    await openAccordion(page, "Inputs");
    await setColor(page, "Input color", "eeeeee");
    await setColor(page, "Input border color", "cccccc");
    await setColor(page, "Input Text", "024eff");
    await setDimension(page, "Border Radius", "5");
    await setDimension(page, "Height", "50");
    await setDimension(page, "Font Size", "16");
    await setDimension(page, "Padding X", "20");
    await setDimension(page, "Padding Y", "10");
    await setDimension(page, "Placeholder Opacity", "0.8");
    // Shadow is a text input – use setDimension helper (label→parent→input)
    // because getByLabel relies on for/id linkage which FormLabel doesn't guarantee
    await setDimension(page, "Shadow", "0 4px 6px -1px rgba(0, 0, 0, 0.1)");

    await page.waitForTimeout(1000);
    css = await page.evaluate(() => document.getElementById("formbricks__css__custom")?.innerHTML);
    expect(css).toContain("--fb-input-background-color: #eeeeee");
    expect(css).toContain("--fb-input-border-color: #cccccc");
    expect(css).toContain("--fb-input-text-color: #024eff");
    expect(css).toContain("--fb-input-placeholder-color:");
    expect(css).toContain("--fb-input-border-radius: 5px");
    expect(css).toContain("--fb-input-height: 50px");
    expect(css).toContain("--fb-input-font-size: 16px");
    expect(css).toContain("--fb-input-padding-x: 20px");
    expect(css).toContain("--fb-input-padding-y: 10px");
    expect(css).toContain("--fb-input-placeholder-opacity: 0.8");
    expect(css).toContain("--fb-input-shadow: 0 4px 6px -1px rgba(0, 0, 0, 0.1)");

    // 3. Buttons
    await openAccordion(page, "Buttons");
    await setColor(page, "Button Background", "ff00ff");
    await setColor(page, "Button Text", "ffffff");
    await setDimension(page, "Border Radius", "12");
    await setDimension(page, "Height", "48");
    await setDimension(page, "Font Size", "18");
    await setDimension(page, "Font Weight", "600");
    await setDimension(page, "Padding X", "24");
    await setDimension(page, "Padding Y", "12");

    await page.waitForTimeout(1000);
    // Partial verification for Buttons to ensure state is good before moving to Options
    css = await page.evaluate(() => document.getElementById("formbricks__css__custom")?.innerHTML);
    expect(css).toContain("--fb-button-bg-color: #ff00ff");
    expect(css).toContain("--fb-button-text-color: #ffffff");
    expect(css).toContain("--fb-button-border-radius: 12px");
    expect(css).toContain("--fb-button-height: 48px");
    expect(css).toContain("--fb-button-font-size: 18px");
    expect(css).toContain("--fb-button-font-weight: 600");
    expect(css).toContain("--fb-button-padding-x: 24px");
    expect(css).toContain("--fb-button-padding-y: 12px");

    // 4. Options (Radio/Checkbox)
    await openAccordion(page, "Options (Radio/Checkbox)");
    await setColor(page, "Background", "dddddd");
    await setColor(page, "Label Color", "111111");
    await setColor(page, "Border Color", "999999");
    // Note: Border Radius is reused, but we can set it here to be sure
    await setDimension(page, "Border Radius", "6");
    await setDimension(page, "Padding X", "12");
    await setDimension(page, "Padding Y", "8");
    await setDimension(page, "Font Size", "15");

    await page.waitForTimeout(1000);
    css = await page.evaluate(() => document.getElementById("formbricks__css__custom")?.innerHTML);
    expect(css).toContain("--fb-option-bg-color: #dddddd");
    expect(css).toContain("--fb-option-label-color: #111111");
    expect(css).toContain("--fb-option-border-color: #999999");
    expect(css).toContain("--fb-option-border-radius: 6px");
    expect(css).toContain("--fb-option-padding-x: 12px");
    expect(css).toContain("--fb-option-padding-y: 8px");
    expect(css).toContain("--fb-option-font-size: 15px");

    // --- Card styling ---
    await openAccordion(page, "Card styling");
    // Toggle progress bar off/on to ensure visibility
    const hideProgressSwitch = page.locator("#hideProgressBar");
    if ((await hideProgressSwitch.getAttribute("aria-checked")) === "true") {
      await hideProgressSwitch.click();
    }
    await setDimension(page, "Track Height", "15");
    await setDimension(page, "Roundness", "20");

    await page.waitForTimeout(1000);
    css = await page.evaluate(() => document.getElementById("formbricks__css__custom")?.innerHTML);
    expect(css).toContain("--fb-progress-track-height: 15px");
    expect(css).toContain("--fb-progress-track-border-radius: 20px");

    await test.step("Dark tab edits dark overrides and previews the app survey in dark", async () => {
      await page.locator('label[for="styling-appearance-dark"]').click();
      await expect(page.locator("#fbjs").first()).toHaveAttribute("data-appearance", "dark");

      await setColor(page, "Card background color", "1b1530");
      await expect
        .poll(() => page.evaluate(() => document.getElementById("formbricks__css__custom")?.innerHTML))
        .toContain("--fb-survey-background-color: #1b1530");
      css = await page.evaluate(() => document.getElementById("formbricks__css__custom")?.innerHTML);
      const darkBlock = css?.split('#fbjs[data-appearance="dark"]')[1] ?? "";
      expect(darkBlock).toContain("--fb-survey-background-color: #1b1530");
      // The light palette is untouched by editing dark: the edited field keeps its light value.
      const lightBlock = css?.split('#fbjs[data-appearance="dark"]')[0] ?? "";
      expect(lightBlock).not.toContain("#1b1530");
      expect(css).toContain("--fb-option-bg-color: #dddddd");

      await page.getByRole("button", { name: "Save", exact: true }).first().click();
      await expect(page.getByText("Styling updated successfully")).toBeVisible();
      await page.reload();
      await page.locator('label[for="styling-appearance-dark"]').click();
      await openAccordion(page, "Card styling");
      const cardBackground = page
        .locator("label")
        .filter({ hasText: "Card background color" })
        .locator("visible=true")
        .last()
        .locator("..")
        .getByRole("textbox");
      await expect(cardBackground).toHaveValue("1b1530");
      // The saved override reaches the preview after a reload, and "Use automatic color" returns to derived.
      await expect
        .poll(() => page.evaluate(() => document.getElementById("formbricks__css__custom")?.innerHTML))
        .toContain("--fb-survey-background-color: #1b1530");
      await page.getByRole("button", { name: "Use automatic color" }).first().click();
      await expect(cardBackground).toHaveValue("");
      await expect
        .poll(() => page.evaluate(() => document.getElementById("formbricks__css__custom")?.innerHTML))
        .not.toContain("--fb-survey-background-color: #1b1530");
    });
  });

  test("Suggest Colors derives all colors from brand color without changing non-color properties", async ({
    page,
    users,
  }) => {
    const user = await users.create();
    await user.login();

    // Navigate to Appearance settings
    await page.waitForURL(/\/workspaces\/[^/]+\/surveys/);
    const workspaceId = /\/workspaces\/([^/]+)\//.exec(page.url())?.[1];
    await page.goto(`/workspaces/${workspaceId}/settings/workspace/look`);
    await page.waitForURL(/\/workspaces\/[^/]+\/settings\/workspace\/look/);

    // Toggle "Allow survey-level styling"
    const addCustomStyles = page.getByLabel("Allow survey-level styling");
    if (!(await addCustomStyles.isChecked())) {
      await addCustomStyles.click();
    }

    // Set some non-color properties BEFORE suggesting colors, so we can verify they aren't overwritten
    await openAccordion(page, "Survey styling");
    await openAccordion(page, "Inputs");
    await setDimension(page, "Border Radius", "12");
    await setDimension(page, "Padding Y", "20");

    await openAccordion(page, "Options (Radio/Checkbox)");
    await setDimension(page, "Border Radius", "10");
    await setDimension(page, "Padding Y", "14");

    await page.waitForTimeout(1000);

    // Verify non-color CSS vars are set before suggesting
    let cssBefore = await page.evaluate(() => document.getElementById("formbricks__css__custom")?.innerHTML);
    expect(cssBefore).toContain("--fb-input-border-radius: 12px");
    expect(cssBefore).toContain("--fb-input-padding-y: 20px");
    expect(cssBefore).toContain("--fb-option-border-radius: 10px");
    expect(cssBefore).toContain("--fb-option-padding-y: 14px");

    // Set a distinctive brand color (rose-600)
    await setColor(page, "Brand color", "e11d48");
    await page.waitForTimeout(500);

    // Click "Suggest colors" button (the top-level one, not the disabled one inside FormStylingSettings)
    await page.getByRole("button", { name: "Suggest colors" }).first().click();

    // Confirm the dialog
    await page.getByRole("button", { name: "Generate" }).click();

    // Wait for the preview to update with derived colors
    await page.waitForTimeout(1500);

    const css = await page.evaluate(() => document.getElementById("formbricks__css__custom")?.innerHTML);
    expect(css).toBeDefined();

    // --- Verify colors ARE derived from brand (not hardcoded) ---

    // Brand color itself should be present
    expect(css).toContain("--fb-brand-color: #e11d48");

    // Input background should be brand-tinted, NOT hardcoded #ffffff
    expect(css).toContain("--fb-input-background-color:");
    expect(css).not.toContain("--fb-input-background-color: #ffffff");

    // Card/survey background should always be white
    expect(css).toContain("--fb-survey-background-color: #ffffff");

    // Question/heading color should be derived, NOT the old hardcoded #2b2524
    expect(css).toContain("--fb-heading-color:");
    expect(css).not.toContain("--fb-heading-color: #2b2524");

    // Input text color should be derived, NOT hardcoded #0f172a
    expect(css).toContain("--fb-input-text-color:");
    expect(css).not.toContain("--fb-input-text-color: #0f172a");

    // Option label color should be derived, NOT hardcoded #0f172a
    expect(css).toContain("--fb-option-label-color:");
    expect(css).not.toContain("--fb-option-label-color: #0f172a");

    // Option background should be brand-tinted (same as input bg)
    expect(css).toContain("--fb-option-bg-color:");
    expect(css).not.toContain("--fb-option-bg-color: #ffffff");

    // --- Verify non-color properties were NOT changed ---
    expect(css).toContain("--fb-input-border-radius: 12px");
    expect(css).toContain("--fb-input-padding-y: 20px");
    expect(css).toContain("--fb-option-border-radius: 10px");
    expect(css).toContain("--fb-option-padding-y: 14px");
  });

  test("Initial load derives button, progress, input, and option colors from brand color", async ({
    page,
    users,
  }) => {
    const user = await users.create();
    await user.login();

    // Navigate to Appearance settings
    await page.waitForURL(/\/workspaces\/[^/]+\/surveys/);
    const workspaceId = /\/workspaces\/([^/]+)\//.exec(page.url())?.[1];
    await page.goto(`/workspaces/${workspaceId}/settings/workspace/look`);
    await page.waitForURL(/\/workspaces\/[^/]+\/settings\/workspace\/look/);

    // Toggle "Allow survey-level styling"
    const addCustomStyles = page.getByLabel("Allow survey-level styling");
    if (!(await addCustomStyles.isChecked())) {
      await addCustomStyles.click();
    }

    // Wait for the preview to render with default styling
    await page.waitForTimeout(1500);

    const css = await page.evaluate(() => document.getElementById("formbricks__css__custom")?.innerHTML);
    expect(css).toBeDefined();

    // On initial load (no saved styling), button and progress bar should derive from brand color (#1e40af)
    // NOT from the old hardcoded dark navy (#0f172a)
    expect(css).not.toContain("--fb-button-bg-color: #0f172a");
    expect(css).not.toContain("--fb-progress-indicator-bg-color: #0f172a");

    // Input text and option label CSS variables are only emitted when
    // the user explicitly sets them, so on initial load they should NOT
    // be present at all (the CSS-variable fallback from globals.css applies).
    // Verify they are not set to the old hardcoded dark navy value.
    expect(css).not.toContain("--fb-input-text-color: #0f172a");
    expect(css).not.toContain("--fb-option-label-color: #0f172a");

    // Option background should be brand-tinted, not plain white
    expect(css).toContain("--fb-option-bg-color:");

    // Headline color should be brand-derived
    expect(css).toContain("--fb-element-headline-color:");
  });

  test("Survey Specific Styling (Survey Editor Override)", async ({ page, users }) => {
    const user = await users.create();
    await user.login();
    await page.waitForURL(/\/workspaces\/[^/]+\/surveys/);

    await createSurveyFromScratch(page);

    // Ensure Welcome Card is active so we can see it
    await page.locator("p", { hasText: "Welcome card" }).first().click({ force: true });
    await page.waitForTimeout(1000);

    // Check toggle state for Welcome Card
    const welcomeToggle = page.locator("#welcome-toggle");
    if ((await welcomeToggle.getAttribute("data-state")) === "unchecked") {
      await welcomeToggle.click();
    }

    // Set Headline Text so we can verify it
    // The selector needs to be robust.
    // We try looking for the input labeled "Note" or aria-labelledby="headline"
    const headlineInput = page.locator('[aria-labelledby="headline"]').first();
    await expect(headlineInput).toBeVisible();
    await headlineInput.fill("My Custom Headline");
    await headlineInput.blur(); // Trigger save
    await expect(headlineInput).toHaveText("My Custom Headline");

    // Navigate to Styling tab
    await page.getByRole("button", { name: "Styling" }).click();

    // Toggle "Use custom styling for this survey" (Survey override)
    const addCustomStyles = page
      .locator("label")
      .filter({ hasText: /custom styling|custom styles/i })
      .first();
    const toggleInput = page.locator(`button#${await addCustomStyles.getAttribute("for")}`);
    // If we can't find by ID, try clicking the label or simple check
    if (!(await toggleInput.isChecked())) {
      await addCustomStyles.click();
    }

    // Apply Overrides
    await openAccordion(page, "Survey styling");
    await openAccordion(page, "Headlines & Descriptions");

    // Override Headline Color (Blue)
    await setColor(page, "Headline Color", "0000ff");
    await setDimension(page, "Headline Font Size", "30");

    // Wait for Preview Update
    await page.waitForTimeout(2000);

    // Click Restart to be sure we are on welcome card
    await page.getByRole("button", { name: "Restart" }).click();
    await page.waitForTimeout(1000);

    // Check Preview Computed Styles
    const headlinePreview = page.locator("#fbjs .label-headline").first();
    await expect(headlinePreview).toBeVisible();
    await expect(headlinePreview).toHaveText("My Custom Headline");

    // Verify color override applied (computed style)
    await expect(headlinePreview).toHaveCSS("color", "rgb(0, 0, 255)"); // Blue

    // The editor's headline size reaches the welcome card headline too (ENG-3554 headline fix).
    await expect(headlinePreview).toHaveCSS("font-size", "30px");
    const editorCss = await page.evaluate(
      () => document.getElementById("formbricks__css__custom")?.innerHTML
    );
    expect(editorCss).toContain("--fb-element-headline-font-size: 30px");
  });

  // ENG-3553. A synthetic CMS-style stylesheet (M2.11): its `@import` is removed (processor unit tests
  // cover the removal and its warning) without blocking the rest, the supported `data-fb-part` hooks
  // style the preview and the published survey, and survey CSS wins over workspace CSS. Self-hosted in
  // E2E, so there is no plan gate.
  test("Custom CSS: workspace and survey CSS reach the link survey", async ({ page, users }) => {
    const user = await users.create();
    await user.login();
    await page.waitForURL(/\/workspaces\/[^/]+\/surveys/);
    const workspaceId = /\/workspaces\/([^/]+)\//.exec(page.url())?.[1];

    const workspaceCss = [
      '@import url("https://fonts.googleapis.com/css2?family=Inter:wght@400;600&display=swap");',
      ":root { --acme-ink: #10283a; --acme-brand: #1f5f8b; }",
      '[data-fb-part="headline"] { color: var(--acme-ink); font-weight: 600; }',
      '[data-fb-part="button-primary"] { background: var(--acme-brand); border-radius: 999px; }',
    ].join("\n");
    const surveyCss = '[data-fb-part="button-primary"] { background-color: rgb(200, 30, 60); }';
    const ink = "rgb(16, 40, 58)";
    const surveyButton = "rgb(200, 30, 60)";

    await test.step("Workspace CSS styles the preview despite the removed @import", async () => {
      await page.goto(`/workspaces/${workspaceId}/settings/workspace/look`);
      await page.getByRole("button", { name: /^Custom CSS/ }).click();
      await page.getByLabel("Base CSS", { exact: true }).fill(workspaceCss);

      // The preview renders only CSS the server validated, so this also waits for validation.
      await expect(page.locator('#fbjs [data-fb-part="headline"]').first()).toHaveCSS("color", ink);
    });

    await test.step("The page's one Save stores the workspace CSS with the theme", async () => {
      await page
        .locator("form")
        .filter({ has: page.getByRole("button", { name: /^Custom CSS/ }) })
        .getByRole("button", { name: "Save", exact: true })
        .click();
      // The removed @import is counted in the page's one confirmation.
      await expect(page.getByText("Styling saved. 1 CSS rule was removed.")).toBeVisible();

      await page.reload();
      await page.getByRole("button", { name: /^Custom CSS/ }).click();
      await expect(page.getByLabel("Base CSS", { exact: true })).toHaveValue(workspaceCss);
    });

    let surveyId = "";
    await test.step("The survey inherits workspace CSS and adds its own", async () => {
      await page.goto(`/workspaces/${workspaceId}/surveys`);
      surveyId = await createSurveyFromScratch(page);
      await page.getByRole("button", { name: "Styling" }).click();
      // Survey CSS follows the survey's other style overrides (ENG-3723).
      const addCustomStyles = page.getByRole("switch", { name: "Use custom styling for this survey" });
      await addCustomStyles.click();
      await page.getByRole("button", { name: /^Custom CSS/ }).click();
      await page.getByLabel("Survey base CSS", { exact: true }).fill(surveyCss);

      const preview = page.locator("#fbjs");
      const previewButton = preview.locator('[data-fb-part="button-primary"]').first();
      await expect(previewButton).toHaveCSS("background-color", surveyButton);
      await expect(preview.locator('[data-fb-part="headline"]').first()).toHaveCSS("color", ink);

      // Off, the survey's CSS stops applying while the workspace CSS stays; on again, it is back.
      await addCustomStyles.click();
      await expect(previewButton).not.toHaveCSS("background-color", surveyButton);
      await expect(preview.locator('[data-fb-part="headline"]').first()).toHaveCSS("color", ink);
      await addCustomStyles.click();
      await expect(previewButton).toHaveCSS("background-color", surveyButton);
    });

    await test.step("Respondents get both, with the survey CSS winning", async () => {
      await publishSurvey(page);
      await page.goto(`/s/${surveyId}`);

      await expect(page.locator('[data-fb-part="headline"]').first()).toHaveCSS("color", ink);
      await expect(page.locator('[data-fb-part="button-primary"]').first()).toHaveCSS(
        "background-color",
        surveyButton
      );
    });
  });
});
