// ========== CADDY RELOAD BUTTON ==========
(function() {
  // Reload Caddy button handler
  document.getElementById('reload-caddy-top')?.addEventListener('click', async () => {
    const button = document.getElementById('reload-caddy-top');
    const originalText = button.textContent;

    try {
      button.textContent = '⏳ Reloading...';
      button.disabled = true;

      const response = await secureFetch('/api/v1/caddy/reload', {
        method: 'POST',
        headers: { 'Content-Type': 'application/json' }
      });

      const result = await response.json();

      if (response.ok && result.success) {
        button.textContent = '✅ Reloaded!';
        setTimeout(() => {
          button.textContent = originalText;
          button.disabled = false;
        }, 2000);
      } else {
        throw new Error(result.error || 'Reload failed');
      }
    } catch (error) {
      button.textContent = '❌ Failed';
      showNotification(`Failed to reload Caddy: ${error.message}`, 'error');
      setTimeout(() => {
        button.textContent = originalText;
        button.disabled = false;
      }, 2000);
    }
  });
})();
