import "./instrumentation.js";

import { createHttpTelemetryMiddleware } from "@pkg/logic/core/http.telemetry";
import { serve } from "@hono/node-server";
import { Hono } from "hono";
import type { AppBindings } from "./core/utils";
import { requireInternalApiKey } from "./core/utils";
import { commandRouter } from "./domains/command/router";
import { orchestrateRouter } from "./domains/orchestrate/router";

const app = new Hono<AppBindings>().use(
    "*",
    createHttpTelemetryMiddleware("orchestrator"),
);

const host = process.env.HOST || "0.0.0.0";
const port = Number(process.env.PORT || "3000");

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

app.get("/ping", (c) => {
    return c.text("pong");
});

app.use("/internal/*", requireInternalApiKey);
app.route("/internal", commandRouter);
app.route("/internal", orchestrateRouter);

serve(
    {
        fetch: app.fetch,
        port,
        hostname: host,
    },
    (info) => {
        console.log(`Server is running on http://${host}:${info.port}`);
    },
);
