from pathlib import Path
import runpy, re
from bs4 import BeautifulSoup, NavigableString

# Load replacements from previous script without executing its write block by parsing the dict literal is overkill;
# instead execute and reuse by importing the module variables (it rewrites v1 as a side effect, acceptable in outbound).
ns = runpy.run_path('/Users/iggy/.hermes/profiles/ignite_team/outbound/st-catherines-copy-map.py')
replacements = ns['replacements']

src = Path('/Users/iggy/.hermes/profiles/ignite_team/outbound/zeekr-original.html')
out = Path('/Users/iggy/.hermes/profiles/ignite_team/outbound/st-catherines-template-text-only.html')
html = src.read_text(errors='ignore')

# First update exact content strings in the embedded page data so the hydrated page keeps the new copy.
# Skip very short/generic terms that could plausibly appear in CSS or code internals.
skip_global = {
    'Models','Offers','Exterior','Interior','Technology','Space','Safety','Specs','Performance','Range','Battery',
    'Single','Dual','Black','Silver','Privacy Policy','Cookie Policy','Australia','English','News','Services','RMI'
}
changed_global = []
for old in sorted(replacements, key=len, reverse=True):
    if old in skip_global or len(old) < 8:
        continue
    if old in html:
        html = html.replace(old, replacements[old])
        changed_global.append(old)

soup = BeautifulSoup(html, 'html.parser')
head = soup.find('head')
if head and not head.find('base'):
    base = soup.new_tag('base', href='https://www.zeekrlife.com/')
    head.insert(0, base)

# Then update any rendered text nodes still present after the global pass. This catches nav labels and short metrics.
changed_nodes = []
for node in soup.find_all(string=True):
    if not isinstance(node, NavigableString):
        continue
    if node.parent and node.parent.name in {'script','style','noscript','code','pre'}:
        continue
    text = str(node)
    stripped = text.strip()
    if stripped in replacements:
        node.replace_with(text.replace(stripped, replacements[stripped]))
        changed_nodes.append(stripped)

if soup.title:
    soup.title.string = 'St Catherine’s School | Concept Page'
for meta in soup.find_all('meta'):
    if meta.get('name') in {'description','twitter:title','twitter:description'} or meta.get('property') in {'og:title','og:description'}:
        val = meta.get('content')
        if val:
            meta['content'] = val.replace('The New Zeekr X', 'St Catherine’s School').replace('Zeekr X', 'St Catherine’s')

out.write_text(str(soup), encoding='utf-8')
print(f'wrote {out}')
print(f'global replacements: {len(changed_global)}')
print(f'text-node replacements: {len(changed_nodes)}')
# quick counts for major replaced copy in output
content = out.read_text(errors='ignore')
print('contains Zeekr X:', 'Zeekr X' in content, 'contains St Catherine:', 'St Catherine' in content)
print('file bytes:', out.stat().st_size)
