{# app/templates/admin/settings.html #}
{% extends "admin/base.html" %}
{% from 'shared/macros/alerts.html' import alert_dynamic, error_state %}
{% from 'shared/macros/headers.html' import page_header_refresh %}
{% from 'shared/macros/tabs.html' import tabs_nav, tab_button %}
{% from 'shared/macros/inputs.html' import number_stepper %}

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

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

{% block content %}
{{ page_header_refresh('Platform Settings') }}

{{ alert_dynamic(type='success', title='Success', message_var='successMessage', show_condition='successMessage') }}

{{ error_state('Error', show_condition='error') }}

<!-- Settings Categories Tabs -->
{% call tabs_nav() %}
    {{ tab_button('logging', 'Logging', icon='document-text') }}
    {{ tab_button('shipping', 'Shipping', icon='truck') }}
    {{ tab_button('system', 'System', icon='cog') }}
    {{ tab_button('security', 'Security', icon='shield-check') }}
    {{ tab_button('notifications', 'Notifications', icon='bell') }}
{% endcall %}

<!-- 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>
                {{ number_stepper(model='logSettings.log_file_max_size_mb', min=1, max=1000, step=10, label='Max File Size') }}
                <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>
                {{ number_stepper(model='logSettings.log_file_backup_count', min=0, max=50, step=1, label='Backup File Count') }}
                <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>
            {{ number_stepper(model='logSettings.db_log_retention_days', min=1, max=365, step=7, label='Retention Days') }}
            <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>

<!-- Shipping Settings Tab -->
<div x-show="activeTab === 'shipping'" 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">
            Shipping & Carrier Configuration
        </h3>
        <p class="text-sm text-gray-600 dark:text-gray-400 mb-6">
            Configure shipping carrier label URL prefixes. These are used to generate shipping label download links.
        </p>

        <!-- Carrier Label URL Settings -->
        <div class="space-y-6">
            <!-- Greco (Letzshop default) -->
            <div>
                <label class="block text-sm font-medium text-gray-700 dark:text-gray-300 mb-2">
                    <span class="flex items-center gap-2">
                        <span class="px-2 py-0.5 text-xs font-medium bg-purple-100 dark:bg-purple-900 text-purple-800 dark:text-purple-200 rounded">Greco</span>
                        Label URL Prefix
                    </span>
                </label>
                <input
                    type="url"
                    x-model="shippingSettings.carrier_greco_label_url"
                    placeholder="https://dispatchweb.fr/Tracky/Home/"
                    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">
                    The shipment number will be appended to this URL. Default for Letzshop: <code class="bg-gray-100 dark:bg-gray-700 px-1 rounded">https://dispatchweb.fr/Tracky/Home/</code>
                </p>
            </div>

            <!-- Colissimo -->
            <div>
                <label class="block text-sm font-medium text-gray-700 dark:text-gray-300 mb-2">
                    <span class="flex items-center gap-2">
                        <span class="px-2 py-0.5 text-xs font-medium bg-yellow-100 dark:bg-yellow-900 text-yellow-800 dark:text-yellow-200 rounded">Colissimo</span>
                        Label URL Prefix
                    </span>
                </label>
                <input
                    type="url"
                    x-model="shippingSettings.carrier_colissimo_label_url"
                    placeholder="https://www.laposte.fr/outils/suivre-vos-envois?code="
                    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">
                    Enter the tracking URL prefix for Colissimo shipments.
                </p>
            </div>

            <!-- XpressLogistics -->
            <div>
                <label class="block text-sm font-medium text-gray-700 dark:text-gray-300 mb-2">
                    <span class="flex items-center gap-2">
                        <span class="px-2 py-0.5 text-xs font-medium bg-blue-100 dark:bg-blue-900 text-blue-800 dark:text-blue-200 rounded">XpressLogistics</span>
                        Label URL Prefix
                    </span>
                </label>
                <input
                    type="url"
                    x-model="shippingSettings.carrier_xpresslogistics_label_url"
                    placeholder="https://tracking.xpresslogistics.com/"
                    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">
                    Enter the tracking URL prefix for XpressLogistics shipments.
                </p>
            </div>
        </div>

        <!-- Info Box -->
        <div class="mt-6 p-4 bg-blue-50 dark:bg-blue-900/20 border border-blue-200 dark:border-blue-800 rounded-lg">
            <div class="flex items-start">
                <span x-html="$icon('information-circle', 'w-5 h-5 text-blue-600 dark:text-blue-400 mt-0.5 mr-3 flex-shrink-0')"></span>
                <div class="text-sm text-blue-800 dark:text-blue-200">
                    <p class="font-medium mb-1">How label URLs work</p>
                    <p>When viewing an order, the system will combine the URL prefix with the shipment number to create a downloadable label link.</p>
                    <p class="mt-1">Example: <code class="bg-blue-100 dark:bg-blue-800 px-1 rounded">https://dispatchweb.fr/Tracky/Home/</code> + <code class="bg-blue-100 dark:bg-blue-800 px-1 rounded">H74683403433</code></p>
                </div>
            </div>
        </div>

        <!-- Save Button -->
        <div class="flex items-center justify-end pt-6 mt-6 border-t border-gray-200 dark:border-gray-700">
            <button
                @click="saveShippingSettings()"
                :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 Shipping Settings</span>
                <span x-show="saving">Saving...</span>
            </button>
        </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>

<!-- Notifications Settings Tab -->
<div x-show="activeTab === 'notifications'" 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">
            Notification Settings
        </h3>
        <p class="text-sm text-gray-600 dark:text-gray-400 mb-6">
            Configure platform notification preferences and delivery channels.
        </p>

        <!-- Quick Link to Full Notifications Page -->
        <div class="mb-6 p-4 bg-purple-50 dark:bg-purple-900/20 border border-purple-200 dark:border-purple-800 rounded-lg">
            <div class="flex items-start">
                <span x-html="$icon('information-circle', 'w-5 h-5 text-purple-600 dark:text-purple-400 mt-0.5 mr-3 flex-shrink-0')"></span>
                <div>
                    <p class="text-sm text-purple-800 dark:text-purple-200">
                        For detailed notification management including templates and delivery logs, visit the full
                        <a href="/admin/notifications-settings" class="font-medium underline hover:text-purple-600">Notifications page</a>
                        in Platform Monitoring.
                    </p>
                </div>
            </div>
        </div>

        <!-- Email Notifications Toggle -->
        <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">Email Notifications</p>
                    <p class="text-sm text-gray-500 dark:text-gray-400">Send notification emails for important events</p>
                </div>
                <label class="relative inline-flex items-center cursor-pointer">
                    <input type="checkbox" x-model="notificationSettings.email_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">In-App Notifications</p>
                    <p class="text-sm text-gray-500 dark:text-gray-400">Show notifications in the admin interface</p>
                </div>
                <label class="relative inline-flex items-center cursor-pointer">
                    <input type="checkbox" x-model="notificationSettings.in_app_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">Critical Alerts Only</p>
                    <p class="text-sm text-gray-500 dark:text-gray-400">Only receive notifications for critical system events</p>
                </div>
                <label class="relative inline-flex items-center cursor-pointer">
                    <input type="checkbox" x-model="notificationSettings.critical_only" 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-end pt-4 border-t border-gray-200 dark:border-gray-700">
            <button
                @click="saveNotificationSettings()"
                :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 Notification Settings</span>
                <span x-show="saving">Saving...</span>
            </button>
        </div>
    </div>
</div>

{% endblock %}

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