/**
 * QM Operation Provider - Defines operations for QuoteMedia data source
 */

import { BaseOperationProvider, OperationRegistry, type OperationConfig, type ProviderConfig } from '../../../shared/operation-manager';

/**
 * QM operation definitions
 */
export const QM_OPERATIONS: OperationConfig[] = [
  // Symbol metadata
  {
    name: 'symbol_info',
    type: 'standard',
    description: 'Update symbol metadata',
    defaultStaleHours: 24 * 7 // Weekly
  },
  
  // Price data
  {
    name: 'price_update',
    type: 'standard',
    description: 'Update daily price data',
    defaultStaleHours: 24
  },
  {
    name: 'intraday_bars',
    type: 'intraday_crawl',
    description: 'Crawl intraday price bars from today backwards',
    requiresFinishedFlag: true,
    defaultStaleHours: 1 // Check every hour for new data
  },
  
  // Fundamental data
  {
    name: 'financials_update_quarterly',
    type: 'standard',
    description: 'Update quarterly financial statements',
    defaultStaleHours: 24 * 7 // Weekly
  },
  {
    name: 'financials_update_annual',
    type: 'standard',
    description: 'Update annual financial statements',
    defaultStaleHours: 24 * 7 // Weekly
  },
  
  // Corporate actions
  {
    name: 'events_update',
    type: 'standard',
    description: 'Update events (earnings, dividends, splits)',
    defaultStaleHours: 24 * 7 // Weekly
  },
  
  // Filings
  {
    name: 'filings_update',
    type: 'standard',
    description: 'Update SEC filings',
    defaultStaleHours: 24 // Daily
  },
  
  // Insider trading
  {
    name: 'insiders_update',
    type: 'standard',
    description: 'Update insider transactions',
    defaultStaleHours: 24 * 7 // Weekly
  },
  
  // News
  {
    name: 'news_update',
    type: 'standard',
    description: 'Update symbol-specific news',
    defaultStaleHours: 24 * 7 // Weekly
  }
];

/**
 * QM Operation Provider
 */
export class QMOperationProvider extends BaseOperationProvider {
  getProviderConfig(): ProviderConfig {
    return {
      name: 'qm',
      collectionName: 'qmSymbols',
      symbolField: 'qmSearchCode',
      description: 'QuoteMedia data provider'
    };
  }

  getOperations(): OperationConfig[] {
    return QM_OPERATIONS;
  }
}

/**
 * Create and initialize QM operation registry
 */
export async function createQMOperationRegistry(
  mongodb: any,
  logger: any
): Promise<OperationRegistry> {
  const registry = new OperationRegistry({ mongodb, logger });
  const provider = new QMOperationProvider({ mongodb, logger });
  await registry.registerProvider(provider);
  return registry;
}