"""Apply SEO fixes: add offers + remove HowTo on bespoke pages."""
import sys, io, os, re
sys.stdout = io.TextIOWrapper(sys.stdout.buffer, encoding='utf-8')

configs = [
    ('app/(public)/societes/creation-de-societe/page.tsx', '650', 'Création de société'),
    ('app/(public)/societes/depot-des-comptes/page.tsx', '295', 'Dépôt des comptes annuels'),
    ('app/(public)/societes/transfert-de-siege-social/page.tsx', '600', 'Transfert de siège'),
    ('app/(public)/marques/depot-de-marques/page.tsx', '340', 'Dépôt de marque'),
]

for fp, price, name in configs:
    if not os.path.exists(fp):
        print(f'SKIP {fp}')
        continue
    with open(fp, 'r', encoding='utf-8') as f:
        content = f.read()

    original = content

    # 1. Ajout offers
    head = content[:content.find('faqSchema') if 'faqSchema' in content else len(content)]
    if 'offers:' not in head:
        match = re.search(
            r'(const serviceSchema = \{(?:[^{}]|\{[^{}]*\})*?url: PAGE_URL),\n(\};)',
            content, re.DOTALL
        )
        if match:
            offers = (
                ',\n'
                '  offers: {\n'
                '    "@type": "Offer",\n'
                f'    name: "{name}",\n'
                f'    price: "{price}",\n'
                '    priceCurrency: "EUR",\n'
                '    url: `${PAGE_URL}/formulaire`,\n'
                '    availability: "https://schema.org/InStock",\n'
                '  },\n'
            )
            content = content[:match.start(2)] + offers + content[match.start(2):]
            print(f'OK offers {fp}')

    # 2. Retire bloc howToSchema (avec accolades imbriquées)
    new_content = re.sub(
        r'const howToSchema = \{(?:[^{}]|\{[^{}]*\})*\};\n+',
        '',
        content,
        flags=re.DOTALL,
    )
    if new_content != content:
        content = new_content
        print(f'OK howTo retiré {fp}')

    if content != original:
        with open(fp, 'w', encoding='utf-8') as f:
            f.write(content)
