(function(){
  // Lightweight replacement for the legacy dashboard items script.
  // Responsibilities kept minimal during migration to Item Lab:
  // - Fetch and render items list
  // - Wire Delete handlers
  // - Redirect Edit actions to the Item Lab

  const $ = id => document.getElementById(id);
  const itemsRoot = $('itemsRoot');
  const guildId = itemsRoot && itemsRoot.dataset ? itemsRoot.dataset.guildId : '';
  const list = $('itemsList');

  async function fetchItems(){
    if(!list) return;
    list.innerHTML = '<div class="text-white/60">Cargando...</div>';
    try{
      const res = await fetch('/api/dashboard/' + encodeURIComponent(guildId) + '/items');
      if(!res.ok) throw new Error('fetch-failed');
      const j = await res.json();
      const items = (j && j.ok && Array.isArray(j.items)) ? j.items : [];
      renderList(items);
    }catch(e){ list.innerHTML = '<div class="text-red-400">Error cargando items</div>'; }
  }

  function escapeHtml(s){ return String(s||'').replace(/[&<>"']/g, c => ({'&':'&amp;','<':'&lt;','>':'&gt;','"':'&quot;',"'":'&#39;'}[c])); }

  function renderList(items){
    if(!list) return;
    list.innerHTML = '';
    if(!items || items.length===0){ list.innerHTML = '<div class="text-white/60">No hay items</div>'; return; }
    items.forEach(it=>{
      const card = document.createElement('div');
      card.className = 'p-3 bg-white/3 rounded mb-2 flex justify-between items-start';
      const left = document.createElement('div'); left.style.flex = '1';
      const titleDiv = document.createElement('div'); titleDiv.className='font-semibold text-white'; titleDiv.textContent = it.name || it.key || 'Sin nombre';
      const descDiv = document.createElement('div'); descDiv.className = 'text-white/60 text-sm mt-1'; descDiv.textContent = it.description || '';
      left.appendChild(titleDiv); left.appendChild(descDiv);
      const right = document.createElement('div'); right.className = 'flex items-center gap-2';
      const editBtn = document.createElement('button'); editBtn.className='editBtn px-2 py-1 bg-indigo-600 rounded text-white text-sm'; editBtn.textContent='Editar'; editBtn.dataset.id = it.id;
      const delBtn = document.createElement('button'); delBtn.className='delBtn px-2 py-1 bg-red-600 rounded text-white text-sm'; delBtn.textContent='Eliminar'; delBtn.dataset.id = it.id;
      right.appendChild(editBtn); right.appendChild(delBtn);
      card.appendChild(left); card.appendChild(right); list.appendChild(card);
    });

    Array.from(list.querySelectorAll('.editBtn')).forEach(b=>b.addEventListener('click', onEdit));
    Array.from(list.querySelectorAll('.delBtn')).forEach(b=>b.addEventListener('click', onDelete));
  }

  async function onDelete(e){ const id = e.currentTarget && e.currentTarget.dataset ? e.currentTarget.dataset.id : null; if(!id) return; if(!confirm('Eliminar item?')) return; try{ const res = await fetch('/api/dashboard/' + encodeURIComponent(guildId) + '/items/' + encodeURIComponent(id), { method:'DELETE' }); if(!res.ok) throw new Error('delete-failed'); await fetchItems(); alert('Item eliminado'); }catch(err){ alert('Error al eliminar'); } }

  function onEdit(e){ const id = e.currentTarget && e.currentTarget.dataset ? e.currentTarget.dataset.id : null; if(!id) return; window.location.href = '/items/lab?guild=' + encodeURIComponent(guildId) + '&edit=' + encodeURIComponent(id); }

  // wire create button (if present) to navigate to lab
  const createBtn = $('createItemBtn'); if(createBtn) createBtn.addEventListener('click', (ev)=>{ ev.preventDefault(); window.location.href = '/items/lab?guild=' + encodeURIComponent(guildId); });

  // initial load
  fetchItems();

})();
