document.addEventListener('DOMContentLoaded', function () {
  document.querySelectorAll('[data-bs-toggle="tooltip"]').forEach(function (el) {
    new bootstrap.Tooltip(el);
  });

  const sidebarToggle = document.querySelector('[data-sidebar-toggle]');
  if (sidebarToggle) {
    sidebarToggle.addEventListener('click', function () {
      document.body.classList.toggle('sidebar-open');
    });
  }

  document.addEventListener('click', function (event) {
    if (window.innerWidth > 991) return;
    if (!document.body.classList.contains('sidebar-open')) return;
    const sidebar = document.querySelector('.sidebar');
    const toggle = document.querySelector('[data-sidebar-toggle]');
    if (!sidebar || sidebar.contains(event.target) || (toggle && toggle.contains(event.target))) return;
    document.body.classList.remove('sidebar-open');
  });

  const plannerCards = document.querySelectorAll('[draggable="true"][data-order-id]');
  const dropzones = document.querySelectorAll('[data-room-dropzone]');
  let draggingCard = null;

  plannerCards.forEach(function(card) {
    card.addEventListener('dragstart', function() {
      draggingCard = card;
      card.classList.add('opacity-50');
    });
    card.addEventListener('dragend', function() {
      card.classList.remove('opacity-50');
      draggingCard = null;
      dropzones.forEach(function(zone){ zone.classList.remove('drag-over'); });
    });
  });

  dropzones.forEach(function(zone) {
    zone.addEventListener('dragover', function(e) {
      e.preventDefault();
      zone.classList.add('drag-over');
    });
    zone.addEventListener('dragleave', function() {
      zone.classList.remove('drag-over');
    });
    zone.addEventListener('drop', function(e) {
      e.preventDefault();
      zone.classList.remove('drag-over');
      if (!draggingCard) return;

      const roomId = zone.getAttribute('data-room-id');
      const roomNo = zone.getAttribute('data-room-no');
      const orderId = draggingCard.getAttribute('data-order-id');
      const checkIn = draggingCard.getAttribute('data-check-in');
      const checkOut = draggingCard.getAttribute('data-check-out');

      if (!confirm('Move booking #' + orderId + ' to Room ' + roomNo + '?')) {
        return;
      }

      fetch('/modules/calendar/move_booking.php', {
        method: 'POST',
        headers: {
          'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'
        },
        body: new URLSearchParams({
          order_id: orderId,
          room_id: roomId,
          check_in: checkIn,
          check_out: checkOut
        }).toString()
      })
      .then(function(r){ return r.json(); })
      .then(function(data) {
        if (!data.ok) {
          alert(data.message || 'Move failed.');
          return;
        }
        zone.appendChild(draggingCard);
        const roomText = draggingCard.querySelector('[data-chip-room]');
        if (roomText) {
          roomText.textContent = 'Room ' + roomNo;
        }
      })
      .catch(function() {
        alert('Could not contact server.');
      });
    });
  });
});
