/**
 * Pipeline Routes
 * API endpoints for data pipeline operations
 */

import { Hono } from 'hono';
import type { IServiceContainer } from '@stock-bot/handlers';
import { getLogger } from '@stock-bot/logger';
import { PipelineService } from '../services/pipeline.service';

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

export function createPipelineRoutes(container: IServiceContainer) {
  const pipeline = new Hono();
  const pipelineService = new PipelineService(container);

  // Symbol sync endpoints
  pipeline.post('/symbols', async c => {
    try {
      const result = await pipelineService.syncQMSymbols();
      return c.json(result, result.success ? 200 : 503);
    } catch (error) {
      logger.error('Error in POST /symbols', { error });
      return c.json({ success: false, error: 'Internal server error' }, 500);
    }
  });

  pipeline.post('/symbols/:provider', async c => {
    try {
      const provider = c.req.param('provider');
      const result = await pipelineService.syncProviderSymbols(provider);
      return c.json(result, result.success ? 200 : 503);
    } catch (error) {
      logger.error('Error in POST /symbols/:provider', { error });
      return c.json({ success: false, error: 'Internal server error' }, 500);
    }
  });

  // Exchange sync endpoints
  pipeline.post('/exchanges', async c => {
    try {
      const result = await pipelineService.syncQMExchanges();
      return c.json(result, result.success ? 200 : 503);
    } catch (error) {
      logger.error('Error in POST /exchanges', { error });
      return c.json({ success: false, error: 'Internal server error' }, 500);
    }
  });

  pipeline.post('/exchanges/all', async c => {
    try {
      const clearFirst = c.req.query('clear') === 'true';
      const result = await pipelineService.syncAllExchanges(clearFirst);
      return c.json(result, result.success ? 200 : 503);
    } catch (error) {
      logger.error('Error in POST /exchanges/all', { error });
      return c.json({ success: false, error: 'Internal server error' }, 500);
    }
  });

  // Provider mapping sync endpoints
  pipeline.post('/provider-mappings/qm', async c => {
    try {
      const result = await pipelineService.syncQMProviderMappings();
      return c.json(result, result.success ? 200 : 503);
    } catch (error) {
      logger.error('Error in POST /provider-mappings/qm', { error });
      return c.json({ success: false, error: 'Internal server error' }, 500);
    }
  });

  pipeline.post('/provider-mappings/ib', async c => {
    try {
      const result = await pipelineService.syncIBExchanges();
      return c.json(result, result.success ? 200 : 503);
    } catch (error) {
      logger.error('Error in POST /provider-mappings/ib', { error });
      return c.json({ success: false, error: 'Internal server error' }, 500);
    }
  });

  // Status endpoint
  pipeline.get('/status', async c => {
    try {
      const result = await pipelineService.getSyncStatus();
      return c.json(result, result.success ? 200 : 503);
    } catch (error) {
      logger.error('Error in GET /status', { error });
      return c.json({ success: false, error: 'Internal server error' }, 500);
    }
  });

  // Clear data endpoint
  pipeline.post('/clear/postgresql', async c => {
    try {
      const dataType = c.req.query('type') as 'exchanges' | 'provider_mappings' | 'all';
      const result = await pipelineService.clearPostgreSQLData(dataType || 'all');
      return c.json(result, result.success ? 200 : 503);
    } catch (error) {
      logger.error('Error in POST /clear/postgresql', { error });
      return c.json({ success: false, error: 'Internal server error' }, 500);
    }
  });

  // Statistics endpoints
  pipeline.get('/stats/exchanges', async c => {
    try {
      const result = await pipelineService.getExchangeStats();
      if (result.success) {
        return c.json(result.data);
      } else {
        return c.json({ error: result.error }, 503);
      }
    } catch (error) {
      logger.error('Error in GET /stats/exchanges', { error });
      return c.json({ error: 'Internal server error' }, 500);
    }
  });

  pipeline.get('/stats/provider-mappings', async c => {
    try {
      const result = await pipelineService.getProviderMappingStats();
      if (result.success) {
        return c.json(result.data);
      } else {
        return c.json({ error: result.error }, 503);
      }
    } catch (error) {
      logger.error('Error in GET /stats/provider-mappings', { error });
      return c.json({ error: 'Internal server error' }, 500);
    }
  });

  return pipeline;
}
