/* OurNetHelps Mortgage Q&A Assistant — Admin Scripts */
/* globals wp */
(function(){
'use strict';

/* ── Photo upload (Broker Profile tab) ── */
var uploadBtn  = document.getElementById('onhmqa-upload-photo');
var removeBtn  = document.getElementById('onhmqa-remove-photo');
var photoInput = document.getElementById('onhmqa-photo-url');

if(uploadBtn && photoInput){
  var frame;
  function syncRemove(){
    if(removeBtn){ removeBtn.style.display = photoInput.value ? 'inline-block' : 'none'; }
  }
  uploadBtn.addEventListener('click',function(e){
    e.preventDefault();
    if(frame){ frame.open(); return; }
    frame = wp.media({title:'Choose Photo',button:{text:'Use this photo'},multiple:false});
    frame.on('select',function(){
      var att = frame.state().get('selection').first().toJSON();
      photoInput.value = att.url;
      var prev = document.querySelector('.onhmqa-photo-preview');
      if(prev){ prev.src = att.url; }
      else {
        var img = document.createElement('img');
        img.src = att.url; img.className = 'onhmqa-photo-preview'; img.alt = '';
        photoInput.insertAdjacentElement('afterend', img);
      }
      syncRemove();
    });
    frame.open();
  });
  if(removeBtn){
    removeBtn.addEventListener('click',function(e){
      e.preventDefault();
      photoInput.value = '';
      var prev = document.querySelector('.onhmqa-photo-preview');
      if(prev) prev.remove();
      syncRemove();
    });
  }
}

/* ── Q&A Editor (Q&A tab) ── */
var qaList   = document.getElementById('onhmqa-qa-list');
var qaHidden = document.getElementById('onhmqa-qa-json');
var qaAdd    = document.getElementById('onhmqa-add-qa');

if(qaList && qaHidden){
  function syncJSON(){
    var rows  = qaList.querySelectorAll('.onhmqa-qa-row');
    var pairs = [];
    rows.forEach(function(row){
      var q = row.querySelector('.onhmqa-q').value.trim();
      var k = row.querySelector('.onhmqa-k').value.trim();
      var a = row.querySelector('.onhmqa-a').value.trim();
      if(q){ pairs.push({question:q, keywords:k, answer:a}); }
    });
    qaHidden.value = JSON.stringify(pairs);
  }

  qaList.addEventListener('input', syncJSON);

  var form = qaHidden.closest('form');
  if(form){ form.addEventListener('submit', syncJSON); }

  function bindRemove(btn, row){
    btn.addEventListener('click',function(){
      row.remove();
      syncJSON();
    });
  }
  document.querySelectorAll('.onhmqa-remove-qa').forEach(function(b){
    bindRemove(b, b.closest('.onhmqa-qa-row'));
  });

  if(qaAdd){
    qaAdd.addEventListener('click',function(){
      var row = document.createElement('div');
      row.className = 'onhmqa-qa-row';
      row.innerHTML = '<div class="onhmqa-qa-fields">'
        + '<div><label>' + (window.onhmqaAdmin && onhmqaAdmin.labelQ ? onhmqaAdmin.labelQ : 'Question / Chip Label') + '</label>'
        + '<input type="text" class="large-text onhmqa-q" placeholder="e.g. What are today\'s rates?" /></div>'
        + '<div><label>' + (window.onhmqaAdmin && onhmqaAdmin.labelK ? onhmqaAdmin.labelK : 'Keywords (comma-separated, optional)') + '</label>'
        + '<input type="text" class="large-text onhmqa-k" placeholder="rates, interest rate, today rate" /></div>'
        + '<div><label>' + (window.onhmqaAdmin && onhmqaAdmin.labelA ? onhmqaAdmin.labelA : 'Answer') + '</label>'
        + '<textarea class="large-text onhmqa-a" rows="2"></textarea></div>'
        + '</div>'
        + '<button type="button" class="button onhmqa-remove-qa">' + (window.onhmqaAdmin && onhmqaAdmin.labelRemove ? onhmqaAdmin.labelRemove : 'Remove') + '</button>';
      qaList.appendChild(row);
      bindRemove(row.querySelector('.onhmqa-remove-qa'), row);
    });
  }
}

})();
