"""Resumable, source-verified float sensor cache for repeated training."""
from __future__ import annotations
import argparse
from concurrent.futures import ThreadPoolExecutor
import json
from pathlib import Path
import shutil

from .loader import cache_frame,load_frame,policy_digest
from .protocol import load_protocol,write_json,file_digest


def cache_all(protocol='outputs/protocol_v5',output='outputs/cache_v5',workers=2,limit=None):
    records,_,identity=load_protocol(protocol)
    output=Path(output);output.mkdir(parents=True,exist_ok=True)
    index_path=output/'index.json'
    index=json.loads(index_path.read_text()) if index_path.exists() else {}
    pending=[row for row in records if row.frame_id not in index or not Path(index[row.frame_id]).exists()]
    if limit is not None:pending=pending[:limit]
    def one(record):
        if shutil.disk_usage(output).free<50*2**30:
            raise RuntimeError('less than 50 GiB free; cache paused')
        key=f'{record.source_sha256}-{policy_digest(record.sensor_config)[:12]}.npz'
        destination=output/'frames'/key
        if destination.exists():
            from dataclasses import replace
            load_frame(replace(record,cache_path=str(destination)))
        else:cache_frame(record,destination)
        return record.frame_id,str(destination.resolve())
    with ThreadPoolExecutor(max_workers=workers) as pool:
        for number,(frame_id,path) in enumerate(pool.map(one,pending),1):
            index[frame_id]=path
            if number%64==0:
                write_json(index_path,index)
                print(f'cached {len(index)}/{len(records)}',flush=True)
    write_json(index_path,index)
    report={'protocol_sha256':file_digest(Path(protocol)/'protocol.json'),
            'indexed_frames':len(index),'expected_frames':len(records),'complete':len(index)==len(records),
            'bytes':sum(p.stat().st_size for p in (output/'frames').glob('*.npz')),
            'index_sha256':file_digest(index_path)}
    write_json(output/'summary.json',report)
    return report


if __name__=='__main__':
    parser=argparse.ArgumentParser();parser.add_argument('--protocol',default='outputs/protocol_v5')
    parser.add_argument('--output',default='outputs/cache_v5');parser.add_argument('--workers',type=int,default=2)
    parser.add_argument('--limit',type=int);args=parser.parse_args()
    print(json.dumps(cache_all(args.protocol,args.output,args.workers,args.limit)))
