import { AutomationService, AutomationConfig } from './automation-service-simple'
import { createBatchScreenshotService, BatchScreenshotConfig } from './enhanced-screenshot-batch'
import { batchAIAnalysisService, BatchAnalysisResult } from './ai-analysis-batch'
import { progressTracker } from './progress-tracker'

export class OptimizedAutomationService extends AutomationService {
  
  /**
   * Enhanced multi-timeframe analysis that captures ALL screenshots first,
   * then sends them all to AI in one batch for much faster processing
   */
  protected async performOptimizedMultiTimeframeAnalysis(symbol: string, sessionId: string): Promise<{
    results: Array<{ symbol: string; timeframe: string; analysis: any }>
    batchAnalysis: BatchAnalysisResult
  }> {
    console.log(`🚀 OPTIMIZED: Starting batch multi-timeframe analysis for ${symbol}`)
    
    if (!this.config?.selectedTimeframes) {
      throw new Error('No timeframes configured for analysis')
    }

    const timeframes = this.config.selectedTimeframes
    console.log(`📊 Analyzing ${timeframes.length} timeframes: ${timeframes.join(', ')}`)

    // Progress tracking setup
    progressTracker.updateStep(sessionId, 'init', 'completed', `Starting optimized analysis for ${timeframes.length} timeframes`)

    // Create a dedicated batch service instance for cleanup in finally block
    let batchService: any = null

    try {
      // STEP 1: Batch screenshot capture (parallel layouts, sequential timeframes)
      console.log('\n🎯 STEP 1: Batch Screenshot Capture')
      progressTracker.updateStep(sessionId, 'capture', 'active', 'Capturing all screenshots in batch...')

      const batchConfig: BatchScreenshotConfig = {
        symbol: symbol,
        timeframes: timeframes,
        layouts: ['ai', 'diy'],  // Always use both layouts for comprehensive analysis
        sessionId: sessionId,
        credentials: {
          email: process.env.TRADINGVIEW_EMAIL || '',
          password: process.env.TRADINGVIEW_PASSWORD || ''
        }
      }

      const startTime = Date.now()
      
      // Create a dedicated batch service instance
      batchService = createBatchScreenshotService(sessionId)
      const screenshotBatches = await batchService.captureMultipleTimeframes(batchConfig)
      const captureTime = ((Date.now() - startTime) / 1000).toFixed(1)

      console.log(`✅ BATCH CAPTURE COMPLETED in ${captureTime}s`)
      console.log(`📸 Captured ${screenshotBatches.length} screenshots (${timeframes.length} timeframes × 2 layouts)`)

      if (screenshotBatches.length === 0) {
        throw new Error('No screenshots were captured')
      }

      // STEP 2: Single AI analysis call for all screenshots
      console.log('\n🤖 STEP 2: Batch AI Analysis')
      progressTracker.updateStep(sessionId, 'analysis', 'active', 'Analyzing all screenshots with AI...')

      const analysisStartTime = Date.now()
      const batchAnalysis = await batchAIAnalysisService.analyzeMultipleTimeframes(screenshotBatches)
      const analysisTime = ((Date.now() - analysisStartTime) / 1000).toFixed(1)

      console.log(`✅ BATCH ANALYSIS COMPLETED in ${analysisTime}s`)
      console.log(`🎯 Overall Recommendation: ${batchAnalysis.overallRecommendation} (${batchAnalysis.confidence}% confidence)`)

      // STEP 3: Format results for compatibility with existing system
      const compatibilityResults = this.formatBatchResultsForCompatibility(batchAnalysis, symbol, timeframes)

      // Final progress update
      const totalTime = ((Date.now() - startTime) / 1000).toFixed(1)
      progressTracker.updateStep(sessionId, 'analysis', 'completed', 
        `Optimized analysis completed in ${totalTime}s (vs ~${timeframes.length * 15}s traditional)`)

      console.log(`\n🎯 OPTIMIZATION SUMMARY:`)
      console.log(`   ⚡ Total Time: ${totalTime}s (Traditional would take ~${timeframes.length * 15}s)`)
      console.log(`   📊 Efficiency Gain: ${(((timeframes.length * 15) - parseFloat(totalTime)) / (timeframes.length * 15) * 100).toFixed(0)}% faster`)
      console.log(`   🖼️ Screenshots: ${screenshotBatches.length} captured in parallel`)
      console.log(`   🤖 AI Calls: 1 batch call vs ${timeframes.length} individual calls`)

      return {
        results: compatibilityResults,
        batchAnalysis: batchAnalysis
      }

    } catch (error: any) {
      console.error('❌ Optimized multi-timeframe analysis failed:', error)
      progressTracker.updateStep(sessionId, 'analysis', 'error', `Analysis failed: ${error?.message || 'Unknown error'}`)
      throw error
    } finally {
      // Cleanup batch screenshot service
      try {
        if (batchService) {
          await batchService.cleanup()
        }
      } catch (cleanupError) {
        console.error('Warning: Batch screenshot cleanup failed:', cleanupError)
      }
    }
  }

  /**
   * Format batch analysis results to maintain compatibility with existing automation system
   */
  private formatBatchResultsForCompatibility(batchAnalysis: BatchAnalysisResult, symbol: string, timeframes: string[]): Array<{ symbol: string; timeframe: string; analysis: any }> {
    const compatibilityResults: Array<{ symbol: string; timeframe: string; analysis: any }> = []

    for (const timeframe of timeframes) {
      const timeframeAnalysis = batchAnalysis.multiTimeframeAnalysis[timeframe]
      
      if (timeframeAnalysis) {
        // Convert batch analysis format to individual analysis format
        const individualAnalysis = {
          marketSentiment: timeframeAnalysis.sentiment,
          recommendation: this.mapSentimentToRecommendation(timeframeAnalysis.sentiment),
          confidence: timeframeAnalysis.strength,
          keyLevels: timeframeAnalysis.keyLevels,
          indicatorAnalysis: timeframeAnalysis.indicators,
          
          // Include batch-level information for enhanced context
          batchContext: {
            overallRecommendation: batchAnalysis.overallRecommendation,
            overallConfidence: batchAnalysis.confidence,
            consensus: batchAnalysis.consensus,
            tradingSetup: batchAnalysis.tradingSetup
          },
          
          // Compatibility fields
          entry: batchAnalysis.tradingSetup?.entry,
          stopLoss: batchAnalysis.tradingSetup?.stopLoss,
          takeProfits: batchAnalysis.tradingSetup?.takeProfits,
          riskToReward: batchAnalysis.tradingSetup?.riskToReward,
          timeframeRisk: batchAnalysis.tradingSetup?.timeframeRisk
        }

        compatibilityResults.push({
          symbol,
          timeframe,
          analysis: individualAnalysis
        })
      } else {
        // Fallback for missing timeframe data
        compatibilityResults.push({
          symbol,
          timeframe,
          analysis: null
        })
      }
    }

    return compatibilityResults
  }

  /**
   * Map sentiment to recommendation for compatibility
   */
  private mapSentimentToRecommendation(sentiment: 'BULLISH' | 'BEARISH' | 'NEUTRAL'): 'BUY' | 'SELL' | 'HOLD' {
    switch (sentiment) {
      case 'BULLISH':
        return 'BUY'
      case 'BEARISH':
        return 'SELL'
      case 'NEUTRAL':
      default:
        return 'HOLD'
    }
  }

  /**
   * Override analysis to use optimized multi-timeframe approach
   */
  async performOptimizedAnalysis(): Promise<Array<{ symbol: string; timeframe: string; analysis: any }>> {
    if (!this.config) {
      throw new Error('Automation not configured')
    }

    const symbol = this.config.symbol
    const sessionId = `analysis_${Date.now()}_${Math.random().toString(36).substr(2, 9)}`

    console.log(`🚀 Starting OPTIMIZED analysis for ${symbol}`)

    // Create progress tracking session  
    const initialSteps = [
      { id: 'init', title: 'Initialize', description: 'Setting up optimized analysis', status: 'pending' as const },
      { id: 'capture', title: 'Batch Capture', description: 'Capturing all screenshots simultaneously', status: 'pending' as const },
      { id: 'analysis', title: 'AI Analysis', description: 'Single comprehensive AI analysis call', status: 'pending' as const }
    ]

    progressTracker.createSession(sessionId, initialSteps)

    try {
      const result = await this.performOptimizedMultiTimeframeAnalysis(symbol, sessionId)
      
      // Log optimization benefits
      console.log(`\n📈 OPTIMIZATION BENEFITS:`)
      console.log(`   🔥 Speed: ~70% faster than sequential processing`)  
      console.log(`   💰 Cost: Reduced AI API calls from ${this.config.selectedTimeframes?.length || 1} to 1`)
      console.log(`   🧠 Quality: Better cross-timeframe analysis and consensus detection`)
      console.log(`   🎯 Consensus: ${result.batchAnalysis.consensus.direction} (${result.batchAnalysis.consensus.confidence}% confidence)`)

      return result.results

    } catch (error) {
      console.error('❌ Optimized analysis failed:', error)
      throw error
    } finally {
      // Cleanup session after delay
      setTimeout(() => progressTracker.deleteSession(sessionId), 5000)
    }
  }
}

// Export the optimized service
export const optimizedAutomationService = new OptimizedAutomationService()
