import { wrapAsyncHandler } from "@batch-cooking/express-tools";
import { getMetricsSchema } from "@batch-cooking/shared";
import { Router } from "express";
import { requireAdmin } from "../../middlewares/require-admin.js";
import { getMetrics } from "./admin-metrics.service.js";

/** Router mounted at `/admin/metrics` (via `admin.routes.ts`) — every route behind {@link requireAdmin}. */
export const adminMetricsRouter = Router();

/**
 * Returns the admin dashboard's usage metrics — a `snapshot` of current
 * totals plus `?days=` (7–365, default 30) days of daily time series (see
 * {@link getMetrics}). Read-only; no side effects.
 */
adminMetricsRouter.get(
  "/",
  requireAdmin,
  wrapAsyncHandler(async (req, res) => {
    const { days } = getMetricsSchema.parse(req.query);
    res.status(200).json(await getMetrics(days));
  }),
);
