import {
  BaseHandler,
  Handler,
  Operation,
  ScheduledOperation,
  type IServiceContainer,
} from '@stock-bot/handlers';
import type { ProxyInfo } from '@stock-bot/proxy';
import { processItems } from '@stock-bot/queue';
import { fetchProxiesFromSources } from './operations/fetch.operations';
import { checkProxy } from './operations/check.operations';

@Handler('proxy')
export class ProxyHandler extends BaseHandler {
  constructor(services: IServiceContainer) {
    super(services);
  }

  @Operation('fetch-from-sources')
  @ScheduledOperation('proxy-fetch-and-check', '0 0 * * 0', {
    priority: 0,
    description: 'Fetch and validate proxy list from sources',
    // immediately: true, // Don't run immediately during startup to avoid conflicts
  })
  async fetchFromSources(): Promise<{
    processed: number;
    jobsCreated: number;
    batchesCreated?: number;
    mode: string;
  }> {
    // Fetch proxies from all configured sources
    this.logger.info('Processing fetch proxies from sources request');
    
    const proxies = await fetchProxiesFromSources();
    this.logger.info('Fetched proxies from sources', { count: proxies.length });

    if (proxies.length === 0) {
      this.logger.warn('No proxies fetched from sources');
      return { processed: 0, jobsCreated: 0, mode: 'direct' };
    }

    // Get QueueManager from service container
    const queueManager = this.queue;
    if (!queueManager) {
      throw new Error('Queue manager not available');
    }
    
    // Batch process the proxies through check-proxy operation
    const batchResult = await processItems(proxies, 'proxy', {
      handler: 'proxy',
      operation: 'check-proxy',
      totalDelayHours: 0.083, // 5 minutes (5/60 hours)
      batchSize: 50, // Process 50 proxies per batch
      priority: 3,
      useBatching: true,
      retries: 1,
      ttl: 30000, // 30 second timeout per proxy check
      removeOnComplete: 5,
      removeOnFail: 3,
    }, queueManager);

    this.logger.info('Batch proxy validation completed', {
      totalProxies: proxies.length,
      jobsCreated: batchResult.jobsCreated,
      mode: batchResult.mode,
      batchesCreated: batchResult.batchesCreated,
      duration: `${batchResult.duration}ms`,
    });

    return {
      processed: proxies.length,
      jobsCreated: batchResult.jobsCreated,
      batchesCreated: batchResult.batchesCreated,
      mode: batchResult.mode,
    };
  }

  @Operation('check-proxy')
  async checkProxyOperation(payload: ProxyInfo): Promise<unknown> {
    // payload is now the raw proxy info object
    this.logger.debug('Processing proxy check request', {
      proxy: `${payload.host}:${payload.port}`,
    });
    return checkProxy(payload);
  }
}