const { PrismaClient } = require('@prisma/client');
const prisma = new PrismaClient();

async function addMissingTrades() {
  try {
    console.log('📝 Adding missing trades based on user screenshots...');
    
    // Trade 1: $11.04 profit (1 hour ago) - SHORT position
    const trade1Id = `manual_${Date.now()}_${Math.random().toString(36).substr(2, 9)}`;
    const oneHourAgo = new Date(Date.now() - 60 * 60 * 1000);
    const fiftyMinutesAgo = new Date(Date.now() - 50 * 60 * 1000);
    
    await prisma.trades.create({
      data: {
        id: trade1Id,
        userId: 'default-user',
        symbol: 'SOLUSD',
        side: 'SHORT',
        amount: 5.5,
        price: 187.053,
        entryPrice: 187.053,
        exitPrice: 185.0,
        profit: 11.04,
        outcome: 'WIN',
        status: 'COMPLETED',
        leverage: 1,
        confidence: 75,
        createdAt: oneHourAgo,
        closedAt: fiftyMinutesAgo,
        driftTxId: `DRIFT_${trade1Id}`,
        tradingMode: 'PERP'
      }
    });
    console.log('✅ Added $11.04 WIN trade (SHORT)');
    
    // Trade 2: -$0.14 loss (30 minutes ago) - SHORT position  
    const trade2Id = `manual_${Date.now() + 1}_${Math.random().toString(36).substr(2, 9)}`;
    const thirtyMinutesAgo = new Date(Date.now() - 30 * 60 * 1000);
    const twentyMinutesAgo = new Date(Date.now() - 20 * 60 * 1000);
    
    await prisma.trades.create({
      data: {
        id: trade2Id,
        userId: 'default-user',
        symbol: 'SOLUSD',
        side: 'SHORT',
        amount: 5.5,
        price: 184.814,
        entryPrice: 184.814,
        exitPrice: 184.795,
        profit: -0.14,
        outcome: 'LOSS',
        status: 'COMPLETED',
        leverage: 1,
        confidence: 75,
        createdAt: thirtyMinutesAgo,
        closedAt: twentyMinutesAgo,
        driftTxId: `DRIFT_${trade2Id}`,
        tradingMode: 'PERP'
      }
    });
    console.log('✅ Added -$0.14 LOSS trade (SHORT)');
    
    // Now get updated statistics
    const allTrades = await prisma.trades.findMany({
      where: {
        status: 'COMPLETED',
        profit: { not: null },
        outcome: { not: null },
        // Filter out simulations
        driftTxId: { not: { startsWith: 'SIM_' } },
        tradingMode: { not: 'SIMULATION' }
      },
      orderBy: { closedAt: 'desc' }
    });
    
    console.log(`📊 Total real completed trades: ${allTrades.length}`);
    
    const totalPnL = allTrades.reduce((sum, trade) => sum + (trade.profit || 0), 0);
    const wins = allTrades.filter(t => (t.profit || 0) > 0).length;
    const losses = allTrades.filter(t => (t.profit || 0) < 0).length;
    const winRate = allTrades.length > 0 ? (wins / allTrades.length * 100) : 0;
    
    console.log('📈 Updated Statistics:');
    console.log(`  Total Trades: ${allTrades.length}`);
    console.log(`  Wins: ${wins}`);
    console.log(`  Losses: ${losses}`);
    console.log(`  Win Rate: ${winRate.toFixed(1)}%`);
    console.log(`  Total P&L: $${totalPnL.toFixed(2)}`);
    
    allTrades.forEach(trade => {
      console.log(`  📊 ${trade.side} - P&L: $${trade.profit?.toFixed(2)} - ${trade.outcome} - ${trade.closedAt?.toISOString()}`);
    });
    
    console.log('✅ Missing trades successfully added to database');
    
  } catch (error) {
    console.error('❌ Error adding missing trades:', error);
  } finally {
    await prisma.$disconnect();
  }
}

addMissingTrades();
