import { NextRequest, NextResponse } from 'next/server' import { aiAnalysisService } from '../../../lib/ai-analysis' import { TradingViewCredentials } from '../../../lib/tradingview-automation' export async function POST(request: NextRequest) { try { const body = await request.json() const { symbol, timeframe, credentials, action } = body // Validate input if (!symbol || !timeframe || !credentials?.email || !credentials?.password) { return NextResponse.json({ success: false, error: 'Missing required fields: symbol, timeframe, and credentials' }, { status: 400 }) } const tradingViewCredentials: TradingViewCredentials = { email: credentials.email, password: credentials.password } switch (action) { case 'capture_and_analyze': // Single symbol and timeframe const analysis = await aiAnalysisService.captureAndAnalyze( symbol, timeframe, tradingViewCredentials ) if (!analysis) { return NextResponse.json({ success: false, error: 'Failed to capture screenshot or analyze chart' }, { status: 500 }) } return NextResponse.json({ success: true, data: { symbol, timeframe, analysis, timestamp: new Date().toISOString() } }) case 'capture_multiple': // Multiple symbols or timeframes const { symbols = [symbol], timeframes = [timeframe] } = body const results = await aiAnalysisService.captureAndAnalyzeMultiple( symbols, timeframes, tradingViewCredentials ) return NextResponse.json({ success: true, data: { results, timestamp: new Date().toISOString() } }) case 'capture_with_config': // Advanced configuration const { layouts } = body const configResult = await aiAnalysisService.captureAndAnalyzeWithConfig({ symbol, timeframe, layouts, credentials: tradingViewCredentials }) return NextResponse.json({ success: true, data: { symbol, timeframe, screenshots: configResult.screenshots, analysis: configResult.analysis, timestamp: new Date().toISOString() } }) default: return NextResponse.json({ success: false, error: 'Invalid action. Use: capture_and_analyze, capture_multiple, or capture_with_config' }, { status: 400 }) } } catch (error) { console.error('Automated analysis API error:', error) return NextResponse.json({ success: false, error: error instanceof Error ? error.message : 'Unknown error occurred' }, { status: 500 }) } } export async function GET() { return NextResponse.json({ success: true, message: 'TradingView Automated Analysis API', endpoints: { POST: { description: 'Automated screenshot capture and AI analysis', actions: [ 'capture_and_analyze - Single symbol/timeframe analysis', 'capture_multiple - Multiple symbols/timeframes', 'capture_with_config - Advanced configuration with layouts' ], required_fields: ['symbol', 'timeframe', 'credentials', 'action'], example: { symbol: 'SOLUSD', timeframe: '5', credentials: { email: 'your_email@example.com', password: 'your_password' }, action: 'capture_and_analyze' } } } }) }