'use client'
import React, { useState, useEffect } from 'react'
import EnhancedAILearningPanel from '../../components/EnhancedAILearningPanel'

// Available timeframes for automation (matching analysis page format)
const timeframes = [
  { label: '5m', value: '5' },
  { label: '15m', value: '15' },
  { label: '30m', value: '30' },
  { label: '1h', value: '60' },
  { label: '2h', value: '120' },
  { label: '4h', value: '240' },
  { label: '1d', value: 'D' },
]

export default function AutomationPageV2() {
  const [config, setConfig] = useState({
    mode: 'LIVE',
    dexProvider: 'DRIFT',
    symbol: 'SOLUSD',
    selectedTimeframes: ['5', '15', '30'], // Default to scalping preset
    tradingAmount: 100,
    balancePercentage: 100, // Default to 100% of available balance
  })

  const [status, setStatus] = useState(null)
  const [balance, setBalance] = useState(null)
  const [positions, setPositions] = useState([])
  const [loading, setLoading] = useState(false)
  const [monitorData, setMonitorData] = useState(null)
  const [automationDisabled, setAutomationDisabled] = useState(false) // Track manual disable state
  const [actionFeedback, setActionFeedback] = useState(null) // Track button action feedback
  const [liveDecisions, setLiveDecisions] = useState([]) // Live trading decisions

  useEffect(() => {
    fetchStatus()
    fetchBalance()
    fetchPositions()
    fetchMonitorData()
    fetchLiveDecisions()
    
    const interval = setInterval(() => {
      fetchStatus()
      fetchBalance()
      fetchPositions()
      fetchMonitorData()
      fetchLiveDecisions() // Fetch live decisions frequently
    }, 30000) // 30 seconds for live data
    return () => clearInterval(interval)
  }, [])

  const toggleTimeframe = (timeframe) => {
    setConfig(prev => ({
      ...prev,
      selectedTimeframes: prev.selectedTimeframes.includes(timeframe)
        ? prev.selectedTimeframes.filter(tf => tf !== timeframe)
        : [...prev.selectedTimeframes, timeframe]
    }))
  }

  const fetchStatus = async () => {
    try {
      console.log('🔍 fetchStatus called at:', new Date().toISOString())
      const response = await fetch('/api/automation/status')
      const data = await response.json()
      console.log('📊 Status response:', data) // Debug log
      
      if (response.ok && !data.error) {
        // If no lastDecision exists, get real analysis data
        if (!data.lastDecision) {
          console.log('📋 No lastDecision found, fetching analysis details...')
          try {
            const analysisResponse = await fetch('/api/automation/analysis-details')
            const analysisData = await analysisResponse.json()
            console.log('🧠 Analysis response:', { success: analysisData.success, hasAnalysis: !!analysisData.data?.analysis })
            
            if (analysisData.success && analysisData.data.analysis) {
              const analysis = analysisData.data.analysis
              const recentTrade = analysisData.data.recentTrades?.[0]
              
              console.log('✅ Creating lastDecision from analysis:', {
                decision: analysis.decision,
                confidence: analysis.confidence,
                hasRecentTrade: !!recentTrade
              })
              
              data.lastDecision = {
                recommendation: analysis.decision || 'HOLD',
                confidence: analysis.confidence || 84,
                minConfidenceRequired: 70,
                executed: recentTrade ? true : false,
                timestamp: analysis.timestamp || Date.now(),
                reasoning: analysis.reasoning || `Recent multi-timeframe analysis shows ${analysis.decision} signal with ${analysis.confidence}% confidence.

Based on comprehensive technical analysis across multiple timeframes:
• **Multi-timeframe consensus**: ${analysis.multiTimeframeResults?.length || 3} timeframes analyzed
• **Current signal**: ${analysis.decision} with ${analysis.confidence}% confidence  
• **Entry level**: $${analysis.entry?.price || '187.25'} ${analysis.entry?.buffer || '±0.25'}
• **Risk management**: Stop at $${analysis.stopLoss?.price || '185.50'}, Target $${analysis.takeProfits?.tp1?.price || '193.00'}
• **Analysis timestamp**: ${new Date(analysis.timestamp).toLocaleString()}`,
                executionDetails: recentTrade ? {
                  leverage: recentTrade.leverage || 3,
                  entryPrice: recentTrade.entryPrice || recentTrade.price,
                  stopLoss: analysis.stopLoss?.price || 185.50,
                  takeProfit: analysis.takeProfits?.tp1?.price || 193.00,
                  positionSize: recentTrade.positionSize || recentTrade.amount || 15.2,
                  side: recentTrade.side || 'BUY',
                  amount: recentTrade.realTradingAmount || recentTrade.tradingAmount || recentTrade.actualInvestment
                } : null,
                isRetrospective: false
              }
            }
          } catch (analysisError) {
            console.warn('❌ Could not fetch analysis details:', analysisError)
          }
        }
        console.log('🎯 Setting status with lastDecision:', !!data.lastDecision)
        setStatus(data) // Status data is returned directly, not wrapped in 'success'
      } else {
        console.error('❌ Status API error:', data.error || 'Unknown error')
      }
    } catch (error) {
      console.error('Failed to fetch status:', error)
    }
  }

  const fetchBalance = async () => {
    try {
      const response = await fetch('/api/drift/balance')
      const data = await response.json()
      if (data.success) {
        setBalance(data)
      }
    } catch (error) {
      console.error('Failed to fetch balance:', error)
    }
  }

  const fetchMonitorData = async () => {
    try {
      const response = await fetch('/api/automation/position-monitor')
      const data = await response.json()
      if (data.success) {
        setMonitorData(data.monitor)
      }
    } catch (error) {
      console.error('Failed to fetch monitor data:', error)
    }
  }

  const fetchLiveDecisions = async () => {
    try {
      const response = await fetch('/api/automation/live-decisions')
      const data = await response.json()
      if (data.success) {
        setLiveDecisions(data.decisions || [])
        console.log('📊 Live decisions fetched:', data.decisions?.length || 0)
      }
    } catch (error) {
      console.error('Failed to fetch live decisions:', error)
    }
  }

  const fetchPositions = async () => {
    try {
      const response = await fetch('/api/drift/positions')
      const data = await response.json()
      if (data.success) {
        setPositions(data.positions || [])
      }
    } catch (error) {
      console.error('Failed to fetch positions:', error)
    }
  }

  const handleStart = async () => {
    console.log('🚀 Starting automation...')
    setLoading(true)
    try {
      if (config.selectedTimeframes.length === 0) {
        console.error('No timeframes selected')
        setLoading(false)
        return
      }

      const automationConfig = {
        symbol: config.symbol,
        selectedTimeframes: config.selectedTimeframes,
        mode: config.mode,
        tradingAmount: config.tradingAmount,
        leverage: config.leverage,
        stopLoss: config.stopLoss,
        takeProfit: config.takeProfit
      }

      const response = await fetch('/api/automation/start', {
        method: 'POST',
        headers: { 'Content-Type': 'application/json' },
        body: JSON.stringify(automationConfig)
      })
      
      const data = await response.json()
      
      if (data.success) {
        console.log('✅ Automation started successfully')
        if (data.learningSystem?.integrated) {
          console.log('🧠 AI Learning System: Activated')
        }
        fetchStatus()
      } else {
        console.error('Failed to start automation:', data.error)
      }
    } catch (error) {
      console.error('Failed to start automation:', error)
    } finally {
      setLoading(false)
    }
  }

  const handleStop = async () => {
    const isCurrentlyDisabled = automationDisabled
    
    if (status?.isActive) {
      // If automation is running, stop it
      console.log('🛑 Stopping active automation...')
      setActionFeedback({ type: 'info', message: 'Stopping automation...' })
    } else if (!isCurrentlyDisabled) {
      // If automation not running but not disabled, disable it
      console.log('🛑 Disabling automation triggers...')
      setActionFeedback({ type: 'warning', message: 'Disabling automation triggers...' })
    } else {
      // If disabled, enable it
      console.log('✅ Enabling automation triggers...')
      setAutomationDisabled(false)
      setActionFeedback({ type: 'success', message: '✅ Automation triggers ENABLED - System ready for automated trading' })
      setLoading(false)
      // Clear feedback after 3 seconds
      setTimeout(() => setActionFeedback(null), 3000)
      return
    }
    
    setLoading(true)
    try {
      const response = await fetch('/api/automation/stop', {
        method: 'POST',
        headers: { 'Content-Type': 'application/json' }
      })
      
      const data = await response.json()
      
      if (data.success) {
        if (status?.isActive) {
          console.log('✅ Automation stopped successfully')
          setActionFeedback({ type: 'success', message: '✅ Automation STOPPED successfully' })
        } else {
          console.log('✅ Automation triggers disabled')
          setAutomationDisabled(true)
          setActionFeedback({ type: 'success', message: '🚫 Automation triggers DISABLED - Safe to close positions manually' })
        }
        fetchStatus()
      } else {
        console.error('Failed to stop automation:', data.error)
        setActionFeedback({ type: 'error', message: '❌ Failed to change automation state' })
      }
    } catch (error) {
      console.error('Failed to stop automation:', error)
      setActionFeedback({ type: 'error', message: '❌ Network error - please try again' })
    } finally {
      setLoading(false)
      // Clear feedback after 3 seconds
      setTimeout(() => setActionFeedback(null), 3000)
    }
  }

  const handleEmergencyStop = async () => {
    console.log('🚨 Emergency stop triggered!')
    setLoading(true)
    try {
      const response = await fetch('/api/automation/emergency-stop', {
        method: 'POST',
        headers: { 'Content-Type': 'application/json' }
      })
      
      const data = await response.json()
      
      if (data.success) {
        console.log('✅ Emergency stop completed successfully')
        fetchStatus()
        fetchPositions()
      fetchMonitorData()
    fetchMonitorData()
      } else {
        console.error('Emergency stop failed:', data.error)
      }
    } catch (error) {
      console.error('Emergency stop error:', error)
    } finally {
      setLoading(false)
    }
  }

  const generateTestDecision = async () => {
    console.log('🧪 Generating test AI decision...')
    setLoading(true)
    try {
      const response = await fetch('/api/automation/test-decision', {
        method: 'POST',
        headers: { 'Content-Type': 'application/json' },
        body: JSON.stringify({
          action: 'generate_test_decision',
          analysis: {
            recommendation: 'STRONG BUY',
            confidence: 89,
            reasoning: `🎯 BULLISH CONVERGENCE DETECTED:

📈 Technical Analysis:
• RSI bounced from oversold (28→54) showing strong recovery momentum  
• MACD histogram turning positive with bullish crossover confirmed
• Price broke above key resistance at $185.40 with 3x normal volume
• 20 EMA (184.92) providing strong support, price trending above all major EMAs

📊 Market Structure:
• Higher lows pattern intact since yesterday's session
• Volume profile shows accumulation at current levels  
• Order book depth favoring buyers (67% buy-side liquidity)

⚡ Entry Trigger:
• Breakout candle closed above $186.00 resistance with conviction
• Next resistance target: $189.75 (2.1% upside potential)
• Risk/Reward ratio: 1:2.3 (excellent risk management setup)

🛡️ Risk Management:
• Stop loss at $184.20 (1.0% below entry) protects against false breakout
• Position sizing optimized for 2% account risk tolerance`,
            stopLoss: 184.20,
            takeProfit: 189.75,
            currentPrice: 186.12,
            stopLossPercent: '1.0% protective stop'
          },
          config: {
            selectedTimeframes: config.selectedTimeframes,
            symbol: config.symbol,
            mode: config.mode,
            enableTrading: config.enableTrading,
            tradingAmount: 62
          }
        })
      })
      
      const data = await response.json()
      
      if (data.success) {
        console.log('✅ Test decision generated successfully')
        fetchStatus() // Refresh to show the decision
      } else {
        console.error('Failed to generate test decision:', data.error)
      }
    } catch (error) {
      console.error('Test decision error:', error)
    } finally {
      setLoading(false)
    }
  }

  const analyzeExistingPosition = async () => {
    console.log('🔍 Analyzing existing position...')
    setLoading(true)
    try {
      // First get the current position data
      const positionResponse = await fetch('/api/automation/position-monitor')
      const positionData = await positionResponse.json()
      
      if (positionData.success && positionData.monitor.hasPosition) {
        // Analyze the existing position
        const response = await fetch('/api/automation/analyze-position', {
          method: 'POST',
          headers: { 'Content-Type': 'application/json' },
          body: JSON.stringify({
            action: 'analyze_existing_position',
            positionData: positionData.monitor.position
          })
        })
        
        const data = await response.json()
        
        if (data.success) {
          console.log('✅ Position analysis generated successfully')
          fetchStatus() // Refresh to show the analysis
        } else {
          console.error('Failed to analyze position:', data.error)
        }
      } else {
        console.log('ℹ️ No position found to analyze')
        alert('No active position found to analyze')
      }
    } catch (error) {
      console.error('Position analysis error:', error)
    } finally {
      setLoading(false)
    }
  }

  return (
    <div className="min-h-screen bg-gradient-to-br from-slate-900 via-purple-900/20 to-slate-900 p-6">
      <div className="max-w-7xl mx-auto space-y-8">
        {/* Page Header */}
        <div className="text-center mb-8">
          <h1 className="text-4xl font-bold text-transparent bg-clip-text bg-gradient-to-r from-blue-400 via-purple-400 to-blue-400 mb-2">
            🤖 AI Trading Automation
          </h1>
          <p className="text-gray-400 text-lg">Advanced AI-powered trading with real-time analysis and risk management</p>
        </div>

        {/* Main Grid Layout */}
        <div className="grid grid-cols-1 xl:grid-cols-4 gap-8">
          {/* Automation Control Panel - Main Column */}
          <div className="xl:col-span-3 space-y-8">
            <div className="bg-gradient-to-br from-gray-900/90 via-slate-800/80 to-gray-900/90 backdrop-blur-xl p-8 rounded-2xl border border-gray-600/30 shadow-2xl">
              {/* Control Section - Header removed to save space */}
              <div className="mb-8">
                {/* Action Feedback */}
                {actionFeedback && (
                  <div className={`mb-4 p-4 rounded-lg border-l-4 ${
                    actionFeedback.type === 'success' ? 'bg-green-900/30 border-green-500 text-green-300' :
                    actionFeedback.type === 'warning' ? 'bg-yellow-900/30 border-yellow-500 text-yellow-300' :
                    actionFeedback.type === 'error' ? 'bg-red-900/30 border-red-500 text-red-300' :
                    'bg-blue-900/30 border-blue-500 text-blue-300'
                  }`}>
                    <div className="font-medium">{actionFeedback.message}</div>
                  </div>
                )}

                {/* Automation Trigger Status Indicator */}
                {(positions.length > 0 && !status?.isActive) && (
                  <div className="mb-4 p-3 rounded-lg border-2 border-dashed border-gray-500/50 bg-gray-800/30">
                    <div className="flex items-center justify-between">
                      <div className="flex items-center space-x-2">
                        <span className="text-lg">⚡</span>
                        <span className="text-gray-300 font-medium">Automation Triggers:</span>
                      </div>
                      <div className={`flex items-center space-x-2 px-3 py-1 rounded-lg border ${
                        automationDisabled 
                          ? 'bg-red-900/30 text-red-300 border-red-500/50' 
                          : 'bg-green-900/30 text-green-300 border-green-500/50'
                      }`}>
                        <span>{automationDisabled ? '🚫' : '✅'}</span>
                        <span className="font-bold">{automationDisabled ? 'DISABLED' : 'ENABLED'}</span>
                      </div>
                    </div>
                    <div className="mt-2 text-sm text-gray-400">
                      {automationDisabled 
                        ? 'Safe to close positions manually - no automation will trigger'
                        : 'Automation can start when conditions are met (position close, signals, etc.)'
                      }
                    </div>
                  </div>
                )}
                
                <div className="flex space-x-4">
                  {(status?.isActive || positions.length > 0) ? (
                    <>
                      <button
                        onClick={handleStop}
                        disabled={loading}
                        className={`px-8 py-4 rounded-xl transition-all duration-200 disabled:opacity-50 font-semibold shadow-lg transform hover:scale-105 ${
                          status?.isActive 
                            ? 'bg-gradient-to-r from-red-600 to-red-700 hover:from-red-700 hover:to-red-800 shadow-red-500/25 text-white'
                            : automationDisabled
                              ? 'bg-gradient-to-r from-green-600 to-green-700 hover:from-green-700 hover:to-green-800 shadow-green-500/25 text-white'
                              : 'bg-gradient-to-r from-yellow-600 to-yellow-700 hover:from-yellow-700 hover:to-yellow-800 shadow-yellow-500/25 text-white'
                        }`}
                        title={
                          status?.isActive 
                            ? "Stop active automation" 
                            : automationDisabled
                              ? "Enable automation triggers - allows automation to start when conditions are met"
                              : "Disable automation triggers - prevents automation from starting on position close"
                        }
                      >
                        {loading ? (
                          <div className="flex items-center space-x-2">
                            <div className="w-4 h-4 border-2 border-white border-t-transparent rounded-full animate-spin"></div>
                            <span>Processing...</span>
                          </div>
                        ) : (
                          <div className="flex items-center space-x-2">
                            <span>{status?.isActive ? '🛑' : automationDisabled ? '✅' : '🛑'}</span>
                            <span>{status?.isActive ? 'STOP' : automationDisabled ? 'ENABLE' : 'DISABLE'}</span>
                          </div>
                        )}
                      </button>
                      <button
                        onClick={handleEmergencyStop}
                        disabled={loading}
                        className="px-6 py-4 bg-gradient-to-r from-red-800 to-red-900 text-white rounded-xl hover:from-red-900 hover:to-red-950 transition-all duration-200 disabled:opacity-50 font-semibold border-2 border-red-600/50 shadow-lg shadow-red-900/50 transform hover:scale-105"
                        title={status?.isActive ? "Emergency Stop - Closes all positions immediately" : "Emergency disable - Prevents any automation triggers"}
                      >
                        <div className="flex items-center space-x-2">
                          <span>🚨</span>
                          <span>EMERGENCY</span>
                        </div>
                      </button>
                    </>
                  ) : (
                    <button
                      onClick={handleStart}
                      disabled={loading}
                      className="px-8 py-4 bg-gradient-to-r from-green-600 to-green-700 text-white rounded-xl hover:from-green-700 hover:to-green-800 transition-all duration-200 disabled:opacity-50 font-semibold shadow-lg shadow-green-500/25 transform hover:scale-105"
                    >
                      {loading ? (
                        <div className="flex items-center space-x-2">
                          <div className="w-4 h-4 border-2 border-white border-t-transparent rounded-full animate-spin"></div>
                          <span>Starting...</span>
                        </div>
                      ) : (
                        <div className="flex items-center space-x-2">
                          <span>{status?.rateLimitHit ? '🔄' : '🚀'}</span>
                          <span>{status?.rateLimitHit ? 'RESTART' : 'START'}</span>
                        </div>
                      )}
                    </button>
                  )}
                  
                  {/* Test & Analysis Buttons */}
                  <button
                    onClick={generateTestDecision}
                    disabled={loading}
                    className="px-6 py-4 bg-gradient-to-r from-purple-600 to-purple-700 text-white rounded-xl hover:from-purple-700 hover:to-purple-800 transition-all duration-200 disabled:opacity-50 font-semibold border-2 border-purple-500/50 shadow-lg shadow-purple-500/25 transform hover:scale-105"
                    title="Generate Test AI Decision - Shows reasoning panel"
                  >
                    <div className="flex items-center space-x-2">
                      <span>🧪</span>
                      <span>TEST AI</span>
                    </div>
                  </button>
                  <button
                    onClick={analyzeExistingPosition}
                    disabled={loading}
                    className="px-6 py-4 bg-gradient-to-r from-blue-600 to-blue-700 text-white rounded-xl hover:from-blue-700 hover:to-blue-800 transition-all duration-200 disabled:opacity-50 font-semibold border-2 border-blue-500/50 shadow-lg shadow-blue-500/25 transform hover:scale-105"
                    title="Analyze Current Position - Generate AI reasoning for existing position"
                  >
                    <div className="flex items-center space-x-2">
                      <span>🔍</span>
                      <span>ANALYZE</span>
                    </div>
                  </button>
                </div>
              </div>
              
              {/* Trading Mode Selection - Enhanced Cards */}
              <div className="mb-8">
                <label className="block text-lg font-bold text-blue-400 mb-4 flex items-center">
                  <span className="mr-2">⚙️</span>
                  Trading Mode
                </label>
                <div className="grid grid-cols-1 md:grid-cols-2 gap-6">
                  <label className="group relative cursor-pointer">
                    <input 
                      type="radio" 
                      className="sr-only" 
                      name="mode" 
                      checked={config.mode === 'SIMULATION'}
                      onChange={() => setConfig({...config, mode: 'SIMULATION'})}
                      disabled={status?.isActive}
                    />
                    <div className={`p-6 rounded-2xl border-2 transition-all duration-200 ${
                      config.mode === 'SIMULATION'
                        ? 'border-blue-500 bg-gradient-to-br from-blue-500/20 to-purple-500/10 shadow-lg shadow-blue-500/25'
                        : status?.isActive
                        ? 'border-gray-600 bg-gray-800/30 cursor-not-allowed opacity-50'
                        : 'border-gray-600 bg-gray-800/30 hover:border-blue-400 hover:bg-blue-500/10 hover:shadow-lg hover:shadow-blue-500/10'
                    }`}>
                      <div className="flex items-center space-x-4">
                        <div className="w-16 h-16 bg-gradient-to-br from-blue-500 to-purple-600 rounded-2xl flex items-center justify-center shadow-lg">
                          <span className="text-3xl">📊</span>
                        </div>
                        <div>
                          <h4 className="text-xl font-bold text-white mb-1">Paper Trading</h4>
                          <p className="text-gray-400">Practice with virtual funds</p>
                          <div className="text-sm text-blue-400 mt-2 font-medium">Risk-free testing environment</div>
                        </div>
                      </div>
                      {config.mode === 'SIMULATION' && (
                        <div className="absolute top-2 right-2 w-4 h-4 bg-blue-500 rounded-full flex items-center justify-center">
                          <div className="w-2 h-2 bg-white rounded-full"></div>
                        </div>
                      )}
                    </div>
                  </label>
                  
                  <label className="group relative cursor-pointer">
                    <input 
                      type="radio" 
                      className="sr-only" 
                      name="mode" 
                      checked={config.mode === 'LIVE'}
                      onChange={() => setConfig({...config, mode: 'LIVE'})}
                      disabled={status?.isActive}
                    />
                    <div className={`p-6 rounded-2xl border-2 transition-all duration-200 ${
                      config.mode === 'LIVE'
                        ? 'border-green-500 bg-gradient-to-br from-green-500/20 to-yellow-500/10 shadow-lg shadow-green-500/25'
                        : status?.isActive
                        ? 'border-gray-600 bg-gray-800/30 cursor-not-allowed opacity-50'
                        : 'border-gray-600 bg-gray-800/30 hover:border-green-400 hover:bg-green-500/10 hover:shadow-lg hover:shadow-green-500/10'
                    }`}>
                      <div className="flex items-center space-x-4">
                        <div className="w-16 h-16 bg-gradient-to-br from-green-500 to-yellow-600 rounded-2xl flex items-center justify-center shadow-lg">
                          <span className="text-3xl">💰</span>
                        </div>
                        <div>
                          <h4 className="text-xl font-bold text-white mb-1">Live Trading</h4>
                          <p className="text-gray-400">Real money transactions</p>
                          <div className="text-sm text-green-400 mt-2 font-medium">⚠️ Use real funds carefully</div>
                        </div>
                      </div>
                      {config.mode === 'LIVE' && (
                        <div className="absolute top-2 right-2 w-4 h-4 bg-green-500 rounded-full flex items-center justify-center">
                          <div className="w-2 h-2 bg-white rounded-full"></div>
                        </div>
                      )}
                    </div>
                  </label>
                </div>
              </div>

              {/* Symbol and Balance Configuration */}
              <div className="grid grid-cols-1 lg:grid-cols-2 gap-8 mb-8">
                <div className="space-y-4">
                  <label className="block text-lg font-bold text-purple-400 flex items-center">
                    <span className="mr-2">🎯</span>
                    Trading Symbol
                  </label>
                  <div className="relative">
                    <select
                      className="w-full p-4 bg-gradient-to-r from-gray-800 to-gray-700 border-2 border-gray-600 rounded-xl text-white focus:border-purple-500 focus:ring-2 focus:ring-purple-500/20 transition-all duration-200 appearance-none text-lg font-medium"
                      value={config.symbol}
                      onChange={(e) => setConfig({...config, symbol: e.target.value})}
                      disabled={status?.isActive}
                    >
                      <option value="SOLUSD">SOL/USD - Solana</option>
                      <option value="BTCUSD">BTC/USD - Bitcoin</option>
                      <option value="ETHUSD">ETH/USD - Ethereum</option>
                      <option value="APTUSD">APT/USD - Aptos</option>
                      <option value="AVAXUSD">AVAX/USD - Avalanche</option>
                      <option value="DOGEUSD">DOGE/USD - Dogecoin</option>
                    </select>
                    <div className="absolute inset-y-0 right-0 flex items-center pr-4 pointer-events-none">
                      <svg className="w-5 h-5 text-gray-400" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                        <path strokeLinecap="round" strokeLinejoin="round" strokeWidth="2" d="M19 9l-7 7-7-7"></path>
                      </svg>
                    </div>
                  </div>
                </div>

                <div className="space-y-4">
                  <label className="block text-lg font-bold text-yellow-400 flex items-center">
                    <span className="mr-2">💳</span>
                    Balance Usage: {config.balancePercentage}%
                    {balance && (
                      <span className="ml-2 text-green-400 font-mono">
                        ${(parseFloat(balance.availableBalance) * config.balancePercentage / 100).toFixed(2)}
                      </span>
                    )}
                  </label>
                  <div className="space-y-3">
                    <div className="relative">
                      <input
                        type="range"
                        className="w-full h-3 bg-gray-700 rounded-lg appearance-none cursor-pointer slider"
                        style={{
                          background: `linear-gradient(to right, #3b82f6 0%, #3b82f6 ${config.balancePercentage}%, #374151 ${config.balancePercentage}%, #374151 100%)`
                        }}
                        min="10"
                        max="100"
                        step="5"
                        value={config.balancePercentage}
                        onChange={(e) => {
                          const percentage = parseFloat(e.target.value);
                          const newAmount = balance ? (parseFloat(balance.availableBalance) * percentage / 100) : 100;
                          setConfig({
                            ...config, 
                            balancePercentage: percentage,
                            tradingAmount: Math.round(newAmount)
                          });
                        }}
                        disabled={status?.isActive}
                      />
                    </div>
                    <div className="flex justify-between text-sm text-gray-400">
                      <span className="flex items-center">
                        <span className="w-2 h-2 bg-yellow-500 rounded-full mr-2"></span>
                        Conservative (10%)
                      </span>
                      <span className="flex items-center">
                        <span className="w-2 h-2 bg-orange-500 rounded-full mr-2"></span>
                        Balanced (50%)
                      </span>
                      <span className="flex items-center">
                        <span className="w-2 h-2 bg-red-500 rounded-full mr-2"></span>
                        Aggressive (100%)
                      </span>
                    </div>
                  </div>
                </div>
              </div>

              {/* Enhanced Multi-Timeframe Selection */}
              <div className="mb-8">
                <label className="block text-xl font-bold text-cyan-400 mb-6 flex items-center">
                  <span className="mr-3">⏰</span>
                  Analysis Timeframes
                  <span className="ml-4 text-sm text-cyan-300 bg-cyan-500/20 px-3 py-1 rounded-full border border-cyan-500/30">
                    {config.selectedTimeframes.length} selected
                  </span>
                  {config.selectedTimeframes.length === 0 && (
                    <span className="ml-4 text-sm text-red-400 bg-red-500/20 px-3 py-1 rounded-full border border-red-500/30 animate-pulse">
                      ⚠️ Select at least one timeframe
                    </span>
                  )}
                </label>
                
                {/* Timeframe Selection Grid */}
                <div className="grid grid-cols-4 lg:grid-cols-7 gap-3 mb-6">
                  {timeframes.map(tf => (
                    <label key={tf.value} className="group relative cursor-pointer">
                      <input
                        type="checkbox"
                        checked={config.selectedTimeframes.includes(tf.value)}
                        onChange={() => toggleTimeframe(tf.value)}
                        disabled={status?.isActive}
                        className="sr-only"
                      />
                      <div className={`relative p-4 rounded-xl border-2 transition-all duration-200 text-center font-bold ${
                        config.selectedTimeframes.includes(tf.value)
                          ? 'border-cyan-500 bg-gradient-to-br from-cyan-500/30 to-blue-500/20 text-cyan-300 shadow-lg shadow-cyan-500/30 transform scale-105'
                          : status?.isActive
                          ? 'border-gray-700 bg-gray-800/30 text-gray-500 cursor-not-allowed opacity-50'
                          : 'border-gray-700 bg-gray-800/30 text-gray-400 hover:border-cyan-400 hover:bg-cyan-500/10 hover:text-cyan-300 hover:shadow-lg hover:shadow-cyan-500/20 hover:scale-105'
                      }`}>
                        <div className="text-lg">{tf.label}</div>
                        {config.selectedTimeframes.includes(tf.value) && (
                          <div className="absolute -top-1 -right-1 w-5 h-5 bg-gradient-to-br from-cyan-400 to-blue-500 rounded-full flex items-center justify-center shadow-lg">
                            <svg className="w-3 h-3 text-white" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                              <path strokeLinecap="round" strokeLinejoin="round" strokeWidth="3" d="M5 13l4 4L19 7"></path>
                            </svg>
                          </div>
                        )}
                      </div>
                    </label>
                  ))}
                </div>

                {/* Selected Timeframes Display */}
                {config.selectedTimeframes.length > 0 && (
                  <div className="p-4 bg-gradient-to-r from-cyan-900/30 to-blue-900/30 rounded-xl border border-cyan-500/30 mb-6">
                    <div className="flex items-center justify-between">
                      <div className="text-sm">
                        <span className="text-gray-400">Selected Timeframes:</span>
                        <span className="ml-2 text-cyan-400 font-medium">
                          {config.selectedTimeframes.map(tf => timeframes.find(t => t.value === tf)?.label || tf).filter(Boolean).join(', ')}
                        </span>
                      </div>
                      <div className="text-xs text-cyan-300 bg-cyan-500/20 px-2 py-1 rounded-lg">
                        💡 Multi-timeframe analysis
                      </div>
                    </div>
                  </div>
                )}
                
                {/* Trading Style Presets - Enhanced Cards */}
                <div className="grid grid-cols-1 md:grid-cols-3 gap-4">
                  <button
                    type="button"
                    onClick={() => setConfig({...config, selectedTimeframes: ['5', '15', '30']})}
                    disabled={status?.isActive}
                    className="group p-6 rounded-xl bg-gradient-to-br from-green-600/20 to-emerald-600/10 border-2 border-green-600/30 hover:border-green-500/50 hover:from-green-600/30 hover:to-emerald-600/20 transition-all duration-200 disabled:opacity-50 disabled:cursor-not-allowed text-left hover:scale-105"
                  >
                    <div className="flex items-center justify-between mb-3">
                      <div className="text-3xl group-hover:scale-110 transition-transform duration-200">📈</div>
                      <div className="text-xs text-green-400 bg-green-500/20 px-2 py-1 rounded-lg">FAST</div>
                    </div>
                    <h4 className="text-lg font-bold text-green-300 mb-2">Scalping</h4>
                    <p className="text-sm text-gray-300 mb-3">Quick trades on short timeframes</p>
                    <div className="text-xs text-green-400/80 font-mono bg-green-900/30 px-2 py-1 rounded">
                      5m • 15m • 30m
                    </div>
                  </button>
                  
                  <button
                    type="button"
                    onClick={() => setConfig({...config, selectedTimeframes: ['60', '120']})}
                    disabled={status?.isActive}
                    className="group p-6 rounded-xl bg-gradient-to-br from-blue-600/20 to-indigo-600/10 border-2 border-blue-600/30 hover:border-blue-500/50 hover:from-blue-600/30 hover:to-indigo-600/20 transition-all duration-200 disabled:opacity-50 disabled:cursor-not-allowed text-left hover:scale-105"
                  >
                    <div className="flex items-center justify-between mb-3">
                      <div className="text-3xl group-hover:scale-110 transition-transform duration-200">⚡</div>
                      <div className="text-xs text-blue-400 bg-blue-500/20 px-2 py-1 rounded-lg">MEDIUM</div>
                    </div>
                    <h4 className="text-lg font-bold text-blue-300 mb-2">Day Trading</h4>
                    <p className="text-sm text-gray-300 mb-3">Intraday momentum strategies</p>
                    <div className="text-xs text-blue-400/80 font-mono bg-blue-900/30 px-2 py-1 rounded">
                      1h • 2h
                    </div>
                  </button>
                  
                  <button
                    type="button"
                    onClick={() => setConfig({...config, selectedTimeframes: ['240', 'D']})}
                    disabled={status?.isActive}
                    className="group p-6 rounded-xl bg-gradient-to-br from-purple-600/20 to-violet-600/10 border-2 border-purple-600/30 hover:border-purple-500/50 hover:from-purple-600/30 hover:to-violet-600/20 transition-all duration-200 disabled:opacity-50 disabled:cursor-not-allowed text-left hover:scale-105"
                  >
                    <div className="flex items-center justify-between mb-3">
                      <div className="text-3xl group-hover:scale-110 transition-transform duration-200">🎯</div>
                      <div className="text-xs text-purple-400 bg-purple-500/20 px-2 py-1 rounded-lg">SLOW</div>
                    </div>
                    <h4 className="text-lg font-bold text-purple-300 mb-2">Swing Trading</h4>
                    <p className="text-sm text-gray-300 mb-3">Long-term trend following</p>
                    <div className="text-xs text-purple-400/80 font-mono bg-purple-900/30 px-2 py-1 rounded">
                      4h • 1d
                    </div>
                  </button>
                </div>
              </div>
            </div>
          </div>

          {/* Enhanced Sidebar */}
          <div className="space-y-6">
            {/* Unified Trading Dashboard Card */}
            <div className="bg-gradient-to-br from-gray-900/90 via-slate-800/80 to-gray-900/90 backdrop-blur-xl p-6 rounded-2xl border border-gray-600/30 shadow-2xl">
              <div className="flex items-center space-x-3 mb-6">
                <div className={`w-14 h-14 rounded-xl flex items-center justify-center ${
                  status?.isActive 
                    ? 'bg-gradient-to-br from-green-500 to-emerald-600 shadow-lg shadow-green-500/25' 
                    : 'bg-gradient-to-br from-gray-600 to-gray-700'
                }`}>
                  <span className="text-2xl">{status?.isActive ? '🟢' : '⚪'}</span>
                </div>
                <div>
                  <h3 className="text-xl font-bold text-white">Trading Dashboard</h3>
                  <p className="text-gray-400">Status • Positions • Risk Monitor</p>
                </div>
              </div>
              
              {/* Bot Status Section */}
              <div className="mb-6">
                <h4 className="text-lg font-semibold text-blue-400 mb-3 flex items-center">
                  <span className="mr-2">🤖</span>Bot Status
                </h4>
                <div className="space-y-3">
                  <div className="flex justify-between items-center p-3 bg-black/20 rounded-lg">
                    <span className="text-gray-400 font-medium">Status:</span>
                    <span className={`px-3 py-1 rounded-lg text-sm font-bold ${
                      status?.isActive 
                        ? 'bg-green-500/20 text-green-300 border border-green-500/30' 
                        : 'bg-gray-600/20 text-gray-300 border border-gray-600/30'
                    }`}>
                      {status?.isActive ? 'RUNNING' : 'STOPPED'}
                    </span>
                  </div>

                  {/* Automation Trigger Status */}
                  <div className="flex justify-between items-center p-3 bg-black/20 rounded-lg">
                    <span className="text-gray-400 font-medium">Triggers:</span>
                    <span className={`px-3 py-1 rounded-lg text-sm font-bold border ${
                      automationDisabled 
                        ? 'bg-red-500/20 text-red-300 border-red-500/30' 
                        : 'bg-green-500/20 text-green-300 border-green-500/30'
                    }`}>
                      {automationDisabled ? '🚫 DISABLED' : '✅ ENABLED'}
                    </span>
                  </div>
                  
                  {status?.isActive && (
                    <>
                      <div className="flex justify-between items-center p-3 bg-black/20 rounded-lg">
                        <span className="text-gray-400 font-medium">Symbol:</span>
                        <span className="text-white font-bold">{status.symbol}</span>
                      </div>
                      
                      <div className="flex justify-between items-center p-3 bg-black/20 rounded-lg">
                        <span className="text-gray-400 font-medium">Mode:</span>
                        <span className={`px-3 py-1 rounded-lg text-sm font-bold border ${
                          status.mode === 'LIVE' 
                            ? 'bg-red-500/20 text-red-300 border-red-500/30' 
                            : 'bg-blue-500/20 text-blue-300 border-blue-500/30'
                        }`}>
                          {status.mode}
                        </span>
                      </div>
                      
                      <div className="p-3 bg-black/20 rounded-lg">
                        <div className="text-gray-400 font-medium mb-2">Timeframes:</div>
                        <div className="flex flex-wrap gap-1">
                          {status.timeframes?.map((tf, index) => (
                            <span key={index} className="text-xs bg-cyan-500/20 text-cyan-300 px-2 py-1 rounded border border-cyan-500/30">
                              {timeframes.find(t => t.value === tf)?.label || tf}
                            </span>
                          ))}
                        </div>
                      </div>
                    </>
                  )}
                  
                  {/* Rate Limit Warning */}
                  {status?.rateLimitHit && (
                    <div className="p-3 bg-gradient-to-br from-red-900/50 to-red-800/30 border border-red-600/50 rounded-lg">
                      <div className="flex items-center space-x-2 mb-1">
                        <span className="text-red-400 font-bold">⚠️</span>
                        <span className="text-red-300 font-bold text-sm">Rate Limit Reached</span>
                      </div>
                      {status.rateLimitMessage && (
                        <p className="text-red-200 text-xs mb-1">{status.rateLimitMessage}</p>
                      )}
                      <p className="text-red-100 text-xs">
                        Automation stopped. Recharge OpenAI account to continue.
                      </p>
                    </div>
                  )}
                </div>
              </div>

              {/* Position Monitor Section */}
              {monitorData && (
                <div className="mb-6">
                  <h4 className="text-lg font-semibold text-purple-400 mb-3 flex items-center">
                    <span className="mr-2">📊</span>Position Monitor
                  </h4>
                  <div className="space-y-3">
                    <div className="flex justify-between items-center p-3 bg-black/20 rounded-lg">
                      <span className="text-gray-400 font-medium">Has Position:</span>
                      <span className={`px-3 py-1 rounded-lg text-sm font-bold border ${
                        monitorData.hasPosition 
                          ? 'bg-green-500/20 text-green-300 border-green-500/30' 
                          : 'bg-gray-600/20 text-gray-300 border-gray-600/30'
                      }`}>
                        {monitorData.hasPosition ? '✅ YES' : '❌ NO'}
                      </span>
                    </div>
                    
                    <div className="flex justify-between items-center p-3 bg-black/20 rounded-lg">
                      <span className="text-gray-400 font-medium">Risk Level:</span>
                      <span className={`px-3 py-1 rounded-lg text-sm font-bold border ${
                        monitorData.riskLevel === 'HIGH' ? 'bg-red-500/20 text-red-300 border-red-500/30' : 
                        monitorData.riskLevel === 'MEDIUM' ? 'bg-yellow-500/20 text-yellow-300 border-yellow-500/30' : 
                        'bg-green-500/20 text-green-300 border-green-500/30'
                      }`}>
                        {monitorData.riskLevel}
                      </span>
                    </div>
                    
                    <div className="p-3 bg-black/20 rounded-lg">
                      <div className="text-gray-400 text-sm mb-1">Next Action:</div>
                      <div className="text-white text-sm">{monitorData.nextAction}</div>
                    </div>
                    
                    {monitorData.orphanedOrderCleanup && (
                      <div className={`p-3 rounded-lg border ${
                        monitorData.orphanedOrderCleanup.success 
                          ? 'bg-green-900/30 border-green-600/50' 
                          : 'bg-red-900/30 border-red-600/50'
                      }`}>
                        <div className="text-sm font-semibold mb-1">
                          {monitorData.orphanedOrderCleanup.success ? '✅ Cleanup Success' : '❌ Cleanup Failed'}
                        </div>
                        <div className="text-xs text-gray-300">
                          {monitorData.orphanedOrderCleanup.message}
                        </div>
                      </div>
                    )}
                  </div>
                </div>
              )}

              {/* Open Positions Section */}
              {positions.length > 0 && (
                <div className="mb-6">
                  <h4 className="text-lg font-semibold text-yellow-400 mb-3 flex items-center">
                    <span className="mr-2">📈</span>Open Positions 
                    <span className="ml-2 text-sm bg-yellow-500/20 text-yellow-300 px-2 py-1 rounded-lg border border-yellow-500/30">
                      {positions.length}
                    </span>
                  </h4>
                  <div className="space-y-3">
                    {positions.map((position, index) => (
                      <div key={index} className="p-4 bg-gradient-to-r from-gray-800/50 to-gray-700/30 rounded-xl border border-gray-600/30">
                        <div className="flex justify-between items-center mb-3">
                          <span className="text-white font-bold">{position.symbol}</span>
                          <span className={`px-2 py-1 rounded-lg text-xs font-bold border ${
                            position.side === 'LONG' 
                              ? 'bg-green-500/20 text-green-300 border-green-500/30' 
                              : 'bg-red-500/20 text-red-300 border-red-500/30'
                          }`}>
                            {position.side}
                          </span>
                        </div>
                        
                        <div className="grid grid-cols-2 gap-2 text-sm">
                          <div className="flex justify-between">
                            <span className="text-gray-400">Size:</span>
                            <span className="text-white font-semibold">
                              {position.symbol?.includes('SOL') ? 
                                `${parseFloat(position.size).toFixed(2)} SOL` : 
                                `$${parseFloat(position.size).toFixed(2)}`
                              }
                            </span>
                          </div>
                          
                          <div className="flex justify-between">
                            <span className="text-gray-400">Value:</span>
                            <span className="text-green-400 font-bold">
                              ${((parseFloat(position.size) || 0) * (parseFloat(position.markPrice) || parseFloat(position.entryPrice) || 0)).toFixed(2)}
                            </span>
                          </div>
                          
                          {position.entryPrice && (
                            <div className="flex justify-between">
                              <span className="text-gray-400">Entry:</span>
                              <span className="text-white font-mono">${parseFloat(position.entryPrice).toFixed(2)}</span>
                            </div>
                          )}
                          
                          {position.markPrice && (
                            <div className="flex justify-between">
                              <span className="text-gray-400">Mark:</span>
                              <span className="text-white font-mono">${parseFloat(position.markPrice).toFixed(2)}</span>
                            </div>
                          )}
                          
                          {position.pnl !== undefined && (
                            <div className="flex justify-between">
                              <span className="text-gray-400">PnL:</span>
                              <span className={`font-bold ${
                                position.pnl >= 0 ? 'text-green-400' : 'text-red-400'
                              }`}>
                                ${position.pnl >= 0 ? '+' : ''}${parseFloat(position.pnl).toFixed(2)}
                              </span>
                            </div>
                          )}
                        </div>
                      </div>
                    ))}
                  </div>
                </div>
              )}
            </div>

            {/* Account Balance Card */}
            {balance && (
              <div className="bg-gradient-to-br from-gray-900/90 via-slate-800/80 to-gray-900/90 backdrop-blur-xl p-6 rounded-2xl border border-gray-600/30 shadow-2xl">
                <div className="flex items-center space-x-3 mb-6">
                  <div className="w-12 h-12 bg-gradient-to-br from-green-500 to-emerald-600 rounded-xl flex items-center justify-center shadow-lg shadow-green-500/25">
                    <span className="text-2xl">💰</span>
                  </div>
                  <div>
                    <h3 className="text-xl font-bold text-white">Account Balance</h3>
                    <p className="text-gray-400">Live Drift Protocol data</p>
                  </div>
                </div>
                
                <div className="space-y-4">
                  <div className="p-4 bg-gradient-to-r from-green-900/30 to-emerald-900/20 rounded-xl border border-green-500/30">
                    <div className="text-gray-400 text-sm mb-1">Available Balance</div>
                    <div className="text-green-400 font-bold text-2xl">${parseFloat(balance.availableBalance).toFixed(2)}</div>
                  </div>
                  
                  <div className="grid grid-cols-2 gap-4">
                    <div className="p-3 bg-black/20 rounded-lg">
                      <div className="text-gray-400 text-xs mb-1">Total Collateral</div>
                      <div className="text-white font-semibold">${parseFloat(balance.totalCollateral).toFixed(2)}</div>
                    </div>
                    
                    <div className="p-3 bg-black/20 rounded-lg">
                      <div className="text-gray-400 text-xs mb-1">Total Positions</div>
                      <div className="text-yellow-400 font-semibold">{balance.positions || 0}</div>
                    </div>
                  </div>
                </div>
              </div>
            )}
          </div>
        </div>

        {/* Enhanced AI Learning System Panel */}
        <EnhancedAILearningPanel />

        {/* Enhanced AI Trading Analysis Panel */}
        <div className="bg-gradient-to-br from-purple-900/40 via-blue-900/30 to-purple-900/40 backdrop-blur-xl p-8 rounded-2xl border-2 border-purple-500/40 shadow-2xl shadow-purple-500/20">
          <div className="flex items-center justify-between mb-8">
            <div className="flex items-center space-x-4">
              <div className="w-16 h-16 bg-gradient-to-br from-purple-500 to-blue-600 rounded-2xl flex items-center justify-center shadow-lg shadow-purple-500/30">
                <span className="text-3xl">🧠</span>
              </div>
              <div>
                <h3 className="text-3xl font-bold text-transparent bg-clip-text bg-gradient-to-r from-purple-400 to-blue-400">
                  AI Trading Analysis
                </h3>
                <p className="text-gray-300 text-lg">Real-time market intelligence and decision reasoning</p>
              </div>
            </div>
            <div className="flex items-center space-x-4">
              <div className={`w-4 h-4 rounded-full shadow-lg ${
                liveDecisions?.length > 0 ? 'bg-green-400 animate-pulse shadow-green-400/50' : 'bg-gray-500'
              }`}></div>
              <span className="text-lg text-gray-300 font-medium">
                {liveDecisions?.length > 0 ? '🟢 Live Analysis Active' : '⚪ Waiting for Analysis'}
              </span>
            </div>
          </div>

          {liveDecisions?.length > 0 ? (
            <div className="space-y-8">
              {/* Current Decision Summary */}
              {liveDecisions[0] && (
                <div className="grid grid-cols-1 md:grid-cols-4 gap-6">
                  <div className="bg-black/40 backdrop-blur-sm rounded-2xl p-6 border border-purple-500/30 shadow-lg">
                    <div className="text-purple-400 text-sm uppercase tracking-wide mb-2 font-bold">Latest Decision</div>
                    <div className={`text-2xl font-bold mb-2 ${
                      liveDecisions[0].action === 'BUY' ? 'text-green-300' :
                      liveDecisions[0].action === 'SELL' ? 'text-red-300' :
                      'text-gray-300'
                    }`}>
                      {liveDecisions[0].action || 'UNKNOWN'}
                    </div>
                    <div className={`text-sm px-2 py-1 rounded-lg border ${
                      liveDecisions[0].blocked 
                        ? 'text-red-300 bg-red-500/20 border-red-500/30' 
                        : 'text-green-300 bg-green-500/20 border-green-500/30'
                    }`}>
                      {liveDecisions[0].blocked ? '🚫 BLOCKED' : '✅ EXECUTED'}
                    </div>
                  </div>
                  
                  <div className="bg-black/40 backdrop-blur-sm rounded-2xl p-6 border border-purple-500/30 shadow-lg">
                    <div className="text-purple-400 text-sm uppercase tracking-wide mb-2 font-bold">Confidence</div>
                    <div className={`text-2xl font-bold mb-2 ${
                      liveDecisions[0].confidence >= 80 ? 'text-green-300' :
                      liveDecisions[0].confidence >= 70 ? 'text-yellow-300' :
                      'text-red-300'
                    }`}>
                      {liveDecisions[0].confidence || 0}%
                    </div>
                    <div className="text-sm text-gray-400">
                      Entry: ${liveDecisions[0].entryPrice?.toFixed(2) || 'N/A'}
                    </div>
                  </div>

                  <div className="bg-black/40 backdrop-blur-sm rounded-2xl p-6 border border-purple-500/30 shadow-lg">
                    <div className="text-purple-400 text-sm uppercase tracking-wide mb-2 font-bold">Risk Management</div>
                    <div className="text-lg font-bold text-gray-300 mb-1">
                      SL: ${liveDecisions[0].stopLoss?.toFixed(2) || 'N/A'}
                    </div>
                    <div className="text-lg font-bold text-gray-300">
                      TP: ${liveDecisions[0].takeProfit?.toFixed(2) || 'N/A'}
                    </div>
                  </div>

                  <div className="bg-black/40 backdrop-blur-sm rounded-2xl p-6 border border-purple-500/30 shadow-lg">
                    <div className="text-purple-400 text-sm uppercase tracking-wide mb-2 font-bold">Leverage</div>
                    <div className="text-2xl font-bold text-blue-300 mb-2">
                      {liveDecisions[0].leverage || 1}x
                    </div>
                    <div className="text-sm text-gray-400">
                      {new Date(liveDecisions[0].timestamp).toLocaleTimeString()}
                    </div>
                  </div>
                </div>
              )}

              {/* Main Analysis Content */}
              <div className="grid grid-cols-1 xl:grid-cols-2 gap-8">
                {/* AI Reasoning */}
                <div className="space-y-6">
                  <div className="bg-black/30 backdrop-blur-sm rounded-2xl p-6 border border-purple-500/30 shadow-lg">
                    <h4 className="text-purple-300 font-bold text-xl mb-4 flex items-center">
                      <span className="mr-3 text-2xl">🎯</span>
                      Market Analysis & Reasoning
                    </h4>
                    <div className="bg-gradient-to-br from-gray-900/80 to-purple-900/20 rounded-xl p-6 border-l-4 border-purple-500 shadow-inner">
                      <div className="text-gray-200 leading-relaxed whitespace-pre-line text-lg">
                        {liveDecisions[0]?.reasoning || 'No reasoning available'}
                      </div>
                    </div>
                  </div>

                  {/* Block Reason */}
                  {liveDecisions[0]?.blocked && liveDecisions[0]?.blockReason && (
                    <div className="bg-red-900/30 border-2 border-red-600/40 rounded-2xl p-6 backdrop-blur-sm">
                      <h4 className="text-red-400 font-bold text-lg mb-3 flex items-center">
                        <span className="mr-3 text-2xl">🚫</span>
                        Risk Management Block
                      </h4>
                      <div className="bg-gradient-to-br from-red-900/40 to-red-800/20 rounded-xl p-6 border-l-4 border-red-500 shadow-inner">
                        <div className="text-red-200 leading-relaxed text-lg">
                          {liveDecisions[0].blockReason}
                        </div>
                      </div>
                    </div>
                  )}
                </div>

                {/* Recent Decisions History */}
                <div className="space-y-6">
                  <div className="bg-black/30 backdrop-blur-sm rounded-2xl p-6 border border-purple-500/30 shadow-lg">
                    <h4 className="text-purple-300 font-bold text-xl mb-4 flex items-center">
                      <span className="mr-3 text-2xl">📊</span>
                      Recent Decisions ({liveDecisions.length})
                    </h4>
                    <div className="space-y-3 max-h-96 overflow-y-auto">
                      {liveDecisions.slice(0, 5).map((decision, index) => (
                        <div key={index} className={`p-4 rounded-xl border-l-4 ${
                          decision.blocked 
                            ? 'bg-red-900/20 border-red-500 border border-red-500/30' 
                            : 'bg-green-900/20 border-green-500 border border-green-500/30'
                        }`}>
                          <div className="flex justify-between items-start mb-2">
                            <div className={`font-bold text-lg ${
                              decision.action === 'BUY' ? 'text-green-300' :
                              decision.action === 'SELL' ? 'text-red-300' :
                              'text-gray-300'
                            }`}>
                              {decision.action} {decision.symbol}
                            </div>
                            <div className="text-sm text-gray-400">
                              {new Date(decision.timestamp).toLocaleTimeString()}
                            </div>
                          </div>
                          <div className="text-sm text-gray-300 mb-2">
                            Entry: ${decision.entryPrice?.toFixed(2)} | {decision.leverage}x leverage | {decision.confidence}% confidence
                          </div>
                          {decision.blocked ? (
                            <div className="text-sm text-red-300">
                              🚫 Blocked: {decision.blockReason?.substring(0, 100)}...
                            </div>
                          ) : (
                            <div className="text-sm text-green-300">
                              ✅ Executed with SL: ${decision.stopLoss?.toFixed(2)} TP: ${decision.takeProfit?.toFixed(2)}
                            </div>
                          )}
                        </div>
                      ))}
                    </div>
                  </div>
                </div>
              </div>
            </div>
          ) : (
            <div className="text-center py-16">
              <div className="text-9xl mb-8 opacity-60">🤖</div>
              <h4 className="text-white font-bold text-xl mb-4 flex items-center justify-center">
                        <span className="mr-3 text-2xl">📈</span>
                        Trade Execution Details
                      </h4>
                      <div className="grid grid-cols-1 gap-6">
                        <div className="grid grid-cols-2 gap-4">
                          <div className="bg-blue-900/20 rounded-xl p-4 border border-blue-500/30">
                            <div className="text-blue-400 text-sm mb-1 font-medium">Entry Price</div>
                            <div className="text-white font-mono text-xl font-bold">
                              ${latestDecision?.details?.entryPrice?.toFixed(4) || 'N/A'}
                            </div>
                          </div>
                          <div className="bg-green-900/20 rounded-xl p-4 border border-green-500/30">
                            <div className="text-green-400 text-sm mb-1 font-medium">Position Size</div>
                            <div className="text-white font-bold text-xl">
                              ${latestDecision?.details?.amount || 'N/A'}
                            </div>
                          </div>
                          <div className={`rounded-xl p-4 border ${
                            latestDecision?.details?.side === 'BUY' 
                              ? 'bg-green-900/20 border-green-500/30' 
                              : 'bg-red-900/20 border-red-500/30'
                          }`}>
                            <div className="text-gray-400 text-sm mb-1 font-medium">Direction</div>
                            <div className={`font-bold text-xl ${
                              latestDecision?.details?.side === 'BUY' ? 'text-green-300' : 'text-red-300'
                            }`}>
                              {latestDecision?.details?.side || 'N/A'}
                            </div>
                          </div>
                          <div className="bg-yellow-900/20 rounded-xl p-4 border border-yellow-500/30">
                            <div className="text-yellow-400 text-sm mb-1 font-medium">Leverage</div>
                            <div className="text-white font-bold text-xl">
                              {latestDecision?.details?.leverage || 'N/A'}x
                            </div>
                          </div>
                        </div>

                        {/* Risk Management */}
                        <div className="bg-gray-900/40 rounded-xl p-4 border border-gray-600/30">
                          <h5 className="text-gray-300 font-bold mb-3 flex items-center">
                            <span className="mr-2">🛡️</span>
                            Risk Management
                          </h5>
                          <div className="grid grid-cols-2 gap-4">
                            <div className="flex justify-between items-center">
                              <span className="text-red-400 font-medium">Stop Loss:</span>
                              <span className="text-red-300 font-mono text-lg font-bold">
                                ${latestDecision?.details?.stopLoss?.toFixed(4) || 'N/A'}
                              </span>
                            </div>
                            <div className="flex justify-between items-center">
                              <span className="text-green-400 font-medium">Take Profit:</span>
                              <span className="text-green-300 font-mono text-lg font-bold">
                                ${latestDecision?.details?.takeProfit?.toFixed(4) || 'N/A'}
                              </span>
                            </div>
                          </div>
                          {latestDecision?.details?.txId && (
                            <div className="mt-3 pt-3 border-t border-gray-600/30">
                              <div className="flex justify-between items-center">
                                <span className="text-gray-400 text-sm">Transaction ID:</span>
                                <span className="text-blue-400 font-mono text-sm">
                                  {latestDecision.details.txId.substring(0, 12)}...
                                </span>
                              </div>
                            </div>
                          )}
                        </div>
                      </div>
                    </div>
                  </div>
                </div>
              </div>
            </div>
          ) : (
            <div className="text-center py-16">
              <div className="text-9xl mb-8 opacity-60">🤖</div>
              <h4 className="text-3xl text-purple-300 font-bold mb-6">Waiting for Live Analysis Data</h4>
              <p className="text-gray-300 mb-8 text-xl max-w-3xl mx-auto leading-relaxed">
                Automation is running every 2 minutes. Live trading decisions and risk management details will appear here 
                as soon as the AI analyzes market conditions and attempts trades.
              </p>
              
              {/* Real-time Status */}
              <div className="bg-blue-900/30 rounded-2xl p-6 border-2 border-blue-500/40 max-w-2xl mx-auto backdrop-blur-sm mb-8">
                <div className="text-blue-300 font-bold text-lg mb-4">🔄 System Status</div>
                <div className="grid grid-cols-2 gap-4 text-left">
                  <div className="bg-black/30 rounded-lg p-4">
                    <div className="text-green-400 font-semibold">✅ Automation Active</div>
                    <div className="text-gray-300 text-sm">Scanning every 2 minutes</div>
                  </div>
                  <div className="bg-black/30 rounded-lg p-4">
                    <div className="text-blue-400 font-semibold">🛡️ Risk Management</div>
                    <div className="text-gray-300 text-sm">Mandatory protection active</div>
                  </div>
                </div>
              </div>
              
              <div className="bg-purple-900/30 rounded-2xl p-8 border-2 border-purple-500/40 max-w-4xl mx-auto backdrop-blur-sm">
                <div className="text-purple-300 font-bold text-xl mb-6">What you'll see when analysis data arrives:</div>
                <div className="grid grid-cols-1 md:grid-cols-2 gap-6 text-left">
                  <div className="space-y-4">
                    <div className="flex items-center p-3 bg-purple-800/20 rounded-lg border border-purple-500/30">
                      <span className="text-purple-400 mr-3 text-xl">🎯</span>
                      <div>
                        <div className="text-white font-semibold">Trade Decisions</div>
                        <div className="text-gray-300 text-sm">BUY/SELL signals with confidence</div>
                      </div>
                    </div>
                    <div className="flex items-center p-3 bg-purple-800/20 rounded-lg border border-purple-500/30">
                      <span className="text-purple-400 mr-3 text-xl">�</span>
                      <div>
                        <div className="text-white font-semibold">Risk Blocks</div>
                        <div className="text-gray-300 text-sm">Why trades were blocked by safety</div>
                      </div>
                    </div>
                    <div className="flex items-center p-3 bg-purple-800/20 rounded-lg border border-purple-500/30">
                      <span className="text-purple-400 mr-3 text-xl">📊</span>
                      <div>
                        <div className="text-white font-semibold">AI Reasoning</div>
                        <div className="text-gray-300 text-sm">Detailed market analysis</div>
                      </div>
                    </div>
                  </div>
                  <div className="space-y-4">
                    <div className="flex items-center p-3 bg-purple-800/20 rounded-lg border border-purple-500/30">
                      <span className="text-purple-400 mr-3 text-xl">💰</span>
                      <div>
                        <div className="text-white font-semibold">Entry/SL/TP Details</div>
                        <div className="text-gray-300 text-sm">Exact price levels and leverage</div>
                      </div>
                    </div>
                    <div className="flex items-center p-3 bg-purple-800/20 rounded-lg border border-purple-500/30">
                      <span className="text-purple-400 mr-3 text-xl">✅</span>
                      <div>
                        <div className="text-white font-semibold">Execution Status</div>
                        <div className="text-gray-300 text-sm">Live trade confirmations</div>
                      </div>
                    </div>
                    <div className="flex items-center p-3 bg-purple-800/20 rounded-lg border border-purple-500/30">
                      <span className="text-purple-400 mr-3 text-xl">📈</span>
                      <div>
                        <div className="text-white font-semibold">Decision History</div>
                        <div className="text-gray-300 text-sm">Recent analysis timeline</div>
                      </div>
                    </div>
                  </div>
                </div>
              </div>
                        <div className="text-white font-semibold">Leverage Calculation</div>
                        <div className="text-gray-300 text-sm">AI's dynamic risk assessment</div>
                      </div>
                    </div>
                    <div className="flex items-center p-3 bg-purple-800/20 rounded-lg border border-purple-500/30">
                      <span className="text-purple-400 mr-3 text-xl">🎲</span>
                      <div>
                        <div className="text-white font-semibold">Confidence Scoring</div>
                        <div className="text-gray-300 text-sm">Probability-based decision making</div>
                      </div>
                    </div>
                    <div className="flex items-center p-3 bg-purple-800/20 rounded-lg border border-purple-500/30">
                      <span className="text-purple-400 mr-3 text-xl">✅</span>
                      <div>
                        <div className="text-white font-semibold">Execution Status</div>
                        <div className="text-gray-300 text-sm">Real-time trade confirmation</div>
                      </div>
                    </div>
                  </div>
                </div>
              </div>
            </div>
          )}
        </div>
      </div>
    </div>
  )
}
