#!/usr/bin/env python3
"""
For each product page:
1. Add <link rel="stylesheet" href="css/nav.css"> in <head>
2. Remove all old inline nav CSS (site-nav, nav-container, nav-brand, etc.)
3. Fix broken JS (stray e.target.value reference in diapers init)
4. Update footer to match strollers style (footer-container wrapper)
"""

import re

NAV_LINK = '  <link rel="stylesheet" href="css/nav.css">\n'

# CSS class names belonging to nav - remove these inline blocks
NAV_CSS_PATTERNS = [
    r'\.site-nav\s*\{[^}]*\}',
    r'\.site-nav\s+\.nav-container\s*\{[^}]*\}',
    r'\.site-nav\s+\.nav-brand\s*\{[^}]*\}',
    r'\.site-nav\s+\.nav-brand:hover\s*\{[^}]*\}',
    r'\.site-nav\s+\.nav-links\s*\{[^}]*\}',
    r'\.site-nav\s+\.nav-links::-webkit-scrollbar\s*\{[^}]*\}',
    r'\.site-nav\s+\.nav-link\s*\{[^}]*\}',
    r'\.site-nav\s+\.nav-link:hover\s*\{[^}]*\}',
    r'\.site-nav\s+\.nav-link\.active\s*\{[^}]*\}',
    r'\.site-nav\s+\.nav-link\s+\.nav-emoji\s*\{[^}]*\}',
    r'\.nav-container\s*\{[^}]*\}',
    r'\.nav-brand\s*\{[^}]*\}',
    r'\.nav-links\s*\{[^}]*\}',
    r'\.nav-link\s*\{[^}]*\}',
    r'\.nav-link:hover\s*\{[^}]*\}',
    r'\.nav-link\.active\s*\{[^}]*\}',
    r'\.nav-pickers\s*\{[^}]*\}',
    r'\.nav-picker\s*\{[^}]*\}',
    r'\.nav-picker\s+option\s*\{[^}]*\}',
    r'\.nav-country-picker\s*\{[^}]*\}',
    r'\.nav-country-picker:hover\s*\{[^}]*\}',
    r'\.nav-lang-picker\s*\{[^}]*\}',
    r'\.nav-lang-picker:hover\s*\{[^}]*\}',
    r'\.nav-hamburger\s*\{[^}]*\}',
    r'\.nav-hamburger\s+span\s*\{[^}]*\}',
    r'\.nav-hamburger\.active\s+span:nth-child\(1\)\s*\{[^}]*\}',
    r'\.nav-hamburger\.active\s+span:nth-child\(2\)\s*\{[^}]*\}',
    r'\.nav-hamburger\.active\s+span:nth-child\(3\)\s*\{[^}]*\}',
    r'\.nav-mobile\s*\{[^}]*\}',
    r'\.nav-mobile\.open\s*\{[^}]*\}',
    r'\.nav-mobile\s+\.nav-link\s*\{[^}]*\}',
    # Remove scrollbar hidden rule for nav
    r'\.site-nav\s+\.nav-links::-ms-overflow-style[^;]*;',
    r'scrollbar-width:\s*none;\s*-ms-overflow-style:\s*none;',
]

# Standard footer HTML for product pages
def make_footer(active_page):
    pages = [
        ('index.html', '🚼', 'Strollers'),
        ('diapers.html', '🧷', 'Diapers'),
        ('formula.html', '🍼', 'Formula'),
        ('wipes.html', '🧻', 'Wipes'),
        ('bottles.html', '🍶', 'Bottles'),
        ('breast-pumps.html', '🤱', 'Pumps'),
        ('baby-monitors.html', '📹', 'Monitors'),
        ('checklist.html', '✅', 'Checklist'),
        ('blog/index.html', '📝', 'Blog'),
    ]
    links = []
    for url, emoji, label in pages:
        active = ' active' if url == active_page else ''
        links.append(f'      <a href="{url}" class="footer-link{active}"><span class="nav-emoji">{emoji}</span> {label}</a>')

    return f'''  <footer class="site-footer">
    <div class="footer-container">
      <nav class="footer-nav">
{chr(10).join(links)}
      </nav>
      <div class="footer-divider"></div>
      <div class="footer-brand">🍼 True Baby Cost</div>
      <p class="footer-info">Real prices. No surprises. © 2026 True Baby Cost</p>
    </div>
  </footer>'''


pages = {
    'diapers.html': 'diapers.html',
    'formula.html': 'formula.html',
    'bottles.html': 'bottles.html',
    'wipes.html': 'wipes.html',
    'breast-pumps.html': 'breast-pumps.html',
    'baby-monitors.html': 'baby-monitors.html',
}

for filename, active_page in pages.items():
    path = f'/root/clawd/stroller-app/{filename}'
    html = open(path).read()
    original = html

    # 1. Add nav.css link if not present
    if 'css/nav.css' not in html:
        html = html.replace('</head>', f'{NAV_LINK}</head>', 1)
        print(f'  + Added nav.css link to {filename}')

    # 2. Remove inline nav CSS blocks (all patterns)
    for pattern in NAV_CSS_PATTERNS:
        html = re.sub(pattern, '', html, flags=re.DOTALL)

    # 3. Clean up leftover empty comment markers and excess blank lines
    html = re.sub(r'\n{4,}', '\n\n', html)

    # 4. Fix diapers broken JS: stray `if (typeof ProductI18n !== "undefined") { await ProductI18n.changeLanguage(e.target.value); }`
    # outside of event handler (e is not defined there)
    html = re.sub(
        r'\n\s*if \(typeof ProductI18n !== "undefined"\) \{ await ProductI18n\.changeLanguage\(e\.target\.value\); \}\n',
        '\n',
        html
    )

    # 5. Replace footer with strollers-style footer
    footer = make_footer(active_page)
    html = re.sub(r'<footer class="site-footer">.*?</footer>', footer, html, flags=re.DOTALL)

    if html != original:
        open(path, 'w').write(html)
        print(f'✓ Updated {filename}')
    else:
        print(f'- No changes in {filename}')

print('\n✅ All product pages updated!')
