/***********************************************************************************************************
 * Name         : MarketingCloudHelper
 * Purpose      : Helper Class to make auth callouts to MC
 **********************************************************************************************************/
public with sharing class MarketingCloudHelper {
    /**
     * @description makes an API call to get the accessToken from marketing cloud
     * @param void
     * @return String (accessToken)
     */
    private static MarketingCloudAuthResponse getToken(String body) {
        HttpRequest requestToken = new HttpRequest();
        HttpResponse responseToken = new HttpResponse();
        Http http = new Http();
        requestToken.setEndpoint(ConfigUtils.getConfigString(ConfigUtils.MC_ENDPOINT_AUTH));
        requestToken.setMethod('POST');
        requestToken.setBody(body);
        requestToken.setHeader('Content-Type', 'application/json');

        responseToken = http.send(requestToken);

        MarketingCloudAuthResponse mcAuth = (MarketingCloudAuthResponse) JSON.deserialize(
            responseToken.getBody(),
            MarketingCloudAuthResponse.class
        );
        return mcAuth;
    }

    public static MarketingCloudAuthResponse getTokenForWelcomeEmail(String mcMID) {
        String bodyWelcomeEmail =
            '{"grant_type":"client_credentials","client_id":"' +
            ConfigUtils.getConfigString(ConfigUtils.MC_WELCOME_EMAIL_CLIENT_ID) +
            '","client_secret":"' +
            ConfigUtils.getConfigString(ConfigUtils.MC_WELCOME_EMAIL_CLIENT_SECRET) +
            '","account_id":"' +
            mcMID +
            '"}';
        return getToken(bodyWelcomeEmail);
    }

    public static MarketingCloudAuthResponseWrapper getTokenForContactsDelete(
        MarketingCloudAuthResponseWrapper authResponseWrapper
    ) {
        if (
            authResponseWrapper == null ||
            authResponseWrapper.tokenExpiration <= System.now().addSeconds(30).getTime()
        ) {
            String bodyContactDelete =
                '{"client_id":"' +
                ConfigUtils.getConfigString(ConfigUtils.MC_ALL_CONTACTS_CLIENT_ID) +
                '","client_secret":"' +
                ConfigUtils.getConfigString(ConfigUtils.MC_ALL_CONTACTS_CLIENT_SECRET) +
                '","grant_type":"client_credentials"}';
            MarketingCloudAuthResponse authResponse = getToken(bodyContactDelete);
            authResponseWrapper = new MarketingCloudAuthResponseWrapper(
                authResponse,
                System.now().addSeconds(authResponse.expires_in).getTime()
            );
        }
        return authResponseWrapper;
    }

    public class MarketingCloudAuthResponse {
        public String access_token;
        public Integer expires_in;
    }

    public class MarketingCloudAuthResponseWrapper {
        public MarketingCloudAuthResponse authResponse;
        public Long tokenExpiration;

        public MarketingCloudAuthResponseWrapper(MarketingCloudAuthResponse authResponse, Long tokenExpiration) {
            this.authResponse = authResponse;
            this.tokenExpiration = tokenExpiration;
        }
    }
}
