{# app/templates/admin/settings.html #}
{% extends "admin/base.html" %}

{% block title %}Platform Settings{% endblock %}

{% block alpine_data %}adminSettings(){% endblock %}

{% block content %}
<!-- Page Header -->
<div class="flex items-center justify-between my-6">
    <h2 class="text-2xl font-semibold text-gray-700 dark:text-gray-200">
        Platform Settings
    </h2>
    <button
        @click="refresh()"
        :disabled="loading"
        class="flex items-center px-4 py-2 text-sm font-medium leading-5 text-white transition-colors duration-150 bg-purple-600 border border-transparent rounded-lg hover:bg-purple-700 focus:outline-none focus:shadow-outline-purple disabled:opacity-50 disabled:cursor-not-allowed"
    >
        <span x-show="!loading" x-html="$icon('refresh', 'w-4 h-4 mr-2')"></span>
        <span x-show="loading" x-html="$icon('spinner', 'w-4 h-4 mr-2')"></span>
        <span x-text="loading ? 'Loading...' : 'Refresh'"></span>
    </button>
</div>

<!-- Success Message -->
<div x-show="successMessage" x-transition class="mb-6 p-4 bg-green-100 border border-green-400 text-green-700 rounded-lg flex items-start">
    <span x-html="$icon('check-circle', 'w-5 h-5 mr-3 mt-0.5 flex-shrink-0')"></span>
    <div>
        <p class="font-semibold">Success</p>
        <p class="text-sm" x-text="successMessage"></p>
    </div>
</div>

<!-- Error Message -->
<div x-show="error" x-transition class="mb-6 p-4 bg-red-100 border border-red-400 text-red-700 rounded-lg flex items-start">
    <span x-html="$icon('exclamation', 'w-5 h-5 mr-3 mt-0.5 flex-shrink-0')"></span>
    <div>
        <p class="font-semibold">Error</p>
        <p class="text-sm" x-text="error"></p>
    </div>
</div>

<!-- Settings Categories Tabs -->
<div class="mb-6">
    <div class="border-b border-gray-200 dark:border-gray-700">
        <nav class="-mb-px flex space-x-8">
            <button
                @click="activeTab = 'logging'"
                :class="activeTab === 'logging' ? 'border-purple-500 text-purple-600 dark:text-purple-400' : 'border-transparent text-gray-500 hover:text-gray-700 hover:border-gray-300 dark:text-gray-400 dark:hover:text-gray-300'"
                class="whitespace-nowrap py-4 px-1 border-b-2 font-medium text-sm transition-colors"
            >
                <span x-html="$icon('document-text', 'inline w-5 h-5 mr-2')"></span>
                Logging
            </button>
            <button
                @click="activeTab = 'system'"
                :class="activeTab === 'system' ? 'border-purple-500 text-purple-600 dark:text-purple-400' : 'border-transparent text-gray-500 hover:text-gray-700 hover:border-gray-300 dark:text-gray-400 dark:hover:text-gray-300'"
                class="whitespace-nowrap py-4 px-1 border-b-2 font-medium text-sm transition-colors"
            >
                <span x-html="$icon('cog', 'inline w-5 h-5 mr-2')"></span>
                System
            </button>
            <button
                @click="activeTab = 'security'"
                :class="activeTab === 'security' ? 'border-purple-500 text-purple-600 dark:text-purple-400' : 'border-transparent text-gray-500 hover:text-gray-700 hover:border-gray-300 dark:text-gray-400 dark:hover:text-gray-300'"
                class="whitespace-nowrap py-4 px-1 border-b-2 font-medium text-sm transition-colors"
            >
                <span x-html="$icon('shield-check', 'inline w-5 h-5 mr-2')"></span>
                Security
            </button>
        </nav>
    </div>
</div>

<!-- Logging Settings Tab -->
<div x-show="activeTab === 'logging'" x-transition>
    <div class="bg-white dark:bg-gray-800 rounded-lg shadow-md p-6">
        <h3 class="text-lg font-semibold text-gray-700 dark:text-gray-200 mb-4">
            Logging Configuration
        </h3>
        <p class="text-sm text-gray-600 dark:text-gray-400 mb-6">
            Configure application logging behavior, file rotation, and retention policies.
        </p>

        <!-- Log Level -->
        <div class="mb-6">
            <label class="block text-sm font-medium text-gray-700 dark:text-gray-300 mb-2">
                Log Level
            </label>
            <select
                x-model="logSettings.log_level"
                class="block w-full md:w-1/2 px-3 py-2 text-gray-700 dark:text-gray-300 bg-white dark:bg-gray-700 border border-gray-300 dark:border-gray-600 rounded-lg focus:outline-none focus:ring-2 focus:ring-purple-600"
            >
                <option value="DEBUG">DEBUG - Detailed information for diagnosing problems</option>
                <option value="INFO">INFO - General informational messages</option>
                <option value="WARNING">WARNING - Warning messages</option>
                <option value="ERROR">ERROR - Error messages</option>
                <option value="CRITICAL">CRITICAL - Critical errors only</option>
            </select>
            <p class="mt-2 text-xs text-gray-500 dark:text-gray-400">
                Changes take effect immediately without restart.
            </p>
        </div>

        <!-- File Rotation Settings -->
        <div class="grid grid-cols-1 md:grid-cols-2 gap-6 mb-6">
            <div>
                <label class="block text-sm font-medium text-gray-700 dark:text-gray-300 mb-2">
                    Max File Size (MB)
                </label>
                <input
                    type="number"
                    x-model.number="logSettings.log_file_max_size_mb"
                    min="1"
                    max="1000"
                    class="block w-full px-3 py-2 text-gray-700 dark:text-gray-300 bg-white dark:bg-gray-700 border border-gray-300 dark:border-gray-600 rounded-lg focus:outline-none focus:ring-2 focus:ring-purple-600"
                />
                <p class="mt-2 text-xs text-gray-500 dark:text-gray-400">
                    Log file will rotate when it reaches this size.
                </p>
            </div>

            <div>
                <label class="block text-sm font-medium text-gray-700 dark:text-gray-300 mb-2">
                    Backup File Count
                </label>
                <input
                    type="number"
                    x-model.number="logSettings.log_file_backup_count"
                    min="0"
                    max="50"
                    class="block w-full px-3 py-2 text-gray-700 dark:text-gray-300 bg-white dark:bg-gray-700 border border-gray-300 dark:border-gray-600 rounded-lg focus:outline-none focus:ring-2 focus:ring-purple-600"
                />
                <p class="mt-2 text-xs text-gray-500 dark:text-gray-400">
                    Number of rotated backup files to keep.
                </p>
            </div>
        </div>

        <!-- Database Retention -->
        <div class="mb-6">
            <label class="block text-sm font-medium text-gray-700 dark:text-gray-300 mb-2">
                Database Log Retention (Days)
            </label>
            <input
                type="number"
                x-model.number="logSettings.db_log_retention_days"
                min="1"
                max="365"
                class="block w-full md:w-1/2 px-3 py-2 text-gray-700 dark:text-gray-300 bg-white dark:bg-gray-700 border border-gray-300 dark:border-gray-600 rounded-lg focus:outline-none focus:ring-2 focus:ring-purple-600"
            />
            <p class="mt-2 text-xs text-gray-500 dark:text-gray-400">
                Logs older than this will be automatically deleted from database.
            </p>
        </div>

        <!-- Logging Toggles -->
        <div class="space-y-4 mb-6">
            <div class="flex items-center justify-between p-4 bg-gray-50 dark:bg-gray-700 rounded-lg">
                <div>
                    <p class="font-medium text-gray-700 dark:text-gray-200">File Logging</p>
                    <p class="text-sm text-gray-500 dark:text-gray-400">Write logs to rotating files on disk</p>
                </div>
                <label class="relative inline-flex items-center cursor-pointer">
                    <input type="checkbox" x-model="logSettings.file_logging_enabled" class="sr-only peer">
                    <div class="w-11 h-6 bg-gray-200 peer-focus:outline-none peer-focus:ring-4 peer-focus:ring-purple-300 dark:peer-focus:ring-purple-800 rounded-full peer dark:bg-gray-600 peer-checked:after:translate-x-full peer-checked:after:border-white after:content-[''] after:absolute after:top-[2px] after:left-[2px] after:bg-white after:border-gray-300 after:border after:rounded-full after:h-5 after:w-5 after:transition-all dark:border-gray-600 peer-checked:bg-purple-600"></div>
                </label>
            </div>

            <div class="flex items-center justify-between p-4 bg-gray-50 dark:bg-gray-700 rounded-lg">
                <div>
                    <p class="font-medium text-gray-700 dark:text-gray-200">Database Logging</p>
                    <p class="text-sm text-gray-500 dark:text-gray-400">Store WARNING/ERROR/CRITICAL logs in database for searching</p>
                </div>
                <label class="relative inline-flex items-center cursor-pointer">
                    <input type="checkbox" x-model="logSettings.db_logging_enabled" class="sr-only peer">
                    <div class="w-11 h-6 bg-gray-200 peer-focus:outline-none peer-focus:ring-4 peer-focus:ring-purple-300 dark:peer-focus:ring-purple-800 rounded-full peer dark:bg-gray-600 peer-checked:after:translate-x-full peer-checked:after:border-white after:content-[''] after:absolute after:top-[2px] after:left-[2px] after:bg-white after:border-gray-300 after:border after:rounded-full after:h-5 after:w-5 after:transition-all dark:border-gray-600 peer-checked:bg-purple-600"></div>
                </label>
            </div>
        </div>

        <!-- Save Button -->
        <div class="flex items-center justify-between pt-4 border-t border-gray-200 dark:border-gray-700">
            <p class="text-sm text-gray-600 dark:text-gray-400">
                <span x-html="$icon('information-circle', 'inline w-4 h-4 mr-1')"></span>
                File rotation settings require application restart to take effect.
            </p>
            <button
                @click="saveLogSettings()"
                :disabled="saving"
                class="px-6 py-2 text-sm font-medium leading-5 text-white transition-colors duration-150 bg-purple-600 border border-transparent rounded-lg hover:bg-purple-700 focus:outline-none focus:shadow-outline-purple disabled:opacity-50 disabled:cursor-not-allowed"
            >
                <span x-show="!saving">Save Logging Settings</span>
                <span x-show="saving">Saving...</span>
            </button>
        </div>
    </div>

    <!-- Quick Actions -->
    <div class="mt-8">
        <div class="mb-8 p-6 bg-white rounded-lg shadow-xs dark:bg-gray-800">
            <h4 class="mb-4 text-lg font-semibold text-gray-700 dark:text-gray-200">
                Quick Actions
            </h4>
            <div class="flex flex-wrap gap-3">
                <a
                    href="/admin/logs"
                    class="inline-flex items-center px-4 py-2 text-sm font-medium text-white transition-colors duration-150 bg-purple-600 border border-transparent rounded-lg hover:bg-purple-700 focus:outline-none focus:shadow-outline-purple"
                >
                    <span x-html="$icon('document-text', 'w-4 h-4 mr-2')"></span>
                    View Logs
                </a>
                <button
                    @click="cleanupOldLogs()"
                    class="inline-flex items-center px-4 py-2 text-sm font-medium text-gray-700 dark:text-gray-300 transition-colors duration-150 bg-white dark:bg-gray-700 border border-gray-300 dark:border-gray-600 rounded-lg hover:bg-gray-50 dark:hover:bg-gray-600 focus:outline-none focus:shadow-outline-gray"
                >
                    <span x-html="$icon('delete', 'w-4 h-4 mr-2')"></span>
                    Cleanup Old Logs
                </button>
            </div>
        </div>
    </div>
</div>

<!-- System Settings Tab -->
<div x-show="activeTab === 'system'" x-transition>
    <div class="bg-white dark:bg-gray-800 rounded-lg shadow-md p-6">
        <h3 class="text-lg font-semibold text-gray-700 dark:text-gray-200 mb-4">
            System Configuration
        </h3>
        <p class="text-sm text-gray-600 dark:text-gray-400 mb-6">
            General system settings and configuration options.
        </p>
        <div class="text-center py-12 text-gray-500 dark:text-gray-400">
            <span x-html="$icon('cog', 'inline w-12 h-12 mb-4')"></span>
            <p>System settings coming soon...</p>
        </div>
    </div>
</div>

<!-- Security Settings Tab -->
<div x-show="activeTab === 'security'" x-transition>
    <div class="bg-white dark:bg-gray-800 rounded-lg shadow-md p-6">
        <h3 class="text-lg font-semibold text-gray-700 dark:text-gray-200 mb-4">
            Security Configuration
        </h3>
        <p class="text-sm text-gray-600 dark:text-gray-400 mb-6">
            Security and authentication settings.
        </p>
        <div class="text-center py-12 text-gray-500 dark:text-gray-400">
            <span x-html="$icon('shield-check', 'inline w-12 h-12 mb-4')"></span>
            <p>Security settings coming soon...</p>
        </div>
    </div>
</div>

{% endblock %}

{% block extra_scripts %}
<script src="{{ url_for('static', path='admin/js/settings.js') }}"></script>
{% endblock %}
