{{ define "config" }}
<div class="space-y-6">
    <!-- Configuration Form -->
    <form id="configForm" class="space-y-6">
        <!-- Tab Navigation Card -->
        <div class="card bg-base-100 shadow-xl">
            <div class="card-body">
                <!-- Modern Tab Navigation -->
                <div class="border-b border-base-300 mb-8">
                    <nav class="flex space-x-8" aria-label="Configuration Tabs">
                        <button type="button" class="tab-button active flex items-center gap-2 py-3 px-1 border-b-2 border-primary text-primary font-medium text-sm" data-tab="general">
                            <i class="bi bi-gear text-lg"></i>
                            <span class="hidden sm:inline">General</span>
                        </button>
                        <button type="button" class="tab-button flex items-center gap-2 py-3 px-1 border-b-2 border-transparent text-base-content/70 hover:text-base-content hover:border-base-300 font-medium text-sm transition-colors" data-tab="debrid">
                            <i class="bi bi-cloud text-lg"></i>
                            <span class="hidden sm:inline">Debrid</span>
                        </button>
                        <button type="button" class="tab-button flex items-center gap-2 py-3 px-1 border-b-2 border-transparent text-base-content/70 hover:text-base-content hover:border-base-300 font-medium text-sm transition-colors" data-tab="qbittorrent">
                            <i class="bi bi-download text-lg"></i>
                            <span class="hidden sm:inline">QBittorrent</span>
                        </button>
                        <button type="button" class="tab-button flex items-center gap-2 py-3 px-1 border-b-2 border-transparent text-base-content/70 hover:text-base-content hover:border-base-300 font-medium text-sm transition-colors" data-tab="arrs">
                            <i class="bi bi-collection text-lg"></i>
                            <span class="hidden sm:inline">*Arrs</span>
                        </button>
                        <button type="button" class="tab-button flex items-center gap-2 py-3 px-1 border-b-2 border-transparent text-base-content/70 hover:text-base-content hover:border-base-300 font-medium text-sm transition-colors" data-tab="repair">
                            <i class="bi bi-wrench text-lg"></i>
                            <span class="hidden sm:inline">Repair</span>
                        </button>
                    </nav>
                </div>

                <!-- Save Button (Sticky) -->
                <div class="sticky top-20 z-30 flex justify-end mb-6">
                    <button type="submit" class="btn btn-success btn-lg shadow-lg">
                        <i class="bi bi-save mr-2"></i>Save Configuration
                    </button>
                </div>

                <!-- Tab Content Container -->
                <div class="tab-content-container">

                    <!-- General Tab Content -->
                    <div class="tab-content" data-tab-content="general">
                        <div class="space-y-6">
                            <h2 class="text-2xl font-bold flex items-center mb-6">
                                <i class="bi bi-gear mr-3 text-primary"></i>General Settings
                            </h2>

                            <div class="grid grid-cols-1 lg:grid-cols-2 gap-4">
                                <div class="form-control">
                                    <label class="label" for="log-level">
                                        <span class="label-text font-medium">Log Level</span>
                                    </label>
                                    <select class="select select-bordered" name="log_level" id="log-level">
                                        <option value="info">Info</option>
                                        <option value="debug">Debug</option>
                                        <option value="warn">Warning</option>
                                        <option value="error">Error</option>
                                        <option value="trace">Trace</option>
                                    </select>
                                </div>
                                <div class="form-control">
                                    <label class="label">
                                        <span class="label-text font-medium">Magnet Link Handler</span>
                                    </label>
                                    <button type="button" class="btn btn-primary" onclick="registerMagnetLinkHandler();" id="registerMagnetLink">
                                        <i class="bi bi-magnet mr-2"></i>Register Magnet Handler
                                    </button>
                                </div>
                            </div>

                            <div class="grid grid-cols-1 lg:grid-cols-3 gap-4">
                                <div class="form-control">
                                    <label class="label" for="urlBase">
                                        <span class="label-text font-medium">URL Base</span>
                                    </label>
                                    <input type="text" class="input input-bordered" id="urlBase" name="url_base" placeholder="/">
                                    <div class="label">
                                        <span class="label-text-alt">URL base for the application</span>
                                    </div>
                                </div>

                                <div class="form-control">
                                    <label class="label" for="bindAddress">
                                        <span class="label-text font-medium">Bind Address</span>
                                    </label>
                                    <input type="text" class="input input-bordered" id="bindAddress" name="bind_address" placeholder="0.0.0.0">
                                    <div class="label">
                                        <span class="label-text-alt">Bind address (default: all interfaces)</span>
                                    </div>
                                </div>

                                <div class="form-control">
                                    <label class="label" for="port">
                                        <span class="label-text font-medium">Port</span>
                                    </label>
                                    <input type="number" class="input input-bordered" id="port" name="port" placeholder="8282">
                                    <div class="label">
                                        <span class="label-text-alt">Application port</span>
                                    </div>
                                </div>
                            </div>

                            <div class="grid grid-cols-1 lg:grid-cols-2 gap-4">
                                <div class="form-control">
                                    <label class="label" for="discordWebhookUrl">
                                        <span class="label-text font-medium">Discord Webhook URL</span>
                                    </label>
                                    <textarea class="textarea textarea-bordered" id="discordWebhookUrl" name="discord_webhook_url" placeholder="https://discord.com/api/webhooks/..."></textarea>
                                </div>

                                <div class="form-control">
                                    <label class="label" for="allowedExtensions">
                                        <span class="label-text font-medium">Allowed File Extensions</span>
                                    </label>
                                    <textarea class="textarea textarea-bordered" id="allowedExtensions" name="allowed_file_types" placeholder="mkv, mp4, avi, mov"></textarea>
                                    <div class="label">
                                        <span class="label-text-alt">Comma-separated list of allowed file extensions</span>
                                    </div>
                                </div>
                            </div>

                            <div class="grid grid-cols-1 lg:grid-cols-3 gap-4">
                                <div class="form-control">
                                    <label class="label" for="minFileSize">
                                        <span class="label-text font-medium">Minimum File Size</span>
                                    </label>
                                    <input type="text" class="input input-bordered" id="minFileSize" name="min_file_size" placeholder="10MB">
                                    <div class="label">
                                        <span class="label-text-alt">Minimum file size to download</span>
                                    </div>
                                </div>

                                <div class="form-control">
                                    <label class="label" for="maxFileSize">
                                        <span class="label-text font-medium">Maximum File Size</span>
                                    </label>
                                    <input type="text" class="input input-bordered" id="maxFileSize" name="max_file_size" placeholder="50GB">
                                    <div class="label">
                                        <span class="label-text-alt">Maximum file size to download</span>
                                    </div>
                                </div>

                                <div class="form-control">
                                    <label class="label" for="removeStalledAfter">
                                        <span class="label-text font-medium">Remove Stalled After</span>
                                    </label>
                                    <input type="text" class="input input-bordered" id="removeStalledAfter" name="remove_stalled_after" placeholder="1h">
                                    <div class="label">
                                        <span class="label-text-alt">Duration before removing stalled torrents</span>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>

                    <!-- Debrid Tab Content -->
                    <div class="tab-content hidden" data-tab-content="debrid">
                        <div class="space-y-6">
                            <div class="flex justify-between items-center">
                                <h2 class="text-2xl font-bold flex items-center">
                                    <i class="bi bi-cloud mr-3 text-secondary"></i>Debrid Services
                                </h2>
                                <button type="button" id="addDebridBtn" class="btn btn-secondary">
                                    <i class="bi bi-plus mr-2"></i>Add Debrid Service
                                </button>
                            </div>

                            <div id="debridConfigs" class="space-y-4">
                                <!-- Dynamic debrid configurations will be added here -->
                            </div>
                        </div>
                    </div>

                    <!-- QBittorrent Tab Content -->
                    <div class="tab-content hidden" data-tab-content="qbittorrent">
                        <div class="space-y-6">
                            <h2 class="text-2xl font-bold flex items-center mb-6">
                                <i class="bi bi-download mr-3 text-accent"></i>QBittorrent Settings
                            </h2>

                            <div class="grid grid-cols-1 lg:grid-cols-2 gap-4">
                                <div class="form-control">
                                    <label class="label" for="qbit.download_folder">
                                        <span class="label-text font-medium">Download Folder</span>
                                    </label>
                                    <input type="text" class="input input-bordered" name="qbit.download_folder" id="qbit.download_folder">
                                    <div class="label">
                                        <span class="label-text-alt">Folder where downloaded files will be stored</span>
                                    </div>
                                </div>

                                <div class="form-control">
                                    <label class="label" for="qbit.refresh_interval">
                                        <span class="label-text font-medium">Refresh Interval (seconds)</span>
                                    </label>
                                    <input type="number" class="input input-bordered" name="qbit.refresh_interval" id="qbit.refresh_interval" min="1" max="3600">
                                </div>

                                <div class="form-control">
                                    <label class="label" for="qbit.max_downloads">
                                        <span class="label-text font-medium">Maximum Downloads</span>
                                    </label>
                                    <input type="number" class="input input-bordered" name="qbit.max_downloads" id="qbit.max_downloads" min="0">
                                    <div class="label">
                                        <span class="label-text-alt">Maximum simultaneous downloads (0 = unlimited)</span>
                                    </div>
                                </div>

                                <div class="form-control">
                                    <label class="label cursor-pointer justify-start gap-3">
                                        <input type="checkbox" class="checkbox" name="qbit.skip_pre_cache" id="qbit.skip_pre_cache">
                                        <div>
                                            <span class="label-text font-medium">Skip Pre-Cache</span>
                                            <div class="label-text-alt">Disable pre-caching to speed up imports</div>
                                        </div>
                                    </label>
                                </div>
                            </div>
                        </div>
                    </div>

                    <!-- Arrs Tab Content -->
                    <div class="tab-content hidden" data-tab-content="arrs">
                        <div class="space-y-6">
                            <div class="flex justify-between items-center">
                                <h2 class="text-2xl font-bold flex items-center">
                                    <i class="bi bi-collection mr-3 text-warning"></i>Arr Applications
                                </h2>
                                <button type="button" id="addArrBtn" class="btn btn-warning">
                                    <i class="bi bi-plus mr-2"></i>Add Arr Service
                                </button>
                            </div>

                            <div id="arrConfigs" class="space-y-4">
                                <!-- Dynamic arr configurations will be added here -->
                            </div>
                        </div>
                    </div>

                    <!-- Repair Tab Content -->
                    <div class="tab-content hidden" data-tab-content="repair">
                        <div class="space-y-6">
                            <h2 class="text-2xl font-bold flex items-center mb-6">
                                <i class="bi bi-wrench mr-3 text-error"></i>Repair Settings
                            </h2>

                            <div class="form-control">
                                <label class="label cursor-pointer justify-start gap-3">
                                    <input type="checkbox" class="checkbox checkbox-lg" name="repair.enabled" id="repair.enabled">
                                    <div>
                                        <span class="label-text font-medium text-lg">Enable Scheduled Repair</span>
                                        <div class="label-text-alt">Automatically repair broken symlinks and missing files</div>
                                    </div>
                                </label>
                            </div>

                            <div class="grid grid-cols-1 lg:grid-cols-2 gap-4">
                                <div class="space-y-4">
                                    <div class="form-control">
                                        <label class="label" for="repair.interval">
                                            <span class="label-text font-medium">Repair Interval</span>
                                        </label>
                                        <input type="text" class="input input-bordered" name="repair.interval" id="repair.interval" placeholder="24h">
                                        <div class="label">
                                            <span class="label-text-alt">How often to run repair (e.g., 24h, 1d, 03:00, or crontab)</span>
                                        </div>
                                    </div>

                                    <div class="form-control">
                                        <label class="label" for="repair.workers">
                                            <span class="label-text font-medium">Worker Threads</span>
                                        </label>
                                        <input type="number" class="input input-bordered" name="repair.workers" id="repair.workers" min="1" max="20">
                                        <div class="label">
                                            <span class="label-text-alt">Number of concurrent repair workers</span>
                                        </div>
                                    </div>

                                    <div class="form-control">
                                        <label class="label" for="repair.strategy">
                                            <span class="label-text font-medium">Repair Strategy</span>
                                        </label>
                                        <select class="select select-bordered" name="repair.strategy" id="repair.strategy">
                                            <option value="per_torrent" selected>Per Torrent</option>
                                            <option value="per_file">Per File</option>
                                        </select>
                                        <div class="label">
                                            <span class="label-text-alt">How to handle repairs</span>
                                        </div>
                                    </div>
                                </div>

                                <div class="space-y-4">
                                    <div class="form-control">
                                        <label class="label" for="repair.zurg_url">
                                            <span class="label-text font-medium">Zurg URL</span>
                                        </label>
                                        <input type="url" class="input input-bordered" name="repair.zurg_url" id="repair.zurg_url" placeholder="http://zurg:9999">
                                        <div class="label">
                                            <span class="label-text-alt">Optional Zurg instance to speed up repairs</span>
                                        </div>
                                    </div>

                                    <div class="form-control">
                                        <label class="label cursor-pointer justify-start gap-3">
                                            <input type="checkbox" class="checkbox" name="repair.use_webdav" id="repair.use_webdav">
                                            <div>
                                                <span class="label-text font-medium">Use WebDAV</span>
                                                <div class="label-text-alt">Use internal WebDAV for repairs</div>
                                            </div>
                                        </label>
                                    </div>

                                    <div class="form-control">
                                        <label class="label cursor-pointer justify-start gap-3">
                                            <input type="checkbox" class="checkbox" name="repair.auto_process" id="repair.auto_process">
                                            <div>
                                                <span class="label-text font-medium">Auto Process</span>
                                                <div class="label-text-alt">Automatically delete broken symlinks and re-search</div>
                                            </div>
                                        </label>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>

                </div> <!-- End tab-content-container -->
            </div>
        </div>
    </form>
</div>

<!-- Loading Overlay -->
<div id="loadingOverlay" class="fixed inset-0 bg-black/50 backdrop-blur-sm z-50 hidden">
    <div class="flex items-center justify-center h-full">
        <div class="card bg-base-100 shadow-2xl">
            <div class="card-body text-center">
                <span class="loading loading-spinner loading-lg text-primary"></span>
                <h3 class="text-lg font-semibold mt-4">Applying Configuration</h3>
                <p class="text-base-content/70">Please wait while we save your settings...</p>
            </div>
        </div>
    </div>
</div>

<script>
    // Tab Navigation Handler
    document.addEventListener('DOMContentLoaded', function() {

        const tabButtons = document.querySelectorAll('.tab-button');
        const tabContents = document.querySelectorAll('.tab-content');

        // Tab switching function
        function switchTab(targetTab) {

            // Remove active class from all tabs
            tabButtons.forEach(btn => {
                btn.classList.remove('active');
            });

            // Add active class to clicked tab
            const activeButton = document.querySelector(`[data-tab="${targetTab}"]`);
            if (activeButton) {
                activeButton.classList.add('active');
            }

            // Hide all tab contents
            tabContents.forEach(content => {
                content.classList.add('hidden');
            });

            // Show selected tab content
            const activeContent = document.querySelector(`[data-tab-content="${targetTab}"]`);
            if (activeContent) {
                activeContent.classList.remove('hidden');
            }

            // Update URL hash for bookmarking
            window.location.hash = `tab-${targetTab}`;
        }

        // Add click handlers to tab buttons
        tabButtons.forEach(button => {
            button.addEventListener('click', (e) => {
                e.preventDefault();
                const tabId = button.getAttribute('data-tab');
                switchTab(tabId);
            });
        });

        // Handle initial tab based on URL hash
        const hash = window.location.hash;
        if (hash.startsWith('#tab-')) {
            const tabId = hash.replace('#tab-', '');
            switchTab(tabId);
        } else {
            switchTab('general');
        }

        // Handle browser back/forward
        window.addEventListener('hashchange', () => {
            const hash = window.location.hash;
            if (hash.startsWith('#tab-')) {
                const tabId = hash.replace('#tab-', '');
                switchTab(tabId);
            }
        });
    });
</script>
{{ end }}