import { describe, it, expect, beforeEach, afterEach } from "vitest";
import fs from "fs";
import os from "os";
import path from "path";
import { sendMessage, sendFile, sendVoice } from "../src/modules/messaging.js";

let tmpDir: string;

function readMessages(tmpDir: string) {
  const messagesDir = path.join(tmpDir, "messages");
  if (!fs.existsSync(messagesDir)) return [];
  return fs.readdirSync(messagesDir)
    .filter((f) => f.endsWith(".json"))
    .map((f) => JSON.parse(fs.readFileSync(path.join(messagesDir, f), "utf8")));
}

beforeEach(() => {
  tmpDir = path.join(os.tmpdir(), `treehouse-test-${Math.random().toString(36).slice(2)}`);
  fs.mkdirSync(tmpDir, { recursive: true });
  process.env.TENANT_ID = "test-tenant";
  process.env.NANOCLAW_CHAT_JID = "test-jid@g.us";
  process.env.NANOCLAW_GROUP_FOLDER = "test/group";
});

afterEach(() => {
  fs.rmSync(tmpDir, { recursive: true, force: true });
  delete process.env.TENANT_ID;
  delete process.env.NANOCLAW_CHAT_JID;
  delete process.env.NANOCLAW_GROUP_FOLDER;
});

describe("sendMessage", () => {
  it("writes send_message IPC file", async () => {
    await sendMessage("Hello world", undefined, tmpDir);
    const msgs = readMessages(tmpDir);
    expect(msgs).toHaveLength(1);
    expect(msgs[0]).toMatchObject({
      type: "message",
      chatJid: "test-jid@g.us",
      groupFolder: "test/group",
      text: "Hello world",
    });
  });

  it("writes send_message with kind and sender", async () => {
    await sendMessage("Progress update", { kind: "progress", sender: "MyBot" }, tmpDir);
    const msgs = readMessages(tmpDir);
    expect(msgs[0]).toMatchObject({
      type: "message",
      text: "Progress update",
      kind: "progress",
      sender: "MyBot",
    });
  });
});

describe("sendFile", () => {
  it("writes send_file IPC file", async () => {
    await sendFile("/workspace/output.pdf", "My report", tmpDir);
    const msgs = readMessages(tmpDir);
    expect(msgs[0]).toMatchObject({
      type: "file",
      chatJid: "test-jid@g.us",
      groupFolder: "test/group",
      filePath: "/workspace/output.pdf",
      caption: "My report",
    });
  });

  it("writes send_file without caption", async () => {
    await sendFile("/workspace/output.pdf", undefined, tmpDir);
    const msgs = readMessages(tmpDir);
    expect(msgs[0]).toMatchObject({ type: "file", filePath: "/workspace/output.pdf" });
  });
});

describe("sendVoice", () => {
  it("writes send_voice_message IPC file", async () => {
    await sendVoice("Hello there", { provider: "edge", voice: "en-GB-SoniaNeural", caption: "Audio message" }, tmpDir);
    const msgs = readMessages(tmpDir);
    expect(msgs[0]).toMatchObject({
      type: "send_voice_message",
      chatJid: "test-jid@g.us",
      groupFolder: "test/group",
      text: "Hello there",
      provider: "edge",
      voice: "en-GB-SoniaNeural",
      caption: "Audio message",
    });
  });

  it("writes send_voice_message with text only", async () => {
    await sendVoice("Hello", undefined, tmpDir);
    const msgs = readMessages(tmpDir);
    expect(msgs[0]).toMatchObject({ type: "send_voice_message", text: "Hello" });
  });
});
