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

// 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 testDocumentAIIntegration() {
  console.log('🧪 Testing Document AI Integration...\n');
  
  try {
    // Test 1: Google Cloud Storage
    console.log('1. Testing Google Cloud Storage...');
    const storage = new Storage();
    
    // Test bucket access
    const [bucketExists] = await storage.bucket(GCS_BUCKET_NAME).exists();
    console.log(`   ✅ GCS Bucket '${GCS_BUCKET_NAME}' exists: ${bucketExists}`);
    
    const [outputBucketExists] = await storage.bucket(DOCUMENT_AI_OUTPUT_BUCKET_NAME).exists();
    console.log(`   ✅ GCS Bucket '${DOCUMENT_AI_OUTPUT_BUCKET_NAME}' exists: ${outputBucketExists}`);
    
    // Test 2: Document AI Client
    console.log('\n2. Testing Document AI Client...');
    const documentAiClient = new DocumentProcessorServiceClient();
    console.log('   ✅ Document AI client initialized successfully');
    
    // Test 3: Service Account Permissions
    console.log('\n3. Testing Service Account Permissions...');
    try {
      // Try to list processors (this will test permissions)
      const [processors] = await documentAiClient.listProcessors({
        parent: `projects/${PROJECT_ID}/locations/${LOCATION}`,
      });
      
      console.log(`   ✅ Found ${processors.length} existing 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}`);
        });
        
        // Use the first processor for testing
        const processorId = processors[0].name.split('/').pop();
        console.log(`\n   🎯 Using processor ID: ${processorId}`);
        console.log(`   Add this to your .env file: DOCUMENT_AI_PROCESSOR_ID=${processorId}`);
        
        return processorId;
      } else {
        console.log('   ⚠️  No processors found. You may need to create one manually.');
        console.log('   💡 Go to: https://console.cloud.google.com/ai/document-ai/processors');
        console.log('   💡 Create a "Document OCR" processor for your project.');
      }
      
    } catch (error) {
      console.log(`   ❌ Permission test failed: ${error.message}`);
      console.log('   💡 This is expected if no processors exist yet.');
    }
    
    // Test 4: File Upload Test
    console.log('\n4. Testing File Upload...');
    const testContent = 'This is a test document for CIM processing.';
    const testFileName = `test-${Date.now()}.txt`;
    
    const bucket = storage.bucket(GCS_BUCKET_NAME);
    const file = bucket.file(testFileName);
    
    await file.save(testContent, {
      metadata: {
        contentType: 'text/plain',
      },
    });
    
    console.log(`   ✅ Uploaded test file: gs://${GCS_BUCKET_NAME}/${testFileName}`);
    
    // Clean up test file
    await file.delete();
    console.log(`   ✅ Cleaned up test file`);
    
    // Test 5: Integration Summary
    console.log('\n5. Integration Summary...');
    console.log('   ✅ Google Cloud Storage: Working');
    console.log('   ✅ Document AI Client: Working');
    console.log('   ✅ Service Account: Configured');
    console.log('   ✅ File Operations: Working');
    
    console.log('\n🎉 Document AI Integration Test Completed Successfully!');
    console.log('\n📋 Next Steps:');
    console.log('1. Create a Document AI processor in the Google Cloud Console');
    console.log('2. Add the processor ID to your .env file');
    console.log('3. Test with a real CIM document');
    
    return null;
    
  } catch (error) {
    console.error('\n❌ Integration test failed:', error.message);
    console.log('\n🔧 Troubleshooting:');
    console.log('1. Check if GOOGLE_APPLICATION_CREDENTIALS is set correctly');
    console.log('2. Verify service account has proper permissions');
    console.log('3. Ensure Document AI API is enabled');
    
    throw error;
  }
}

async function testWithSampleDocument() {
  console.log('\n📄 Testing with Sample Document...');
  
  try {
    // Create a sample CIM-like document
    const sampleCIM = `
INVESTMENT MEMORANDUM

Company: Sample Tech Corp
Industry: Technology
Investment Size: $10M

FINANCIAL SUMMARY
Revenue: $5M (2023)
EBITDA: $1.2M
Growth Rate: 25% YoY

MARKET OPPORTUNITY
Total Addressable Market: $50B
Market Position: Top 3 in segment
Competitive Advantages: Proprietary technology, strong team

INVESTMENT THESIS
1. Strong product-market fit
2. Experienced management team
3. Large market opportunity
4. Proven revenue model

RISK FACTORS
1. Market competition
2. Regulatory changes
3. Technology obsolescence

EXIT STRATEGY
IPO or strategic acquisition within 5 years
Expected return: 3-5x
    `;
    
    console.log('   ✅ Sample CIM document created');
    console.log(`   📊 Document length: ${sampleCIM.length} characters`);
    
    return sampleCIM;
    
  } catch (error) {
    console.error('   ❌ Failed to create sample document:', error.message);
    throw error;
  }
}

async function main() {
  try {
    // Set up credentials
    process.env.GOOGLE_APPLICATION_CREDENTIALS = path.join(__dirname, '../serviceAccountKey.json');
    
    const processorId = await testDocumentAIIntegration();
    const sampleDocument = await testWithSampleDocument();
    
    console.log('\n📋 Configuration Summary:');
    console.log(`Project ID: ${PROJECT_ID}`);
    console.log(`Location: ${LOCATION}`);
    console.log(`GCS Bucket: ${GCS_BUCKET_NAME}`);
    console.log(`Output Bucket: ${DOCUMENT_AI_OUTPUT_BUCKET_NAME}`);
    if (processorId) {
      console.log(`Processor ID: ${processorId}`);
    }
    
    console.log('\n🚀 Ready to integrate with your CIM processing system!');
    
  } catch (error) {
    console.error('Test failed:', error);
    process.exit(1);
  }
}

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

module.exports = { testDocumentAIIntegration, testWithSampleDocument }; 