#!/usr/bin/env node

/**
 * Test Drift Feedback Loop System
 * Comprehensive test of the real-trade learning feedback system
 */

async function testDriftFeedbackLoop() {
  console.log('🧪 TESTING DRIFT FEEDBACK LOOP SYSTEM')
  console.log('='.repeat(60))
  
  try {
    console.log('📊 Step 1: Testing Feedback Loop API Endpoints...')
    
    // Test 1: Get current status
    console.log('\n🔍 Checking current feedback loop status...')
    const statusResponse = await fetch('http://localhost:3000/api/drift/feedback', {
      method: 'GET'
    })
    const statusResult = await statusResponse.json()
    console.log('Status:', statusResult)
    
    // Test 2: Start monitoring
    console.log('\n🚀 Starting feedback loop monitoring...')
    const startResponse = await fetch('http://localhost:3000/api/drift/feedback', {
      method: 'POST',
      headers: { 'Content-Type': 'application/json' },
      body: JSON.stringify({
        action: 'start_monitoring',
        userId: 'drift-user'
      })
    })
    const startResult = await startResponse.json()
    console.log('Start result:', startResult)
    
    // Test 3: Check trades manually
    console.log('\n🔍 Triggering manual trade check...')
    const checkResponse = await fetch('http://localhost:3000/api/drift/feedback', {
      method: 'POST',
      headers: { 'Content-Type': 'application/json' },
      body: JSON.stringify({
        action: 'check_trades',
        userId: 'drift-user'
      })
    })
    const checkResult = await checkResponse.json()
    console.log('Check result:', checkResult)
    
    // Test 4: Get learning insights
    console.log('\n🧠 Getting learning insights...')
    const insightsResponse = await fetch('http://localhost:3000/api/drift/feedback', {
      method: 'POST',
      headers: { 'Content-Type': 'application/json' },
      body: JSON.stringify({
        action: 'get_insights',
        userId: 'drift-user'
      })
    })
    const insightsResult = await insightsResponse.json()
    console.log('Learning insights:', JSON.stringify(insightsResult, null, 2))
    
    // Test 5: Test with a real small trade (if confirmed)
    if (process.argv.includes('--place-test-trade')) {
      console.log('\n💰 Placing small test trade to verify learning capture...')
      
      const testTradeResponse = await fetch('http://localhost:3000/api/drift/trade', {
        method: 'POST',
        headers: { 'Content-Type': 'application/json' },
        body: JSON.stringify({
          action: 'place_order',
          symbol: 'SOL',
          side: 'buy',
          amount: 3, // Small amount for testing
          leverage: 1,
          stopLoss: true,
          takeProfit: true,
          stopLossPercent: 1.0,
          takeProfitPercent: 2.0
        })
      })
      
      const testTradeResult = await testTradeResponse.json()
      console.log('Test trade result:', JSON.stringify(testTradeResult, null, 2))
      
      if (testTradeResult.success && testTradeResult.result.success) {
        console.log('✅ Test trade placed successfully!')
        console.log('🔗 Transaction ID:', testTradeResult.result.transactionId)
        
        // Wait a moment, then check if learning record was created
        console.log('\n⏳ Waiting 10 seconds for learning record creation...')
        await new Promise(resolve => setTimeout(resolve, 10000))
        
        const updatedInsights = await fetch('http://localhost:3000/api/drift/feedback', {
          method: 'POST',
          headers: { 'Content-Type': 'application/json' },
          body: JSON.stringify({
            action: 'get_insights',
            userId: 'drift-user'
          })
        })
        const updatedInsightsResult = await updatedInsights.json()
        console.log('Updated insights after trade:', updatedInsightsResult.insights.recentPerformance)
      }
    }
    
    console.log('\n📈 Step 2: Testing Database Learning Records...')
    
    // Test database connection and recent records
    const { PrismaClient } = await import('@prisma/client')
    const prisma = new PrismaClient()
    
    // Check recent trades
    const recentTrades = await prisma.trade.findMany({
      where: {
        userId: 'drift-user',
        driftTxId: { not: null }
      },
      orderBy: { createdAt: 'desc' },
      take: 5
    })
    
    console.log(`📊 Found ${recentTrades.length} recent Drift trades in database`)
    recentTrades.forEach((trade, index) => {
      console.log(`   ${index + 1}. ${trade.symbol} ${trade.side} - Status: ${trade.status} - Outcome: ${trade.outcome || 'PENDING'}`)
    })
    
    // Check AI learning data
    const learningRecords = await prisma.aILearningData.findMany({
      where: {
        userId: 'drift-user'
      },
      orderBy: { createdAt: 'desc' },
      take: 3
    })
    
    console.log(`🧠 Found ${learningRecords.length} AI learning records`)
    
    await prisma.$disconnect()
    
    console.log('\n🎯 Step 3: Feedback Loop Validation...')
    
    // Test feedback loop components
    const components = {
      'Trade Execution Capture': recentTrades.length > 0,
      'Learning Record Creation': learningRecords.length > 0,
      'API Endpoints': statusResult.success,
      'Monitoring System': startResult.success,
      'Real-time Checking': checkResult.success,
      'Insights Generation': insightsResult.success
    }
    
    console.log('\n✅ Component Status:')
    Object.entries(components).forEach(([component, status]) => {
      console.log(`   ${status ? '✅' : '❌'} ${component}`)
    })
    
    const allWorking = Object.values(components).every(status => status)
    
    console.log('\n🎉 FEEDBACK LOOP TEST RESULTS:')
    console.log('='.repeat(40))
    
    if (allWorking) {
      console.log('✅ ALL SYSTEMS OPERATIONAL!')
      console.log('🔄 Drift feedback loop is ready for real trading')
      console.log('📚 AI will learn from every real Drift trade')
      console.log('🚀 System will continuously improve based on outcomes')
    } else {
      console.log('⚠️ Some components need attention')
      console.log('🔧 Check the failed components above')
    }
    
    console.log('\n💡 USAGE INSTRUCTIONS:')
    console.log('1. Start monitoring: POST /api/drift/feedback {"action":"start_monitoring"}')
    console.log('2. Place trades normally via /api/drift/trade')
    console.log('3. System automatically captures outcomes and learns')
    console.log('4. Get insights: POST /api/drift/feedback {"action":"get_insights"}')
    
    if (!process.argv.includes('--place-test-trade')) {
      console.log('\n💰 To test with real trade: node test-drift-feedback-loop.js --place-test-trade')
    }
    
  } catch (error) {
    console.error('❌ Test failed:', error.message)
    
    if (error.message.includes('ECONNREFUSED')) {
      console.log('\n💡 Solution: Make sure the trading bot is running:')
      console.log('   docker compose -f docker-compose.dev.yml up')
    }
  }
}

async function demonstrateWorkflow() {
  console.log('\n🔄 DRIFT FEEDBACK LOOP WORKFLOW DEMONSTRATION')
  console.log('='.repeat(60))
  
  console.log(`
📈 NORMAL TRADING WORKFLOW WITH FEEDBACK LOOP:

1. 🤖 AI analyzes chart screenshot
   → Generates analysis with confidence score
   → Stored in ai_learning_data table

2. 💰 Trade is placed on Drift Protocol  
   → Creates trade record with AI metadata
   → Links to AI analysis via tradeId

3. 🔄 Feedback loop monitors trade outcomes
   → Checks every 30 seconds for position changes
   → Detects when stop loss/take profit is hit

4. 📊 Outcome is captured and analyzed
   → Updates trade record with outcome (WIN/LOSS/BREAKEVEN)
   → Calculates actual P&L and risk/reward ratio
   → Links back to original AI analysis

5. 🧠 AI learning is updated
   → Analysis accuracy is measured
   → Confidence validation is performed
   → Pattern success rates are calculated

6. 🚀 AI improves for next trade
   → Uses historical outcome data
   → Adjusts confidence based on past accuracy
   → Optimizes strategies based on what works

RESULT: Self-improving AI that gets better with each trade! 🎯
`)
  
  console.log('📚 DATABASE SCHEMA FOR LEARNING:')
  console.log(`
Trades Table:
- Records every Drift trade with outcome
- Links to AI analysis that generated it
- Tracks P&L, risk/reward, execution details

AI Learning Data Table:  
- Stores every AI analysis and prediction
- Updated with actual outcomes when available
- Builds database of AI accuracy over time

Feedback Loop Process:
- Monitors Drift positions in real-time
- Captures exact trade outcomes
- Feeds results back to AI learning system
`)
}

if (require.main === module) {
  testDriftFeedbackLoop().then(() => {
    if (!process.argv.includes('--no-demo')) {
      demonstrateWorkflow()
    }
  })
}

module.exports = { testDriftFeedbackLoop }
