import { beforeEach, describe, expect, it, vi } from "vitest";

import { sendMessage, sendPoll } from "./message.js";

const callGatewayMock = vi.fn();
vi.mock("../../gateway/call.js", () => ({
  callGateway: (...args: unknown[]) => callGatewayMock(...args),
  randomIdempotencyKey: () => "idem-1",
}));

describe("sendMessage provider normalization", () => {
  beforeEach(() => {
    callGatewayMock.mockReset();
  });

  it("normalizes Teams alias", async () => {
    const sendMSTeams = vi.fn(async () => ({
      messageId: "m1",
      conversationId: "c1",
    }));
    const result = await sendMessage({
      cfg: {},
      to: "conversation:19:abc@thread.tacv2",
      content: "hi",
      provider: "teams",
      deps: { sendMSTeams },
    });

    expect(sendMSTeams).toHaveBeenCalledWith(
      "conversation:19:abc@thread.tacv2",
      "hi",
    );
    expect(result.provider).toBe("msteams");
  });

  it("normalizes iMessage alias", async () => {
    const sendIMessage = vi.fn(async () => ({ messageId: "i1" }));
    const result = await sendMessage({
      cfg: {},
      to: "someone@example.com",
      content: "hi",
      provider: "imsg",
      deps: { sendIMessage },
    });

    expect(sendIMessage).toHaveBeenCalledWith(
      "someone@example.com",
      "hi",
      expect.any(Object),
    );
    expect(result.provider).toBe("imessage");
  });
});

describe("sendPoll provider normalization", () => {
  beforeEach(() => {
    callGatewayMock.mockReset();
  });

  it("normalizes Teams alias for polls", async () => {
    callGatewayMock.mockResolvedValueOnce({ messageId: "p1" });

    const result = await sendPoll({
      cfg: {},
      to: "conversation:19:abc@thread.tacv2",
      question: "Lunch?",
      options: ["Pizza", "Sushi"],
      provider: "Teams",
    });

    const call = callGatewayMock.mock.calls[0]?.[0] as {
      params?: Record<string, unknown>;
    };
    expect(call?.params?.provider).toBe("msteams");
    expect(result.provider).toBe("msteams");
  });
});
