/***********************************************************************************************************
 * Name         : LeadDupsFinderHelper
 * Purpose      : Helper methods for leadDupsFinder LWC
 **********************************************************************************************************/
public with sharing class LeadDupsFinderHelper {
    /**
     * @description Looks for existing records having the same
     *   - First Name + Last Name OR
     *   - Email address OR
     *   - Spotify URL
     * @param recordId   Id of the record identified by the LWC component
     * @param firstName  Lead's first name
     * @param lastName   Lead's last name
     * @param email      Lead's email address
     * @param spotifyUrl Lead's Spotify URL
     * @return DupsWrapper   wrapper instance
     */
    @AuraEnabled
    public static DupsWrapper getDups(
        String recordId,
        String firstName,
        String lastName,
        String email,
        String spotifyUrl
    ) {
        DupsWrapper dupsWrapperInstance = new DupsWrapper(0, 0, 0);

        String query = 'SELECT FirstName, LastName, Email, SpotifyUrl__c FROM Lead WHERE Id != :recordId AND (';

        Boolean hasCriteria = false;
        if (String.isNotBlank(firstName) && String.isNotBlank(lastName)) {
            query += '(FirstName = :firstName AND LastName = :lastName)';
            hasCriteria = true;
        }
        if (String.isNotBlank(email)) {
            query += hasCriteria ? ' OR Email = :email' : 'Email = :email';
            hasCriteria = true;
        }
        if (String.isNotBlank(spotifyUrl)) {
            query += hasCriteria ? ' OR SpotifyUrl__c = :spotifyUrl' : 'SpotifyUrl__c = :spotifyUrl';
        }

        if (hasCriteria) {
            query += ')';
            for (Lead dupLead : Database.query(query)) {
                if (
                    String.isNotBlank(firstName) &&
                    String.isNotBlank(lastName) &&
                    dupLead.FirstName == firstName &&
                    dupLead.LastName == lastName
                ) {
                    dupsWrapperInstance.dupsByName++;
                }
                if (String.isNotBlank(email) && dupLead.Email == email) {
                    dupsWrapperInstance.dupsByEmail++;
                }
                if (String.isNotBlank(spotifyUrl) && dupLead.SpotifyUrl__c == spotifyUrl) {
                    dupsWrapperInstance.dupsBySpotifyUrl++;
                }
            }
        }
        return dupsWrapperInstance;
    }

    public class DupsWrapper {
        @AuraEnabled
        public Integer dupsByEmail;
        @AuraEnabled
        public Integer dupsBySpotifyUrl;
        @AuraEnabled
        public Integer dupsByName;

        public DupsWrapper(Integer dupsByEmail, Integer dupsBySpotifyUrl, Integer dupsByName) {
            this.dupsByEmail = dupsByEmail;
            this.dupsBySpotifyUrl = dupsBySpotifyUrl;
            this.dupsByName = dupsByName;
        }
    }
}
