import { registerProvider } from "@flue/runtime";
import { flue } from "@flue/runtime/routing";
import { Hono } from "hono";
import { cors } from "hono/cors";

import { env } from "./tools/runtime";

// Register the GLM 5.2 provider once at module load.
registerProvider(env.AGENT_MODEL_PROVIDER, {
  api: env.AGENT_MODEL_API,
  apiKey: env.AGENT_MODEL_API_KEY,
  baseUrl: env.AGENT_MODEL_BASE_URL,
  models: {
    [env.AGENT_MODEL_NAME]: {
      contextWindow: env.AGENT_MODEL_CONTEXT_WINDOW,
      maxTokens: env.AGENT_MODEL_MAX_TOKENS,
    },
  },
});

const app = new Hono();

// The chat UI is served from the web app origin, so browser calls here are
// cross-origin; without CORS headers every preflight 405s and sends fail.
app.use("*", cors());

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

app.route("/", flue());

export default app;
