import { BaseHandler, Handler, type IServiceContainer } from '@stock-bot/handlers';

@Handler('qm')
export class QMHandler extends BaseHandler {
  constructor(services: IServiceContainer) {
    super(services); // Handler name read from @Handler decorator
  }

  // @Operation('check-sessions')
  // @QueueSchedule('0 */15 * * *', {
  //   priority: 7,
  //   immediately: true,
  //   description: 'Check and maintain QM sessions'
  // })
  // async checkSessions(input: unknown, context: ExecutionContext): Promise<unknown> {
  //   // Call the session maintenance action
  //   const { checkSessions } = await import('./actions/session.action');
  //   return await checkSessions(this);
  // }

  // @Operation('create-session')
  // async createSession(input: unknown, context: ExecutionContext): Promise<unknown> {
  //   // Call the individual session creation action
  //   const { createSingleSession } = await import('./actions/session.action');
  //   return await createSingleSession(this, input);
  // }

  // @Operation('search-symbols')
  // async searchSymbols(_input: unknown, _context: ExecutionContext): Promise<unknown> {
  //   this.logger.info('Searching QM symbols with new DI pattern...');
  //   try {
  //     // Check existing symbols in MongoDB
  //     const symbolsCollection = this.mongodb.collection('qm_symbols');
  //     const symbols = await symbolsCollection.find({}).limit(100).toArray();

  //     this.logger.info('QM symbol search completed', { count: symbols.length });

  //     if (symbols && symbols.length > 0) {
  //       // Cache result for performance
  //       await this.cache.set('qm-symbols-sample', symbols.slice(0, 10), 1800);

  //       return {
  //         success: true,
  //         message: 'QM symbol search completed successfully',
  //         count: symbols.length,
  //         symbols: symbols.slice(0, 10), // Return first 10 symbols as sample
  //       };
  //     } else {
  //       // No symbols found - this is expected initially
  //       this.logger.info('No QM symbols found in database yet');
  //       return {
  //         success: true,
  //         message: 'No symbols found yet - database is empty',
  //         count: 0,
  //       };
  //     }

  //   } catch (error) {
  //     this.logger.error('Failed to search QM symbols', { error });
  //     throw error;
  //   }
  // }

  // @Operation('spider-symbol-search')
  // @QueueSchedule('0 0 * * 0', {
  //   priority: 10,
  //   immediately: false,
  //   description: 'Comprehensive symbol search using QM API'
  // })
  // async spiderSymbolSearch(payload: SymbolSpiderJob | undefined, context: ExecutionContext): Promise<unknown> {
  //   // Set default payload for scheduled runs
  //   const jobPayload: SymbolSpiderJob = payload || {
  //     prefix: null,
  //     depth: 1,
  //     source: 'qm',
  //     maxDepth: 4
  //   };

  //   this.logger.info('Starting QM spider symbol search', { payload: jobPayload });

  //   // Store spider job info in cache (temporary data)
  //   const spiderJobId = `spider:qm:${Date.now()}:${Math.random().toString(36).substr(2, 9)}`;
  //   const spiderResult = {
  //     payload: jobPayload,
  //     startTime: new Date().toISOString(),
  //     status: 'started',
  //     jobId: spiderJobId
  //   };

  //   // Store in cache with 1 hour TTL (temporary data)
  //   await this.cache.set(spiderJobId, spiderResult, 3600);
  //   this.logger.debug('Spider job stored in cache', { spiderJobId, ttl: 3600 });

  //   // Schedule follow-up processing if needed
  //   await this.scheduleOperation('search-symbols', { source: 'spider', spiderJobId }, { delay: 5000 });

  //   return {
  //     success: true,
  //     message: 'QM spider search initiated',
  //     spiderJobId
  //   };
  // }
}
