@IsTest
private class AppraiserCasePayloadBuilderTest {
    
    @TestSetup
    static void setupTestData() {
        // Create test Appraiser Case
        Appraiser_Case__c testCase = new Appraiser_Case__c(
            Appraiser_Field_Review_Date__c = Date.parse('04/02/2026'),
            Property_Address__c = '123 Main St, Denver, CO 80202'
        );
        insert testCase;
        
        // Create test deficiency records
        List<Appraiser_Case_Deficiency__c> testDefs = new List<Appraiser_Case_Deficiency__c>();
        testDefs.add(new Appraiser_Case_Deficiency__c(
            Appraiser_Case__c = testCase.Id,
            Deficiency_Number__c = 1,
            Description__c = 'Missing comparable sale adjustment detail.',
            Resolution__c = 'Added adjustment rationale and supporting calculations.'
        ));
        testDefs.add(new Appraiser_Case_Deficiency__c(
            Appraiser_Case__c = testCase.Id,
            Deficiency_Number__c = 2,
            Description__c = 'Neighborhood trend explanation insufficient.',
            Resolution__c = 'Expanded market trend narrative with MLS evidence.'
        ));
        insert testDefs;
    }
    
    @IsTest
    static void testBuildPayload() {
        Appraiser_Case__c testCase = [SELECT Id FROM Appraiser_Case__c LIMIT 1];
        
        Map<String, Object> payload = AppraiserCasePayloadBuilder.buildPayload(testCase.Id);
        
        Assert.isNotNull(payload, 'Payload should not be null');
        Assert.isTrue(payload.containsKey('AppraiserCaseNumber'), 'Payload should contain AppraiserCaseNumber');
        Assert.isTrue(payload.containsKey('AppraiserFieldReviewDate'), 'Payload should contain AppraiserFieldReviewDate');
        Assert.isTrue(payload.containsKey('PropertyAddress'), 'Payload should contain PropertyAddress');
        Assert.isTrue(payload.containsKey('DeficiencyList'), 'Payload should contain DeficiencyList');
        
        List<Object> deficiencyList = (List<Object>) payload.get('DeficiencyList');
        Assert.areEqual(2, deficiencyList.size(), 'DeficiencyList should contain 2 items');
    }
    
    @IsTest
    static void testBuildPayloadJson() {
        Appraiser_Case__c testCase = [SELECT Id FROM Appraiser_Case__c LIMIT 1];
        
        String jsonPayload = AppraiserCasePayloadBuilder.buildPayloadJson(testCase.Id);
        
        Assert.isNotNull(jsonPayload, 'JSON payload should not be null');
        Assert.isTrue(jsonPayload.contains('AppraiserCaseNumber'), 'JSON should contain AppraiserCaseNumber');
        Assert.isTrue(jsonPayload.contains('DeficiencyList'), 'JSON should contain DeficiencyList');
    }
    
    @IsTest
    static void testPayloadWithNullDate() {
        // Create case without review date
        Appraiser_Case__c testCase = new Appraiser_Case__c(
            Property_Address__c = '456 Oak Ave, Boulder, CO 80301'
        );
        insert testCase;
        
        Map<String, Object> payload = AppraiserCasePayloadBuilder.buildPayload(testCase.Id);
        
        Assert.isNotNull(payload, 'Payload should not be null even with null date');
        Assert.isNull(payload.get('AppraiserFieldReviewDate'), 'Null date should map to null in payload');
    }
    
    @IsTest
    static void testInvalidCaseId() {
        try {
            AppraiserCasePayloadBuilder.buildPayload('a0wKW000000000000');
            Assert.fail('Should have thrown exception for invalid case id');
        } catch (IllegalArgumentException ex) {
            Assert.isTrue(ex.getMessage().contains('Appraiser Case not found'));
        }
    }
}
