const prometheus = require('prom-client');
const logger = require('./logger');

const collectDefaultMetrics = prometheus.collectDefaultMetrics;
const Registry = prometheus.Registry;
const register = new Registry();

const httpRequestDurationMicroseconds = new prometheus.Histogram({
  name: 'http_request_duration_seconds',
  help: 'Duration of HTTP requests in seconds',
  labelNames: ['method', 'route', 'status_code'],
  buckets: [0.1, 0.5, 1, 2, 5]
});

const initializeMonitoring = () => {
  try {
    collectDefaultMetrics({ register });
    register.registerMetric(httpRequestDurationMicroseconds);
    logger.info('Monitoring initialized successfully');
  } catch (error) {
    logger.error('Failed to initialize monitoring:', error);
  }
};

module.exports = {
  initializeMonitoring,
  register,
  httpRequestDurationMicroseconds
};