const jsforce = require('jsforce');

async function findProperties() {
    try {
        console.log('🔐 Finding the 23 Properties from Interface...');
        
        const conn = new jsforce.Connection({
            loginUrl: 'https://test.salesforce.com'
        });
        
        await conn.login('contact+tso3@propertycrm.ae.r1', 'Demo@123');
        console.log('✅ Login successful!');
        
        // From the screenshot, I can see the URL shows: pcrm__Property__c
        // This suggests the object has a namespace: pcrm__
        console.log('\n🔍 Checking for Namespaced Property Object...');
        
        // Try different possible object names
        const possibleNames = [
            'Property__c',
            'pcrm__Property__c',
            'Property',
            'pcrm__Property'
        ];
        
        for (const objectName of possibleNames) {
            console.log(`\n📊 Trying object: ${objectName}`);
            
            try {
                // Try to describe the object
                const describeResult = await conn.sobject(objectName).describe();
                console.log(`✅ ${objectName} describe successful`);
                console.log('Total fields:', describeResult.fields.length);
                console.log('Fields:', describeResult.fields.map(f => f.name).join(', '));
                
                // Try to query records
                const query = `SELECT Id, Name FROM ${objectName} LIMIT 5`;
                console.log('Query:', query);
                
                const result = await conn.query(query);
                console.log(`Properties found in ${objectName}:`, result.totalSize);
                
                if (result.totalSize > 0) {
                    console.log('\n📋 Property Data:');
                    result.records.forEach((prop, i) => {
                        console.log(`${i+1}. ${prop.Name} (ID: ${prop.Id})`);
                    });
                    
                    // If we found properties, let's get more details
                    if (result.totalSize >= 5) {
                        console.log('\n📊 Fetching More Properties...');
                        const moreProps = await conn.query(`SELECT Id, Name FROM ${objectName} LIMIT 23`);
                        console.log('Total properties found:', moreProps.totalSize);
                        
                        if (moreProps.totalSize > 0) {
                            console.log('\n📋 All Properties:');
                            moreProps.records.forEach((prop, i) => {
                                console.log(`${i+1}. ${prop.Name} (ID: ${prop.Id})`);
                            });
                        }
                    }
                    
                    console.log(`\n🎉 SUCCESS! Found properties in ${objectName}`);
                    break;
                    
                } else {
                    console.log(`No properties found in ${objectName}`);
                }
                
            } catch (error) {
                console.log(`❌ ${objectName} failed:`, error.message);
            }
        }
        
        // Also check if there are any custom objects with "Property" in the name
        console.log('\n🔍 Checking for Custom Objects with "Property" in name...');
        try {
            const customObjects = await conn.query('SELECT Id, Name, DeveloperName, NamespacePrefix FROM CustomObject WHERE DeveloperName LIKE \'%Property%\'');
            console.log('Custom objects with "Property" found:', customObjects.totalSize);
            
            if (customObjects.totalSize > 0) {
                customObjects.records.forEach(obj => {
                    console.log(`- ${obj.Name} (${obj.DeveloperName}) - Namespace: ${obj.NamespacePrefix || 'None'}`);
                });
            }
        } catch (customObjError) {
            console.log('❌ Custom objects query failed:', customObjError.message);
        }
        
        // Check if there are any objects with "Property" in the label
        console.log('\n🔍 Checking for Objects with "Property" in label...');
        try {
            const allObjects = await conn.query('SELECT Id, Name, DeveloperName, NamespacePrefix FROM CustomObject');
            console.log('Total custom objects:', allObjects.totalSize);
            
            const propertyObjects = allObjects.records.filter(obj => 
                obj.Name.toLowerCase().includes('property') || 
                obj.DeveloperName.toLowerCase().includes('property')
            );
            
            if (propertyObjects.length > 0) {
                console.log('Objects with "Property" in name/label:');
                propertyObjects.forEach(obj => {
                    console.log(`- ${obj.Name} (${obj.DeveloperName}) - Namespace: ${obj.NamespacePrefix || 'None'}`);
                });
            }
        } catch (allObjError) {
            console.log('❌ All objects query failed:', allObjError.message);
        }
        
        console.log('\n🔍 ANALYSIS:');
        console.log('From the screenshot, we can see 23 Property records in the interface.');
        console.log('We need to find the correct object name to access them via SOQL.');
        console.log('The URL suggests it might be: pcrm__Property__c');
        
    } catch (error) {
        console.error('❌ Error:', error.message);
    }
}

findProperties(); 