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

const logger = getLogger('enhanced-sync-routes');

export function createEnhancedSyncRoutes(container: IServiceContainer) {
  const enhancedSync = new Hono();

  // Enhanced sync endpoints
  enhancedSync.post('/exchanges/all', async c => {
    try {
      const clearFirst = c.req.query('clear') === 'true';
      const queueManager = container.queue;
      if (!queueManager) {
        return c.json({ success: false, error: 'Queue manager not available' }, 503);
      }

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

      const job = await exchangesQueue.addJob('sync-all-exchanges', {
        handler: 'exchanges',
        operation: 'sync-all-exchanges',
        payload: { clearFirst },
      });

      return c.json({ success: true, jobId: job.id, message: 'Enhanced exchange sync job queued' });
    } catch (error) {
      logger.error('Failed to queue enhanced exchange sync job', { error });
      return c.json(
        { success: false, error: error instanceof Error ? error.message : 'Unknown error' },
        500
      );
    }
  });

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

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

      const job = await exchangesQueue.addJob('sync-qm-provider-mappings', {
        handler: 'exchanges',
        operation: 'sync-qm-provider-mappings',
        payload: {},
      });

      return c.json({
        success: true,
        jobId: job.id,
        message: 'QM provider mappings sync job queued',
      });
    } catch (error) {
      logger.error('Failed to queue QM provider mappings sync job', { error });
      return c.json(
        { success: false, error: error instanceof Error ? error.message : 'Unknown error' },
        500
      );
    }
  });

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

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

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

      return c.json({
        success: true,
        jobId: job.id,
        message: 'IB exchanges sync job queued',
      });
    } catch (error) {
      logger.error('Failed to queue IB exchanges sync job', { error });
      return c.json(
        { success: false, error: error instanceof Error ? error.message : 'Unknown error' },
        500
      );
    }
  });

  enhancedSync.get('/status', async c => {
    try {
      const queueManager = container.queue;
      if (!queueManager) {
        return c.json({ success: false, error: 'Queue manager not available' }, 503);
      }

      const symbolsQueue = queueManager.getQueue('symbols');

      const job = await symbolsQueue.addJob('sync-status', {
        handler: 'symbols',
        operation: 'sync-status',
        payload: {},
      });

      return c.json({ success: true, jobId: job.id, message: 'Sync status job queued' });
    } catch (error) {
      logger.error('Failed to queue sync status job', { error });
      return c.json(
        { success: false, error: error instanceof Error ? error.message : 'Unknown error' },
        500
      );
    }
  });

  enhancedSync.post('/clear/postgresql', async c => {
    try {
      const dataType = c.req.query('type') as 'exchanges' | 'provider_mappings' | 'all';
      const queueManager = container.queue;
      if (!queueManager) {
        return c.json({ success: false, error: 'Queue manager not available' }, 503);
      }

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

      const job = await exchangesQueue.addJob('clear-postgresql-data', {
        handler: 'exchanges',
        operation: 'clear-postgresql-data',
        payload: { dataType: dataType || 'all' },
      });

      return c.json({
        success: true,
        jobId: job.id,
        message: 'PostgreSQL data clear job queued',
      });
    } catch (error) {
      logger.error('Failed to queue PostgreSQL clear job', { error });
      return c.json(
        { success: false, error: error instanceof Error ? error.message : 'Unknown error' },
        500
      );
    }
  });

  return enhancedSync;
}
