#!/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 nav - find from <nav to the first closing </nav>
nav_start = index_html.find('<nav class="site-nav">')
if nav_start == -1:
    print("ERROR: Could not find nav start")
    exit(1)

# Find the matching </nav>
nav_end = index_html.find('</nav>', nav_start)
if nav_end == -1:
    print("ERROR: Could not find nav end")
    exit(1)

nav_template = index_html[nav_start:nav_end+6]  # +6 for </nav>

print("Extracted nav template from index.html")
print(f"Length: {len(nav_template)} characters")

# 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">')
    
    # Find and replace existing nav
    nav_start_pos = html.find('<nav class="site-nav">')
    if nav_start_pos == -1:
        print(f'⚠️  No nav found in {filename}')
        continue
    
    nav_end_pos = html.find('</nav>', nav_start_pos)
    if nav_end_pos == -1:
        print(f'⚠️  No nav end found in {filename}')
        continue
    
    # Replace nav
    new_html = html[:nav_start_pos] + nav + html[nav_end_pos+6:]
    
    with open(filepath, 'w') as f:
        f.write(new_html)
    
    print(f'✓ Updated {filename}')

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