import { expect, test } from "vitest";
import { appReducer, initialAppState } from "./reducer";
import { CleanConfig } from "./types";

const mockConfig: CleanConfig = {
  strip_utms: false,
  fix_quotes: false,
  fix_newlines: false,
  play_sound: false,
  autostart: true,
  hotkey: "Ctrl+Shift+D",
  custom_params: [],
};

test("INITIALIZE action when licensed", () => {
  const result = appReducer(initialAppState, {
    type: "INITIALIZE",
    payload: {
      config: mockConfig,
      hotkeyConflict: true,
      hwid: "device-123",
      isLicensed: true,
    },
  });

  expect(result.isLicensed).toBe(true);
  expect(result.hwid).toBe("device-123");
  expect(result.config.hotkey).toBe("Ctrl+Shift+D");
  expect(result.tabState.tab).toBe("general");
});

test("INITIALIZE action when unlicensed", () => {
  const result = appReducer(initialAppState, {
    type: "INITIALIZE",
    payload: {
      config: mockConfig,
      hotkeyConflict: false,
      hwid: "device-123",
      isLicensed: false,
    },
  });

  expect(result.isLicensed).toBe(false);
  expect(result.tabState.tab).toBe("license");

  if (result.tabState.tab === "license") {
    expect(result.tabState.keyInput).toBe("");
  }
});

test("SET_TAB transition", () => {
  const licensedState = appReducer(initialAppState, {
    type: "INITIALIZE",
    payload: {
      config: mockConfig,
      hotkeyConflict: false,
      hwid: "",
      isLicensed: true,
    },
  });

  const state = appReducer(licensedState, {
    type: "SET_TAB",
    payload: "rules",
  });

  expect(state.tabState.tab).toBe("rules");
});

test("SET_CONFIG partial update", () => {
  const state = appReducer(initialAppState, {
    type: "SET_CONFIG",
    payload: { play_sound: false },
  });

  expect(state.config.play_sound).toBe(false);
});

test("SET_RECORDING on general tab", () => {
  let state = appReducer(initialAppState, {
    type: "INITIALIZE",
    payload: {
      config: mockConfig,
      hotkeyConflict: false,
      hwid: "",
      isLicensed: true,
    },
  });

  state = appReducer(state, {
    type: "SET_TAB",
    payload: "general",
  });

  state = appReducer(state, {
    type: "SET_RECORDING",
    payload: true,
  });

  expect(state.tabState.tab).toBe("general");

  if (state.tabState.tab === "general") {
    expect(state.tabState.isRecording).toBe(true);
  }
});

test("ACTIVATION lifecycle", () => {
  let state = appReducer(initialAppState, {
    type: "SET_TAB",
    payload: "license",
  });

  state = appReducer(state, {
    type: "SET_LICENSE_KEY_INPUT",
    payload: "test-key",
  });

  if (state.tabState.tab === "license") {
    expect(state.tabState.keyInput).toBe("test-key");
  }

  state = appReducer(state, { type: "START_ACTIVATION" });

  if (state.tabState.tab === "license") {
    expect(state.tabState.isActivating).toBe(true);
  }

  state = appReducer(state, {
    type: "ACTIVATION_FAILURE",
    payload: "Invalid Key",
  });

  if (state.tabState.tab === "license") {
    expect(state.tabState.isActivating).toBe(false);
    expect(state.tabState.error).toBe("Invalid Key");
  }

  state = appReducer(state, { type: "ACTIVATION_SUCCESS" });

  expect(state.isLicensed).toBe(true);
  expect(state.tabState.tab).toBe("license");

  if (state.tabState.tab === "license") {
    expect(state.tabState.keyInput).toBe("");
    expect(state.tabState.isActivating).toBe(false);
    expect(state.tabState.error).toBe("");
  }
});

test("SET_TAB to general is blocked when unlicensed", () => {
  const unlicensedState = appReducer(initialAppState, {
    type: "INITIALIZE",
    payload: {
      config: mockConfig,
      hotkeyConflict: false,
      hwid: "",
      isLicensed: false,
    },
  });

  expect(unlicensedState.tabState.tab).toBe("license");

  const after = appReducer(unlicensedState, {
    type: "SET_TAB",
    payload: "general",
  });

  expect(after.tabState.tab).toBe("license");
});

test("SET_TAB to rules is blocked when unlicensed", () => {
  const unlicensedState = appReducer(initialAppState, {
    type: "INITIALIZE",
    payload: {
      config: mockConfig,
      hotkeyConflict: false,
      hwid: "",
      isLicensed: false,
    },
  });

  const after = appReducer(unlicensedState, {
    type: "SET_TAB",
    payload: "rules",
  });

  expect(after.tabState.tab).toBe("license");
});

test("updating config does not reset active tab", () => {
  let state = appReducer(initialAppState, {
    type: "INITIALIZE",
    payload: {
      config: mockConfig,
      hotkeyConflict: false,
      hwid: "",
      isLicensed: true,
    },
  });
  state = appReducer(state, { type: "SET_TAB", payload: "rules" });

  expect(state.tabState.tab).toBe("rules");

  state = appReducer(state, { type: "SET_HOTKEY_CONFLICT", payload: false });

  expect(state.tabState.tab).toBe("rules");
});
