/*
  ============================================================
  Digital Pathshala - Main JavaScript (`js/main.js`)
  Author: Biplab Mitra
  Description: Global UI interactions, navbar behavior, and utilities
  ============================================================
*/

document.addEventListener('DOMContentLoaded', () => {
  // 1. Highlight Active Nav Link based on current page
  const currentPath = window.location.pathname.split('/').pop() || 'index.html';
  const navLinks = document.querySelectorAll('.navbar-nav .nav-link');

  navLinks.forEach(link => {
    const linkHref = link.getAttribute('href');
    if (linkHref === currentPath || (currentPath === '' && linkHref === 'index.html')) {
      link.classList.add('active');
    } else {
      link.classList.remove('active');
    }
  });

  // 2. Auto-collapse Bootstrap mobile navbar on link click
  const navbarCollapse = document.getElementById('navbarNav');
  if (navbarCollapse) {
    const bsCollapse = new bootstrap.Collapse(navbarCollapse, { toggle: false });
    navLinks.forEach(link => {
      link.addEventListener('click', () => {
        if (window.innerWidth < 992 && navbarCollapse.classList.contains('show')) {
          bsCollapse.hide();
        }
      });
    });
  }

  // 3. Navbar scroll effect (add shadow on scroll)
  const navbar = document.querySelector('.navbar');
  if (navbar) {
    window.addEventListener('scroll', () => {
      if (window.scrollY > 30) {
        navbar.style.boxShadow = '0 4px 12px rgba(0, 0, 0, 0.08)';
      } else {
        navbar.style.boxShadow = 'none';
      }
    });
  }
});