#!/usr/bin/env node

/**
 * Superior Screenshot Command - Direct Implementation
 * Uses the proven parallel technique without API dependencies
 * Ready for integration into the main system
 */

const SCALP_TIMEFRAMES = [
  { name: '1m', tv: '1', description: 'Ultra-short scalping' },
  { name: '3m', tv: '3', description: 'Short scalping' },
  { name: '5m', tv: '5', description: 'Standard scalping' },
  { name: '15m', tv: '15', description: 'Swing scalping' },
  { name: '30m', tv: '30', description: 'Longer scalping' },
  { name: '1h', tv: '60', description: 'Context timeframe' },
  { name: '4h', tv: '240', description: 'Trend context' }
]

class SuperiorScreenshotCommand {
  constructor() {
    this.apiUrl = 'http://localhost:9001'
  }

  async captureScalpPreset(symbol = 'SOLUSD', layouts = ['ai', 'diy']) {
    console.log('🚀 SUPERIOR SCREENSHOT: Scalp Preset Capture')
    console.log(`📊 Symbol: ${symbol}`)
    console.log(`🎨 Layouts: ${layouts.join(', ')}`)
    console.log(`⏱️ Timeframes: ${SCALP_TIMEFRAMES.length}`)
    console.log(`📸 Total Screenshots: ${SCALP_TIMEFRAMES.length * layouts.length}`)

    const startTime = Date.now()

    try {
      // Use the proven parallel approach - each timeframe in parallel
      const capturePromises = SCALP_TIMEFRAMES.map(async (tf, index) => {
        try {
          console.log(`📸 [${index + 1}/${SCALP_TIMEFRAMES.length}] ${tf.name} (${tf.tv})...`)
          
          // Direct API call to the working enhanced-screenshot endpoint
          const response = await fetch(`${this.apiUrl}/api/enhanced-screenshot`, {
            method: 'POST',
            headers: { 'Content-Type': 'application/json' },
            body: JSON.stringify({
              symbol: symbol,
              timeframe: tf.tv,
              layouts: layouts,
              analyze: false
            })
          })
          
          if (!response.ok) {
            throw new Error(`HTTP ${response.status}`)
          }
          
          const result = await response.json()
          
          if (result.success && result.screenshots) {
            console.log(`✅ ${tf.name}: ${result.screenshots.length} screenshots`)
            return {
              timeframe: tf.tv,
              name: tf.name,
              success: true,
              screenshots: result.screenshots,
              count: result.screenshots.length
            }
          } else {
            throw new Error(result.error || 'API returned no screenshots')
          }
          
        } catch (error) {
          console.error(`❌ ${tf.name}: ${error.message}`)
          return {
            timeframe: tf.tv,
            name: tf.name,
            success: false,
            error: error.message
          }
        }
      })

      console.log('\n⏳ Waiting for all parallel captures...')
      const results = await Promise.all(capturePromises)

      const endTime = Date.now()
      const duration = (endTime - startTime) / 1000

      // Analyze results
      const successful = results.filter(r => r.success)
      const failed = results.filter(r => !r.success)
      const totalScreenshots = successful.reduce((sum, r) => sum + (r.count || 0), 0)

      console.log('\n✅ SCALP PRESET COMPLETED!')
      console.log(`⏱️ Duration: ${duration.toFixed(2)} seconds`)
      console.log(`📸 Screenshots: ${totalScreenshots}/${SCALP_TIMEFRAMES.length * layouts.length}`)
      console.log(`🎯 Success Rate: ${((successful.length / SCALP_TIMEFRAMES.length) * 100).toFixed(1)}%`)
      console.log(`🚀 Speed: ${(totalScreenshots / duration).toFixed(1)} screenshots/second`)

      // Show detailed results
      console.log('\n📊 TIMEFRAME RESULTS:')
      SCALP_TIMEFRAMES.forEach(tf => {
        const result = results.find(r => r.name === tf.name)
        if (result) {
          if (result.success) {
            console.log(`   ✅ ${tf.name.padEnd(4)}: ${result.count} screenshots`)
          } else {
            console.log(`   ❌ ${tf.name.padEnd(4)}: ${result.error}`)
          }
        }
      })

      if (successful.length === SCALP_TIMEFRAMES.length) {
        console.log('\n🎉 PERFECT SUCCESS: All scalping timeframes captured!')
      } else if (successful.length > 0) {
        console.log(`\n⚠️ PARTIAL SUCCESS: ${successful.length}/${SCALP_TIMEFRAMES.length} timeframes`)
      } else {
        console.log('\n❌ COMPLETE FAILURE: No timeframes captured')
      }

      return {
        success: successful.length > 0,
        duration,
        totalScreenshots,
        successfulTimeframes: successful.length,
        totalTimeframes: SCALP_TIMEFRAMES.length,
        successRate: (successful.length / SCALP_TIMEFRAMES.length) * 100,
        results
      }

    } catch (error) {
      console.error('❌ Scalp preset failed:', error.message)
      throw error
    }
  }

  async captureQuick(symbol = 'SOLUSD', timeframe = '60', layouts = ['ai']) {
    console.log(`🚀 QUICK CAPTURE: ${symbol} ${timeframe}`)
    
    try {
      const response = await fetch(`${this.apiUrl}/api/enhanced-screenshot`, {
        method: 'POST',
        headers: { 'Content-Type': 'application/json' },
        body: JSON.stringify({
          symbol: symbol,
          timeframe: timeframe,
          layouts: layouts,
          analyze: false
        })
      })
      
      if (!response.ok) {
        throw new Error(`HTTP ${response.status}`)
      }
      
      const result = await response.json()
      
      if (result.success && result.screenshots) {
        console.log(`✅ Quick capture: ${result.screenshots.length} screenshots`)
        return result.screenshots
      } else {
        throw new Error(result.error || 'No screenshots returned')
      }
      
    } catch (error) {
      console.error('❌ Quick capture failed:', error.message)
      throw error
    }
  }
}

// CLI interface
async function main() {
  const command = new SuperiorScreenshotCommand()
  
  const args = process.argv.slice(2)
  const mode = args[0] || 'scalp'
  const symbol = args[1] || 'SOLUSD'
  
  try {
    if (mode === 'quick') {
      const timeframe = args[2] || '60'
      await command.captureQuick(symbol, timeframe)
    } else {
      // Default to scalp preset
      await command.captureScalpPreset(symbol)
    }
    
    console.log('\n🎯 SUPERIOR SCREENSHOT COMMAND COMPLETED!')
    
  } catch (error) {
    console.error('\n❌ Command failed:', error.message)
    process.exit(1)
  }
}

// Export for integration
module.exports = { SuperiorScreenshotCommand, SCALP_TIMEFRAMES }

// Run if called directly
if (require.main === module) {
  main()
}
