/***********************************************************************************************************
 * Name         : CA_DeleteLeadHelper
 * Purpose      : Handles Lead deletion in system mode: AuraEnabled entry point for the quick action.
 **********************************************************************************************************/
public without sharing class CA_DeleteLeadHelper {
    /**
     * @description Deletes the given Lead by Id running without sharing.
     * @param leadId Id of the Lead record to delete.
     * @return ApexToLwcResponseWrapper with the deleted Lead Id as data on success, null data if the Lead
     *  was not found or not accessible, or error message on failure.
     */
    @AuraEnabled
    public static ApexToLwcResponseWrapper deleteLead(Id leadId) {
        ApexToLwcResponseWrapper result = new ApexToLwcResponseWrapper(null, null);
        try {
            List<Lead> leads = new WithSharingQuery().getLeadById(leadId);
            if (!leads.isEmpty()) {
                delete leads[0];
                result.data = leadId;
            }
        } catch (Exception ex) {
            ErrorLogger.logError(ex);
            result.error = ex.getMessage();
        }
        return result;
    }

    /**
     * @description Queries Lead records in user context to enforce record-level access.
     */
    private with sharing class WithSharingQuery {
        /**
         * @description Returns the Lead with the given Id if the running user has access to it.
         * @param leadId Id of the Lead to query.
         * @return List containing the Lead, or an empty list if not found or not accessible.
         */
        private List<Lead> getLeadById(Id leadId) {
            return [SELECT Id FROM Lead WHERE Id = :leadId LIMIT 1];
        }
    }
}
