/***********************************************************************************************************
 * Name         : SubUploadEditHelper
 * Purpose      : Apex methods for subUploadEdit lwc
 **********************************************************************************************************/
public with sharing class SubUploadEditHelper {
    /**
     * @description Updates or inserts the subUpload record
     * @param  String   subUploadId
     * @param  String   mailing list Id
     * @param  String   tla Id
     * @param  String   dsp value
     * @param  String   is crm generated value
     * @param  String   fileSourceDesc value
     * @param  String   uploadName value
     * @return  Id  subUpload Id
     */
    @AuraEnabled
    public static Id upsertSubUpload(
        String subUploadId,
        String mlId,
        String tlaId,
        String dsp,
        String crm,
        String fileSourceDesc,
        String uploadName,
        Boolean overwriteFanData,
        String acquisitionChannel
    ) {
        try {
            TLA__c tla = [
                SELECT Id, Artist_ID__c, TL_ID__r.Territory_ID__c, TL_ID__r.Label_ID__c
                FROM TLA__c
                WHERE Id = :tlaId
            ];
            Sub_Upload_Util__c subUpload = new Sub_Upload_Util__c(
                Id = subUploadId,
                Name = uploadName,
                Territory__c = tla.TL_ID__r.Territory_ID__c,
                Label__c = tla.TL_ID__r.Label_ID__c,
                Artist__c = tla.Artist_ID__c,
                Mailing_List__c = mlId,
                File_Source_Description__c = fileSourceDesc,
                DSP__c = dsp,
                Is_Crm_Generated__c = crm,
                OverwriteFanData__c = overwriteFanData,
                AcquisitionChannel__c = acquisitionChannel
            );

            upsert subUpload;
            return subUpload.Id;
        } catch (Exception ex) {
            SMELogger.logError(ex);
            throw new AuraHandledException(ex.getMessage());
        }
    }

    /**
     * @description Retrieves all the active Mailing Lists and their related data
     * @return  List of OptionWrapper 
     */
    @AuraEnabled(cacheable=true)
    public static List<OptionWrapper> getData() {
        try {
            List<OptionWrapper> options = new List<OptionWrapper>();
            for (Mailing_List__c ml : [
                SELECT
                    Id,
                    TLA_ID__c,
                    TLA_ID__r.TL_ID__r.Territory_ID__r.Territory__c,
                    TLA_ID__r.TL_ID__r.Label_ID__r.Name,
                    TLA_ID__r.Artist_ID__r.Artist__c,
                    Mailing_List_Name__c
                FROM Mailing_List__c
                WHERE
                    Active__c = TRUE
                    AND TLA_ID__r.Active__c = TRUE
                    AND TLA_ID__r.TL_ID__r.Territory_ID__r.Territory__c != NULL
                    AND TLA_ID__r.Artist_ID__r.Artist__c != NULL
                ORDER BY
                    TLA_ID__r.TL_ID__r.Territory_ID__r.Territory__c,
                    TLA_ID__r.TL_ID__r.Label_ID__r.Name,
                    TLA_ID__r.Artist_ID__r.Artist__c
            ]) {
                options.add(
                    new OptionWrapper(
                        ml.Id,
                        ml.TLA_ID__c,
                        ml.Mailing_List_Name__c,
                        ml.TLA_ID__r.TL_ID__r.Territory_ID__r.Territory__c,
                        ml.TLA_ID__r.TL_ID__r.Label_ID__r.Name,
                        ml.TLA_ID__r.Artist_ID__r.Artist__c
                    )
                );
            }
            return options;
        } catch (Exception ex) {
            SMELogger.logError(ex);
            throw new AuraHandledException(ex.getMessage());
        }
    }

    public class OptionWrapper {
        @AuraEnabled
        public String mlId;
        @AuraEnabled
        public String tlaId;
        @AuraEnabled
        public String mlName;
        @AuraEnabled
        public String territory;
        @AuraEnabled
        public String label;
        @AuraEnabled
        public String artist;

        public OptionWrapper(String mlId, String tlaId, String mlName, String territory, String label, String artist) {
            this.mlId = mlId;
            this.tlaId = tlaId;
            this.mlName = mlName;
            this.territory = territory;
            this.label = label;
            this.artist = artist;
        }
    }
}