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

async function cleanupSyntheticTrades() {
  console.log('🧹 Cleaning up synthetic/fake trades from database...')
  
  const prisma = new PrismaClient()
  
  try {
    // Delete synthetic trades that don't have real transaction IDs
    const result = await prisma.trade.deleteMany({
      where: {
        OR: [
          { driftTxId: null },
          { driftTxId: { startsWith: 'settled_pnl' } },
          { driftTxId: { startsWith: 'market_' } },
          { driftTxId: { startsWith: 'position_' } },
          { driftTxId: { startsWith: 'close_' } },
          { driftTxId: { startsWith: 'external_' } },
          { driftTxId: { startsWith: 'api_trade_' } },
          { driftTxId: { startsWith: 'order_' } },
          { driftTxId: { startsWith: 'filled_order_' } },
          // Also clean trades with suspicious amounts (like 7.515070799999999)
          { amount: { gt: 7.5, lt: 7.6 } },
          // Clean trades with round prices like exactly $150
          { price: 150 }
        ]
      }
    })
    
    console.log(`✅ Deleted ${result.count} synthetic trades from database`)
    
    // Show remaining real trades
    const remainingTrades = await prisma.trade.findMany({
      orderBy: { executedAt: 'desc' },
      take: 10
    })
    
    console.log(`📊 Remaining real trades: ${remainingTrades.length}`)
    remainingTrades.forEach((trade, index) => {
      console.log(`${index + 1}. ${trade.symbol} ${trade.side} ${trade.amount} @ $${trade.price} | TX: ${trade.driftTxId}`)
    })
    
  } catch (error) {
    console.error('❌ Error cleaning up synthetic trades:', error)
  } finally {
    await prisma.$disconnect()
  }
}

cleanupSyntheticTrades()
