"""Create a static, unassigned scan-to-biological-sample inventory."""
import argparse
import json
from pathlib import Path
from ingest_review import digest


def main():
    p=argparse.ArgumentParser(description=__doc__);p.add_argument('--scans',type=Path,required=True);p.add_argument('--output',type=Path,required=True);a=p.parse_args()
    if not a.scans.is_dir():raise ValueError('Scans directory not found')
    entries=[]
    for path in sorted(a.scans.iterdir(),key=lambda x:x.name):
        if path.is_file():
            size=path.stat().st_size
            if size>=1_000_000_000:entries.append({'name':path.name,'size_bytes':size})
    if not entries:raise ValueError('No files at least 1 GB found in scans directory')
    if a.output.exists() and any(a.output.iterdir()):raise FileExistsError('Output directory must be empty')
    a.output.mkdir(parents=True,exist_ok=True);inventory={'format':'diatom-scan-inventory-v1','minimum_size_bytes':1_000_000_000,'slides':entries,
        'warning':'Filename and size are inventory metadata, not file hashes, copy-completion proof, or biological sample identities.'}
    file=a.output/'inventory.json';file.write_text(json.dumps(inventory,indent=2)+'\n');payload={'inventory':inventory,'inventory_sha256':digest(file)}
    template=Path(__file__).with_name('sample_registry.html').read_text();embedded=json.dumps(payload,ensure_ascii=True,separators=(',',':')).replace('</','<\\/')
    if template.count('INVENTORY_PAYLOAD')!=1:raise ValueError('Invalid sample registry template')
    (a.output/'index.html').write_text(template.replace('INVENTORY_PAYLOAD',embedded));print(f'Prepared {len(entries)} unassigned scans; no biological sample identities inferred.')


if __name__=='__main__':main()
