"""Report actual data, review and training progress without mutating frozen labels."""
from __future__ import annotations

from collections import Counter
import json
from pathlib import Path

from pallet.annotation import load_jsonl,is_resolved
from pallet.data.protocol import file_digest,load_protocol,write_json


def report(output='outputs/progress_v4.json'):
    records,_,protocol=load_protocol('outputs/protocol_v5')
    latest=json.loads(Path('outputs/review_v3/latest.json').read_text())
    labels=load_jsonl(latest['annotation_path'])
    counts={split:Counter() for split in ('train','val','test')}
    for frame in labels.frames.values():
        status=frame.status if is_resolved(frame,require_blind=frame.split in {'val','test'}) else 'unresolved'
        counts[frame.split][status]+=1
    pilot=json.loads(Path('outputs/protocol_v5/pilot.json').read_text())
    pilot_counts={split:Counter(labels.frames[fid].status if is_resolved(labels.frames[fid],require_blind=split in {'val','test'})
                                  else 'unresolved' for fid in ids) for split,ids in pilot.items()}
    review_counts={}
    for side in ('a','b','c'):
        review_counts[side]={}
        for split in ('train','val','test'):
            files=list(Path('outputs/review_v3/reviews').glob(f'{side}_{split}_*.json'))
            review_counts[side][split]=sum(len(json.loads(p.read_text())['frames']) for p in files)
    runs=[]
    for marker in Path('outputs').glob('**/completed.json'):
        status=json.loads(marker.read_text())
        if not (marker.parent/'run.json').exists():continue
        run=json.loads((marker.parent/'run.json').read_text())
        result={'run':str(marker.parent),'epochs':status['completed_epochs'],
                'reason':status['stopped_reason'],'label_sha256':run.get('annotation_sha256')}
        for split in ('val','test'):
            path=marker.parent/f'{split}.json'
            if path.exists():
                values=json.loads(path.read_text())
                result[split]={key:values.get(key) for key in ('AP50','AP75','AP50_95','images','objects','reference_coverage')}
        runs.append(result)
    inventory=json.loads(Path('outputs/inventory_v3/summary.json').read_text())
    result={'protocol':protocol,'protocol_sha256':file_digest('outputs/protocol_v5/protocol.json'),
            'annotation_path':latest['annotation_path'],'annotation_sha256':file_digest(latest['annotation_path']),
            'annotation_counts':{k:dict(v) for k,v in counts.items()},
            'pilot_counts':{k:dict(v) for k,v in pilot_counts.items()},
            'independent_review_counts':review_counts,'ingest_errors':latest['errors'],
            'pending_adjudication':latest['pending_adjudication'],
            'raw_inventory':{k:inventory[k] for k in ('files','archive_member_rows','classifications_by_kind','usable_new_frame_candidates')},
            'completed_runs':runs,'full_research_complete':False,
            'reference':'machine-reviewed references; no human-ground-truth claim'}
    write_json(output,result)
    return result


if __name__=='__main__':
    r=report();print(json.dumps({k:r[k] for k in ('pilot_counts','pending_adjudication','completed_runs')},ensure_ascii=False))
