import { BaseHandler, Handler, Operation, ScheduledOperation } from '@stock-bot/handlers';
import type { IServiceContainer } from '@stock-bot/types';
import { clearPostgreSQLData } from './operations/clear-postgresql-data.operations';
import { getSyncStatus } from './operations/enhanced-sync-status.operations';
import { getExchangeStats } from './operations/exchange-stats.operations';
import { getProviderMappingStats } from './operations/provider-mapping-stats.operations';
import { syncQMExchanges } from './operations/qm-exchanges.operations';
import { syncAllExchanges } from './operations/sync-all-exchanges.operations';
import { syncIBExchanges } from './operations/sync-ib-exchanges.operations';
import { syncQMProviderMappings } from './operations/sync-qm-provider-mappings.operations';

@Handler('exchanges')
class ExchangesHandler extends BaseHandler {
  constructor(services: IServiceContainer) {
    super(services);
  }

  /**
   * Sync all exchanges - weekly full sync
   */
  @Operation('sync-all-exchanges')
  @ScheduledOperation('sync-all-exchanges', '0 0 * * 0', {
    priority: 10,
    description: 'Weekly full exchange sync on Sunday at midnight',
  })
  async syncAllExchanges(payload?: { clearFirst?: boolean }): Promise<unknown> {
    const finalPayload = payload || { clearFirst: true };
    this.log('info', 'Starting sync of all exchanges', finalPayload);
    return syncAllExchanges(finalPayload, this.services);
  }

  /**
   * Sync exchanges from QuestionsAndMethods
   */
  @Operation('sync-qm-exchanges')
  @ScheduledOperation('sync-qm-exchanges', '0 1 * * *', {
    priority: 5,
    description: 'Daily sync of QM exchanges at 1 AM',
  })
  async syncQMExchanges(): Promise<unknown> {
    this.log('info', 'Starting QM exchanges sync...');
    return syncQMExchanges({}, this.services);
  }

  /**
   * Sync exchanges from Interactive Brokers
   */
  @Operation('sync-ib-exchanges')
  @ScheduledOperation('sync-ib-exchanges', '0 3 * * *', {
    priority: 3,
    description: 'Daily sync of IB exchanges at 3 AM',
  })
  async syncIBExchanges(): Promise<unknown> {
    this.log('info', 'Starting IB exchanges sync...');
    return syncIBExchanges({}, this.services);
  }

  /**
   * Sync provider mappings from QuestionsAndMethods
   */
  @Operation('sync-qm-provider-mappings')
  @ScheduledOperation('sync-qm-provider-mappings', '0 3 * * *', {
    priority: 7,
    description: 'Daily sync of QM provider mappings at 3 AM',
  })
  async syncQMProviderMappings(): Promise<unknown> {
    this.log('info', 'Starting QM provider mappings sync...');
    return syncQMProviderMappings({}, this.services);
  }

  /**
   * Clear PostgreSQL data - maintenance operation
   */
  @Operation('clear-postgresql-data')
  async clearPostgreSQLData(payload: {
    type?: 'exchanges' | 'provider_mappings' | 'all';
  }): Promise<unknown> {
    this.log('warn', 'Clearing PostgreSQL data', payload);
    return clearPostgreSQLData(payload, this.services);
  }

  /**
   * Get exchange statistics
   */
  @Operation('get-exchange-stats')
  async getExchangeStats(): Promise<unknown> {
    this.log('info', 'Getting exchange statistics...');
    return getExchangeStats({}, this.services);
  }

  /**
   * Get provider mapping statistics
   */
  @Operation('get-provider-mapping-stats')
  async getProviderMappingStats(): Promise<unknown> {
    this.log('info', 'Getting provider mapping statistics...');
    return getProviderMappingStats({}, this.services);
  }

  /**
   * Get enhanced sync status
   */
  @Operation('enhanced-sync-status')
  async getEnhancedSyncStatus(): Promise<unknown> {
    this.log('info', 'Getting enhanced sync status...');
    return getSyncStatus({}, this.services);
  }
}

export { ExchangesHandler };
