"""Inventory of every saved page under raw/sites-*: which manifest references it (capture, probe attempt, supplement) and which
files are superseded (kept on disk for the record, referenced by no manifest, for example the first probe run's files that the
per-attempt run replaced). Writes data/file-inventory-<date>.json and prints the totals the SCOPE quotes.
Usage: python3 scripts/inventory.py 2026-09-02"""
import json,glob,os,hashlib,sys,datetime
date=sys.argv[1]
ref={}
for c in ('cohort1','cohort2','cohort3','appendix'):
    for kind,fn in (('capture',f'data/capture-{c}-{date}.json'),('probe',f'data/profiles-probe-{c}-{date}.json'),('supplement',f'data/supplement-{c}-{date}.json'),('signature',f'data/signatures-{c}-{date}.json')):
        if not os.path.exists(fn): continue
        d=json.load(open(fn)); rows=d['rows'] if isinstance(d,dict) else d
        for r in rows:
            if r.get('saved'): ref.setdefault(os.path.normpath(r['saved']),set()).add(f'{kind}:{c}')
            for a in r.get('attempts',[]):
                if a.get('saved'): ref.setdefault(os.path.normpath(a['saved']),set()).add(f'probe-attempt:{c}')
files=sorted(glob.glob('raw/sites-*/**/*.html',recursive=True))
inv=[]
for f in files:
    fb=open(f,'rb').read(); k=os.path.normpath(f)
    inv.append({'file':f,'bytes':len(fb),'sha256':hashlib.sha256(fb).hexdigest(),'mtime_utc':datetime.datetime.fromtimestamp(os.path.getmtime(f),datetime.UTC).strftime('%Y-%m-%dT%H:%M:%SZ'),'referenced_by':sorted(ref.get(k,[])),'status':('referenced' if k in ref else 'superseded, referenced by no manifest (kept on disk for the record)')})
json.dump({'date':date,'files':inv,'referenced':sum(1 for x in inv if x['referenced_by']),'superseded':sum(1 for x in inv if not x['referenced_by'])},open(f'data/file-inventory-{date}.json','w'),indent=1)
print('saved pages on disk',len(inv),'| referenced by a manifest',sum(1 for x in inv if x['referenced_by']),'| superseded',sum(1 for x in inv if not x['referenced_by']))
for x in inv:
    if not x['referenced_by']: print('   superseded:',x['file'])
