import { describe, it, expect, vi, beforeEach } from "vitest";
import { setActivePinia, createPinia } from "pinia";
import { useScriptsStore } from "../scripts.js";

vi.mock("../../api/modules/scripts.js", () => ({
  scriptsApi: {
    actionsList: vi.fn(),
    regularList: vi.fn(),
    scopesList: vi.fn(),
    runAction: vi.fn(),
    setRegularState: vi.fn(),
    setScopeState: vi.fn(),
  },
}));

import { scriptsApi } from "../../api/modules/scripts.js";

describe("useScriptsStore", () => {
  beforeEach(() => {
    setActivePinia(createPinia());
    vi.clearAllMocks();
  });

  describe("loadActions", () => {
    it("sets actions on success", async () => {
      scriptsApi.actionsList.mockResolvedValue({
        ok: true,
        data: { data: { scripts: [{ alias: "test", name: "Test" }] } },
      });

      const store = useScriptsStore();
      await store.loadActions();

      expect(store.actions).toEqual([{ alias: "test", name: "Test" }]);
      expect(store.isLoadingActions).toBe(false);
    });

    it("sets error on failure", async () => {
      scriptsApi.actionsList.mockResolvedValue({
        ok: false,
        error: { message: "Failed" },
      });

      const store = useScriptsStore();
      await store.loadActions();

      expect(store.errorActions).toEqual({ message: "Failed" });
    });
  });

  describe("runScript", () => {
    it("sets lastRunResult on success", async () => {
      scriptsApi.runAction.mockResolvedValue({
        ok: true,
        data: { data: { return: { result: { ok: true }, exec_time: "0.042 seconds" } } },
      });

      const store = useScriptsStore();
      await store.runScript("kitchen_light");

      expect(store.lastRunResult).toMatchObject({
        alias: "kitchen_light",
        ok: true,
        data: { ok: true },
        execTime: "0.042 seconds",
      });
    });

    it("sets lastRunResult on failure", async () => {
      scriptsApi.runAction.mockResolvedValue({
        ok: false,
        error: { message: "Script not found" },
      });

      const store = useScriptsStore();
      await store.runScript("missing");

      expect(store.lastRunResult).toMatchObject({
        alias: "missing",
        ok: false,
        error: { message: "Script not found" },
      });
    });
  });

  describe("setRegularState", () => {
    it("updates state in list on success", async () => {
      scriptsApi.setRegularState.mockResolvedValue({ ok: true });

      const store = useScriptsStore();
      store.regular = [{ alias: "check", state: "enabled" }];

      await store.setRegularState("check", false);

      expect(store.regular[0].state).toBe("disabled");
    });
  });

  describe("setScopeState", () => {
    it("updates state in list on success", async () => {
      scriptsApi.setScopeState.mockResolvedValue({ ok: true });

      const store = useScriptsStore();
      store.scopes = [{ name: "HubScope", state: "enabled" }];

      await store.setScopeState("HubScope", false);

      expect(store.scopes[0].state).toBe("disabled");
    });
  });
});
