public with sharing class PdfProxyPageController {
    
    public String result { get; set; }
    
    public void init() {
        // Initialize the page
        result = 'PDF Proxy Ready';
    }
    
    @RemoteAction
    public static Map<String, Object> makeHttpCall(String endpoint, String dataJson) {
        try {
            System.debug('=== HTTP PROXY CALL ===');
            System.debug('Endpoint: ' + endpoint);
            System.debug('Data JSON: ' + dataJson);
            
            // Parse the data
            Map<String, Object> data = new Map<String, Object>();
            if (String.isNotBlank(dataJson)) {
                try {
                    data = (Map<String, Object>) JSON.deserializeUntyped(dataJson);
                } catch (Exception e) {
                    System.debug('Error parsing JSON: ' + e.getMessage());
                }
            }
            
            // Make the HTTP callout
            Http http = new Http();
            HttpRequest request = new HttpRequest();
            request.setEndpoint(endpoint);
            request.setMethod('POST');
            request.setHeader('Content-Type', 'application/json');
            request.setBody(JSON.serialize(data));
            request.setTimeout(120000); // 2 minutes
            
            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 HTTP proxy: ' + 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;
        }
    }
} 