import React, { useEffect, useState } from "react";
import "@testing-library/jest-dom";
import { fireEvent, render, screen, waitFor } from "@testing-library/react";

import { clearAuthClientState, setAuthUserKey } from "./auth";
import JobTrackerThemeProvider from "./ThemeProvider";
import { THEME_BOOTSTRAP_SCRIPT } from "./themeBootstrap";
import {
  getThemeModePref,
  resolveEffectiveThemeMode,
  setThemeModePref,
  subscribeToThemePreferenceChanges,
  THEME_MODE_KEY,
} from "./themePrefs";

function setSystemDark(matches: boolean) {
  Object.defineProperty(window, "matchMedia", {
    configurable: true,
    value: jest.fn().mockImplementation(() => ({
      matches,
      media: "(prefers-color-scheme: dark)",
      onchange: null,
      addEventListener: jest.fn(),
      removeEventListener: jest.fn(),
      addListener: jest.fn(),
      removeListener: jest.fn(),
      dispatchEvent: jest.fn(),
    })),
  });
}

describe("deterministic theme state", () => {
  beforeEach(() => {
    window.localStorage.clear();
    document.documentElement.removeAttribute("data-color-scheme");
    document.documentElement.style.colorScheme = "";
    setSystemDark(false);
  });

  it("uses one browser preference and falls back to System", () => {
    expect(getThemeModePref()).toBe("system");
    setThemeModePref("light");
    expect(getThemeModePref()).toBe("light");

    setAuthUserKey("user-a", false);
    expect(getThemeModePref()).toBe("light");
    setThemeModePref("dark");
    expect(getThemeModePref()).toBe("dark");

    setAuthUserKey("user-b", false);
    expect(getThemeModePref()).toBe("dark");
    window.localStorage.removeItem(THEME_MODE_KEY);
    expect(getThemeModePref()).toBe("system");
  });

  it("does not change theme on login or logout", () => {
    window.localStorage.setItem("themeMode:anon", "light");
    window.localStorage.setItem("themeMode:user-a", "dark");
    const observed: string[] = [];
    const unsubscribe = subscribeToThemePreferenceChanges(() => observed.push(getThemeModePref()));

    setAuthUserKey("user-a", false);
    clearAuthClientState(false);

    expect(observed).toEqual([]);
    expect(getThemeModePref()).toBe("light");
    unsubscribe();
  });

  it("synchronizes only the canonical cross-tab preference", () => {
    setAuthUserKey("user-a", false);
    const sync = jest.fn();
    const unsubscribe = subscribeToThemePreferenceChanges(sync);

    window.dispatchEvent(new StorageEvent("storage", { key: "themeMode:user-a", newValue: "dark" }));
    window.dispatchEvent(new StorageEvent("storage", { key: "unrelated", newValue: "value" }));
    window.dispatchEvent(new StorageEvent("storage", { key: "authUserKey", newValue: "user-b" }));
    window.dispatchEvent(new StorageEvent("storage", { key: THEME_MODE_KEY, newValue: "dark" }));

    expect(sync).toHaveBeenCalledTimes(1);
    unsubscribe();
  });

  it("migrates the previous account-scoped preference", () => {
    setAuthUserKey("user-a", false);
    window.localStorage.setItem("themeMode:user-a", "light");

    expect(getThemeModePref()).toBe("light");
    expect(window.localStorage.getItem(THEME_MODE_KEY)).toBe("light");
  });

  it("uses system preference only for System mode", () => {
    expect(resolveEffectiveThemeMode("light", true)).toBe("light");
    expect(resolveEffectiveThemeMode("dark", false)).toBe("dark");
    expect(resolveEffectiveThemeMode("system", true)).toBe("dark");
    expect(resolveEffectiveThemeMode("system", false)).toBe("light");
  });

  it("applies the saved preference before the client application renders", () => {
    window.localStorage.setItem(THEME_MODE_KEY, "light");
    setSystemDark(true);

    new Function(THEME_BOOTSTRAP_SCRIPT)();

    expect(document.documentElement).toHaveAttribute("data-color-scheme", "light");
    expect(document.documentElement.style.colorScheme).toBe("light");
  });

  it("changes color scheme without remounting application state", async () => {
    let mounts = 0;
    function StatefulProbe() {
      const [count, setCount] = useState(0);
      useEffect(() => { mounts += 1; }, []);
      return <button onClick={() => setCount((value) => value + 1)}>Count {count}</button>;
    }

    const view = render(
      <JobTrackerThemeProvider preference="light" systemPrefersDark>
        <StatefulProbe />
      </JobTrackerThemeProvider>,
    );
    fireEvent.click(screen.getByRole("button", { name: "Count 0" }));

    view.rerender(
      <JobTrackerThemeProvider preference="dark" systemPrefersDark={false}>
        <StatefulProbe />
      </JobTrackerThemeProvider>,
    );

    await waitFor(() => expect(document.documentElement).toHaveAttribute("data-color-scheme", "dark"));
    expect(screen.getByRole("button", { name: "Count 1" })).toBeInTheDocument();
    expect(mounts).toBe(1);
  });
});
