import { NextResponse } from 'next/server'

export async function POST(request) {
  try {
    const body = await request.json()
    const { 
      symbol, 
      side, 
      amount, 
      leverage = 1, 
      stopLoss, 
      takeProfit, 
      useRealDEX = false 
    } = body

    console.log('🔥 Drift Perpetuals trade request:', { 
      symbol, 
      side, 
      amount, 
      leverage, 
      stopLoss, 
      takeProfit, 
      useRealDEX 
    })

    // Validate inputs
    if (!symbol || !side || !amount) {
      return NextResponse.json(
        { 
          success: false, 
          error: 'Missing required fields: symbol, side, amount' 
        },
        { status: 400 }
      )
    }

    if (!['BUY', 'SELL', 'LONG', 'SHORT'].includes(side.toUpperCase())) {
      return NextResponse.json(
        { 
          success: false, 
          error: 'Invalid side. Must be LONG/SHORT or BUY/SELL' 
        },
        { status: 400 }
      )
    }

    if (amount <= 0) {
      return NextResponse.json(
        { 
          success: false, 
          error: 'Amount must be greater than 0' 
        },
        { status: 400 }
      )
    }

    if (leverage < 1 || leverage > 100) {
      return NextResponse.json(
        { 
          success: false, 
          error: 'Leverage must be between 1x and 100x' 
        },
        { status: 400 }
      )
    }

    // 🛡️ MANDATORY RISK MANAGEMENT VALIDATION - STOP LOSS REQUIRED
    if (!stopLoss) {
      return NextResponse.json(
        {
          success: false,
          error: 'STOP-LOSS REQUIRED: No stop-loss provided',
          details: 'Every trade must have a stop-loss to limit potential losses.',
          riskManagementFailed: true
        },
        { status: 400 }
      )
    }

    // Take profit is recommended but not mandatory for entry
    if (!takeProfit) {
      console.log('⚠️ No take profit provided - trade will rely on manual exit or stop loss');
    }

    console.log(`✅ RISK MANAGEMENT VALIDATION PASSED - SL: $${stopLoss}, TP: $${takeProfit || 'NONE'}`);

    if (!useRealDEX) {
      // Simulation mode
      console.log('🎮 Executing SIMULATED Drift perpetual trade')
      
      const currentPrice = symbol === 'SOL' ? 166.75 : symbol === 'BTC' ? 121819 : 3041.66
      const leveragedAmount = amount * leverage
      const entryFee = leveragedAmount * 0.001 // 0.1% opening fee
      const liquidationPrice = side.toUpperCase().includes('LONG') || side.toUpperCase() === 'BUY'
        ? currentPrice * (1 - 0.9 / leverage) // Approximate liquidation price
        : currentPrice * (1 + 0.9 / leverage)
      
      await new Promise(resolve => setTimeout(resolve, 1200))

      return NextResponse.json({
        success: true,
        trade: {
          txId: `drift_sim_${Date.now()}_${Math.random().toString(36).substr(2, 8)}`,
          orderId: `drift_order_${Date.now()}`,
          symbol: symbol.toUpperCase(),
          side: side.toUpperCase(),
          positionSize: amount,
          leverage: leverage,
          leveragedAmount: leveragedAmount,
          entryPrice: currentPrice,
          liquidationPrice: liquidationPrice,
          entryFee: entryFee,
          timestamp: Date.now(),
          status: 'OPEN',
          platform: 'Drift Protocol (Simulation)',
          stopLoss: stopLoss,
          takeProfit: takeProfit,
          monitoring: !!(stopLoss || takeProfit),
          pnl: 0
        },
        message: `${side.toUpperCase()} perpetual position opened: $${amount} at ${leverage}x leverage - SIMULATED`
      })
    }

    // Real Drift trading implementation
    console.log('💰 Executing REAL Drift perpetual trade')

    // Import Drift SDK components including Wallet and BN
    const { DriftClient, initialize, MarketType, PositionDirection, OrderType, OrderTriggerCondition, Wallet, BN } = await import('@drift-labs/sdk')
    const { Connection, Keypair } = await import('@solana/web3.js')

    // Initialize connection and wallet with configured RPC endpoints in priority order
    const rpcEndpoints = [
      process.env.SOLANA_RPC_URL_PRIMARY,    // Helius (best for trading)
      process.env.SOLANA_RPC_URL_SECONDARY,  // Solana official
      process.env.SOLANA_RPC_URL_TERTIARY,   // Alchemy
      process.env.SOLANA_RPC_URL_BACKUP,     // Ankr
      process.env.SOLANA_RPC_URL,            // Fallback env var
      'https://mainnet.helius-rpc.com/?api-key=5e236449-f936-4af7-ae38-f15e2f1a3757'
    ].filter(Boolean)

    let connection = null
    let connectionError = null

    // Try each RPC endpoint until one works
    for (const endpoint of rpcEndpoints) {
      try {
        console.log(`🌐 Attempting to connect to RPC: ${endpoint}`)
        connection = new Connection(endpoint, 'confirmed')
        
        // Test the connection
        await connection.getLatestBlockhash()
        console.log(`✅ Successfully connected to RPC: ${endpoint}`)
        break
      } catch (error) {
        console.log(`❌ RPC ${endpoint} failed: ${error.message}`)
        connectionError = error
        connection = null
      }
    }

    if (!connection) {
      console.error('❌ All RPC endpoints failed:', connectionError)
      return NextResponse.json({
        success: false,
        error: 'Unable to connect to Solana network',
        details: connectionError?.message
      }, { status: 503 })
    }

    if (!process.env.SOLANA_PRIVATE_KEY) {
      return NextResponse.json({
        success: false,
        error: 'Drift trading not configured - missing SOLANA_PRIVATE_KEY'
      }, { status: 400 })
    }

    const privateKeyArray = JSON.parse(process.env.SOLANA_PRIVATE_KEY)
    const keypair = Keypair.fromSecretKey(new Uint8Array(privateKeyArray))
    const wallet = new Wallet(keypair)

    console.log('🚀 Initializing Drift client...')

    // Initialize Drift SDK
    const env = 'mainnet-beta'
    const sdkConfig = initialize({ env })
    
    const driftClient = new DriftClient({
      connection,
      wallet,
      programID: sdkConfig.DRIFT_PROGRAM_ID,
      opts: {
        commitment: 'confirmed',
      },
    })

    await driftClient.subscribe()

    try {
      // Get market index for the symbol
      const marketIndex = symbol === 'SOL' ? 0 : symbol === 'BTC' ? 1 : 0 // SOL-PERP is typically index 0
      
      // Determine position direction
      const direction = side.toUpperCase().includes('LONG') || side.toUpperCase() === 'BUY' 
        ? PositionDirection.LONG 
        : PositionDirection.SHORT

      // Calculate position size in base asset units
      const currentPrice = 166.75 // Get from oracle in production
      const calculatedAmount = (amount * leverage) / currentPrice * 1e9 // Convert to lamports for SOL
      
      // Ensure minimum order size (Drift requires at least 10,000,000 units)
      const minOrderSize = 10000000 // 0.01 SOL in protocol units
      const baseAssetAmount = Math.max(calculatedAmount, minOrderSize)

      console.log('📊 Trade parameters:', {
        marketIndex,
        direction: direction === PositionDirection.LONG ? 'LONG' : 'SHORT',
        requestedAmount: calculatedAmount.toString(),
        baseAssetAmount: baseAssetAmount.toString(),
        leverage,
        minOrderSize: minOrderSize.toString()
      })

      // Place market order
      const orderParams = {
        orderType: OrderType.MARKET,
        marketType: MarketType.PERP,
        direction,
        baseAssetAmount: new BN(Math.floor(baseAssetAmount)),
        marketIndex,
      }

      console.log('🎯 Placing Drift perpetual market order...')
      const txSig = await driftClient.placeAndTakePerpOrder(orderParams)
      
      console.log('✅ Drift order placed:', txSig)

      // Validate trigger distances before placing SL/TP orders
      const MIN_TRIGGER_DISTANCE_PERCENT = 2.0; // Minimum 2% distance from current price
      
      // Set up stop loss and take profit if specified
      let stopLossOrderId = null
      let takeProfitOrderId = null

      if (stopLoss) {
        // Validate stop loss distance
        const slDistance = Math.abs(stopLoss - currentPrice) / currentPrice * 100;
        
        if (slDistance < MIN_TRIGGER_DISTANCE_PERCENT) {
          console.warn(`⚠️ Stop loss too close: ${slDistance.toFixed(2)}% < ${MIN_TRIGGER_DISTANCE_PERCENT}% minimum`);
          // Adjust stop loss to minimum distance
          if (direction === PositionDirection.LONG) {
            stopLoss = currentPrice * (1 - MIN_TRIGGER_DISTANCE_PERCENT / 100);
          } else {
            stopLoss = currentPrice * (1 + MIN_TRIGGER_DISTANCE_PERCENT / 100);
          }
          console.log(`🔧 Adjusted stop loss to: $${stopLoss.toFixed(4)} (${MIN_TRIGGER_DISTANCE_PERCENT}% distance)`);
        }
        
        try {
          const stopLossParams = {
            orderType: OrderType.TRIGGER_LIMIT,
            marketType: MarketType.PERP,
            direction: direction === PositionDirection.LONG ? PositionDirection.SHORT : PositionDirection.LONG,
            baseAssetAmount: new BN(Math.floor(baseAssetAmount)),
            price: new BN(Math.floor(stopLoss * 1e6)), // Price in 6 decimal format
            marketIndex,
            triggerPrice: new BN(Math.floor(stopLoss * 1e6)),
            triggerCondition: direction === PositionDirection.LONG ? OrderTriggerCondition.BELOW : OrderTriggerCondition.ABOVE,
            reduceOnly: true,
          }
          
          const slTxSig = await driftClient.placePerpOrder(stopLossParams)
          stopLossOrderId = slTxSig
          console.log('🛑 Stop loss order placed:', slTxSig)
        } catch (slError) {
          console.warn('⚠️ Stop loss order failed:', slError.message)
        }
      }

      if (takeProfit) {
        // Validate take profit distance
        const tpDistance = Math.abs(takeProfit - currentPrice) / currentPrice * 100;
        
        if (tpDistance < MIN_TRIGGER_DISTANCE_PERCENT) {
          console.warn(`⚠️ Take profit too close: ${tpDistance.toFixed(2)}% < ${MIN_TRIGGER_DISTANCE_PERCENT}% minimum`);
          // Adjust take profit to minimum distance
          if (direction === PositionDirection.LONG) {
            takeProfit = currentPrice * (1 + MIN_TRIGGER_DISTANCE_PERCENT / 100);
          } else {
            takeProfit = currentPrice * (1 - MIN_TRIGGER_DISTANCE_PERCENT / 100);
          }
          console.log(`🔧 Adjusted take profit to: $${takeProfit.toFixed(4)} (${MIN_TRIGGER_DISTANCE_PERCENT}% distance)`);
        }
        
        try {
          const takeProfitParams = {
            orderType: OrderType.TRIGGER_LIMIT,
            marketType: MarketType.PERP,
            direction: direction === PositionDirection.LONG ? PositionDirection.SHORT : PositionDirection.LONG,
            baseAssetAmount: new BN(Math.floor(baseAssetAmount)),
            price: new BN(Math.floor(takeProfit * 1e6)), // Price in 6 decimal format
            marketIndex,
            triggerPrice: new BN(Math.floor(takeProfit * 1e6)),
            triggerCondition: direction === PositionDirection.LONG ? OrderTriggerCondition.ABOVE : OrderTriggerCondition.BELOW,
            reduceOnly: true,
          }
          
          const tpTxSig = await driftClient.placePerpOrder(takeProfitParams)
          takeProfitOrderId = tpTxSig
          console.log('🎯 Take profit order placed:', tpTxSig)
        } catch (tpError) {
          console.warn('⚠️ Take profit order failed:', tpError.message)
        }
      }

      // Calculate liquidation price
      const liquidationPrice = direction === PositionDirection.LONG
        ? currentPrice * (1 - 0.9 / leverage)
        : currentPrice * (1 + 0.9 / leverage)

      const result = {
        success: true,
        trade: {
          txId: txSig,
          orderId: `drift_${Date.now()}`,
          symbol: symbol.toUpperCase(),
          side: direction === PositionDirection.LONG ? 'LONG' : 'SHORT',
          positionSize: amount,
          leverage: leverage,
          leveragedAmount: amount * leverage,
          entryPrice: currentPrice,
          liquidationPrice: liquidationPrice,
          entryFee: (amount * leverage) * 0.001,
          timestamp: Date.now(),
          status: 'PENDING',
          platform: 'Drift Protocol',
          dex: 'DRIFT_REAL',
          stopLoss: stopLoss,
          takeProfit: takeProfit,
          stopLossOrderId: stopLossOrderId,
          takeProfitOrderId: takeProfitOrderId,
          monitoring: !!(stopLoss || takeProfit),
          pnl: 0
        },
        message: `${direction === PositionDirection.LONG ? 'LONG' : 'SHORT'} perpetual position opened: $${amount} at ${leverage}x leverage`,
        warnings: [
          `⚠️ Liquidation risk at $${liquidationPrice.toFixed(4)}`,
          '📊 Position requires active monitoring',
          '💰 Real funds at risk'
        ]
      }

      return NextResponse.json(result)

    } finally {
      // Clean up
      try {
        await driftClient.unsubscribe()
      } catch (e) {
        console.warn('⚠️ Cleanup warning:', e.message)
      }
    }

  } catch (error) {
    console.error('❌ Drift perpetual trade execution error:', error)
    
    return NextResponse.json(
      { 
        success: false, 
        error: 'Internal server error',
        message: `Failed to execute Drift perpetual trade: ${error.message}`,
        details: error.message
      },
      { status: 500 }
    )
  }
}

export async function GET() {
  return NextResponse.json({
    message: 'Drift Protocol Perpetuals Trading API',
    endpoints: {
      'POST /api/trading/execute-drift': 'Execute real perpetual trades via Drift Protocol',
    },
    status: 'Active',
    features: [
      'Real leveraged perpetual trading (1x-100x)',
      'Long/Short positions with liquidation risk',
      'Stop Loss & Take Profit orders',
      'Real-time position tracking',
      'Automatic margin management'
    ],
    requirements: [
      'SOLANA_PRIVATE_KEY environment variable',
      'Sufficient USDC collateral in Drift account',
      'Active Drift user account'
    ],
    note: 'This API executes real trades with real money and liquidation risk. Use with caution.'
  })
}
