// app/modules/marketplace/static/admin/js/letzshop.js
/**
 * Admin Letzshop management page logic
 */

// Use centralized logger (with fallback)
const letzshopLog = window.LogConfig?.createLogger?.('letzshop') ||
    window.LogConfig?.loggers?.letzshop ||
    { info: () => {}, warn: () => {}, error: () => {}, debug: () => {} };

letzshopLog.info('Loading...');

function adminLetzshop() {
    letzshopLog.info('adminLetzshop() called');

    return {
        // Inherit base layout state
        ...data(),

        // Set page identifier
        currentPage: 'letzshop',

        // Loading states
        loading: false,
        savingConfig: false,
        loadingOrders: false,

        // Messages
        error: '',
        successMessage: '',

        // Stores data
        stores: [],
        totalStores: 0,
        page: 1,
        limit: 50,

        // Filters
        filters: {
            configuredOnly: false
        },

        // Stats
        stats: {
            total: 0,
            configured: 0,
            autoSync: 0,
            pendingOrders: 0
        },

        // Confirm modals
        showDeleteStoreConfigModal: false,

        // Configuration modal
        showConfigModal: false,
        selectedStore: null,
        storeCredentials: null,
        configForm: {
            api_key: '',
            auto_sync_enabled: false,
            sync_interval_minutes: 15
        },
        showApiKey: false,

        // Orders modal
        showOrdersModal: false,
        storeOrders: [],

        async init() {
            // Load i18n translations
            await I18n.loadModule('marketplace');

            // Guard against multiple initialization
            if (window._adminLetzshopInitialized) {
                return;
            }
            window._adminLetzshopInitialized = true;

            letzshopLog.info('Initializing...');
            await this.loadStores();
        },

        /**
         * Load stores with Letzshop status
         */
        async loadStores() {
            this.loading = true;
            this.error = '';

            try {
                const params = new URLSearchParams({
                    skip: ((this.page - 1) * this.limit).toString(),
                    limit: this.limit.toString(),
                    configured_only: this.filters.configuredOnly.toString()
                });

                const response = await apiClient.get(`/admin/letzshop/stores?${params}`);
                this.stores = response.stores || [];
                this.totalStores = response.total || 0;

                // Calculate stats
                this.stats.total = this.totalStores;
                this.stats.configured = this.stores.filter(v => v.is_configured).length;
                this.stats.autoSync = this.stores.filter(v => v.auto_sync_enabled).length;
                this.stats.pendingOrders = this.stores.reduce((sum, v) => sum + (v.pending_orders || 0), 0);

                letzshopLog.info('Loaded stores:', this.stores.length);
            } catch (error) {
                letzshopLog.error('Failed to load stores:', error);
                this.error = error.message || 'Failed to load stores';
            } finally {
                this.loading = false;
            }
        },

        /**
         * Refresh all data
         */
        async refreshData() {
            await this.loadStores();
            this.successMessage = 'Data refreshed';
            setTimeout(() => this.successMessage = '', 3000);
        },

        /**
         * Open configuration modal for a store
         */
        async openConfigModal(store) {
            this.selectedStore = store;
            this.storeCredentials = null;
            this.configForm = {
                api_key: '',
                auto_sync_enabled: store.auto_sync_enabled || false,
                sync_interval_minutes: 15
            };
            this.showApiKey = false;
            this.showConfigModal = true;

            // Load existing credentials if configured
            if (store.is_configured) {
                try {
                    const response = await apiClient.get(`/admin/letzshop/stores/${store.store_id}/credentials`);
                    this.storeCredentials = response;
                    this.configForm.auto_sync_enabled = response.auto_sync_enabled;
                    this.configForm.sync_interval_minutes = response.sync_interval_minutes || 15;
                } catch (error) {
                    if (error.status !== 404) {
                        letzshopLog.error('Failed to load credentials:', error);
                    }
                }
            }
        },

        /**
         * Save store configuration
         */
        async saveStoreConfig() {
            if (!this.configForm.api_key && !this.storeCredentials) {
                this.error = 'Please enter an API key';
                return;
            }

            this.savingConfig = true;
            this.error = '';

            try {
                const payload = {
                    auto_sync_enabled: this.configForm.auto_sync_enabled,
                    sync_interval_minutes: parseInt(this.configForm.sync_interval_minutes)
                };

                if (this.configForm.api_key) {
                    payload.api_key = this.configForm.api_key;
                }

                await apiClient.post(
                    `/admin/letzshop/stores/${this.selectedStore.store_id}/credentials`,
                    payload
                );

                this.showConfigModal = false;
                this.successMessage = 'Configuration saved successfully';
                await this.loadStores();
            } catch (error) {
                letzshopLog.error('Failed to save config:', error);
                this.error = error.message || 'Failed to save configuration';
            } finally {
                this.savingConfig = false;
                setTimeout(() => this.successMessage = '', 5000);
            }
        },

        /**
         * Delete store configuration
         */
        async deleteStoreConfig() {
            try {
                await apiClient.delete(`/admin/letzshop/stores/${this.selectedStore.store_id}/credentials`);
                this.showConfigModal = false;
                this.successMessage = 'Configuration removed';
                await this.loadStores();
            } catch (error) {
                letzshopLog.error('Failed to delete config:', error);
                this.error = error.message || 'Failed to remove configuration';
            }
            setTimeout(() => this.successMessage = '', 5000);
        },

        /**
         * Test connection for a store
         */
        async testConnection(store) {
            this.error = '';

            try {
                const response = await apiClient.post(`/admin/letzshop/stores/${store.store_id}/test`);

                if (response.success) {
                    this.successMessage = `Connection successful for ${store.store_name} (${response.response_time_ms?.toFixed(0)}ms)`;
                } else {
                    this.error = response.error_details || 'Connection failed';
                }
            } catch (error) {
                letzshopLog.error('Connection test failed:', error);
                this.error = error.message || 'Connection test failed';
            }
            setTimeout(() => this.successMessage = '', 5000);
        },

        /**
         * Trigger sync for a store
         */
        async triggerSync(store) {
            this.error = '';

            try {
                const response = await apiClient.post(`/admin/letzshop/stores/${store.store_id}/sync`);

                if (response.success) {
                    this.successMessage = response.message || 'Sync completed';
                    await this.loadStores();
                } else {
                    this.error = response.message || 'Sync failed';
                }
            } catch (error) {
                letzshopLog.error('Sync failed:', error);
                this.error = error.message || 'Sync failed';
            }
            setTimeout(() => this.successMessage = '', 5000);
        },

        /**
         * View orders for a store
         */
        async viewOrders(store) {
            this.selectedStore = store;
            this.storeOrders = [];
            this.loadingOrders = true;
            this.showOrdersModal = true;

            try {
                const response = await apiClient.get(`/admin/letzshop/stores/${store.store_id}/orders?limit=100`);
                this.storeOrders = response.orders || [];
            } catch (error) {
                letzshopLog.error('Failed to load orders:', error);
                this.error = error.message || 'Failed to load orders';
            } finally {
                this.loadingOrders = false;
            }
        },

        /**
         * Format date for display
         */
        formatDate(dateStr) {
            if (!dateStr) return 'N/A';
            const date = new Date(dateStr);
            return date.toLocaleDateString() + ' ' + date.toLocaleTimeString([], { hour: '2-digit', minute: '2-digit' });
        }
    };
}

letzshopLog.info('Module loaded');
