import { NextResponse } from 'next/server'

export async function POST(request) {
  try {
    const body = await request.json()
    const { symbol, timeframe, layout } = body

    console.log('📊 TradingView capture request:', { symbol, timeframe, layout })

    // Mock TradingView chart capture
    const chartData = {
      symbol,
      timeframe,
      layout: layout || 'ai',
      timestamp: new Date().toISOString(),
      screenshot: `/screenshots/chart_${symbol}_${timeframe}_${Date.now()}.png`,
      technicalIndicators: {
        rsi: Math.floor(Math.random() * 100),
        macd: {
          value: (Math.random() - 0.5) * 10,
          signal: (Math.random() - 0.5) * 8,
          histogram: (Math.random() - 0.5) * 5
        },
        bb: {
          upper: (Math.random() * 50 + 200).toFixed(2),
          middle: (Math.random() * 50 + 150).toFixed(2),
          lower: (Math.random() * 50 + 100).toFixed(2)
        }
      },
      priceAction: {
        currentPrice: (Math.random() * 100 + 100).toFixed(2),
        change24h: ((Math.random() - 0.5) * 20).toFixed(2),
        volume: Math.floor(Math.random() * 1000000000),
        trend: Math.random() > 0.5 ? 'bullish' : 'bearish'
      },
      patterns: [
        { type: 'support', level: (Math.random() * 50 + 120).toFixed(2), strength: 'strong' },
        { type: 'resistance', level: (Math.random() * 50 + 180).toFixed(2), strength: 'medium' }
      ]
    }

    return NextResponse.json({
      success: true,
      data: chartData,
      message: 'TradingView chart captured successfully'
    })

  } catch (error) {
    console.error('TradingView capture error:', error)
    return NextResponse.json({
      success: false,
      error: 'Failed to capture TradingView chart',
      message: error instanceof Error ? error.message : 'Unknown error'
    }, { status: 500 })
  }
}

export async function GET() {
  return NextResponse.json({
    message: 'TradingView Chart API - Use POST to capture charts',
    supportedLayouts: ['ai', 'Diy module'],
    supportedTimeframes: ['1', '5', '15', '60', '240', 'D', 'W'],
    requiredFields: ['symbol', 'timeframe']
  })
}
