import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
import { SystemRole } from "@capakraken/shared";

import { createToolContext, executeTool } from "./assistant-tools-settings-test-helpers.js";

describe("assistant settings tool get_ai_configured", () => {
  beforeEach(() => {
    vi.clearAllMocks();
  });

  afterEach(() => {
    vi.unstubAllGlobals();
    vi.unstubAllEnvs();
  });

  it("reports AI configuration only to admin users through the real settings path", async () => {
    vi.stubEnv("OPENAI_API_KEY", "env-openai-key");

    const ctx = createToolContext({
      systemSettings: {
        findUnique: vi.fn().mockResolvedValue({
          aiProvider: "openai",
          azureOpenAiDeployment: "gpt-4o-mini",
          azureOpenAiApiKey: null,
        }),
      },
    });

    const result = await executeTool("get_ai_configured", JSON.stringify({}), ctx);

    expect(JSON.parse(result.content)).toEqual({ configured: true });
  });

  it("rejects get_ai_configured for non-admin assistant users", async () => {
    const ctx = createToolContext(
      {
        systemSettings: {
          findUnique: vi.fn(),
        },
      },
      SystemRole.MANAGER,
    );

    const result = await executeTool("get_ai_configured", JSON.stringify({}), ctx);

    expect(JSON.parse(result.content)).toEqual({
      error: "You do not have permission to perform this action.",
    });
  });
});
