// ============================================================
// TRANSCRIPTION SETTINGS — Web Speech API
// ============================================================

  var _inited = false;

  document.addEventListener('tabChanged', function(e) {
    if (e.detail.tab !== 'settings' || _inited) return;
    _inited = true;
    initTranscriptionSettings();
  });

  // Also init on direct page load
  if (document.readyState === 'loading') {
    document.addEventListener('DOMContentLoaded', function() {
      setTimeout(function() {
        if (!_inited && document.getElementById('web-speech-enabled')) {
          _inited = true;
          initTranscriptionSettings();
        }
      }, 500);
    });
  } else {
    setTimeout(function() {
      if (!_inited && document.getElementById('web-speech-enabled')) {
        _inited = true;
        initTranscriptionSettings();
      }
    }, 500);
  }

  function initTranscriptionSettings() {
    console.log('[TranscriptionSettings] Initializing...');

    // ── Web Speech Recognition ───────────────────────────────
    var speechCheckbox = document.getElementById('web-speech-enabled');
    var speechStatus = document.getElementById('web-speech-status');
    var speechBrowserInfo = document.getElementById('web-speech-browser-info');

    if (speechCheckbox && window.WebSpeechRecognition) {
      speechCheckbox.checked = WebSpeechRecognition.isEnabled();
      speechStatus.textContent = WebSpeechRecognition.isEnabled() ? 'On — real-time streaming' : 'Off';

      // Show privacy info
      if (speechBrowserInfo) {
        var privacyInfo = WebSpeechRecognition.getPrivacyInfo();
        speechBrowserInfo.textContent = '';
        appendPrivacyLine(speechBrowserInfo, 'Provider: ', privacyInfo.provider);
        appendPrivacyLine(speechBrowserInfo, 'Privacy: ', privacyInfo.privacy);
        appendPrivacyLine(speechBrowserInfo, 'Warning: ', privacyInfo.warning, true);
      }

      speechCheckbox.addEventListener('change', function() {
        if (speechCheckbox.checked) {
          // Show confirmation for privacy warning
          showConfirm(
            'WARNING: Real-time streaming uses your browser\'s speech recognition, ' +
            'which may send audio to cloud servers (e.g., Google). ' +
            'This is NOT HIPAA-compliant. ' +
            'Only enable if you understand and accept this privacy trade-off.',
            function() {
              WebSpeechRecognition.setEnabled(true);
              speechStatus.textContent = 'On — real-time streaming';
            },
            { danger: true, confirmText: 'Enable' }
          );
          // Revert checkbox until confirmed
          speechCheckbox.checked = false;
          return;
        }

        WebSpeechRecognition.setEnabled(false);
        speechStatus.textContent = 'Off';
      });

      // If not supported, disable and show message
      if (!WebSpeechRecognition.isSupported()) {
        speechCheckbox.disabled = true;
        speechStatus.textContent = 'Not supported in this browser';
        if (speechBrowserInfo) {
          speechBrowserInfo.textContent = 'Not supported - Web Speech API not available in this browser.';
        }
      }
    }

    console.log('✅ Transcription settings initialized');
  }

  function appendPrivacyLine(container, label, value, warning) {
    var strong = document.createElement('strong');
    strong.textContent = label;
    if (warning) strong.style.color = 'var(--orange)';
    container.appendChild(strong);
    container.appendChild(document.createTextNode(value));
    container.appendChild(document.createElement('br'));
  }
