import { test, expect, type Page } from "@playwright/test";
import { openApp, gotoPage } from "./helpers";

// Locks the Settings AUTO-SAVE contract (the draft + Save button + leave-guard were removed): an
// Appearance change persists on its own via PUT /me/preferences and a transient indicator confirms
// it; leaving Settings after a change is no longer guarded.
test.describe("settings auto-save", () => {
  const TOGGLE = "Performance mode"; // an Appearance switch (locale pinned to en)

  // The save is debounced (~500ms), so wait for the PUT rather than assuming it's synchronous.
  const putPrefs = (page: Page) =>
    page.waitForResponse(
      (r) => r.url().includes("/me/preferences") && r.request().method() === "PUT",
    );

  test("changing a setting auto-saves and shows the saved indicator", async ({ page }) => {
    await openApp(page);
    await gotoPage(page, "settings");

    const toggle = page.getByRole("switch", { name: TOGGLE });
    const indicator = page.getByTestId("settings-save-indicator");

    // Idle: nothing to save, so no indicator (and no draft Save bar).
    await expect(indicator).toHaveCount(0);

    // Change it → the PUT fires on its own (no Save click) and the indicator confirms it.
    const [saved] = await Promise.all([putPrefs(page), toggle.click()]);
    expect(saved.ok()).toBeTruthy();
    await expect(indicator).toHaveAttribute("data-state", "saved");

    // Restore the baseline so the shared account stays clean.
    await Promise.all([putPrefs(page), toggle.click()]);
  });

  test("a changed setting no longer blocks leaving the page", async ({ page }) => {
    await openApp(page);
    await gotoPage(page, "settings");

    const toggle = page.getByRole("switch", { name: TOGGLE });
    await Promise.all([putPrefs(page), toggle.click()]);

    // No unsaved-changes prompt — navigating away just lands on the feed.
    await page.getByTestId("nav-feed").click();
    await expect(page.getByTestId("nav-feed")).toHaveAttribute("aria-current", "page");
    await expect(page.getByTestId("video-card").first()).toBeVisible();

    // Restore the baseline (back on Settings).
    await gotoPage(page, "settings");
    await Promise.all([putPrefs(page), page.getByRole("switch", { name: TOGGLE }).click()]);
  });
});
