import { NextResponse } from "next/server"; export async function POST(request) { try { console.log("🛡️ SAFE PAPER TRADING API: Starting REAL analysis (paper trading only)..."); const body = await request.json(); const { symbol = "SOLUSD", selectedTimeframes = ["60"], timeframe = "60", mode, paperTrading, isolatedMode, isContinuous = false } = body; const timeframesToAnalyze = selectedTimeframes.length > 0 ? selectedTimeframes : [timeframe]; if (mode !== "PAPER_ONLY" || !paperTrading || !isolatedMode) { return NextResponse.json({ success: false, error: "SAFETY VIOLATION: This API only supports isolated paper trading" }, { status: 403 }); } console.log(`📊 Getting REAL market analysis for ${symbol} on timeframes: ${timeframesToAnalyze.join(", ")} (${isContinuous ? "continuous learning" : "manual"})`); const analysis = await getRealAnalysis({ symbol, selectedTimeframes: timeframesToAnalyze, isContinuous }); console.log("✅ Safe paper analysis complete - REAL DATA, NO TRADING RISK"); return NextResponse.json({ success: true, analysis: { ...analysis, paperTrading: true, isolated: true, noRealTrading: true, realData: true, source: "REAL_MARKET_ANALYSIS", timeframes: timeframesToAnalyze, analysisMode: isContinuous ? "CONTINUOUS_LEARNING" : "MANUAL" }, safety: { paperTrading: true, isolated: true, noRealTrading: true, realData: true, source: "REAL_MARKET_ANALYSIS" }, timestamp: new Date().toISOString() }); } catch (error) { console.error("❌ Safe paper trading API error:", error); return NextResponse.json({ success: false, error: `Real analysis failed: ${error.message}`, details: "Paper trading requires real market data. Please try again.", realDataOnly: true }, { status: 500 }); } } async function getRealAnalysis(config) { try { const { symbol, selectedTimeframes, isContinuous = false } = config; const primaryTimeframe = selectedTimeframes[0]; console.log(`� Attempting to get real analysis for ${symbol} ${primaryTimeframe}m (${selectedTimeframes.length} timeframes)...`); const analysisUrl = `http://localhost:3000/api/ai-analysis/latest?symbol=${symbol}&timeframe=${primaryTimeframe}`; console.log(`📡 Calling: ${analysisUrl}`); const response = await fetch(analysisUrl, { headers: { "Content-Type": "application/json" }, signal: AbortSignal.timeout(180000) }); console.log(`📡 Response status: ${response.status}`); if (!response.ok) { const errorText = await response.text(); console.error(`❌ Analysis API error: ${response.status} - ${errorText}`); throw new Error(`Analysis API returned ${response.status}: ${errorText}`); } const data = await response.json(); console.log(`📊 Raw analysis response received`); if (!data.success || !data.data?.analysis) { console.error("❌ No analysis data in response:", data); throw new Error("No analysis data received from API"); } const realAnalysis = data.data.analysis; console.log(`✅ Real analysis received: ${realAnalysis.recommendation} with ${realAnalysis.confidence}% confidence`); return { ...realAnalysis, paperTrading: true, isolated: true, noRealTrading: true, realData: true, source: "REAL_MARKET_ANALYSIS", timeframes: selectedTimeframes, primaryTimeframe: primaryTimeframe, analysisMode: isContinuous ? "CONTINUOUS_LEARNING" : "MANUAL", reasoning: `PAPER TRADING - REAL MARKET ANALYSIS${isContinuous ? " (Continuous Learning)" : ""}: Multi-Timeframe Analysis: ${selectedTimeframes.join(", ")} Primary Focus: ${primaryTimeframe}m ${realAnalysis.reasoning || "Real market analysis completed"} SAFETY: This is paper trading only - no real trades will be executed. ${isContinuous ? "� LEARNING: System is continuously learning from market patterns." : ""}` }; } catch (error) { console.error("❌ Failed to get real analysis:", error.message); console.error("❌ Error details:", error); throw error; } }