public with sharing class PropertyDataController {
    
    @AuraEnabled(cacheable=true)
    public static List<PropertyWrapper> getProperties() {
        List<PropertyWrapper> properties = new List<PropertyWrapper>();
            
        try {
            // Query the correct object: pcrm__Property__c
            List<pcrm__Property__c> propertyRecords = [
                SELECT Id, Name, 
                       pcrm__Property_Type__c, 
                       pcrm__Status__c, 
                       pcrm__Bathrooms__c, 
                       pcrm__Bedrooms__c, 
                       pcrm__Size__c, 
                       pcrm__Sale_Price_min__c,
                       pcrm__Sale_Price_max__c,
                       pcrm__Rent_Price_min__c,
                       pcrm__Rent_Price_max__c,
                       pcrm__Description_English__c,
                       pcrm__Title_English__c,
                       pcrm__City_Bayut_Dubizzle__c,
                       pcrm__Community_Propertyfinder__c,
                       pcrm__Furnished__c,
                       pcrm__Floor__c,
                       pcrm__Build_Year__c,
                       pcrm__Parking_Spaces__c
                FROM pcrm__Property__c 
                ORDER BY Name 
                LIMIT 100
            ];
            
            for (pcrm__Property__c prop : propertyRecords) {
                PropertyWrapper wrapper = new PropertyWrapper();
                wrapper.Id = prop.Id;
                wrapper.Name = prop.Name;
                wrapper.PropertyType = prop.pcrm__Property_Type__c;
                wrapper.Location = prop.pcrm__City_Bayut_Dubizzle__c != null ? prop.pcrm__City_Bayut_Dubizzle__c : 
                                 (prop.pcrm__Community_Propertyfinder__c != null ? prop.pcrm__Community_Propertyfinder__c : 'N/A');
                wrapper.Status = prop.pcrm__Status__c;
                wrapper.Bedrooms = prop.pcrm__Bedrooms__c != null ? String.valueOf(prop.pcrm__Bedrooms__c) : '';
                wrapper.Bathrooms = prop.pcrm__Bathrooms__c != null ? String.valueOf(prop.pcrm__Bathrooms__c) : '';
                wrapper.Area = prop.pcrm__Size__c != null ? String.valueOf(prop.pcrm__Size__c) : '';
                
                // Generate price based on available data
                if (prop.pcrm__Sale_Price_min__c != null && prop.pcrm__Sale_Price_max__c != null) {
                    wrapper.Price = 'AED ' + formatNumber(prop.pcrm__Sale_Price_min__c) + ' - ' + formatNumber(prop.pcrm__Sale_Price_max__c);
                } else if (prop.pcrm__Rent_Price_min__c != null && prop.pcrm__Rent_Price_max__c != null) {
                    wrapper.Price = 'AED ' + formatNumber(prop.pcrm__Rent_Price_min__c) + ' - ' + formatNumber(prop.pcrm__Rent_Price_max__c) + ' (Rent)';
                } else {
                    wrapper.Price = 'Price on Application';
            }
            
                wrapper.TitleEnglish = prop.pcrm__Title_English__c != null ? prop.pcrm__Title_English__c : prop.Name;
                wrapper.Description = prop.pcrm__Description_English__c != null ? prop.pcrm__Description_English__c : 
                                    generateDescription(prop.Name, prop.pcrm__Property_Type__c);
                
                properties.add(wrapper);
            }
            
        } catch (Exception e) {
            System.debug('Error fetching properties: ' + e.getMessage());
            // Return empty list if there's an error
        }
        
        return properties;
    }
    
    @AuraEnabled(cacheable=true)
    public static PropertyWrapper getPropertyDetails(String propertyId) {
        try {
            pcrm__Property__c prop = [
                SELECT Id, Name, 
                       pcrm__Property_Type__c, 
                       pcrm__Status__c, 
                       pcrm__Bathrooms__c, 
                       pcrm__Bedrooms__c, 
                       pcrm__Size__c, 
                       pcrm__Sale_Price_min__c,
                       pcrm__Sale_Price_max__c,
                       pcrm__Rent_Price_min__c,
                       pcrm__Rent_Price_max__c,
                       pcrm__Description_English__c,
                       pcrm__Title_English__c,
                       pcrm__City_Bayut_Dubizzle__c,
                       pcrm__Community_Propertyfinder__c,
                       pcrm__Furnished__c,
                       pcrm__Floor__c,
                       pcrm__Build_Year__c,
                       pcrm__Parking_Spaces__c
                FROM pcrm__Property__c 
                WHERE Id = :propertyId
                LIMIT 1
            ];
                
                PropertyWrapper wrapper = new PropertyWrapper();
                wrapper.Id = prop.Id;
                wrapper.Name = prop.Name;
            wrapper.PropertyType = prop.pcrm__Property_Type__c;
            wrapper.Location = prop.pcrm__City_Bayut_Dubizzle__c != null ? prop.pcrm__City_Bayut_Dubizzle__c : 
                             (prop.pcrm__Community_Propertyfinder__c != null ? prop.pcrm__Community_Propertyfinder__c : 'N/A');
            wrapper.Status = prop.pcrm__Status__c;
            wrapper.Bedrooms = prop.pcrm__Bedrooms__c != null ? String.valueOf(prop.pcrm__Bedrooms__c) : '';
            wrapper.Bathrooms = prop.pcrm__Bathrooms__c != null ? String.valueOf(prop.pcrm__Bathrooms__c) : '';
            wrapper.Area = prop.pcrm__Size__c != null ? String.valueOf(prop.pcrm__Size__c) : '';
            
            // Generate price based on available data
            if (prop.pcrm__Sale_Price_min__c != null && prop.pcrm__Sale_Price_max__c != null) {
                wrapper.Price = 'AED ' + formatNumber(prop.pcrm__Sale_Price_min__c) + ' - ' + formatNumber(prop.pcrm__Sale_Price_max__c);
            } else if (prop.pcrm__Rent_Price_min__c != null && prop.pcrm__Rent_Price_max__c != null) {
                wrapper.Price = 'AED ' + formatNumber(prop.pcrm__Rent_Price_min__c) + ' - ' + formatNumber(prop.pcrm__Rent_Price_max__c) + ' (Rent)';
            } else {
                wrapper.Price = 'Price on Application';
            }
            
            wrapper.TitleEnglish = prop.pcrm__Title_English__c != null ? prop.pcrm__Title_English__c : prop.Name;
            wrapper.Description = prop.pcrm__Description_English__c != null ? prop.pcrm__Description_English__c : 
                                generateDescription(prop.Name, prop.pcrm__Property_Type__c);
            
            return wrapper;
            
        } catch (Exception e) {
            System.debug('Error fetching property details: ' + e.getMessage());
            return null;
        }
    }
    
    // Helper method to format numbers with commas
    private static String formatNumber(Decimal num) {
        if (num == null) return '0';
        return num.format();
    }
    
    // Helper method to generate description if none exists
    private static String generateDescription(String propertyName, String propertyType) {
        if (propertyType == null) {
            return 'Modern property with contemporary amenities and excellent location.';
        }
        
        if (propertyType == 'AP') {
            return 'Modern apartment with contemporary amenities, stunning views, and excellent location.';
        } else if (propertyType == 'VH') {
            return 'Exclusive villa with private pool, garden, and premium finishes in prestigious location.';
        } else if (propertyType == 'BU') {
            return 'Spacious bungalow with modern design, private garden, and family-friendly layout.';
        } else if (propertyType == 'BW') {
            return 'Modern warehouse facility with excellent logistics access and ample storage space.';
        } else {
        return 'Modern property with contemporary amenities and excellent location.';
    }
    }
    
    public class PropertyWrapper {
        @AuraEnabled public String Id;
        @AuraEnabled public String Name;
        @AuraEnabled public String PropertyType;
        @AuraEnabled public String Location;
        @AuraEnabled public String Status;
        @AuraEnabled public String Price;
        @AuraEnabled public String Bedrooms;
        @AuraEnabled public String Bathrooms;
        @AuraEnabled public String Area;
        @AuraEnabled public String TitleEnglish;
        @AuraEnabled public String Description;
    }
} 