import { Router, Request, Response } from "express";
import { checkDbConnection } from "../db";
import { getBackendVersion } from "../version";

const router = Router();

router.get("/health", (_req: Request, res: Response) => {
  res.json({ status: "ok", version: getBackendVersion() });
});

/** Версия для UI; путь без «health», чтобы реже резался фильтрами/прокси. */
router.get("/meta", (_req: Request, res: Response) => {
  res.json({ version: getBackendVersion() });
});

router.get("/ready", async (_req: Request, res: Response) => {
  const dbOk = await checkDbConnection();
  if (dbOk) {
    res.json({ status: "ready", db: "connected" });
  } else {
    res.status(503).json({ error: "database_unavailable", db: "disconnected" });
  }
});

export default router;
