import { NextRequest, NextResponse } from 'next/server' import { enhancedScreenshotService } from '../../../../lib/enhanced-screenshot' import { aiAnalysisService } from '../../../../lib/ai-analysis' export async function POST(request: NextRequest) { try { const body = await request.json() const { symbol, timeframe, credentials } = body // Validate required fields (credentials optional if using .env) if (!symbol || !timeframe) { return NextResponse.json( { error: 'Missing required fields: symbol, timeframe' }, { status: 400 } ) } console.log(`Starting automated analysis for ${symbol} ${timeframe}`) // Take screenshot with automated login and navigation const screenshots = await enhancedScreenshotService.captureWithLogin({ symbol, timeframe, credentials // Will use .env if not provided }) if (screenshots.length === 0) { throw new Error('Failed to capture screenshots') } // Analyze the first screenshot const analysis = await aiAnalysisService.analyzeScreenshot(screenshots[0]) if (!analysis) { throw new Error('Failed to analyze screenshot') } return NextResponse.json({ success: true, data: { screenshots, analysis, symbol, timeframe, timestamp: new Date().toISOString() } }) } catch (error: any) { console.error('Automated analysis error:', error) return NextResponse.json( { error: 'Failed to perform automated analysis', details: error?.message || 'Unknown error' }, { status: 500 } ) } } export async function GET() { try { // Health check for the automation system const healthCheck = await enhancedScreenshotService.healthCheck() return NextResponse.json({ status: healthCheck.status, message: healthCheck.message, timestamp: new Date().toISOString(), dockerEnvironment: true }) } catch (error: any) { return NextResponse.json( { status: 'error', message: `Health check failed: ${error?.message || 'Unknown error'}`, dockerEnvironment: true }, { status: 500 } ) } }