"""Read every canonical sensor payload and retain corruption evidence."""
from __future__ import annotations
import argparse
from concurrent.futures import ThreadPoolExecutor
from dataclasses import replace
import hashlib
import json
from pathlib import Path
import numpy as np

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


def validate(protocol,output,workers=4,limit=None):
    records,_,_=load_protocol(protocol)
    output=Path(output);output.mkdir(parents=True,exist_ok=True)
    path=output/'payloads.jsonl';done={}
    protocol_sha256 = file_digest(Path(protocol) / 'protocol.json')
    # Grid restoration and the audit's validity rules are part of decoding too.
    decoder_sha256 = hashlib.sha256(':'.join((decoder_digest(),
        file_digest(Path(__file__).with_name('manifest.py')),
        file_digest(__file__))).encode()).hexdigest()
    identities = {r.frame_id: {'source_sha256': r.source_sha256,
                  'protocol_sha256': protocol_sha256,
                  'policy_sha256': policy_digest(r.sensor_config),
                  'decoder_sha256': decoder_sha256} for r in records}
    if path.exists():
        for line in path.read_text().splitlines():
            try:
                row = json.loads(line)
            except json.JSONDecodeError:
                # A killed append may leave an incomplete final record.
                continue
            expected = identities.get(row.get('frame_id'))
            if expected and all(row.get(key) == value for key, value in expected.items()):
                done[row['frame_id']] = row
    pending=[r for r in records if r.frame_id not in done]
    if limit is not None:pending=pending[:limit]
    def one(record):
        result={'frame_id':record.frame_id, **identities[record.frame_id]}
        try:
            frame=load_frame(replace(record, cache_path=None))
            result.update(status='valid',shape=list(frame.shape),
                          valid_depth_fraction=float(frame[...,2].mean()),
                          valid_intensity_fraction=float(frame[...,3].mean()),
                          intensity_nonconstant=bool(np.ptp(frame[...,1])>0))
        except (ValueError,OSError,RuntimeError) as exc:
            result.update(status='damaged',error=str(exc),pcd=str(record.pcd))
        return result
    with path.open('a') as stream,ThreadPoolExecutor(max_workers=workers) as pool:
        stream.write('\n')
        for index,result in enumerate(pool.map(one,pending),1):
            stream.write(json.dumps(result)+'\n');done[result['frame_id']]=result
            stream.flush()
            if index%64==0:print(f'validated {len(done)}/{len(records)}',flush=True)
    failures=[r for r in done.values() if r['status']!='valid']
    summary={'protocol_sha256':protocol_sha256,'decoder_sha256':decoder_sha256,'frames':len(done),
             'expected_frames':len(records),'complete':len(done)==len(records),
             'valid':len(done)-len(failures),'failures':failures}
    if summary['complete']:
        summary['validity_index_sha256']=hashlib.sha256(json.dumps(
            [done[fid] for fid in sorted(done)], sort_keys=True).encode()).hexdigest()
    write_json(output/'summary.json',summary)
    return summary


if __name__=='__main__':
    parser=argparse.ArgumentParser();parser.add_argument('--protocol',default='outputs/protocol_v4')
    parser.add_argument('--output',default='outputs/payload_audit_v4');parser.add_argument('--workers',type=int,default=4)
    parser.add_argument('--limit',type=int)
    args=parser.parse_args();r=validate(args.protocol,args.output,args.workers,args.limit)
    print(json.dumps({'frames':r['frames'],'valid':r['valid'],'damaged':len(r['failures'])}))
