import { NextRequest, NextResponse } from 'next/server' import { driftTradingService } from '../../../../lib/drift-trading' export async function POST(request: NextRequest) { try { const { action } = await request.json() if (action === 'start') { console.log('🚀 Starting real-time monitoring...') const result = await driftTradingService.startRealtimeMonitoring() if (result.success) { return NextResponse.json({ success: true, message: 'Real-time monitoring started successfully', status: driftTradingService.getRealtimeMonitoringStatus() }) } else { return NextResponse.json({ success: false, error: result.error, message: 'Failed to start real-time monitoring' }, { status: 500 }) } } else if (action === 'stop') { console.log('🛑 Stopping real-time monitoring...') await driftTradingService.stopRealtimeMonitoring() return NextResponse.json({ success: true, message: 'Real-time monitoring stopped', status: driftTradingService.getRealtimeMonitoringStatus() }) } else if (action === 'status') { const status = driftTradingService.getRealtimeMonitoringStatus() return NextResponse.json({ success: true, status, message: status.isActive ? 'Real-time monitoring is active' : 'Real-time monitoring is not active' }) } else if (action === 'clear') { driftTradingService.clearRealtimeTradesCache() return NextResponse.json({ success: true, message: 'Real-time trades cache cleared', status: driftTradingService.getRealtimeMonitoringStatus() }) } else { return NextResponse.json({ success: false, error: 'Invalid action. Use: start, stop, status, or clear' }, { status: 400 }) } } catch (error: any) { console.error('❌ Error in realtime monitoring endpoint:', error) return NextResponse.json({ success: false, error: error.message, message: 'Internal server error' }, { status: 500 }) } } export async function GET(request: NextRequest) { try { const status = driftTradingService.getRealtimeMonitoringStatus() return NextResponse.json({ success: true, status, message: status.isActive ? 'Real-time monitoring is active' : 'Real-time monitoring is not active' }) } catch (error: any) { console.error('❌ Error getting monitoring status:', error) return NextResponse.json({ success: false, error: error.message }, { status: 500 }) } }