const OpenAI = require('openai').default

async function testOpenAIVision() {
  try {
    console.log('🔍 Testing OpenAI Vision capabilities...')
    
    const openai = new OpenAI({
      apiKey: process.env.OPENAI_API_KEY,
    })
    
    // Test with a simple text-only request first
    console.log('📝 Testing text-only request...')
    
    const textResponse = await openai.chat.completions.create({
      model: "gpt-4o",
      messages: [
        {
          role: "user",
          content: "Respond with a simple JSON: {'test': 'success'}"
        }
      ],
      max_tokens: 50
    })
    
    console.log('✅ Text response:', textResponse.choices[0]?.message?.content)
    
    // Test with a simple image URL (public image)
    console.log('🖼️ Testing with public image...')
    
    const imageResponse = await openai.chat.completions.create({
      model: "gpt-4o",
      messages: [
        {
          role: "user",
          content: [
            { type: "text", text: "What do you see in this image? Respond with JSON: {'description': 'your description'}" },
            { 
              type: "image_url", 
              image_url: { 
                url: "https://upload.wikimedia.org/wikipedia/commons/thumb/d/dd/Gfp-wisconsin-madison-the-nature-boardwalk.jpg/2560px-Gfp-wisconsin-madison-the-nature-boardwalk.jpg"
              } 
            }
          ]
        }
      ],
      max_tokens: 100
    })
    
    console.log('✅ Image response:', imageResponse.choices[0]?.message?.content)
    
    // Check account details
    console.log('🔍 Checking available models...')
    
    const models = await openai.models.list()
    const visionModels = models.data.filter(model => 
      model.id.includes('gpt-4') && (model.id.includes('vision') || model.id.includes('gpt-4o'))
    )
    
    console.log('🎯 Available vision models:')
    visionModels.forEach(model => console.log(`  - ${model.id}`))
    
  } catch (error) {
    console.error('❌ Error:', error.message)
    if (error.response) {
      console.error('📊 Response status:', error.response.status)
      console.error('📝 Response data:', JSON.stringify(error.response.data, null, 2))
    }
  }
}

testOpenAIVision()
