// static/admin/js/platform-modules.js
// Platform module configuration management

const moduleConfigLog = window.LogConfig?.loggers?.moduleConfig || window.LogConfig?.createLogger?.('moduleConfig') || console;

function adminPlatformModules(platformCode) {
    return {
        // Inherit base layout functionality from init-alpine.js
        ...data(),

        // Page-specific state
        currentPage: 'platforms',
        platformCode: platformCode,
        loading: true,
        error: null,
        successMessage: null,
        saving: false,
        showEnableAllModal: false,
        showDisableOptionalModal: false,

        // Data
        platform: null,
        moduleConfig: null,

        // Computed properties
        get coreModules() {
            if (!this.moduleConfig?.modules) return [];
            return this.moduleConfig.modules.filter(m => m.is_core);
        },

        get optionalModules() {
            if (!this.moduleConfig?.modules) return [];
            return this.moduleConfig.modules.filter(m => !m.is_core);
        },

        get coreModulesCount() {
            return this.coreModules.length;
        },

        get enabledOptionalCount() {
            return this.optionalModules.filter(m => m.is_enabled).length;
        },

        // Module icons mapping (must match icons.js definitions)
        getModuleIcon(moduleCode) {
            const icons = {
                'core': 'home',
                'platform-admin': 'office-building',
                'billing': 'credit-card',
                'inventory': 'archive',
                'orders': 'shopping-cart',
                'marketplace': 'shopping-bag',
                'customers': 'users',
                'cms': 'document-text',
                'analytics': 'chart-bar',
                'messaging': 'chat',
                'dev-tools': 'code',
                'monitoring': 'chart-pie'
            };
            return icons[moduleCode] || 'puzzle-piece';
        },

        // Modules with configuration options
        hasConfig(moduleCode) {
            return ['billing', 'inventory', 'orders', 'marketplace',
                    'customers', 'cms', 'analytics', 'messaging', 'monitoring'].includes(moduleCode);
        },

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

            // Guard against duplicate initialization
            if (window._platformModulesInitialized) {
                moduleConfigLog.warn('Already initialized, skipping');
                return;
            }
            window._platformModulesInitialized = true;

            moduleConfigLog.info('=== PLATFORM MODULES PAGE INITIALIZING ===');
            moduleConfigLog.info('Platform code:', this.platformCode);

            try {
                await this.loadPlatform();
                await this.loadModuleConfig();
                moduleConfigLog.info('=== PLATFORM MODULES PAGE INITIALIZED ===');
            } catch (error) {
                moduleConfigLog.error('Failed to initialize modules page:', error);
                this.error = 'Failed to load page data. Please refresh.';
            }
        },

        async refresh() {
            this.error = null;
            this.successMessage = null;
            await this.loadModuleConfig();
        },

        async loadPlatform() {
            try {
                this.platform = await apiClient.get(`/admin/platforms/${this.platformCode}`);
                moduleConfigLog.info('Loaded platform:', this.platform?.name);
            } catch (error) {
                moduleConfigLog.error('Failed to load platform:', error);
                throw error;
            }
        },

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

            try {
                const platformId = this.platform?.id;
                if (!platformId) {
                    throw new Error('Platform not loaded');
                }

                this.moduleConfig = await apiClient.get(`/admin/modules/platforms/${platformId}`);
                moduleConfigLog.info('Loaded module config:', {
                    total: this.moduleConfig?.total,
                    enabled: this.moduleConfig?.enabled,
                    disabled: this.moduleConfig?.disabled
                });
            } catch (error) {
                moduleConfigLog.error('Failed to load module config:', error);
                this.error = error.message || 'Failed to load module configuration';
            } finally {
                this.loading = false;
            }
        },

        async toggleModule(module) {
            if (module.is_core) {
                moduleConfigLog.warn('Cannot toggle core module:', module.code);
                return;
            }

            this.saving = true;
            this.error = null;
            this.successMessage = null;

            const action = module.is_enabled ? 'disable' : 'enable';

            try {
                const platformId = this.platform?.id;
                const endpoint = `/admin/modules/platforms/${platformId}/${action}`;

                const result = await apiClient.post(endpoint, {
                    module_code: module.code
                });

                moduleConfigLog.info(`${action}d module:`, module.code, result);

                // Show success message
                if (result.also_enabled?.length > 0) {
                    this.successMessage = `Module '${module.name}' enabled. Also enabled dependencies: ${result.also_enabled.join(', ')}`;
                } else if (result.also_disabled?.length > 0) {
                    this.successMessage = `Module '${module.name}' disabled. Also disabled dependents: ${result.also_disabled.join(', ')}`;
                } else {
                    this.successMessage = `Module '${module.name}' ${action}d successfully`;
                }

                // Reload module config to get updated state
                await this.loadModuleConfig();

                // Clear success message after delay
                setTimeout(() => {
                    this.successMessage = null;
                }, 5000);

            } catch (error) {
                moduleConfigLog.error(`Failed to ${action} module:`, error);
                this.error = error.message || `Failed to ${action} module`;
            } finally {
                this.saving = false;
            }
        },

        async enableAll() {
            this.saving = true;
            this.error = null;
            this.successMessage = null;

            try {
                const platformId = this.platform?.id;
                const result = await apiClient.post(`/admin/modules/platforms/${platformId}/enable-all`);

                moduleConfigLog.info('Enabled all modules:', result);
                this.successMessage = `All ${result.enabled_count} modules enabled`;

                // Reload module config
                await this.loadModuleConfig();

                setTimeout(() => {
                    this.successMessage = null;
                }, 5000);

            } catch (error) {
                moduleConfigLog.error('Failed to enable all modules:', error);
                this.error = error.message || 'Failed to enable all modules';
            } finally {
                this.saving = false;
            }
        },

        async disableOptional() {
            this.saving = true;
            this.error = null;
            this.successMessage = null;

            try {
                const platformId = this.platform?.id;
                const result = await apiClient.post(`/admin/modules/platforms/${platformId}/disable-optional`);

                moduleConfigLog.info('Disabled optional modules:', result);
                this.successMessage = `Optional modules disabled. Core modules kept: ${result.core_modules.join(', ')}`;

                // Reload module config
                await this.loadModuleConfig();

                setTimeout(() => {
                    this.successMessage = null;
                }, 5000);

            } catch (error) {
                moduleConfigLog.error('Failed to disable optional modules:', error);
                this.error = error.message || 'Failed to disable optional modules';
            } finally {
                this.saving = false;
            }
        }
    };
}
