public with sharing class TestNewFlow {
    
    @AuraEnabled
    public static String testNewPDFFlow() {
        try {
            // Test with minimal HTML to verify the new flow
            String testHtml = '<html><body><h1>Test PDF</h1><p>Testing new flow with PDF generation first, then download link.</p></body></html>';
            
            Http http = new Http();
            HttpRequest request = new HttpRequest();
            request.setEndpoint('https://salesforce.tech4biz.io/generate-pdf');
            request.setMethod('POST');
            request.setHeader('Content-Type', 'application/json');
            request.setHeader('Accept', 'application/json');
            
            Map<String, Object> requestBody = new Map<String, Object>();
            requestBody.put('html_content', testHtml);
            requestBody.put('pageSize', 'A4');
            requestBody.put('filename', 'test_new_flow.pdf');
            requestBody.put('return_download_link', true);
            requestBody.put('store_on_server', true);
            requestBody.put('pdf_id', 'TEST_NEW_FLOW_' + DateTime.now().getTime());
            
            String jsonBody = JSON.serialize(requestBody);
            request.setBody(jsonBody);
            request.setTimeout(60000); // 1 minute timeout for PDF generation
            
            System.debug('=== TESTING NEW FLOW ===');
            System.debug('Request Body: ' + jsonBody);
            
            HttpResponse response = http.send(request);
            
            System.debug('Response Status: ' + response.getStatusCode());
            System.debug('Response Body Length: ' + response.getBody().length());
            System.debug('Response Body: ' + response.getBody());
            
            if (response.getStatusCode() == 200) {
                String responseBody = response.getBody();
                Map<String, Object> responseMap = (Map<String, Object>) JSON.deserializeUntyped(responseBody);
                
                // Check if the response contains the expected fields
                Boolean hasDownloadUrl = responseMap.containsKey('download_url') || responseMap.containsKey('downloadUrl');
                Boolean hasPdfId = responseMap.containsKey('pdf_id');
                Boolean hasSuccess = responseMap.containsKey('success');
                
                String result = 'SUCCESS: New flow working correctly!\n';
                result += 'Response Size: ' + response.getBody().length() + ' bytes\n';
                result += 'Has Download URL: ' + hasDownloadUrl + '\n';
                result += 'Has PDF ID: ' + hasPdfId + '\n';
                result += 'Has Success Flag: ' + hasSuccess + '\n';
                result += 'Full Response: ' + responseBody;
                
                return result;
            } else {
                return 'ERROR: HTTP ' + response.getStatusCode() + ' - ' + response.getBody();
            }
            
        } catch (Exception e) {
            return 'ERROR: ' + e.getMessage() + '\nStack: ' + e.getStackTraceString();
        }
    }
}
