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

const prisma = new PrismaClient()

async function removeDemoTrades() {
  try {
    console.log('🧹 Removing demo trades...')

    // Remove demo trades
    const deletedTrades = await prisma.trade.deleteMany({
      where: {
        userId: 'demo-user',
        tradingMode: 'SIMULATION',
        isAutomated: true
      }
    })

    console.log(`✅ Removed ${deletedTrades.count} demo trades`)

    // Optionally remove demo user if no other data
    const remainingUserData = await prisma.trade.count({
      where: { userId: 'demo-user' }
    })

    if (remainingUserData === 0) {
      await prisma.user.delete({
        where: { id: 'demo-user' }
      })
      console.log('✅ Removed demo user (no remaining data)')
    } else {
      console.log(`ℹ️  Demo user kept (has ${remainingUserData} other records)`)
    }

    console.log('🎉 Demo data cleanup completed!')

  } catch (error) {
    console.error('❌ Error removing demo trades:', error)
  } finally {
    await prisma.$disconnect()
  }
}

if (require.main === module) {
  removeDemoTrades()
}

module.exports = { removeDemoTrades }
