// app/modules/loyalty/static/storefront/js/loyalty-dashboard.js
// Customer loyalty dashboard
const loyaltyDashboardLog = window.LogConfig.loggers.loyaltyDashboard || window.LogConfig.createLogger('loyaltyDashboard');

function customerLoyaltyDashboard() {
    return {
        ...storefrontLayoutData(),

        // Data
        card: null,
        program: null,
        rewards: [],
        transactions: [],
        locations: [],

        // Wallet
        walletUrls: { google_wallet_url: null, apple_wallet_url: null },

        // UI state
        loading: false,
        showBarcode: false,

        async init() {
            loyaltyDashboardLog.info('Customer loyalty dashboard initializing...');
            await this.loadData();
        },

        async loadData() {
            this.loading = true;
            try {
                await Promise.all([
                    this.loadCard(),
                    this.loadTransactions()
                ]);
            } catch (error) {
                loyaltyDashboardLog.error('Failed to load loyalty data:', error);
            } finally {
                this.loading = false;
            }
        },

        async loadCard() {
            try {
                const response = await apiClient.get('/storefront/loyalty/card');
                if (response) {
                    this.card = response.card;
                    this.program = response.program;
                    this.rewards = response.program?.points_rewards || [];
                    this.locations = response.locations || [];
                    this.walletUrls = response.wallet_urls || { google_wallet_url: null, apple_wallet_url: null };
                    loyaltyDashboardLog.info('Loyalty card loaded:', this.card?.card_number);
                }
            } catch (error) {
                if (error.status === 404) {
                    loyaltyDashboardLog.info('No loyalty card found');
                    this.card = null;
                } else {
                    throw error;
                }
            }
        },

        async loadTransactions() {
            try {
                const response = await apiClient.get('/storefront/loyalty/transactions?limit=10');
                if (response && response.transactions) {
                    this.transactions = response.transactions;
                }
            } catch (error) {
                loyaltyDashboardLog.warn('Failed to load transactions:', error.message);
            }
        },

        formatNumber(num) {
            if (num == null) return '0';
            return new Intl.NumberFormat('en-US').format(num);
        },

        formatDate(dateString) {
            if (!dateString) return '-';
            try {
                return new Date(dateString).toLocaleDateString('en-US', {
                    year: 'numeric',
                    month: 'short',
                    day: 'numeric'
                });
            } catch (e) {
                return dateString;
            }
        }
    };
}
