import { describe, it, expect, beforeEach } from "vitest";
import { useUiStore } from "./uiStore";

beforeEach(() => useUiStore.setState({ searchQuery: "", activeFolderId: null, activeTagIds: [], activeDbTypes: [], activeEnvironment: null, activeView: "home", prefilledConnectionString: null, activeConnectionId: null, settingsReturnView: null }));

describe("uiStore", () => {
  it("starts on home view", () => expect(useUiStore.getState().activeView).toBe("home"));
  it("setActiveView changes view", () => {
    useUiStore.getState().setActiveView("settings");
    expect(useUiStore.getState().activeView).toBe("settings");
  });
  it("setSearchQuery updates query", () => {
    useUiStore.getState().setSearchQuery("prod");
    expect(useUiStore.getState().searchQuery).toBe("prod");
  });
  it("toggleTag adds and removes tag", () => {
    useUiStore.getState().toggleTag("t1");
    expect(useUiStore.getState().activeTagIds).toEqual(["t1"]);
    useUiStore.getState().toggleTag("t1");
    expect(useUiStore.getState().activeTagIds).toEqual([]);
  });
  it("toggleDbType adds and removes type", () => {
    useUiStore.getState().toggleDbType("redis");
    expect(useUiStore.getState().activeDbTypes).toEqual(["redis"]);
    useUiStore.getState().toggleDbType("redis");
    expect(useUiStore.getState().activeDbTypes).toEqual([]);
  });
  it("clearFilters resets search and filters but not view", () => {
    useUiStore.getState().setSearchQuery("x");
    useUiStore.getState().toggleTag("t1");
    useUiStore.getState().setActiveView("settings");
    useUiStore.getState().clearFilters();
    expect(useUiStore.getState().searchQuery).toBe("");
    expect(useUiStore.getState().activeTagIds).toEqual([]);
    expect(useUiStore.getState().activeView).toBe("settings");
  });
  it("sets and clears prefilled connection string", () => {
    useUiStore.getState().setPrefilledConnectionString("postgresql://a@b/c");
    expect(useUiStore.getState().prefilledConnectionString).toBe("postgresql://a@b/c");
    useUiStore.getState().clearPrefilledConnectionString();
    expect(useUiStore.getState().prefilledConnectionString).toBeNull();
  });

  it("setEnvironment sets activeEnvironment", () => {
    useUiStore.getState().setEnvironment("production");
    expect(useUiStore.getState().activeEnvironment).toBe("production");
  });

  it("clearFilters resets activeEnvironment", () => {
    useUiStore.getState().setEnvironment("staging");
    useUiStore.getState().clearFilters();
    expect(useUiStore.getState().activeEnvironment).toBeNull();
  });

  it("sets and clears activeConnectionId", () => {
    useUiStore.getState().setActiveConnectionId("c1");
    expect(useUiStore.getState().activeConnectionId).toBe("c1");
    useUiStore.getState().setActiveConnectionId(null);
    expect(useUiStore.getState().activeConnectionId).toBeNull();
  });

  it("openSettings from home records home and closeSettings returns to home", () => {
    useUiStore.getState().openSettings();
    expect(useUiStore.getState().settingsReturnView).toBe("home");
    expect(useUiStore.getState().activeView).toBe("settings");
    useUiStore.getState().closeSettings();
    expect(useUiStore.getState().activeView).toBe("home");
    expect(useUiStore.getState().settingsReturnView).toBeNull();
  });

  it("openSettings from db-viewer records db-viewer and closeSettings returns to it", () => {
    useUiStore.getState().setActiveView("db-viewer");
    useUiStore.getState().openSettings();
    expect(useUiStore.getState().settingsReturnView).toBe("db-viewer");
    expect(useUiStore.getState().activeView).toBe("settings");
    useUiStore.getState().closeSettings();
    expect(useUiStore.getState().activeView).toBe("db-viewer");
    expect(useUiStore.getState().settingsReturnView).toBeNull();
  });

  it("double openSettings keeps the original return view", () => {
    useUiStore.getState().setActiveView("db-viewer");
    useUiStore.getState().openSettings();
    useUiStore.getState().openSettings();
    expect(useUiStore.getState().settingsReturnView).toBe("db-viewer");
    expect(useUiStore.getState().activeView).toBe("settings");
    useUiStore.getState().closeSettings();
    expect(useUiStore.getState().activeView).toBe("db-viewer");
  });

  it("closeSettings falls back to home when no return view is recorded", () => {
    useUiStore.getState().setActiveView("settings");
    useUiStore.getState().closeSettings();
    expect(useUiStore.getState().activeView).toBe("home");
    expect(useUiStore.getState().settingsReturnView).toBeNull();
  });
});
