import { Router, type Request, type Response } from "express";
import { metricsService } from "../lib/metrics.js";
import { makeLogger } from "../lib/logger.js";

const router = Router();
const logger = makeLogger("metrics");

router.get("/metrics", async (_req: Request, res: Response) => {
  try {
    const snapshot = await metricsService.snapshot();
    res.json(snapshot);
  } catch (err) {
    const message = err instanceof Error ? err.message : "Failed to collect metrics";
    logger.error("snapshot failed", { error: message });
    res.status(500).json({ error: message });
  }
});

export default router;
