// === Selection handlers ===

function selectInGroup(el, groupSelector) {
  const section = el.closest('.section');
  section.querySelectorAll(groupSelector).forEach(s => s.classList.remove('selected'));
  el.classList.add('selected');
  el.querySelector('input[type="radio"]').checked = true;
}

function selectOption(el) { selectInGroup(el, '.option'); }
function selectFaq(el) { selectInGroup(el, '.faq-set'); }
function selectCat(el) { selectInGroup(el, '.cat-pill'); }

function toggleRelated(el) {
  const cb = el.querySelector('input[type="checkbox"]');
  setTimeout(() => el.classList.toggle('selected', cb.checked), 0);
}

function toggleFeedback(el) {
  const area = el.nextElementSibling;
  area.classList.toggle('open');
  el.textContent = area.classList.contains('open') ? '− Hide feedback' : '+ Add feedback';
}

// === Inline editing ===

function enableEdit(el) {
  el.contentEditable = 'true';
  el.focus();
}

function disableEdit(el) {
  el.contentEditable = 'false';
}

// === Export ===

function getFieldValue(section) {
  const field = section.dataset.field;

  if (field === 'relatedTips') {
    const slugs = Array.from(section.querySelectorAll('input[type="checkbox"]:checked')).map(cb => cb.value);
    return slugs.length > 0 ? slugs.join(', ') : 'none';
  }
  if (field === 'faqs') {
    const label = section.querySelector('.faq-set.selected .faq-set-label');
    return label ? label.textContent : 'none';
  }
  if (field === 'category') {
    const pill = section.querySelector('.cat-pill.selected');
    return pill ? pill.textContent.trim() : 'none';
  }
  const text = section.querySelector('.option.selected .option-text');
  return text ? text.textContent.trim() : 'none';
}

function getFeedback(section) {
  const input = section.querySelector('.feedback-input');
  return input && input.value.trim() ? input.value.trim() : null;
}

function exportSelections() {
  let out = '';
  document.querySelectorAll('.section').forEach(section => {
    const field = section.dataset.field;
    out += `${field}: ${getFieldValue(section)}\n`;
    const fb = getFeedback(section);
    if (fb) out += `${field}_feedback: ${fb}\n`;
  });
  document.getElementById('output').textContent = out;
  document.getElementById('modal').classList.add('open');
}

function copyToClipboard() {
  navigator.clipboard.writeText(document.getElementById('output').textContent).then(() => {
    const btn = event.target;
    const orig = btn.textContent;
    btn.textContent = 'Copied!';
    setTimeout(() => btn.textContent = orig, 1500);
  });
}

function closeModal() {
  document.getElementById('modal').classList.remove('open');
}

function resetAll() {
  document.querySelectorAll('.option, .faq-set, .cat-pill, .related-option').forEach(el => el.classList.remove('selected'));
  document.querySelectorAll('input[type="radio"], input[type="checkbox"]').forEach(el => el.checked = false);
  document.querySelectorAll('.feedback-input').forEach(el => el.value = '');
  document.querySelectorAll('.feedback-area').forEach(el => el.classList.remove('open'));
}
