#!/usr/bin/env bun

/**
 * Test script for CEO handler operations
 */
import { initializeServiceConfig } from '@stock-bot/config';
import { createServiceContainer, initializeServices } from '@stock-bot/di';
import { getLogger } from '@stock-bot/logger';

const logger = getLogger('test-ceo-operations');

async function testCeoOperations() {
  logger.info('Testing CEO handler operations...');

  try {
    // Initialize config
    const config = initializeServiceConfig();

    // Create Awilix container
    const awilixConfig = {
      redis: {
        host: config.database.dragonfly.host,
        port: config.database.dragonfly.port,
        db: config.database.dragonfly.db,
      },
      mongodb: {
        uri: config.database.mongodb.uri,
        database: config.database.mongodb.database,
      },
      postgres: {
        host: config.database.postgres.host,
        port: config.database.postgres.port,
        database: config.database.postgres.database,
        user: config.database.postgres.user,
        password: config.database.postgres.password,
      },
      questdb: {
        enabled: false,
        host: config.database.questdb.host,
        httpPort: config.database.questdb.httpPort,
        pgPort: config.database.questdb.pgPort,
        influxPort: config.database.questdb.ilpPort,
        database: config.database.questdb.database,
      },
    };

    const container = createServiceContainer(awilixConfig);
    await initializeServices(container);

    const serviceContainer = container.resolve('serviceContainer');

    // Import and create CEO handler
    const { CeoHandler } = await import('./src/handlers/ceo/ceo.handler');
    const ceoHandler = new CeoHandler(serviceContainer);

    // Test 1: Check if there are any CEO symbols in the database
    logger.info('Checking for existing CEO symbols...');
    const collection = serviceContainer.mongodb.collection('ceoSymbols');
    const count = await collection.countDocuments();
    logger.info(`Found ${count} CEO symbols in database`);

    if (count > 0) {
      // Test 2: Run process-unique-symbols operation
      logger.info('Testing process-unique-symbols operation...');
      const result = await ceoHandler.updateUniqueSymbols(undefined, {});
      logger.info('Process unique symbols result:', result);

      // Test 3: Test individual symbol processing
      logger.info('Testing process-individual-symbol operation...');
      const sampleSymbol = await collection.findOne({});
      if (sampleSymbol) {
        const individualResult = await ceoHandler.processIndividualSymbol(
          {
            ceoId: sampleSymbol.ceoId,
            symbol: sampleSymbol.symbol,
            exchange: sampleSymbol.exchange,
            name: sampleSymbol.name,
          },
          {}
        );
        logger.info('Process individual symbol result:', individualResult);
      }
    } else {
      logger.warn('No CEO symbols found. Run the service to populate data first.');
    }

    // Clean up
    await serviceContainer.mongodb.disconnect();
    await serviceContainer.postgres.disconnect();
    if (serviceContainer.cache) {
      await serviceContainer.cache.disconnect();
    }

    logger.info('Test completed successfully!');
    process.exit(0);
  } catch (error) {
    logger.error('Test failed:', error);
    process.exit(1);
  }
}

// Run the test
testCeoOperations();
