const { DocumentProcessorServiceClient } = require('@google-cloud/documentai');
const { Storage } = require('@google-cloud/storage');

// Configuration
const PROJECT_ID = 'cim-summarizer';
const LOCATION = 'us';
const GCS_BUCKET_NAME = 'cim-summarizer-uploads';
const DOCUMENT_AI_OUTPUT_BUCKET_NAME = 'cim-summarizer-document-ai-output';

async function simpleTest() {
  console.log('🧪 Simple Document AI Test...\n');
  
  try {
    // Test 1: Google Cloud Storage with user account
    console.log('1. Testing Google Cloud Storage...');
    const storage = new Storage();
    
    // List buckets to test access
    const [buckets] = await storage.getBuckets();
    console.log(`   ✅ Found ${buckets.length} buckets`);
    
    const uploadBucket = buckets.find(b => b.name === GCS_BUCKET_NAME);
    const outputBucket = buckets.find(b => b.name === DOCUMENT_AI_OUTPUT_BUCKET_NAME);
    
    console.log(`   📦 Upload bucket exists: ${!!uploadBucket}`);
    console.log(`   📦 Output bucket exists: ${!!outputBucket}`);
    
    // Test 2: Document AI Client
    console.log('\n2. Testing Document AI Client...');
    const documentAiClient = new DocumentProcessorServiceClient();
    console.log('   ✅ Document AI client initialized');
    
    // Test 3: List processors
    console.log('\n3. Testing Document AI Processors...');
    try {
      const [processors] = await documentAiClient.listProcessors({
        parent: `projects/${PROJECT_ID}/locations/${LOCATION}`,
      });
      
      console.log(`   ✅ Found ${processors.length} processors`);
      
      if (processors.length > 0) {
        processors.forEach((processor, index) => {
          console.log(`   📋 Processor ${index + 1}: ${processor.displayName}`);
          console.log(`      ID: ${processor.name.split('/').pop()}`);
          console.log(`      Type: ${processor.type}`);
        });
        
        const processorId = processors[0].name.split('/').pop();
        console.log(`\n   🎯 Recommended processor ID: ${processorId}`);
        
        return processorId;
      } else {
        console.log('   ⚠️  No processors found');
        console.log('   💡 Create one at: https://console.cloud.google.com/ai/document-ai/processors');
      }
      
    } catch (error) {
      console.log(`   ❌ Error listing processors: ${error.message}`);
    }
    
    // Test 4: File upload test
    console.log('\n4. Testing File Upload...');
    if (uploadBucket) {
      const testContent = 'Test CIM document content';
      const testFileName = `test-${Date.now()}.txt`;
      
      const file = uploadBucket.file(testFileName);
      await file.save(testContent, {
        metadata: { contentType: 'text/plain' }
      });
      
      console.log(`   ✅ Uploaded: gs://${GCS_BUCKET_NAME}/${testFileName}`);
      
      // Clean up
      await file.delete();
      console.log(`   ✅ Cleaned up test file`);
    }
    
    console.log('\n🎉 Simple test completed!');
    console.log('\n📋 Next Steps:');
    console.log('1. Create a Document AI processor in the console');
    console.log('2. Add the processor ID to your .env file');
    console.log('3. Test with real CIM documents');
    
    return null;
    
  } catch (error) {
    console.error('\n❌ Test failed:', error.message);
    throw error;
  }
}

async function main() {
  try {
    await simpleTest();
  } catch (error) {
    console.error('Test failed:', error);
    process.exit(1);
  }
}

if (require.main === module) {
  main();
}

module.exports = { simpleTest }; 