/***********************************************************************************************************
 * Name         : LoadCCPAListTriggerHelper
 * Purpose      : Implementation for methods called by LoadCCPAListTriggerHandler
 **********************************************************************************************************/
public with sharing class LoadCCPAListTriggerHelper {
    /**
     * @description Retrieves the fans associated with the emails in the CCPA records
     * @param  ccpaRecords CCPA records
     * @return Map<String, Fan__c>  Email - Fan map
     */
    public static Map<String, Fan__c> getFans(List<Load_CCPA_List__c> ccpaRecords) {
        Map<String, Fan__c> emailFanMap = new Map<String, Fan__c>();
        List<String> emails = new List<String>();
        for (Load_CCPA_List__c ccpaRecord : ccpaRecords) {
            emails.add(ccpaRecord.Email__c);
        }
        List<Fan__c> fans = [SELECT Id, Email__c, CCPA_Do_Not_Sell__c FROM Fan__c WHERE Email__c IN :emails];
        for (Fan__c fan : fans) {
            emailFanMap.put(fan.Email__c, fan);
        }
        return emailFanMap;
    }

    /**
     * @description Updates Fan CCPA_Do_Not_Sell__c field
     * @param emailFanMap Map<String, Fan__c>  Email - Fan map
     */
    public static void updateFans(Map<String, Fan__c> emailFanMap) {
        for (Fan__c fan : emailFanMap.values()) {
            fan.CCPA_Do_Not_Sell__c = true;
        }

        TriggerControl.disableTrigger(FanTriggerHandler.SELF);
        update emailFanMap.values();
        TriggerControl.enableTrigger(FanTriggerHandler.SELF);
    }

    /**
     * @description Populates Fan fields on CCPA records
     * @param ccpaRecords   CCPA records to update
     * @param emailFanMap   Map<String, Fan__c>  Email - Fan map
     */
    public static void populateCCPAFields(List<Load_CCPA_List__c> ccpaRecords, Map<String, Fan__c> emailFanMap) {
        for (Load_CCPA_List__c ccpaRecord : ccpaRecords) {
            if (emailFanMap.containsKey(ccpaRecord.Email__c)) {
                ccpaRecord.Fan_Matched__c = true;
                ccpaRecord.Fan_Id__c = emailFanMap.get(ccpaRecord.Email__c).Id;
            }
        }
    }
}
