from pathlib import Path
import json,re
r=Path(__file__).resolve().parents[1]
curated=json.loads((r/'translations/fa-foundations.json').read_text())
for slug,d in curated.items():
 p=r/'content/fa/anatomy'/f'{slug}.mdx';s=p.read_text();parts=s.split('---',2);body=parts[2]
 # Intro ends at the first interactive educational component.
 boundary=re.search(r'<(?:Diagram|EcgMonitor)\b',body)
 assert boundary,slug
 body='\n\n'+d['intro'].strip()+'\n\n'+body[boundary.start():]
 blocks=iter(d['patient']);assert len(re.findall(r'<Patient>',body))==len(d['patient']),slug
 body=re.sub(r'<Patient>[\s\S]*?</Patient>',lambda m:'<Patient>\n\n'+next(blocks).strip()+'\n\n</Patient>',body)
 s='---'+parts[1]+'---'+body
 p.write_text(s)
print('Curated foundational articles:',len(curated))
