import type { IServiceContainer } from '@stock-bot/handlers';
import { getLogger } from '@stock-bot/logger';
import type { JobPayload } from '../../../types/job-payloads';

const logger = getLogger('enhanced-sync-exchange-stats');

interface ExchangeStats {
  total_exchanges: string;
  active_exchanges: string;
  countries: string;
  currencies: string;
}

export async function getExchangeStats(
  payload: JobPayload,
  container: IServiceContainer
): Promise<ExchangeStats> {
  logger.info('Getting exchange statistics...');

  try {
    const postgresClient = container.postgres;
    const query = `
      SELECT 
        COUNT(*) as total_exchanges,
        COUNT(CASE WHEN active = true THEN 1 END) as active_exchanges,
        COUNT(DISTINCT country) as countries,
        COUNT(DISTINCT currency) as currencies
      FROM exchanges
    `;
    const result = await postgresClient.query(query);

    logger.info('Retrieved exchange statistics');
    return result.rows[0];
  } catch (error) {
    logger.error('Failed to get exchange statistics', { error });
    throw error;
  }
}
