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

const prisma = new PrismaClient()

async function createDemoTrade() {
  try {
    console.log('🎯 Creating demo trade for price monitoring...')

    // Create a demo user if doesn't exist
    const user = await prisma.user.upsert({
      where: { id: 'demo-user' },
      update: {},
      create: {
        id: 'demo-user',
        email: 'demo@trading-bot.com',
        name: 'Demo User'
      }
    })

    // Create a demo trade - SOLUSD Buy position
    const currentSolPrice = 190.82 // Current SOL price
    const trade = await prisma.trade.create({
      data: {
        userId: user.id,
        symbol: 'SOLUSD',
        side: 'BUY',
        amount: 100, // $100 investment
        price: currentSolPrice,
        entryPrice: currentSolPrice,
        status: 'OPEN',
        stopLoss: currentSolPrice * 0.98, // 2% stop loss
        takeProfit: currentSolPrice * 1.06, // 6% take profit
        leverage: 1,
        timeframe: '1h',
        tradingMode: 'SIMULATION',
        confidence: 85,
        isAutomated: true,
        createdAt: new Date(),
        updatedAt: new Date()
      }
    })

    console.log('✅ Demo trade created:')
    console.log(`   ID: ${trade.id}`)
    console.log(`   Symbol: ${trade.symbol}`)
    console.log(`   Side: ${trade.side}`)
    console.log(`   Entry Price: $${trade.entryPrice}`)
    console.log(`   Stop Loss: $${trade.stopLoss?.toFixed(2)}`)
    console.log(`   Take Profit: $${trade.takeProfit?.toFixed(2)}`)
    console.log(`   Amount: $${trade.amount}`)
    console.log()

    // Also create one for BTC
    const currentBtcPrice = 119050
    const btcTrade = await prisma.trade.create({
      data: {
        userId: user.id,
        symbol: 'BTCUSD',
        side: 'SELL',
        amount: 200,
        price: currentBtcPrice,
        entryPrice: currentBtcPrice,
        status: 'OPEN',
        stopLoss: currentBtcPrice * 1.02, // 2% stop loss (higher for short)
        takeProfit: currentBtcPrice * 0.94, // 6% take profit (lower for short)
        leverage: 2,
        timeframe: '4h',
        tradingMode: 'SIMULATION',
        confidence: 78,
        isAutomated: true,
        createdAt: new Date(),
        updatedAt: new Date()
      }
    })

    console.log('✅ Second demo trade created:')
    console.log(`   ID: ${btcTrade.id}`)
    console.log(`   Symbol: ${btcTrade.symbol}`)
    console.log(`   Side: ${btcTrade.side}`)
    console.log(`   Entry Price: $${btcTrade.entryPrice}`)
    console.log(`   Stop Loss: $${btcTrade.stopLoss?.toFixed(2)}`)
    console.log(`   Take Profit: $${btcTrade.takeProfit?.toFixed(2)}`)
    console.log(`   Amount: $${btcTrade.amount}`)

    console.log('\n🎉 Demo trades ready! Now you can test the price monitoring system.')
    console.log('💡 Visit http://localhost:9001/automation to see them in action!')

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

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

module.exports = { createDemoTrade }
