import { Hono } from 'hono';
import type { IServiceContainer } from '@stock-bot/handlers';
import { getLogger } from '@stock-bot/logger';

const logger = getLogger('stats-routes');

export function createStatsRoutes(container: IServiceContainer) {
  const stats = new Hono();

  // Statistics endpoints
  stats.get('/exchanges', async c => {
    try {
      const queueManager = container.queue;
      if (!queueManager) {
        return c.json({ error: 'Queue manager not available' }, 503);
      }

      const exchangesQueue = queueManager.getQueue('exchanges');

      const job = await exchangesQueue.addJob('get-exchange-stats', {
        handler: 'exchanges',
        operation: 'get-exchange-stats',
        payload: {},
      });

      // Wait for job to complete and return result
      const result = await job.waitUntilFinished();
      return c.json(result);
    } catch (error) {
      logger.error('Failed to get exchange stats', { error });
      return c.json({ error: error instanceof Error ? error.message : 'Unknown error' }, 500);
    }
  });

  stats.get('/provider-mappings', async c => {
    try {
      const queueManager = container.queue;
      if (!queueManager) {
        return c.json({ error: 'Queue manager not available' }, 503);
      }

      const exchangesQueue = queueManager.getQueue('exchanges');

      const job = await exchangesQueue.addJob('get-provider-mapping-stats', {
        handler: 'exchanges',
        operation: 'get-provider-mapping-stats',
        payload: {},
      });

      // Wait for job to complete and return result
      const result = await job.waitUntilFinished();
      return c.json(result);
    } catch (error) {
      logger.error('Failed to get provider mapping stats', { error });
      return c.json({ error: error instanceof Error ? error.message : 'Unknown error' }, 500);
    }
  });

  return stats;
}
