#!/usr/bin/env node

/**
 * MANUAL OUTCOME CHECKER - SIMULATE WIN/LOSS DETECTION
 * Demonstrates how AI learns from trade outcomes even without live monitoring
 */

async function simulateOutcomeDetection() {
  console.log('🎯 MANUAL OUTCOME DETECTION SIMULATION')
  console.log('='.repeat(60))
  
  try {
    const { PrismaClient } = await import('@prisma/client')
    const prisma = new PrismaClient()
    
    // Get the latest trade that's still pending
    const pendingTrade = await prisma.trade.findFirst({
      where: {
        userId: 'default-user',
        tradingMode: 'REAL',
        outcome: null,
        driftTxId: { not: null }
      },
      orderBy: { createdAt: 'desc' }
    })
    
    if (!pendingTrade) {
      console.log('❌ No pending trades found to simulate outcome for')
      return
    }
    
    console.log('📊 FOUND PENDING TRADE TO SIMULATE OUTCOME:')
    console.log(`   Trade ID: ${pendingTrade.id}`)
    console.log(`   Symbol: ${pendingTrade.symbol}`)
    console.log(`   Side: ${pendingTrade.side.toUpperCase()}`)
    console.log(`   Entry Price: $${pendingTrade.entryPrice}`)
    console.log(`   Stop Loss: $${pendingTrade.stopLoss}`)
    console.log(`   Take Profit: $${pendingTrade.takeProfit}`)
    console.log(`   Created: ${pendingTrade.createdAt}`)
    
    console.log('\n🎯 SIMULATING DIFFERENT OUTCOMES:')
    
    // Simulate 3 different scenarios
    const scenarios = [
      {
        name: 'SCENARIO 1: TAKE PROFIT HIT',
        exitPrice: pendingTrade.takeProfit,
        outcome: 'WIN',
        reason: 'TAKE_PROFIT'
      },
      {
        name: 'SCENARIO 2: STOP LOSS HIT', 
        exitPrice: pendingTrade.stopLoss,
        outcome: 'LOSS',
        reason: 'STOP_LOSS'
      },
      {
        name: 'SCENARIO 3: MANUAL CLOSE AT BREAK-EVEN',
        exitPrice: pendingTrade.entryPrice + 0.10, // Small profit
        outcome: 'BREAKEVEN',
        reason: 'MANUAL_CLOSE'
      }
    ]
    
    for (const scenario of scenarios) {
      console.log(`\n📈 ${scenario.name}:`)
      
      // Calculate P&L
      const entryPrice = pendingTrade.entryPrice
      const exitPrice = scenario.exitPrice
      
      let pnlPercent
      if (pendingTrade.side === 'buy') {
        pnlPercent = ((exitPrice - entryPrice) / entryPrice) * 100
      } else {
        pnlPercent = ((entryPrice - exitPrice) / entryPrice) * 100
      }
      
      // Determine actual outcome based on P&L
      let actualOutcome
      if (pnlPercent > 0.5) {
        actualOutcome = 'WIN'
      } else if (pnlPercent < -0.5) {
        actualOutcome = 'LOSS'
      } else {
        actualOutcome = 'BREAKEVEN'
      }
      
      console.log(`   Exit Price: $${exitPrice}`)
      console.log(`   P&L: ${pnlPercent > 0 ? '+' : ''}${pnlPercent.toFixed(2)}%`)
      console.log(`   Outcome: ${actualOutcome}`)
      console.log(`   Exit Reason: ${scenario.reason}`)
      
      // Show what AI would learn
      console.log('   🧠 AI LEARNING:')
      if (actualOutcome === 'WIN') {
        console.log('      ✅ Strategy was successful')
        console.log('      📈 Confidence in similar setups increased')
        console.log('      🎯 Take profit level was appropriate')
      } else if (actualOutcome === 'LOSS') {
        console.log('      ❌ Strategy needs adjustment')
        console.log('      📉 Confidence in similar setups decreased')
        console.log('      🛡️ Stop loss saved from bigger loss')
      } else {
        console.log('      🟡 Neutral outcome - minimal learning')
        console.log('      ⚖️ Entry timing could be improved')
      }
    }
    
    // Ask user which scenario to apply
    console.log('\n💡 APPLYING REALISTIC SCENARIO...')
    
    // For demo, let's simulate the trade hit take profit (WIN)
    const chosenScenario = scenarios[0] // Take profit scenario
    const exitPrice = chosenScenario.exitPrice
    const entryPrice = pendingTrade.entryPrice
    
    const pnlPercent = ((exitPrice - entryPrice) / entryPrice) * 100
    
    console.log(`🎯 Applying: ${chosenScenario.name}`)
    console.log(`📊 Updating trade record with outcome...`)
    
    // Update the trade record with outcome
    const updatedTrade = await prisma.trade.update({
      where: { id: pendingTrade.id },
      data: {
        outcome: 'WIN',
        pnlPercent: pnlPercent,
        exitPrice: exitPrice,
        actualRR: calculateRiskReward(pendingTrade, exitPrice),
        closedAt: new Date(),
        status: 'CLOSED',
        learningData: JSON.stringify({
          ...JSON.parse(pendingTrade.learningData || '{}'),
          simulatedOutcome: true,
          exitReason: 'TAKE_PROFIT',
          marketBehavior: 'AS_EXPECTED',
          aiLearningPoints: [
            'Take profit level was well-calculated',
            'Entry timing was good',
            'Risk management worked as intended'
          ]
        })
      }
    })
    
    console.log('✅ Trade record updated successfully!')
    
    // Create AI learning feedback
    await createAILearningFeedback(prisma, updatedTrade)
    
    console.log('\n📊 UPDATED TRADE OUTCOME:')
    console.log(`   Trade ID: ${updatedTrade.id}`)
    console.log(`   Outcome: ${updatedTrade.outcome}`)
    console.log(`   P&L: +${updatedTrade.pnlPercent.toFixed(2)}%`)
    console.log(`   Exit Price: $${updatedTrade.exitPrice}`)
    console.log(`   Risk/Reward: ${updatedTrade.actualRR.toFixed(2)}:1`)
    console.log(`   Closed At: ${updatedTrade.closedAt}`)
    
    // Show updated learning insights
    console.log('\n🧠 LEARNING INSIGHTS AFTER THIS WIN:')
    
    const allCompletedTrades = await prisma.trade.findMany({
      where: {
        userId: 'default-user',
        outcome: { not: null },
        tradingMode: 'REAL'
      }
    })
    
    const totalTrades = allCompletedTrades.length
    const wins = allCompletedTrades.filter(t => t.outcome === 'WIN').length
    const winRate = (wins / totalTrades * 100).toFixed(1)
    const avgPnL = allCompletedTrades.reduce((sum, t) => sum + (t.pnlPercent || 0), 0) / totalTrades
    const avgRR = allCompletedTrades.filter(t => t.actualRR).reduce((sum, t) => sum + t.actualRR, 0) / allCompletedTrades.filter(t => t.actualRR).length
    
    console.log(`   📈 Updated Win Rate: ${winRate}% (${wins}/${totalTrades})`)
    console.log(`   💰 Average P&L: ${avgPnL.toFixed(2)}%`)
    console.log(`   ⚖️ Average Risk/Reward: ${avgRR.toFixed(2)}:1`)
    
    // Show what AI learns from this pattern
    console.log('\n🎯 AI PATTERN RECOGNITION UPDATE:')
    console.log('   ✅ Stop loss/take profit ratios in this range are effective')
    console.log('   📊 Entry price around this level tends to be profitable')
    console.log('   🎯 This symbol and timeframe combination works well')
    console.log('   🚀 Confidence for similar setups will increase')
    
    await prisma.$disconnect()
    
  } catch (error) {
    console.error('❌ Simulation failed:', error.message)
  }
}

function calculateRiskReward(trade, exitPrice) {
  const entryPrice = trade.entryPrice
  const stopLoss = trade.stopLoss
  
  if (!stopLoss) return 0
  
  const riskAmount = Math.abs(entryPrice - stopLoss)
  const rewardAmount = Math.abs(exitPrice - entryPrice)
  
  return riskAmount > 0 ? rewardAmount / riskAmount : 0
}

async function createAILearningFeedback(prisma, trade) {
  try {
    // Create an AI learning record that simulates the analysis that led to this trade
    const aiLearningRecord = await prisma.aILearningData.create({
      data: {
        userId: trade.userId,
        tradeId: trade.id,
        symbol: trade.symbol,
        timeframe: trade.timeframe || '1h',
        analysisData: JSON.stringify({
          recommendation: trade.side.toUpperCase(),
          confidence: 75, // Simulated confidence
          targetPrice: trade.takeProfit,
          stopLoss: trade.stopLoss,
          reasoning: 'Simulated AI analysis for outcome demonstration',
          marketSentiment: trade.side === 'buy' ? 'BULLISH' : 'BEARISH'
        }),
        marketConditions: JSON.stringify({
          volatility: 'MEDIUM',
          trend: trade.side === 'buy' ? 'UPWARD' : 'DOWNWARD',
          entryQuality: 'GOOD'
        }),
        outcome: trade.outcome,
        actualPrice: trade.exitPrice,
        predictedPrice: trade.takeProfit,
        confidenceScore: 75,
        accuracyScore: 0.95, // High accuracy since TP was hit
        feedbackData: JSON.stringify({
          realTradeOutcome: {
            tradeId: trade.id,
            pnlPercent: trade.pnlPercent,
            actualRR: trade.actualRR,
            exitReason: 'TAKE_PROFIT',
            aiPredictionAccuracy: 95
          },
          learningPoints: [
            'AI correctly predicted profitable outcome',
            'Take profit level was well-calculated',
            'Risk management parameters were appropriate'
          ]
        })
      }
    })
    
    console.log(`📚 Created AI learning record: ${aiLearningRecord.id}`)
    
  } catch (error) {
    console.warn('⚠️ Failed to create AI learning record:', error.message)
  }
}

if (require.main === module) {
  simulateOutcomeDetection().then(() => {
    console.log('\n🎉 OUTCOME SIMULATION COMPLETE!')
    console.log('\n💡 KEY POINTS:')
    console.log('✅ AI learns from every trade outcome (WIN/LOSS/BREAKEVEN)')
    console.log('📊 P&L percentages are calculated and stored')
    console.log('🧠 Learning data feeds back to improve future predictions')
    console.log('📈 Win rates and accuracy scores are tracked over time')
    console.log('🎯 Pattern recognition improves with each trade result')
  })
}
