/***********************************************************************************************************
 * Name         : ExportTrackerSnowflakeQueueableTest
 * Purpose      : Test class for ExportTrackerSnowflakeQueueable
 **********************************************************************************************************/
@isTest
private class ExportTrackerSnowflakeQueueableTest {
    /************************************************************************************
     * Method: execute
     * APEX method tested : ExportTrackerSnowflakeQueueable.execute
     * Description : Verifies that the CSV File is properly created and the Export record
     *  is updated
     *************************************************************************************/
    @isTest
    static void execute() {
        final String FORM_NAME = 'Test Form';
        Artist__c artist = TestFactory.createArtist();
        Form__c form = TestFactory.getForm(TestFactory.createTerritoryLabelArtist().Id);
        form.Form_Name__c = FORM_NAME;
        insert form;

        Export__c exportRecord = new Export__c(
            Export_Type__c = 'Form Response',
            Artist_Id__c = artist.Id,
            Form_Name__c = true,
            Form_ID__c = form.Id
        );
        insert exportRecord;
        List<String> fields = ExportTrackerSnowflakeQueueable.EXPORT_FIELDS;
        List<Map<String, String>> listMapFieldValue = new List<Map<String, String>>();
        for (Integer i = 1; i < 4; i++) {
            Map<String, String> mapFieldValue = new Map<String, String>();
            for (String field : fields) {
                if (field.equals('Birthday_c')) {
                    mapFieldValue.put(field.toUpperCase(), String.valueOf(Date.newInstance(2000, i, i)));
                } else {
                    mapFieldValue.put(field.toUpperCase(), field + ' ' + i);
                }
            }
            listMapFieldValue.add(mapFieldValue);
        }
        String jsonResult = JSON.serialize(new Map<String, Object>{ 'rows' => listMapFieldValue });

        Test.startTest();
        ExportTrackerSnowflakeQueueable instance = new ExportTrackerSnowflakeQueueable(jsonResult, exportRecord.Id);
        System.enqueueJob(instance);
        Test.stopTest();

        Assert.areEqual(
            3,
            [SELECT Exported_Record_Count__c FROM Export__c WHERE ID = :exportRecord.Id].Exported_Record_Count__c
        );

        List<Attachment> atts = [SELECT Body FROM Attachment];
        Assert.areEqual(1, atts.size());
        List<String> csvLines = atts[0].Body.toString().split('\n');
        Assert.areEqual(4, csvLines.size());
        Assert.areEqual(instance.getHeader(fields), csvLines[0]);

        for (Integer i = 1; i < 4; i++) {
            List<String> row = csvLines[i].split(',');
            for (Integer j = 0; j < fields.size(); j++) {
                if (fields[j] == 'Form_Name_c') {
                    Assert.areEqual('Test Form', row[j]);
                } else if (fields[j] != 'Birthday_c') {
                    Assert.areEqual(fields[j] + ' ' + i, row[j]);
                } else {
                    Assert.areEqual(
                        Datetime.newInstance(Date.newInstance(2000, i, i), Time.newInstance(0, 0, 0, 0))
                            .format('MM/dd/yyyy'),
                        row[j]
                    );
                }
            }
        }
    }
}
