/**
 * EOD Operation Provider - Defines operations for EOD Historical Data source
 */

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

/**
 * EOD operation definitions
 */
export const EOD_OPERATIONS: OperationConfig[] = [
  // Exchange data
  {
    name: 'exchange_update',
    type: 'standard',
    description: 'Update exchange list',
    defaultStaleHours: 24 * 7 // Weekly
  },
  
  // Symbol data
  {
    name: 'symbol_update',
    type: 'standard',
    description: 'Update symbol list for exchange',
    defaultStaleHours: 24 // Daily
  },
  
  // Price data
  {
    name: 'price_update',
    type: 'standard',
    description: 'Update daily price data',
    defaultStaleHours: 24 * 7 // Weekly - EOD's schedule-fetch-prices runs for symbols not updated in last week
  },
  
  // Intraday data
  {
    name: 'intraday_1m',
    type: 'intraday_crawl',
    description: 'Crawl 1-minute intraday data',
    requiresFinishedFlag: true,
    defaultStaleHours: 24 // Daily check for new data
  },
  {
    name: 'intraday_5m',
    type: 'intraday_crawl',
    description: 'Crawl 5-minute intraday data',
    requiresFinishedFlag: true,
    defaultStaleHours: 24 // Daily check for new data
  },
  {
    name: 'intraday_1h',
    type: 'intraday_crawl',
    description: 'Crawl 1-hour intraday data',
    requiresFinishedFlag: true,
    defaultStaleHours: 24 // Daily check for new data
  },
  
  // Fundamental data
  {
    name: 'fundamentals_update',
    type: 'standard',
    description: 'Update fundamental data',
    defaultStaleHours: 24 * 7 // Weekly
  },
  
  // Corporate actions
  {
    name: 'dividends_update',
    type: 'standard',
    description: 'Update dividend data',
    defaultStaleHours: 24 * 30 // Monthly
  },
  {
    name: 'splits_update',
    type: 'standard',
    description: 'Update stock split data',
    defaultStaleHours: 24 * 30 // Monthly
  },
  
  // Symbol changes
  {
    name: 'symbol_change_update',
    type: 'standard',
    description: 'Update symbol change history',
    defaultStaleHours: 24 * 7 // Weekly
  }
];

/**
 * EOD Operation Provider
 */
export class EODOperationProvider extends BaseOperationProvider {
  getProviderConfig(): ProviderConfig {
    return {
      name: 'eod',
      collectionName: 'eodSymbols',
      symbolField: 'eodSearchCode',
      description: 'EOD Historical Data provider'
    };
  }

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

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