"""Serve the restored four-image gallery from versioned inline assets or local reviewed files."""
from pathlib import Path
import json,base64,hashlib
from lxml import html
OUT=Path(__file__).resolve().parent;ROOT=OUT.parents[1]
def load_gallery_assets():
 manifest=json.loads((OUT/'before-after-image-manifest.json').read_text())
 existing=ROOT/'AI_AGENT_PROJECT_INTRO_FOR_BEGINNERS.html'
 document=html.fromstring(existing.read_text()) if existing.exists() else None
 assets={}
 for row in manifest['images']:
  path=OUT/row['display_file'];mime='image/jpeg' if path.suffix=='.jpg' else 'image/png'
  if path.exists():data=path.read_bytes()
  else:
   matches=document.xpath('//img[@data-before-after-id="'+row['id']+'"]/@src') if document is not None else []
   if not matches:raise RuntimeError('Missing reviewed before/after image: '+row['id'])
   data=base64.b64decode(matches[0].split(',',1)[1])
  assert hashlib.sha256(data).hexdigest()==row['display_sha256'],row['id']
  assets[row['id']]='data:'+mime+';base64,'+base64.b64encode(data).decode()
 return assets
