import { describe, expect, it } from "vitest";
import { useProviderDialogs } from "../src/dimsdk/pages/utils/provider-dialogs.ts";

function createStore(input?: Partial<{
  activeId: string;
  connections: Record<string, any>;
  customProviders: any[];
  env: Record<string, string>;
  configModel: string;
  activeModelByProvider: Record<string, string>;
}>): any {
  return {
    state: {
      ui: {
        onboarding: {
          lastSeenVersion: "",
        },
        providerFunnel: {
          counts: {},
          events: [],
        },
        workspaceSelectionByCwd: {},
      },
      provider: {
        activeId: input?.activeId ?? "openai",
        connections: input?.connections ?? {},
        customProviders: input?.customProviders ?? [],
        custom: {
          baseUrl: "",
          apiKey: "",
          models: [],
        },
        activeModelByProvider: input?.activeModelByProvider ?? {},
      },
      config: { model: input?.configModel ?? "", baseUrl: "", apiKey: "" },
      context: { env: input?.env ?? {}, cwd: "." },
      messages: [],
    },
    setEnvVar: () => {},
  };
}

describe("provider dialogs send guard", () => {
  it("blocks first send and opens /connect when active provider is not configured", () => {
    const prev = process.env.DIMCODE_TEST;
    delete process.env.DIMCODE_TEST;
    try {
      const toasts: string[] = [];
      const store = createStore({ activeId: "openai", connections: {} });
      const dialogs = useProviderDialogs({
        store,
        nodeLike: true,
        showToast: (text: string) => toasts.push(text),
        openCustomSettings: () => {},
      });

      const ready = dialogs.ensureActiveProviderReadyForChat();
      expect(ready).toBe(false);
      expect(dialogs.showConnect.value).toBe(true);
      expect(toasts.join("\n")).toContain("/connect");
    } finally {
      if (prev == null) {
        delete process.env.DIMCODE_TEST;
      } else {
        process.env.DIMCODE_TEST = prev;
      }
    }
  });

  it("allows send when API key exists in env for the active provider", () => {
    const store = createStore({
      activeId: "openai",
      connections: {},
      env: { OPENAI_API_KEY: "sk-test" },
    });
    const dialogs = useProviderDialogs({
      store,
      nodeLike: true,
      showToast: () => {},
      openCustomSettings: () => {},
    });

    expect(dialogs.ensureActiveProviderReadyForChat()).toBe(true);
  });

  it("skips first-use /models guide when model is already configured", () => {
    const prev = process.env.DIMCODE_TEST;
    delete process.env.DIMCODE_TEST;
    try {
      const toasts: string[] = [];
      const store = createStore({
        activeId: "openai",
        connections: {},
        configModel: "gpt-4.1-mini",
        activeModelByProvider: { openai: "gpt-4.1-mini" },
      });
      const dialogs = useProviderDialogs({
        store,
        nodeLike: true,
        showToast: (text: string) => toasts.push(text),
        openCustomSettings: () => {},
      });

      expect(dialogs.ensureActiveProviderReadyForChat()).toBe(false);
      expect(toasts.length).toBeGreaterThan(0);
      expect(toasts[0]).not.toContain("/models");
      expect(toasts[0]).toContain("/connect");
    } finally {
      if (prev == null) {
        delete process.env.DIMCODE_TEST;
      } else {
        process.env.DIMCODE_TEST = prev;
      }
    }
  });
});
