'use strict';

const express = require('express');

function buildStatsRoutes(opts) {
  const { state, proxyRegistry } = opts;
  const router = express.Router();

  router.get('/', (req, res) => {
    res.json({
      activeBots: state.activeBots.size,
      totalBotsSpawned: state.counters.totalBotsSpawned,
      totalConnectionsFailed: state.counters.totalConnectionsFailed,
      totalKeepAlivesSent: state.counters.totalKeepAlivesSent,
      totalHandshakesOk: state.counters.totalHandshakesOk,
      testRunning: state.testRunning,
      currentTarget: state.currentTarget,
      activeProxies: proxyRegistry.list({ status: 'alive' }).length,
      deadProxies: proxyRegistry.list({ status: 'dead' }).length,
      bots: state.snapshot(),
    });
  });

  router.get('/logs', (req, res) => {
    res.json({ logs: state.logs });
  });

  router.delete('/logs', (req, res) => {
    state.clearLogs();
    res.json({ success: true });
  });

  return router;
}

module.exports = { buildStatsRoutes };
