// FILE: backend/src/routes/ops.js
import { Router } from "express";
import sql from "../db/index.js";
import fs from "node:fs";
import path from "node:path";

const r = Router();

r.get("/ready", async (_req, res) => {
  try {
    await sql.query("SELECT 1");
    res.json({ ready: true });
  } catch {
    res.status(500).json({ ready: false });
  }
});

r.get("/live", (_req, res) => res.json({ live: true }));

r.get("/version", (_req, res) => {
  try {
    const pkgPath = path.resolve(process.cwd(), "package.json");
    const pkg = JSON.parse(fs.readFileSync(pkgPath, "utf-8"));
    res.json({ name: pkg.name, version: pkg.version });
  } catch {
    res.json({ name: "dms-backend", version: "unknown" });
  }
});

export default r;
