import os
import re

# List of ALL pages
all_pages = [
    'index.html', 'quotation.html', 'fleet.html', 'contact.html',
    'locations.html', 'sitemap-towns.html', 'q-and-a.html', 
    'privacy.html', 'terms.html',
    'admin-drivers.html', 'admin-messages.html', 'admin-new-booking.html',
    'admin-pricing.html', 'admin-quotes.html', 'admin.html', 'admin-login.html',
    'acceptable-use.html', 'gdpr.html', 'promotional-terms.html',
    'sign-in.html', 'sign-up.html', 'reset-password.html'
]

mobile_menu_button = '''      <button class="menu-toggle" id="menuToggle" aria-label="Toggle menu">
        <span></span>
        <span></span>
        <span></span>
      </button>'''

mobile_menu_script = '''
  <script>
    // Mobile Menu Toggle
    const menuToggle = document.getElementById('menuToggle');
    const nav = document.querySelector('.nav');
    
    if (menuToggle && nav) {
      menuToggle.addEventListener('click', () => {
        nav.classList.toggle('active');
        menuToggle.classList.toggle('active');
      });
    }
  </script>'''

for page in all_pages:
    if not os.path.exists(page):
        print(f"⚠️  {page} - File not found")
        continue
    
    with open(page, 'r', encoding='utf-8') as f:
        content = f.read()
    
    original_content = content
    
    # Check if menu toggle button already exists
    if '<button class="menu-toggle"' in content:
        print(f"ℹ️  {page} - Menu button already exists")
        continue
    
    # Add menu toggle button before </nav> or before nav closing
    # Find the nav section and add button before the nav closes
    if '<nav class="nav">' in content or '<div class="nav">' in content:
        # Add button after nav opening tag
        content = re.sub(
            r'(<nav class="nav">|<div class="nav">)',
            r'\1\n' + mobile_menu_button,
            content,
            count=1
        )
    elif '</header>' in content:
        # Add button before header closes
        content = re.sub(
            r'(</header>)',
            mobile_menu_button + '\n      \1',
            content,
            count=1
        )
    
    # Add mobile menu script before </body> if not exists
    if 'menuToggle' not in content or 'Mobile Menu Toggle' not in content:
        content = re.sub(
            r'(</body>)',
            mobile_menu_script + '\n\1',
            content,
            count=1
        )
    
    if content != original_content:
        with open(page, 'w', encoding='utf-8') as f:
            f.write(content)
        print(f"✅ Added menu icon to {page}")
    else:
        print(f"ℹ️  {page} - No changes needed")

print("\n" + "="*60)
print("✅ Mobile menu icons added to all pages!")
print("="*60)
