#!/usr/bin/env python3
import re

files = {
    'formula.html': {
        'product': 'formula',
        'hero_title': 'The $2,500 Formula Decision',
        'hero_subtitle': "Formula feeding? That's $100-200/month for 12 months. Choose wisely—the brand you pick adds up fast."
    },
    'bottles.html': {
        'product': 'bottles',
        'hero_title': 'Baby Bottles — One-Time Purchase',
        'hero_subtitle': "Unlike diapers and formula, bottles are a one-time cost. Here's how to budget for your feeding setup."
    },
    'wipes.html': {
        'product': 'wipes',
        'hero_title': 'The $600 Wipes Decision',
        'hero_subtitle': "Baby wipes seem cheap until you realize you'll use 8,000+ of them. Let's do the math."
    },
    'breast-pumps.html': {
        'product': 'breast-pumps',
        'hero_title': 'Breast Pump True Cost Calculator',
        'hero_subtitle': 'The pump is just the start. Parts, bags, and supplies add up over months of pumping.'
    }
}

for filename, data in files.items():
    filepath = f'/root/clawd/stroller-app/{filename}'
    
    with open(filepath, 'r') as f:
        html = f.read()
    
    # 1. Add i18n scripts before </body>
    if 'product-i18n.js' not in html:
        html = html.replace('</body>', f'  <script>window.productPageType = "{data["product"]}";</script>\n  <script src="js/product-i18n.js"></script>\n</body>')
    
    # 2. Add data-i18n to hero title
    html = re.sub(
        rf'<h1>([^<]*){re.escape(data["hero_title"])}</h1>',
        r'<h1>\1<span data-i18n="hero.title">' + data["hero_title"] + '</span></h1>',
        html
    )
    
    # 3. Add data-i18n to hero subtitle (first <p> in hero section)
    html = re.sub(
        r'(<section class="hero">.*?<p>)([^<]+)(</p>)',
        r'\1<span data-i18n="hero.subtitle">\2</span>\3',
        html,
        flags=re.DOTALL
    )
    
    # 4. Add data-i18n to tabs
    html = html.replace(
        '<button class="tab active" data-tab="calculator">💰 Calculator</button>',
        '<button class="tab active" data-tab="calculator">💰 <span data-i18n="tabs.calculator">Calculator</span></button>'
    )
    html = html.replace(
        '<button class="tab" data-tab="products">🛒 Browse Products</button>',
        '<button class="tab" data-tab="products">🛒 <span data-i18n="tabs.products">Browse Products</span></button>'
    )
    
    # 5. Add ProductI18n.init after TrueBabyCost.init if not already there
    if 'ProductI18n.init' not in html:
        html = re.sub(
            r'(await TrueBabyCost\.init\(\);)',
            r'\1\n      if (typeof ProductI18n !== "undefined" && window.productPageType) { await ProductI18n.init(window.productPageType); }',
            html
        )
    
    # 6. Add ProductI18n.changeLanguage to language selector
    if 'ProductI18n.changeLanguage' not in html:
        html = re.sub(
            r"(TrueBabyCost\.changeLanguage\(e\.target\.value\))",
            r"\1;if(typeof ProductI18n!=='undefined'){await ProductI18n.changeLanguage(e.target.value)}",
            html
        )
    
    with open(filepath, 'w') as f:
        f.write(html)
    
    print(f'✓ Fixed {filename}')

print('\n✅ All files updated!')
