#!/usr/bin/env python3
"""
Copy working navigation from strollers (index.html) to all product pages.
"""

import re

# Read the nav from index.html
with open('/root/clawd/stroller-app/index.html', 'r') as f:
    index_html = f.read()

# Extract just the nav section
nav_match = re.search(r'(<nav class="site-nav">.*?</nav>.*?<div class="nav-mobile".*?</div>)', index_html, re.DOTALL)
if not nav_match:
    print("ERROR: Could not find nav in index.html")
    exit(1)

nav_template = nav_match.group(1)

# Pages to update with their active link
pages = {
    'diapers.html': 'diapers.html',
    'formula.html': 'formula.html',
    'wipes.html': 'wipes.html',
    'bottles.html': 'bottles.html',
    'breast-pumps.html': 'breast-pumps.html',
    'baby-monitors.html': 'baby-monitors.html'
}

for filename, active_link in pages.items():
    filepath = f'/root/clawd/stroller-app/{filename}'
    
    try:
        with open(filepath, 'r') as f:
            html = f.read()
    except FileNotFoundError:
        print(f'⚠️  {filename} not found, skipping')
        continue
    
    # Create nav with correct active link
    nav = nav_template
    
    # Remove active from index.html
    nav = nav.replace('<a href="index.html" class="nav-link active">', '<a href="index.html" class="nav-link">')
    
    # Add active to current page
    nav = nav.replace(f'<a href="{active_link}" class="nav-link">', f'<a href="{active_link}" class="nav-link active">')
    
    # Replace existing nav in the page
    # Pattern: from <nav to end of nav-mobile div
    pattern = r'<nav class="site-nav">.*?</nav>(\s*<div class="nav-mobile".*?</div>)?'
    
    if re.search(pattern, html, re.DOTALL):
        html = re.sub(pattern, nav, html, flags=re.DOTALL)
        
        with open(filepath, 'w') as f:
            f.write(html)
        
        print(f'✓ Updated {filename}')
    else:
        print(f'⚠️  Could not find nav in {filename}')

print('\n✅ Navigation copied from strollers page to all product pages!')
