import { createKyselyMock } from "@tailor-platform/sdk/vitest";
import { describe, expect, it } from "vitest";
import type { Namespace } from "../generated/kysely-tailordb";
import { run } from "./getSlackWorkspaceIntegration";

const redactedRow = {
  id: "swc-1",
  teamId: "T123",
  teamName: "Acme",
  botUserId: "U999",
  status: "ACTIVE" as const,
  installedAt: new Date("2026-01-01T00:00:00.000Z"),
  lastValidatedAt: null,
  revokedAt: null,
  createdAt: new Date("2026-01-01T00:00:00.000Z"),
  updatedAt: null,
};

describe("getSlackWorkspaceIntegration", () => {
  it("returns the SlackWorkspaceIntegration as redacted metadata", async () => {
    const mock = createKyselyMock<Namespace[keyof Namespace]>();
    mock.enqueueResults([{ ...redactedRow }]);

    const result = await run(mock.db, {});

    expect(result.ok).toBe(true);
    if (result.ok) {
      expect(result.value.slackWorkspaceIntegration?.teamId).toBe("T123");
      expect(result.value.slackWorkspaceIntegration?.status).toBe("ACTIVE");
    }
  });

  it("returns null when no connection exists", async () => {
    const mock = createKyselyMock<Namespace[keyof Namespace]>();
    mock.enqueueResults([]);

    const result = await run(mock.db, {});

    expect(result.ok).toBe(true);
    if (result.ok) {
      expect(result.value.slackWorkspaceIntegration).toBeNull();
    }
  });

  it("never includes bot token in the result", async () => {
    const mock = createKyselyMock<Namespace[keyof Namespace]>();
    mock.enqueueResults([{ ...redactedRow }]);

    const result = await run(mock.db, {});

    expect(result.ok).toBe(true);
    if (result.ok && result.value.slackWorkspaceIntegration) {
      expect("botToken" in result.value.slackWorkspaceIntegration).toBe(false);
    }
  });
});
