import { NextResponse } from 'next/server'

// We'll import dynamically to avoid module loading issues
// import { DriftFeedbackLoop } from '../../../lib/drift-feedback-loop.js'

// Global feedback loop instance
let feedbackLoop = null

export async function POST(request) {
  try {
    const { action, userId = 'default-user' } = await request.json()

    switch (action) {
      case 'start_monitoring':
        return await startMonitoring(userId)
      
      case 'stop_monitoring':
        return await stopMonitoring()
      
      case 'get_status':
        return await getMonitoringStatus()
      
      case 'check_trades':
        return await checkTradesNow(userId)
        
      case 'get_insights':
        return await getLearningInsights(userId)
      
      default:
        return NextResponse.json({
          success: false,
          error: `Unknown action: ${action}`
        }, { status: 400 })
    }
    
  } catch (error) {
    console.error('❌ Feedback loop API error:', error)
    
    return NextResponse.json({
      success: false,
      error: 'Internal server error',
      details: error.message
    }, { status: 500 })
  }
}

async function startMonitoring(userId) {
  try {
    if (feedbackLoop && feedbackLoop.isMonitoring) {
      return NextResponse.json({
        success: true,
        message: 'Feedback loop already running',
        status: 'ALREADY_RUNNING'
      })
    }

    console.log('🚀 Starting Drift feedback loop monitoring...')
    
    // Dynamic import to avoid ES module issues
    const { DriftFeedbackLoop } = await import('../../../../lib/drift-feedback-loop.js')
    feedbackLoop = new DriftFeedbackLoop()
    await feedbackLoop.initialize()
    await feedbackLoop.startMonitoring(userId)
    
    return NextResponse.json({
      success: true,
      message: 'Drift feedback loop started successfully',
      status: 'STARTED',
      monitoringUserId: userId,
      checkInterval: '30 seconds'
    })
    
  } catch (error) {
    console.error('❌ Failed to start monitoring:', error)
    
    return NextResponse.json({
      success: false,
      error: 'Failed to start monitoring',
      details: error.message
    }, { status: 500 })
  }
}

async function stopMonitoring() {
  try {
    if (!feedbackLoop || !feedbackLoop.isMonitoring) {
      return NextResponse.json({
        success: true,
        message: 'Feedback loop is not running',
        status: 'NOT_RUNNING'
      })
    }

    console.log('⏹️ Stopping Drift feedback loop...')
    
    await feedbackLoop.stopMonitoring()
    feedbackLoop = null
    
    return NextResponse.json({
      success: true,
      message: 'Drift feedback loop stopped successfully',
      status: 'STOPPED'
    })
    
  } catch (error) {
    console.error('❌ Failed to stop monitoring:', error)
    
    return NextResponse.json({
      success: false,
      error: 'Failed to stop monitoring',
      details: error.message
    }, { status: 500 })
  }
}

async function getMonitoringStatus() {
  try {
    const isRunning = feedbackLoop && feedbackLoop.isMonitoring
    
    return NextResponse.json({
      success: true,
      monitoring: {
        isRunning,
        status: isRunning ? 'ACTIVE' : 'STOPPED',
        uptime: isRunning ? 'Active' : 'Not running',
        lastCheck: isRunning ? 'Monitoring every 30 seconds' : 'Not monitoring'
      }
    })
    
  } catch (error) {
    return NextResponse.json({
      success: false,
      error: 'Failed to get status',
      details: error.message
    }, { status: 500 })
  }
}

async function checkTradesNow(userId) {
  try {
    if (!feedbackLoop) {
      // Create temporary instance for one-time check
      const { DriftFeedbackLoop } = await import('../../../../lib/drift-feedback-loop.js')
      const tempLoop = new DriftFeedbackLoop()
      await tempLoop.initialize()
      await tempLoop.checkTradeOutcomes(userId)
      await tempLoop.stopMonitoring()
      
      return NextResponse.json({
        success: true,
        message: 'Manual trade check completed',
        type: 'ONE_TIME_CHECK'
      })
    }
    
    // Use existing instance
    await feedbackLoop.checkTradeOutcomes(userId)
    
    return NextResponse.json({
      success: true,
      message: 'Trade outcomes checked successfully',
      type: 'ONGOING_MONITORING'
    })
    
  } catch (error) {
    console.error('❌ Failed to check trades:', error)
    
    return NextResponse.json({
      success: false,
      error: 'Failed to check trades',
      details: error.message
    }, { status: 500 })
  }
}

async function getLearningInsights(userId) {
  try {
    const { PrismaClient } = await import('@prisma/client')
    const prisma = new PrismaClient()
    
    // Get recent learning insights
    const insights = await prisma.aILearningData.findFirst({
      where: {
        userId,
        symbol: 'INSIGHTS',
        createdAt: {
          gte: new Date(Date.now() - 24 * 60 * 60 * 1000) // Last 24 hours
        }
      },
      orderBy: { createdAt: 'desc' }
    })
    
    // Get recent Drift trades summary
    const recentTrades = await prisma.trade.findMany({
      where: {
        userId,
        driftTxId: { not: null },
        outcome: { not: null },
        closedAt: {
          gte: new Date(Date.now() - 7 * 24 * 60 * 60 * 1000) // Last 7 days
        }
      },
      orderBy: { closedAt: 'desc' }
    })
    
    const winRate = recentTrades.length > 0 
      ? recentTrades.filter(t => t.outcome === 'WIN').length / recentTrades.length 
      : 0
      
    const avgPnL = recentTrades.length > 0
      ? recentTrades.reduce((sum, t) => sum + (t.pnlPercent || 0), 0) / recentTrades.length
      : 0
    
    await prisma.$disconnect()
    
    return NextResponse.json({
      success: true,
      insights: {
        latestInsights: insights ? JSON.parse(insights.analysisData) : null,
        recentPerformance: {
          totalTrades: recentTrades.length,
          winRate: (winRate * 100).toFixed(1) + '%',
          avgPnL: avgPnL.toFixed(2) + '%',
          timeRange: 'Last 7 days'
        },
        feedbackLoopStatus: feedbackLoop && feedbackLoop.isMonitoring ? 'ACTIVE' : 'INACTIVE'
      }
    })
    
  } catch (error) {
    console.error('❌ Failed to get learning insights:', error)
    
    return NextResponse.json({
      success: false,
      error: 'Failed to get learning insights',
      details: error.message
    }, { status: 500 })
  }
}

export async function GET(request) {
  // GET endpoint for quick status check
  return await getMonitoringStatus()
}
