import { NextResponse } from 'next/server'; export async function GET(request) { try { const { searchParams } = new URL(request.url); const symbol = searchParams.get('symbol') || 'SOLUSD'; const timeframe = searchParams.get('timeframe') || '60'; // 1h default console.log(`🔍 Getting latest AI analysis for ${symbol} on ${timeframe} timeframe...`); // Get fresh screenshot and analysis console.log('🔥 Fetching real screenshot analysis...') const screenshotResponse = await fetch('http://localhost:3000/api/enhanced-screenshot', { method: 'POST', headers: { 'Content-Type': 'application/json', }, body: JSON.stringify({ symbol, timeframe, layouts: ['ai', 'diy'], analyze: true }) }) if (!screenshotResponse.ok) { throw new Error(`Screenshot API failed: ${screenshotResponse.status}`) } const screenshotData = await screenshotResponse.json() console.log('📸 Screenshot response received:', { success: screenshotData.success, hasAnalysis: !!screenshotData.analysis, analysisType: typeof screenshotData.analysis, timestamp: screenshotData.timestamp }) if (!screenshotData.success) { throw new Error('Screenshot system returned failure status') } if (!screenshotData.analysis) { throw new Error('No analysis data from screenshot system') } // Handle case where analysis might have an error property if (screenshotData.analysis.error) { throw new Error(`Analysis failed: ${screenshotData.analysis.error}`) } // Extract real analysis data const analysis = screenshotData.analysis; return NextResponse.json({ success: true, data: { symbol, timeframe, timestamp: new Date().toISOString(), analysis: analysis, screenshots: screenshotData.screenshots, source: 'REAL_SCREENSHOT_ANALYSIS' } }); } catch (error) { console.error('Error getting latest AI analysis:', error); return NextResponse.json({ success: false, error: error.message }, { status: 500 }); } }