/**
 * @description Tests for DocusignEnvelopeRequestHandler
 * @author Paul Huliganga
 * @date 2026-02-25
 */
@isTest
public class DocusignEnvelopeRequestHandlerTest {
    
    @isTest
    static void testValidateRequest_Success() {
        // Setup
        DocusignEnvelopeRequest req = new DocusignEnvelopeRequest();
        req.templateIds = new List<String>{ 'template1', 'template2' };
        req.recordId = '001xx000003DHf';
        req.language = 'en';
        req.emailSubject = 'Test Subject';
        
        // Test
        Test.startTest();
        DocusignEnvelopeRequestHandler.validateRequest(req);
        Test.stopTest();
        
        // Verify - no exception thrown
        Assert.isTrue(true, 'Validation should pass');
    }
    
    @isTest
    static void testValidateRequest_NoTemplateIds() {
        // Setup
        DocusignEnvelopeRequest req = new DocusignEnvelopeRequest();
        req.templateIds = new List<String>();
        req.recordId = '001xx000003DHf';
        
        // Test & Verify
        Test.startTest();
        try {
            DocusignEnvelopeRequestHandler.validateRequest(req);
            Assert.fail('Should throw IllegalArgumentException');
        } catch (IllegalArgumentException e) {
            Assert.isTrue(e.getMessage().contains('At least one template ID'), 'Correct error message');
        }
        Test.stopTest();
    }
    
    @isTest
    static void testValidateRequest_TooManyTemplates() {
        // Setup
        DocusignEnvelopeRequest req = new DocusignEnvelopeRequest();
        req.templateIds = new List<String>();
        for (Integer i = 0; i < 15; i++) {
            req.templateIds.add('template' + i);
        }
        req.recordId = '001xx000003DHf';
        
        // Test & Verify
        Test.startTest();
        try {
            DocusignEnvelopeRequestHandler.validateRequest(req);
            Assert.fail('Should throw IllegalArgumentException');
        } catch (IllegalArgumentException e) {
            Assert.isTrue(e.getMessage().contains('Maximum 14 templates'), 'Correct error message');
        }
        Test.stopTest();
    }
    
    @isTest
    static void testValidateRequest_NoRecordId() {
        // Setup
        DocusignEnvelopeRequest req = new DocusignEnvelopeRequest();
        req.templateIds = new List<String>{ 'template1' };
        req.recordId = '';
        
        // Test & Verify
        Test.startTest();
        try {
            DocusignEnvelopeRequestHandler.validateRequest(req);
            Assert.fail('Should throw IllegalArgumentException');
        } catch (IllegalArgumentException e) {
            Assert.isTrue(e.getMessage().contains('Salesforce record ID'), 'Correct error message');
        }
        Test.stopTest();
    }
    
    @isTest
    static void testValidateRequest_BlankTemplateId() {
        // Setup
        DocusignEnvelopeRequest req = new DocusignEnvelopeRequest();
        req.templateIds = new List<String>{ 'template1', '', 'template3' };
        req.recordId = '001xx000003DHf';
        
        // Test & Verify
        Test.startTest();
        try {
            DocusignEnvelopeRequestHandler.validateRequest(req);
            Assert.fail('Should throw IllegalArgumentException');
        } catch (IllegalArgumentException e) {
            Assert.isTrue(e.getMessage().contains('Template ID cannot be blank'), 'Correct error message');
        }
        Test.stopTest();
    }
    
    @isTest
    static void testBuildEnvelopeJSON_Success() {
        // Setup
        DocusignEnvelopeRequest req = new DocusignEnvelopeRequest();
        req.templateIds = new List<String>{ 'template2', 'template1', 'template2' };  // duplicates and unsorted
        req.recordId = '001xx000003DHf';
        req.language = 'es';
        req.emailSubject = 'Custom Subject';
        
        // Test
        Test.startTest();
        String json = DocusignEnvelopeRequestHandler.buildEnvelopeJSON(req);
        Test.stopTest();
        
        // Verify
        Assert.isNotNull(json, 'JSON should be generated');
        Assert.isTrue(json.contains('sent'), 'Should contain sent status');
        Assert.isTrue(json.contains('Custom Subject'), 'Should contain custom subject');
        Assert.isTrue(json.contains('compositeTemplates'), 'Should contain compositeTemplates');
    }
    
    @isTest
    static void testBuildEnvelopeJSON_DefaultSubject() {
        // Setup
        DocusignEnvelopeRequest req = new DocusignEnvelopeRequest();
        req.templateIds = new List<String>{ 'template1' };
        req.recordId = '001xx000003DHf';
        req.emailSubject = null;
        
        // Test
        Test.startTest();
        String json = DocusignEnvelopeRequestHandler.buildEnvelopeJSON(req);
        Test.stopTest();
        
        // Verify
        Assert.isTrue(json.contains('Please review and sign these forms'), 'Should use default subject');
    }
    
    @isTest
    static void testBuildEnvelopeJSON_TemplatesAreSorted() {
        // Setup
        DocusignEnvelopeRequest req = new DocusignEnvelopeRequest();
        req.templateIds = new List<String>{ 'template3', 'template1', 'template2' };
        req.recordId = '001xx000003DHf';
        
        // Test
        Test.startTest();
        String json = DocusignEnvelopeRequestHandler.buildEnvelopeJSON(req);
        Test.stopTest();
        
        // Verify - templates should be in sorted order (template1, template2, template3)
        Integer idx1 = json.indexOf('template1');
        Integer idx2 = json.indexOf('template2');
        Integer idx3 = json.indexOf('template3');
        Assert.isTrue(idx1 < idx2 && idx2 < idx3, 'Templates should be sorted alphabetically');
    }
    
    @isTest
    static void testBuildEnvelopeJSON_DuplicatesRemoved() {
        // Setup
        DocusignEnvelopeRequest req = new DocusignEnvelopeRequest();
        req.templateIds = new List<String>{ 'template1', 'template1', 'template2' };
        req.recordId = '001xx000003DHf';
        
        // Test
        Test.startTest();
        String json = DocusignEnvelopeRequestHandler.buildEnvelopeJSON(req);
        Test.stopTest();
        
        // Verify - should only have 2 compositeTemplate entries
        Integer count = 0;
        Integer pos = 0;
        while ((pos = json.indexOf('compositeTemplateId', pos)) != -1) {
            count++;
            pos++;
        }
        Assert.areEqual(2, count, 'Should have 2 unique templates after deduplication');
    }
}
