public with sharing class UltraSimplePdfController {
    
    @AuraEnabled
    public static Map<String, Object> generatePreview(String template, String layout, String propertyName, String propertyType, String location, String price, String bedrooms, String bathrooms, String area, String description) {
        try {
            System.debug('=== ULTRA SIMPLE PREVIEW ===');
            System.debug('Template: ' + template);
            System.debug('Layout: ' + layout);
            System.debug('Property Name: ' + propertyName);
            
            // Make HTTP callout to Python API
            Http http = new Http();
            HttpRequest request = new HttpRequest();
            request.setEndpoint('http://160.187.166.67:8000/api/preview');
            request.setMethod('POST');
            request.setHeader('Content-Type', 'application/json');
            
            // Build request body manually to avoid JSON parsing issues
            String requestBody = '{' +
                '"template": "' + (template != null ? template : 'default') + '",' +
                '"layout": "' + (layout != null ? layout : 'standard') + '",' +
                '"propertyData": {' +
                    '"propertyName": "' + (propertyName != null ? propertyName : 'Sample Property') + '",' +
                    '"propertyType": "' + (propertyType != null ? propertyType : 'AP') + '",' +
                    '"location": "' + (location != null ? location : 'Dubai') + '",' +
                    '"price": "' + (price != null ? price : 'AED 2,500,000') + '",' +
                    '"bedrooms": "' + (bedrooms != null ? bedrooms : '2') + '",' +
                    '"bathrooms": "' + (bathrooms != null ? bathrooms : '2') + '",' +
                    '"area": "' + (area != null ? area : '1,200 sq ft') + '",' +
                    '"description": "' + (description != null ? description : 'Modern property with contemporary amenities') + '"' +
                '},' +
                '"customizationOptions": {' +
                    '"headerStyle": "modern",' +
                    '"colorScheme": "professional",' +
                    '"fontStyle": "clean"' +
                '},' +
                '"generatePreview": true' +
            '}';
            
            request.setBody(requestBody);
            request.setTimeout(120000); // 2 minutes
            
            System.debug('Request body: ' + requestBody);
            
            HttpResponse response = http.send(request);
            
            System.debug('Response Status: ' + response.getStatusCode());
            System.debug('Response Body: ' + response.getBody());
            
            Map<String, Object> result = new Map<String, Object>();
            
            if (response.getStatusCode() == 200) {
                try {
                    Map<String, Object> responseData = (Map<String, Object>) JSON.deserializeUntyped(response.getBody());
                    result.put('success', true);
                    result.put('data', responseData);
                } catch (Exception e) {
                    result.put('success', true);
                    result.put('data', response.getBody());
                }
            } else {
                result.put('success', false);
                result.put('error', 'HTTP ' + response.getStatusCode() + ': ' + response.getBody());
            }
            
            return result;
            
        } catch (Exception e) {
            System.debug('Error in UltraSimplePdfController: ' + e.getMessage());
            System.debug('Stack trace: ' + e.getStackTraceString());
            
            Map<String, Object> errorResult = new Map<String, Object>();
            errorResult.put('success', false);
            errorResult.put('error', 'Exception: ' + e.getMessage());
            return errorResult;
        }
    }
    
    @AuraEnabled
    public static Map<String, Object> generatePdf(String template, String layout, String propertyName, String propertyType, String location, String price, String bedrooms, String bathrooms, String area, String description) {
        try {
            System.debug('=== ULTRA SIMPLE PDF ===');
            System.debug('Template: ' + template);
            System.debug('Layout: ' + layout);
            System.debug('Property Name: ' + propertyName);
            
            // Make HTTP callout to Python API
            Http http = new Http();
            HttpRequest request = new HttpRequest();
            request.setEndpoint('http://160.187.166.67:8000/api/generate-pdf');
            request.setMethod('POST');
            request.setHeader('Content-Type', 'application/json');
            
            // Build request body manually to avoid JSON parsing issues
            String requestBody = '{' +
                '"template": "' + (template != null ? template : 'default') + '",' +
                '"layout": "' + (layout != null ? layout : 'standard') + '",' +
                '"propertyData": {' +
                    '"propertyName": "' + (propertyName != null ? propertyName : 'Sample Property') + '",' +
                    '"propertyType": "' + (propertyType != null ? propertyType : 'AP') + '",' +
                    '"location": "' + (location != null ? location : 'Dubai') + '",' +
                    '"price": "' + (price != null ? price : 'AED 2,500,000') + '",' +
                    '"bedrooms": "' + (bedrooms != null ? bedrooms : '2') + '",' +
                    '"bathrooms": "' + (bathrooms != null ? bathrooms : '2') + '",' +
                    '"area": "' + (area != null ? area : '1,200 sq ft') + '",' +
                    '"description": "' + (description != null ? description : 'Modern property with contemporary amenities') + '"' +
                '},' +
                '"customizationOptions": {' +
                    '"headerStyle": "modern",' +
                    '"colorScheme": "professional",' +
                    '"fontStyle": "clean"' +
                '},' +
                '"generatePDF": true' +
            '}';
            
            request.setBody(requestBody);
            request.setTimeout(120000); // 2 minutes
            
            System.debug('Request body: ' + requestBody);
            
            HttpResponse response = http.send(request);
            
            System.debug('Response Status: ' + response.getStatusCode());
            System.debug('Response Body: ' + response.getBody());
            
            Map<String, Object> result = new Map<String, Object>();
            
            if (response.getStatusCode() == 200) {
                try {
                    Map<String, Object> responseData = (Map<String, Object>) JSON.deserializeUntyped(response.getBody());
                    result.put('success', true);
                    result.put('data', responseData);
                } catch (Exception e) {
                    result.put('success', true);
                    result.put('data', response.getBody());
                }
            } else {
                result.put('success', false);
                result.put('error', 'HTTP ' + response.getStatusCode() + ': ' + response.getBody());
            }
            
            return result;
            
        } catch (Exception e) {
            System.debug('Error in UltraSimplePdfController: ' + e.getMessage());
            System.debug('Stack trace: ' + e.getStackTraceString());
            
            Map<String, Object> errorResult = new Map<String, Object>();
            errorResult.put('success', false);
            errorResult.put('error', 'Exception: ' + e.getMessage());
            return errorResult;
        }
    }
} 