// app/modules/loyalty/static/merchant/js/loyalty-settings.js
// noqa: js-006 - async init pattern is safe, loadData has try/catch

const loyaltySettingsLog = window.LogConfig.loggers.loyaltySettings || window.LogConfig.createLogger('loyaltySettings');

function merchantLoyaltySettings() {
    return {
        ...data(),
        ...createProgramFormMixin(),
        currentPage: 'program',

        loading: false,
        error: null,

        async init() {
            loyaltySettingsLog.info('=== MERCHANT LOYALTY SETTINGS PAGE INITIALIZING ===');
            if (window._merchantLoyaltySettingsInitialized) return;
            window._merchantLoyaltySettingsInitialized = true;

            // Load sidebar menu (from base data())
            this.loadMenuConfig();

            await this.loadSettings();
            loyaltySettingsLog.info('=== MERCHANT LOYALTY SETTINGS PAGE INITIALIZATION COMPLETE ===');
        },

        async loadSettings() {
            this.loading = true;
            this.error = null;

            try {
                const response = await apiClient.get('/merchants/loyalty/program');
                if (response) {
                    this.populateSettings(response);
                    this.isNewProgram = false;
                    loyaltySettingsLog.info('Settings loaded');
                }
            } catch (error) {
                if (error.status === 404) {
                    loyaltySettingsLog.info('No program found, creating new');
                    this.isNewProgram = true;
                } else {
                    throw error;
                }
            } finally {
                this.loading = false;
            }
        },

        async saveSettings() {
            this.saving = true;

            try {
                const payload = this.buildPayload();
                if (!payload) { this.saving = false; return; }

                if (this.isNewProgram) {
                    await apiClient.post('/merchants/loyalty/program', payload);
                    this.isNewProgram = false;
                } else {
                    await apiClient.patch('/merchants/loyalty/program', payload);
                }

                Utils.showToast(I18n.t('loyalty.toasts.settings_saved'), 'success');
                loyaltySettingsLog.info('Settings saved');
            } catch (error) {
                Utils.showToast(I18n.t('loyalty.toasts.save_failed', {message: error.message}), 'error');
                loyaltySettingsLog.error('Save failed:', error);
            } finally {
                this.saving = false;
            }
        },

        async deleteProgram() {
            this.deleting = true;

            try {
                await apiClient.delete('/merchants/loyalty/program');
                Utils.showToast(I18n.t('loyalty.toasts.program_deleted'), 'success');
                loyaltySettingsLog.info('Program deleted');
                window.location.href = '/merchants/loyalty/program';
            } catch (error) {
                Utils.showToast(I18n.t('loyalty.toasts.delete_failed', {message: error.message}), 'error');
                loyaltySettingsLog.error('Delete failed:', error);
            } finally {
                this.deleting = false;
                this.showDeleteModal = false;
            }
        },
    };
}

if (!window.LogConfig.loggers.loyaltySettings) {
    window.LogConfig.loggers.loyaltySettings = window.LogConfig.createLogger('loyaltySettings');
}
loyaltySettingsLog.info('Merchant loyalty settings module loaded');
