import { Hono } from "hono";
import type { Database } from "bun:sqlite";
import { getThreads, getMessagesForThread } from "../db/index";

export function emailRoutes(db: Database): Hono {
	const router = new Hono();

	router.get("/threads", (c) => {
		const accountId = c.req.query("accountId");
		if (!accountId) {
			return c.json({ error: "accountId query parameter required" }, 400);
		}
		const threads = getThreads(db, accountId);
		return c.json({ threads });
	});

	router.get("/threads/:threadId/messages", (c) => {
		const threadId = c.req.param("threadId");
		const messages = getMessagesForThread(db, threadId);
		return c.json({ messages });
	});

	return router;
}
