const statePrompt = document.getElementById('state-prompt');
const stateGranted = document.getElementById('state-granted');
const stateDenied = document.getElementById('state-denied');

function showState(state) {
  statePrompt.classList.add('hidden');
  stateGranted.classList.add('hidden');
  stateDenied.classList.add('hidden');
  state.classList.remove('hidden');
}

document.getElementById('btn-grant').addEventListener('click', async () => {
  try {
    const stream = await navigator.mediaDevices.getUserMedia({ audio: true });
    // Stop the stream immediately - we just needed the permission grant
    stream.getTracks().forEach(t => t.stop());
    showState(stateGranted);
    // Notify background
    chrome.runtime.sendMessage({ action: 'micPermissionGranted' });
    // Auto-close after brief delay
    setTimeout(() => window.close(), 1500);
  } catch (err) {
    console.error('Mic permission denied:', err);
    showState(stateDenied);
    chrome.runtime.sendMessage({ action: 'micPermissionDenied' });
  }
});

document.getElementById('btn-skip').addEventListener('click', () => {
  chrome.runtime.sendMessage({ action: 'micPermissionSkipped' });
  window.close();
});

document.getElementById('btn-close-denied').addEventListener('click', () => {
  window.close();
});
