(function($) {
  'use strict';

  function detectIsDarkMode() {
    var savedMode = localStorage.getItem('mindweco-theme-mode');
    if (savedMode) return savedMode === 'dark';
    if (window.matchMedia) {
      return window.matchMedia('(prefers-color-scheme: dark)').matches;
    }
    return false;
  }

  function updateThemeToggleIcon(theme) {
    var $themeBtn = $('#mindweco-theme-toggle');
    if (!$themeBtn.length) return;

    if (theme === 'dark') {
      $themeBtn.html(
        '<svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" fill="currentColor" viewBox="0 0 24 24">' +
        '<path d="M12 17.5a5.5 5.5 0 1 0 0-11 5.5 5.5 0 0 0 0 11zm0-13a1 1 0 0 0 1-1V2a1 1 0 0 0-2 0v1.5a1 1 0 0 0 1 1zm0 17a1 1 0 0 0-1 1V24a1 1 0 0 0 2 0v-1.5a1 1 0 0 0-1-1zM22 11h-1.5a1 1 0 0 0 0 2H22a1 1 0 0 0 0-2zM4.5 11H3a1 1 0 0 0 0 2h1.5a1 1 0 0 0 0-2zm13.44-6.56a1 1 0 0 0 0 1.41l1.06 1.06a1 1 0 0 0 1.41-1.41l-1.06-1.06a1 1 0 0 0-1.41 0zM5.05 17.09l-1.06 1.06a1 1 0 0 0 1.41 1.41l1.06-1.06a1 1 0 0 0-1.41-1.41zm13.44 0a1 1 0 0 0-1.41 1.41l1.06 1.06a1 1 0 0 0 1.41-1.41l-1.06-1.06zM6.51 6.51a1 1 0 0 0 0-1.41L5.45 4.04A1 1 0 0 0 4.04 5.45l1.06 1.06a1 1 0 0 0 1.41 0z"/>' +
        '</svg>'
      ).attr('title', 'Switch to light mode');
    } else {
      $themeBtn.html(
        '<svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" viewBox="0 0 16 16">' +
        '<path d="M6 .278a.768.768 0 0 1 .08.858 7.208 7.208 0 0 0-.878 3.46c0 4.021 3.278 7.277 7.318 7.277.527 0 1.04-.055 1.533-.16a.787.787 0 0 1 .81.316.733.733 0 0 1-.031.893A8.349 8.349 0 0 1 8.344 16C3.734 16 0 12.286 0 7.71 0 4.266 2.114 1.312 5.124.06A.752.752 0 0 1 6 .278z"/>' +
        '</svg>'
      ).attr('title', 'Switch to dark mode');
    }
  }

  function applyTheme(theme) {
    var $container = $('#mindweco-container');
    var $modal = $('#startNewChatModal');

    $container.attr('data-theme', theme);
    $modal.attr('data-theme', theme);

    var $root = $('#mindweb-mindweco-root');
    if ($root.length) $root.attr('data-theme', theme);

    localStorage.setItem('mindweco-theme-mode', theme);
    updateThemeToggleIcon(theme);
  }

  function toggleTheme() {
    var currentTheme = localStorage.getItem('mindweco-theme-mode') || 'light';
    var newTheme = currentTheme === 'dark' ? 'light' : 'dark';
    applyTheme(newTheme);
  }

  function init() {
    var isDark = detectIsDarkMode();
    applyTheme(isDark ? 'dark' : 'light');

    if (window.matchMedia) {
      window.matchMedia('(prefers-color-scheme: dark)').addEventListener('change', function(e) {
        if (!localStorage.getItem('mindweco-theme-mode')) {
          applyTheme(e.matches ? 'dark' : 'light');
        }
      });
    }

    $(document).on('click', '#mindweco-theme-toggle', function(e) {
      e.stopPropagation();
      toggleTheme();
    });
  }

  window.MindwecoTheme = {
    init: init,
    toggle: toggleTheme,
    apply: applyTheme,
    updateIcon: updateThemeToggleIcon,
    detect: detectIsDarkMode
  };

})(jQuery);