/**
 * Version route — exposes the running application version and runtime metadata.
 *
 * The version comes from package.json at module load time so the response
 * always matches the running code. Extracted from src/app.js into its own
 * module so production wiring and tests share the same code path.
 */
const express = require('express');

let appVersion = '0.0.0';
let appName = 'dashcaddy-api';
try {
  const pkg = require('../package.json');
  if (pkg && pkg.version) appVersion = pkg.version;
  if (pkg && pkg.name) appName = pkg.name;
} catch (_) {
  /* package.json unreadable — keep fallback */
}

function getVersion() {
  return appVersion;
}

function getName() {
  return appName;
}

function buildRouter() {
  const router = express.Router();
  router.get('/version', (req, res) => {
    res.json({
      success: true,
      name: appName,
      version: appVersion,
      node: process.version,
      platform: process.platform,
      arch: process.arch,
      uptime: process.uptime(),
      instanceId: process.env.DASHCADDY_INSTANCE_ID || null
    });
  });
  return router;
}

// Allow direct use as a factory (no-op for version since it has no deps)
// or destructuring of { buildRouter, getVersion, getName }.
module.exports = module.exports.default || module.exports;
module.exports.buildRouter = buildRouter;
module.exports.getVersion = getVersion;
module.exports.getName = getName;
module.exports.default = function factory() { return buildRouter(); };
