const fs = require('fs')
const path = require('path')
const OpenAI = require('openai').default

// Initialize OpenAI
const openai = new OpenAI({
  apiKey: process.env.OPENAI_API_KEY,
})

async function testAIAnalysis() {
  try {
    console.log('🔍 Testing AI analysis with a real screenshot...')
    
    // Use the specific screenshot you mentioned
    const screenshotsDir = path.join(process.cwd(), 'screenshots')
    const testImage = 'debug_before_timeframe_change_1752324987294.png'
    const imagePath = path.join(screenshotsDir, testImage)
    
    console.log(`📸 Using screenshot: ${imagePath}`)
    
    // Check if file exists
    if (!fs.existsSync(imagePath)) {
      console.error('❌ Screenshot file not found!')
      return
    }
    
    console.log('✅ Screenshot file found')
    
    // Read and encode image
    const imageBuffer = fs.readFileSync(imagePath)
    const base64Image = imageBuffer.toString('base64')
    
    console.log(`📊 Image size: ${imageBuffer.length} bytes`)
    console.log(`🔒 Base64 length: ${base64Image.length} characters`)
    console.log(`🎯 Base64 preview: ${base64Image.substring(0, 100)}...`)
    
    // Improved prompt for testing
    const prompt = `You are a technical chart analysis expert. Please analyze this TradingView chart image and provide objective technical analysis data.

**Important**: This is for educational and research purposes only. Please analyze the technical indicators, price levels, and chart patterns visible in the image.

Examine the chart and identify:
- Current price action and trend direction
- Key support and resistance levels visible on the chart
- Technical indicator readings (RSI, moving averages, volume if visible)
- Chart patterns or formations
- Market structure elements

Provide your analysis in this exact JSON format (replace values with your analysis):

{
  "summary": "Objective description of what you observe in the chart",
  "marketSentiment": "BULLISH",
  "recommendation": "BUY",
  "confidence": 75
}

Return only the JSON object with your technical analysis.`

    console.log('🤖 Sending request to OpenAI...')
    
    const response = await openai.chat.completions.create({
      model: "gpt-4o", // Latest vision model
      messages: [
        {
          role: "user",
          content: [
            { type: "text", text: prompt },
            { 
              type: "image_url", 
              image_url: { 
                url: `data:image/png;base64,${base64Image}`,
                detail: "low" // Add detail parameter to reduce token usage
              } 
            }
          ]
        }
      ],
      max_tokens: 500,
      temperature: 0.1
    })

    console.log('✅ Response received from OpenAI')
    
    const content = response.choices[0]?.message?.content
    console.log('📝 Full response content:')
    console.log(content)
    
    // Try to extract JSON
    const jsonMatch = content?.match(/\{[\s\S]*\}/)
    if (jsonMatch) {
      console.log('✅ JSON found in response:')
      console.log(jsonMatch[0])
      
      try {
        const parsed = JSON.parse(jsonMatch[0])
        console.log('✅ JSON successfully parsed:')
        console.log(JSON.stringify(parsed, null, 2))
      } catch (e) {
        console.error('❌ Failed to parse JSON:', e.message)
      }
    } else {
      console.error('❌ No JSON found in response')
    }
    
  } catch (error) {
    console.error('❌ Error testing AI analysis:', error.message)
    console.error('Full error:', error)
  }
}

// Run the test
testAIAnalysis()
