const express = require('express');
const queue = require('../queue/sqliteQueue');

function createHealthRouter(connectors = []) {
  const router = express.Router();

  router.get('/', async (req, res) => {
    const connectorStatuses = connectors.map((connector) => connector.health());
    const pending = await queue.pendingCount();
    const retrying = await queue.retryingCount();
    const deadLetters = await queue.deadLetterCount();
    res.json({
      status: 'ok',
      connectors: connectorStatuses,
      metrics: {
        pending,
        retrying,
        deadLetters
      }
    });
  });

  router.get('/ready', async (req, res) => {
    try {
      await queue.ping();
      res.json({ status: 'ready' });
    } catch (err) {
      res.status(503).json({ status: 'unready', reason: err.message });
    }
  });

  return router;
}

module.exports = { createHealthRouter };
