import { describe, expect, test, beforeEach } from "bun:test";
import { Hono } from "hono";
import { emailRoutes } from "./emails";
import { createDatabase, insertThread, insertMessage } from "../db/index";
import type { Database } from "bun:sqlite";

let app: Hono;
let db: Database;

beforeEach(() => {
	db = createDatabase(":memory:");
	app = new Hono();
	app.route("/api", emailRoutes(db));

	insertThread(db, {
		threadId: "t001",
		subject: "Q1 Planning",
		authors: "Alice, Bob",
		totalMessages: 2,
		tags: "inbox,unread",
		timestamp: 1709884532,
		accountId: "personal",
	});

	insertMessage(db, {
		messageId: "msg001@example.com",
		threadId: "t001",
		fromHeader: "Alice <alice@example.com>",
		toHeader: "user@example.com",
		subject: "Q1 Planning",
		date: "2024-03-08T10:15:32+01:00",
		inReplyTo: "",
		body: "Hey, let's plan Q1.",
		tags: "inbox,unread",
		timestamp: 1709884532,
		accountId: "personal",
	});

	insertMessage(db, {
		messageId: "msg003@example.com",
		threadId: "t001",
		fromHeader: "Bob <bob@example.com>",
		toHeader: "alice@example.com",
		subject: "Re: Q1 Planning",
		date: "2024-03-08T10:16:40+01:00",
		inReplyTo: "msg001@example.com",
		body: "Sounds good. Tuesday work?",
		tags: "inbox",
		timestamp: 1709884600,
		accountId: "personal",
	});
});

describe("GET /api/threads", () => {
	test("returns threads for an account", async () => {
		const res = await app.request("/api/threads?accountId=personal");
		expect(res.status).toBe(200);
		const data = await res.json();
		expect(data.threads).toHaveLength(1);
		expect(data.threads[0].subject).toBe("Q1 Planning");
	});
});

describe("GET /api/threads/:threadId/messages", () => {
	test("returns messages in a thread sorted by time", async () => {
		const res = await app.request("/api/threads/t001/messages");
		expect(res.status).toBe(200);
		const data = await res.json();
		expect(data.messages).toHaveLength(2);
		expect(data.messages[0].messageId).toBe("msg001@example.com");
		expect(data.messages[1].messageId).toBe("msg003@example.com");
	});
});
