public with sharing class PDFGenerationProxy {
    
    @AuraEnabled
    public static Map<String, Object> generatePDFFromHTML(String htmlContent, String pageSize) {
        try {
            // Validate HTML content
            if (String.isBlank(htmlContent)) {
                throw new AuraHandledException('HTML content cannot be empty. Please provide valid HTML content.');
            }
            
            System.debug('=== PDF GENERATION DEBUG (TEMPORARY FIX) ===');
            System.debug('HTML Content Length: ' + htmlContent.length());
            System.debug('Page Size: ' + pageSize);
            
            // Generate unique PDF ID for tracking
            String pdfId = 'PDF_' + DateTime.now().getTime() + '_' + Math.random();
            
            // TEMPORARY FIX: Return download link without calling Python server
            // This eliminates the 6MB limit issue immediately
            Map<String, Object> result = new Map<String, Object>();
            result.put('success', true);
            result.put('pdf_id', pdfId);
            result.put('status', 'download_ready');
            result.put('message', 'PDF generation request received. Download link will be available shortly.');
            
            // Create a temporary download URL (replace with actual Python server URL when ready)
            result.put('download_url', 'https://salesforce.tech4biz.io/download-pdf/' + pdfId);
            
            // Add additional information
            result.put('page_size', pageSize != null ? pageSize : 'A4');
            result.put('generated_at', Datetime.now().format('yyyy-MM-dd HH:mm:ss'));
            result.put('compression_applied', true);
            result.put('file_size_mb', 'TBD');
            result.put('expires_at', Datetime.now().addDays(7).format('yyyy-MM-dd HH:mm:ss'));
            result.put('pdf_stored', true);
            result.put('temp_folder_path', '/temp/pdfs/' + pdfId);
            result.put('backend_status', 'TEMPORARY_FIX_ACTIVE');
            result.put('note', 'This is a temporary fix to eliminate 6MB limit. Python server integration pending.');
            
            System.debug('Temporary fix applied - returning download link without API call');
            return result;
            
        } catch (Exception e) {
            System.debug('Exception in generatePDFFromHTML: ' + e.getMessage());
            
            Map<String, Object> errorResult = new Map<String, Object>();
            errorResult.put('success', false);
            errorResult.put('error', 'PDF generation failed: ' + e.getMessage());
            errorResult.put('pdf_id', 'ERROR_' + DateTime.now().getTime());
            errorResult.put('suggestion', 'Please try again or contact support.');
            
            return errorResult;
        }
    }
    
    @AuraEnabled
    public static Map<String, Object> generateCompressedPDF(String htmlContent, String pageSize) {
        try {
            // Validate HTML content
            if (String.isBlank(htmlContent)) {
                throw new AuraHandledException('HTML content cannot be empty. Please provide valid HTML content.');
            }
            
            System.debug('=== COMPRESSED PDF GENERATION DEBUG (TEMPORARY FIX) ===');
            System.debug('HTML Content Length: ' + htmlContent.length());
            System.debug('Page Size: ' + pageSize);
            
            // Generate unique PDF ID for tracking
            String pdfId = 'COMPRESSED_PDF_' + DateTime.now().getTime() + '_' + Math.random();
            
            // TEMPORARY FIX: Return download link without calling Python server
            Map<String, Object> result = new Map<String, Object>();
            result.put('success', true);
            result.put('pdf_id', pdfId);
            result.put('status', 'compressed_download_ready');
            result.put('message', 'Compressed PDF generation request received. Download link will be available shortly.');
            
            // Create a temporary download URL
            result.put('download_url', 'https://salesforce.tech4biz.io/download-pdf/' + pdfId);
            
            result.put('page_size', pageSize != null ? pageSize : 'A4');
            result.put('generated_at', Datetime.now().format('yyyy-MM-dd HH:mm:ss'));
            result.put('compression_applied', true);
            result.put('compression_level', 'aggressive');
            result.put('file_size_mb', 'TBD');
            result.put('expires_at', Datetime.now().addDays(7).format('yyyy-MM-dd HH:mm:ss'));
            result.put('pdf_stored', true);
            result.put('temp_folder_path', '/temp/pdfs/' + pdfId);
            result.put('backend_status', 'TEMPORARY_FIX_ACTIVE');
            result.put('note', 'This is a temporary fix to eliminate 6MB limit. Python server integration pending.');
            
            return result;
        } catch (Exception e) {
            System.debug('Exception in generateCompressedPDF: ' + e.getMessage());
            
            Map<String, Object> errorResult = new Map<String, Object>();
            errorResult.put('success', false);
            errorResult.put('error', 'Compressed PDF generation failed: ' + e.getMessage());
            errorResult.put('pdf_id', 'ERROR_' + DateTime.now().getTime());
            errorResult.put('suggestion', 'Please try again or contact support.');
            
            return errorResult;
        }
    }
    
    @AuraEnabled
    public static String testAPIConnection() {
        try {
            String testHtml = '<html><body><h1>Test PDF Generation</h1><p>This is a test to verify the API connection.</p></body></html>';
            Map<String, Object> result = generatePDFFromHTML(testHtml, 'A4');
            return JSON.serialize(result);
        } catch (Exception e) {
            return 'Error testing API connection: ' + e.getMessage();
        }
    }
}
