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

// Configuration
const PROJECT_ID = 'cim-summarizer';
const LOCATION = 'us';

async function getProcessorType() {
  console.log('🔍 Getting OCR Processor Type...\n');
  
  const client = new DocumentProcessorServiceClient();
  
  try {
    const [processorTypes] = await client.listProcessorTypes({
      parent: `projects/${PROJECT_ID}/locations/${LOCATION}`,
    });
    
    console.log(`Found ${processorTypes.length} processor types:\n`);
    
    // Find OCR processor
    const ocrProcessor = processorTypes.find(pt => 
      pt.name && pt.name.includes('OCR_PROCESSOR')
    );
    
    if (ocrProcessor) {
      console.log('🎯 Found OCR Processor:');
      console.log(`   Name: ${ocrProcessor.name}`);
      console.log(`   Category: ${ocrProcessor.category}`);
      console.log(`   Allow Creation: ${ocrProcessor.allowCreation}`);
      console.log('');
      
      // Try to get more details
      try {
        const [processorType] = await client.getProcessorType({
          name: ocrProcessor.name,
        });
        
        console.log('📋 Processor Type Details:');
        console.log(`   Display Name: ${processorType.displayName}`);
        console.log(`   Name: ${processorType.name}`);
        console.log(`   Category: ${processorType.category}`);
        console.log(`   Location: ${processorType.location}`);
        console.log(`   Allow Creation: ${processorType.allowCreation}`);
        console.log('');
        
        return processorType;
        
      } catch (error) {
        console.log('Could not get detailed processor type info:', error.message);
        return ocrProcessor;
      }
    } else {
      console.log('❌ OCR processor not found');
      
      // List all processor types for reference
      console.log('\n📋 All available processor types:');
      processorTypes.forEach((pt, index) => {
        console.log(`${index + 1}. ${pt.name}`);
      });
      
      return null;
    }
    
  } catch (error) {
    console.error('❌ Error getting processor type:', error.message);
    throw error;
  }
}

async function main() {
  try {
    const processorType = await getProcessorType();
    
    if (processorType) {
      console.log('✅ OCR Processor Type found!');
      console.log(`Use this type: ${processorType.name}`);
    } else {
      console.log('❌ OCR Processor Type not found');
    }
    
  } catch (error) {
    console.error('Failed to get processor type:', error);
    process.exit(1);
  }
}

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

module.exports = { getProcessorType }; 