/* ============================================================
   DroidTester — Main JS (Global)
   ============================================================ */

(function () {
  'use strict';

  // ── Sidebar Toggle (Mobile) ──
  const sidebar = document.getElementById('sidebar');
  const overlay = document.getElementById('sidebarOverlay');
  const burgerBtn = document.getElementById('burgerBtn');

  function openSidebar() {
    if (sidebar) sidebar.classList.add('open');
    if (overlay) overlay.classList.add('show');
    document.body.style.overflow = 'hidden';
  }

  function closeSidebar() {
    if (sidebar) sidebar.classList.remove('open');
    if (overlay) overlay.classList.remove('show');
    document.body.style.overflow = '';
  }

  if (burgerBtn) burgerBtn.addEventListener('click', openSidebar);
  if (overlay) overlay.addEventListener('click', closeSidebar);

  // ── Active Nav Item ──
  const currentPath = window.location.pathname;
  document.querySelectorAll('.sidebar-nav-item').forEach(function (link) {
    const href = link.getAttribute('href') || '';
    if (href && currentPath.endsWith(href.split('/').pop())) {
      link.classList.add('active');
    }
  });

  // ── Search Bar Redirect ──
  const searchInput = document.getElementById('globalSearch');
  if (searchInput) {
    searchInput.addEventListener('keydown', function (e) {
      if (e.key === 'Enter' && this.value.trim()) {
        const base = currentPath.includes('/dashboard/') ? '' : 'dashboard/';
        window.location.href = base + 'search-campaign.html?q=' + encodeURIComponent(this.value.trim());
      }
    });
  }

  // ── Page Fade-in Animation ──
  document.querySelectorAll('.fade-in-up').forEach(function (el, i) {
    el.style.animationDelay = (i * 0.08) + 's';
    el.style.opacity = '0';
  });

  // ── Smooth Scroll for Anchors ──
  document.querySelectorAll('a[href^="#"]').forEach(function (anchor) {
    anchor.addEventListener('click', function (e) {
      const target = document.querySelector(this.getAttribute('href'));
      if (target) {
        e.preventDefault();
        target.scrollIntoView({ behavior: 'smooth', block: 'start' });
      }
    });
  });

  // ── Navbar scroll effect (public pages) ──
  const publicNav = document.querySelector('.public-navbar');
  if (publicNav) {
    window.addEventListener('scroll', function () {
      if (window.scrollY > 20) {
        publicNav.classList.add('scrolled');
      } else {
        publicNav.classList.remove('scrolled');
      }
    });
  }

  // ── Tooltip Init (Bootstrap) ──
  if (typeof bootstrap !== 'undefined' && bootstrap.Tooltip) {
    document.querySelectorAll('[data-bs-toggle="tooltip"]').forEach(function (el) {
      new bootstrap.Tooltip(el);
    });
  }

  // ── Search Campaign pre-fill from URL ──
  const urlParams = new URLSearchParams(window.location.search);
  const qParam = urlParams.get('q');
  if (qParam) {
    const searchFields = document.querySelectorAll('.search-query-fill');
    searchFields.forEach(function (f) { f.value = qParam; });

    // Filter campaign rows if on search page
    const rows = document.querySelectorAll('.campaign-row');
    let matchCount = 0;
    rows.forEach(function (row) {
      const text = row.innerText.toLowerCase();
      if (text.includes(qParam.toLowerCase())) {
        row.style.display = '';
        matchCount++;
      } else {
        row.style.display = 'none';
      }
    });
    const matchEl = document.getElementById('matchCount');
    if (matchEl) matchEl.textContent = matchCount + ' Index Matches';
  }
})();
