import { Hono } from "hono";
import { cors } from "hono/cors";
import { emailRoutes } from "./routes/emails";
import { eventRoutes } from "./routes/events";
import { syncRoutes } from "./routes/sync";
import { createDatabase } from "./db/index";
import { EventBus } from "./services/eventbus";

const db = createDatabase(process.env.DB_PATH ?? "magnumopus.db");
const bus = new EventBus();

const app = new Hono();

app.use("*", cors());

app.get("/health", (c) => c.json({ status: "ok" }));

app.route("/api", emailRoutes(db));
app.route("/api", eventRoutes(bus));
app.route("/api", syncRoutes(db, bus));

export default {
	port: Number(process.env.PORT ?? 3000),
	fetch: app.fetch,
};
