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

// Use centralized logger
const loyaltyMerchantSettingsLog = window.LogConfig.loggers.loyaltyMerchantSettings || window.LogConfig.createLogger('loyaltyMerchantSettings');

// ============================================
// LOYALTY MERCHANT SETTINGS FUNCTION
// ============================================
function adminLoyaltyMerchantSettings() {
    return {
        // Inherit base layout functionality
        ...data(),

        // Page identifier for sidebar active state
        currentPage: 'loyalty-programs',

        // Merchant ID from URL
        merchantId: null,

        // Merchant data
        merchant: null,

        // Settings form data
        settings: {
            staff_pin_policy: 'optional',
            staff_pin_lockout_attempts: 5,
            staff_pin_lockout_minutes: 30,
            allow_self_enrollment: true,
            allow_void_transactions: true,
            allow_cross_location_redemption: true
        },

        // State
        loading: false,
        saving: false,
        error: null,

        // Back URL
        get backUrl() {
            return `/admin/loyalty/merchants/${this.merchantId}`;
        },

        // Initialize
        async init() {
            loyaltyMerchantSettingsLog.info('=== LOYALTY MERCHANT SETTINGS PAGE INITIALIZING ===');

            // Prevent multiple initializations
            if (window._loyaltyMerchantSettingsInitialized) {
                loyaltyMerchantSettingsLog.warn('Loyalty merchant settings page already initialized, skipping...');
                return;
            }
            window._loyaltyMerchantSettingsInitialized = true;

            // Extract merchant ID from URL
            const pathParts = window.location.pathname.split('/');
            const merchantsIndex = pathParts.indexOf('merchants');
            if (merchantsIndex !== -1 && pathParts[merchantsIndex + 1]) {
                this.merchantId = parseInt(pathParts[merchantsIndex + 1]);
            }

            if (!this.merchantId) {
                this.error = 'Invalid merchant ID';
                loyaltyMerchantSettingsLog.error('Could not extract merchant ID from URL');
                return;
            }

            loyaltyMerchantSettingsLog.info('Merchant ID:', this.merchantId);

            loyaltyMerchantSettingsLog.group('Loading merchant settings data');
            await this.loadData();
            loyaltyMerchantSettingsLog.groupEnd();

            loyaltyMerchantSettingsLog.info('=== LOYALTY MERCHANT SETTINGS PAGE INITIALIZATION COMPLETE ===');
        },

        // Load all data
        async loadData() {
            this.loading = true;
            this.error = null;

            try {
                // Load merchant info and settings in parallel
                await Promise.all([
                    this.loadMerchant(),
                    this.loadSettings()
                ]);
            } catch (error) {
                loyaltyMerchantSettingsLog.error('Failed to load data:', error);
                this.error = error.message || 'Failed to load settings';
            } finally {
                this.loading = false;
            }
        },

        // Load merchant basic info
        async loadMerchant() {
            try {
                loyaltyMerchantSettingsLog.info('Fetching merchant info...');

                const response = await apiClient.get(`/admin/merchants/${this.merchantId}`);

                if (response) {
                    this.merchant = response;
                    loyaltyMerchantSettingsLog.info('Merchant loaded:', this.merchant.name);
                }
            } catch (error) {
                loyaltyMerchantSettingsLog.error('Failed to load merchant:', error);
                throw error;
            }
        },

        // Load settings
        async loadSettings() {
            try {
                loyaltyMerchantSettingsLog.info('Fetching merchant loyalty settings...');

                const response = await apiClient.get(`/admin/loyalty/merchants/${this.merchantId}/settings`);

                if (response) {
                    // Merge with defaults to ensure all fields exist
                    this.settings = {
                        staff_pin_policy: response.staff_pin_policy || 'optional',
                        staff_pin_lockout_attempts: response.staff_pin_lockout_attempts || 5,
                        staff_pin_lockout_minutes: response.staff_pin_lockout_minutes || 30,
                        allow_self_enrollment: response.allow_self_enrollment !== false,
                        allow_void_transactions: response.allow_void_transactions !== false,
                        allow_cross_location_redemption: response.allow_cross_location_redemption !== false
                    };

                    loyaltyMerchantSettingsLog.info('Settings loaded:', this.settings);
                }
            } catch (error) {
                loyaltyMerchantSettingsLog.warn('Failed to load settings, using defaults:', error.message);
                // Keep default settings
            }
        },

        // Save settings
        async saveSettings() {
            this.saving = true;

            try {
                loyaltyMerchantSettingsLog.info('Saving merchant loyalty settings...');

                const response = await apiClient.patch(
                    `/admin/loyalty/merchants/${this.merchantId}/settings`,
                    this.settings
                );

                if (response) {
                    loyaltyMerchantSettingsLog.info('Settings saved successfully');
                    Utils.showToast(I18n.t('loyalty.toasts.settings_saved'), 'success');

                    // Navigate back to merchant detail
                    window.location.href = this.backUrl;
                }
            } catch (error) {
                loyaltyMerchantSettingsLog.error('Failed to save settings:', error);
                Utils.showToast(I18n.t('loyalty.toasts.settings_save_failed', {message: error.message}), 'error');
            } finally {
                this.saving = false;
            }
        }
    };
}

// Register logger for configuration
if (!window.LogConfig.loggers.loyaltyMerchantSettings) {
    window.LogConfig.loggers.loyaltyMerchantSettings = window.LogConfig.createLogger('loyaltyMerchantSettings');
}

loyaltyMerchantSettingsLog.info('Loyalty merchant settings module loaded');
