// static/admin/js/settings.js

const settingsLog = window.LogConfig?.loggers?.settings || console;

function adminSettings() {
    // Get base data
    const baseData = typeof data === 'function' ? data() : {};

    return {
        // Inherit base layout functionality from init-alpine.js
        ...baseData,

        // Settings-specific state
        currentPage: 'settings',
        loading: true,
        saving: false,
        error: null,
        successMessage: null,
        activeTab: 'logging',
        logSettings: {
            log_level: 'INFO',
            log_file_max_size_mb: 10,
            log_file_backup_count: 5,
            db_log_retention_days: 30,
            file_logging_enabled: true,
            db_logging_enabled: true
        },

        async init() {
            try {
                settingsLog.info('=== SETTINGS PAGE INITIALIZING ===');
                await this.loadLogSettings();
            } catch (error) {
                settingsLog.error('Init failed:', error);
                this.error = 'Failed to initialize settings page';
            }
        },

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

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

            try {
                const data = await apiClient.get('/admin/logs/settings');
                this.logSettings = data;
                settingsLog.info('Log settings loaded:', this.logSettings);
            } catch (error) {
                settingsLog.error('Failed to load log settings:', error);
                this.error = error.response?.data?.detail || 'Failed to load log settings';
            } finally {
                this.loading = false;
            }
        },

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

            try {
                const data = await apiClient.put('/admin/logs/settings', this.logSettings);
                this.successMessage = data.message || 'Log settings saved successfully';

                // Auto-hide success message after 5 seconds
                setTimeout(() => {
                    this.successMessage = null;
                }, 5000);

                settingsLog.info('Log settings saved successfully');
            } catch (error) {
                settingsLog.error('Failed to save log settings:', error);
                this.error = error.response?.data?.detail || 'Failed to save log settings';
            } finally {
                this.saving = false;
            }
        },

        async cleanupOldLogs() {
            if (!confirm(`This will delete all logs older than ${this.logSettings.db_log_retention_days} days. Continue?`)) {
                return;
            }

            this.error = null;
            this.successMessage = null;

            try {
                const data = await apiClient.delete(
                    `/admin/logs/database/cleanup?retention_days=${this.logSettings.db_log_retention_days}&confirm=true`
                );
                this.successMessage = data.message || 'Old logs cleaned up successfully';

                // Auto-hide success message after 5 seconds
                setTimeout(() => {
                    this.successMessage = null;
                }, 5000);

                settingsLog.info('Old logs cleaned up successfully');
            } catch (error) {
                settingsLog.error('Failed to cleanup logs:', error);
                this.error = error.response?.data?.detail || 'Failed to cleanup old logs';
            }
        }
    };
}

settingsLog.info('Settings module loaded');
