import { NextResponse } from 'next/server' import { PrismaClient } from '@prisma/client' const prisma = new PrismaClient() export async function GET() { try { // Get the latest automation session with correct data const session = await prisma.automationSession.findFirst({ where: { userId: 'default-user', symbol: 'SOLUSD', timeframe: '1h' }, orderBy: { createdAt: 'desc' } }) if (!session) { return NextResponse.json({ success: true, status: { isActive: false, mode: 'SIMULATION', symbol: 'SOLUSD', timeframe: '1h', totalTrades: 0, successfulTrades: 0, winRate: 0, totalPnL: 0, errorCount: 0 } }) } // Get actual trade data to calculate real statistics const trades = await prisma.trade.findMany({ where: { userId: session.userId, symbol: session.symbol }, orderBy: { createdAt: 'desc' } }) const completedTrades = trades.filter(t => t.status === 'COMPLETED') const successfulTrades = completedTrades.filter(t => { const profit = t.profit || 0 return profit > 0 }) const totalPnL = completedTrades.reduce((sum, trade) => { const profit = trade.profit || 0 return sum + profit }, 0) const winRate = completedTrades.length > 0 ? (successfulTrades.length / completedTrades.length * 100) : 0 return NextResponse.json({ success: true, status: { isActive: session.status === 'ACTIVE', mode: session.mode, symbol: session.symbol, timeframe: session.timeframe, totalTrades: completedTrades.length, successfulTrades: successfulTrades.length, winRate: Math.round(winRate * 10) / 10, // Round to 1 decimal totalPnL: Math.round(totalPnL * 100) / 100, // Round to 2 decimals lastAnalysis: session.lastAnalysis, lastTrade: session.lastTrade, nextScheduled: session.nextScheduled, errorCount: session.errorCount, lastError: session.lastError } }) } catch (error) { console.error('Automation status error:', error) return NextResponse.json({ success: false, error: 'Failed to get automation status' }, { status: 500 }) } }