import { Hono } from "hono";
import type { WsBridge } from "./ws-bridge.js";

export interface HealthDeps {
  version: string;
  wsBridge: WsBridge;
}

/**
 * Creates health and readiness endpoints.
 * These are meant to be mounted BEFORE auth middleware so they remain unauthenticated.
 * Only basic status information is exposed — no session counts or internal stats.
 */
export function createHealthRoutes(deps: HealthDeps): Hono {
  const app = new Hono();

  app.get("/health", (c) => {
    return c.json({
      status: "ok",
      version: deps.version,
      uptime: process.uptime(),
    });
  });

  app.get("/ready", (c) => {
    return c.json({
      status: "ok",
      version: deps.version,
    });
  });

  return app;
}

/**
 * Creates detailed health endpoint with session stats.
 * Must be mounted AFTER auth middleware so it requires authentication.
 */
export function createDetailedHealthRoutes(deps: HealthDeps): Hono {
  const app = new Hono();

  app.get("/detailed", (c) => {
    try {
      const activeSessions = deps.wsBridge.getAllSessions().length;
      return c.json({
        status: "ok",
        version: deps.version,
        uptime: process.uptime(),
        activeSessions,
      });
    } catch {
      return c.json(
        { status: "error", message: "Session store unavailable" },
        503,
      );
    }
  });

  return app;
}
