"""Native-coordinate, blind review batches with source and task fingerprints."""
from __future__ import annotations

import argparse
from dataclasses import replace
import hashlib
import json
from pathlib import Path
import shutil

import numpy as np
from PIL import Image

from pallet.annotation import load_jsonl
from pallet.data.loader import load_frame, cache_frame, policy_digest
from pallet.data.protocol import load_protocol, file_digest, write_json


def display(values, valid, *, log=False, invert=False):
    valid = (valid > 0) & np.isfinite(values)
    if not valid.any():
        return np.zeros(values.shape, np.uint8)
    data = np.log1p(np.maximum(values, 0)) if log else values.copy()
    low, high = np.percentile(data[valid], [1, 99])
    scaled = np.clip((data-low)/max(high-low, 1e-8), 0, 1)
    if invert:
        scaled = 1-scaled
    return np.where(valid, scaled*255, 0).astype(np.uint8)


def prepare_batches(protocol_dir, annotations_path, output, *, batch_size=20,
                    selection_path=None, task_offset=0, policy_file=None,
                    sealed_cache_index=None):
    records, _, protocol = load_protocol(protocol_dir)
    by_id = {r.frame_id: r for r in records}
    annotations = load_jsonl(annotations_path)
    selection = json.loads(Path(selection_path or Path(protocol_dir)/'pilot.json').read_text())
    from pallet.data.eligibility import require_eligible
    for split in ('train', 'val', 'test'):
        for fid in selection.get(split, []):
            require_eligible(by_id[fid])
    sealed = json.loads(Path(sealed_cache_index).read_text()) if sealed_cache_index else None
    policy = None
    if policy_file:
        policy_path = Path(policy_file).resolve()
        policy = {'path': str(policy_path), 'sha256': file_digest(policy_path)}
        examples = policy_path.parent/'assets'/policy_path.stem/'manifest.json'
        if examples.exists():
            policy['examples_manifest_sha256'] = file_digest(examples)
    output = Path(output).resolve()
    output.mkdir(parents=True, exist_ok=True)
    cache_index = json.loads((output/'cache_index.json').read_text()) if (output/'cache_index.json').exists() else {}
    errors = []
    for split in ('val', 'test', 'train'):
        if split not in selection:
            continue
        tasks = []
        for frame_id in selection.get(split,[]):
            row = by_id[frame_id]
            frame = annotations.frames[frame_id]
            key = hashlib.sha256(frame_id.encode()).hexdigest()[:20]
            directory = output/'views'/key
            directory.mkdir(parents=True, exist_ok=True)
            try:
                if shutil.disk_usage(output).free < 50*2**30:
                    raise RuntimeError('below 50 GiB disk reserve')
                if sealed is not None:
                    entry = sealed[frame_id]
                    cached = replace(row, cache_path=entry['path'], cache_sha256=entry['sha256'])
                else:
                    cached = cache_frame(row, output/'cache'/f'{row.source_sha256}-{policy_digest(row.sensor_config)[:12]}.npz')
                data, metadata = load_frame(cached, return_metadata=True)
                if data.shape[:2] != (frame.height, frame.width):
                    raise ValueError('decoded sensor grid differs from frozen annotation dimensions')
                cache_index[frame_id] = cached.cache_path
                intensity = directory/'intensity.png'
                depth = directory/'depth.png'
                masks = directory/'validity.png'
                Image.fromarray(display(data[...,1], data[...,3], log=True)).save(intensity)
                Image.fromarray(display(data[...,0], data[...,2], invert=True)).save(depth)
                Image.fromarray((np.stack((data[...,2],data[...,3],np.zeros(data.shape[:2])),axis=-1)*255).astype(np.uint8)).save(masks)
                task = {'frame_id': frame_id, 'source_anchor_hash': row.source_sha256,
                        'group_id': row.group_id, 'split': split, 'domain': row.domain,
                        'width': frame.width, 'height': frame.height,
                        'intensity': str(intensity), 'depth': str(depth), 'validity': str(masks),
                        'depth_available': bool(data[...,2].any()),
                        'views_sha256': {p.name:file_digest(p) for p in (intensity,depth,masks)},
                        'rgb_side_evidence': str(row.paths['rgb']) if 'rgb' in row.paths else None}
                tasks.append(task)
            except Exception as exc:
                errors.append({'frame_id':frame_id,'error':str(exc)})
            if len(tasks) and (len(tasks)%batch_size == 0 or frame_id == selection[split][-1]):
                start = ((len(tasks)-1)//batch_size)*batch_size
                batch = tasks[start:]
                for i,item in enumerate(batch):
                    item['index'] = i
                name=f'{split}_{start//batch_size+task_offset:03d}'
                instructions = ('Annotation policy v3: native pixel xywh; top edge at the visible pallet deck, bottom edge at the lowest visible foot or runner, left/right at the pallet body. Exclude cargo, floor/shadow, fork tines, fixed racks and conveyors. Separately box discernible stack layers. Use depth only where valid. Negative only when the whole view clearly lacks a pallet; uncertain identity or boundary requires ignore with evidence. Review each original single view independently; never copy boxes from a contact sheet or another frame. Do not view proposals.'
                                if policy is None else
                                f'Follow {policy["path"]} (sha256={policy["sha256"]}). Review each original native sensor view independently in pixel xywh. Do not view other reviews or model predictions.')
                task_record = {
                    'task_id':name,'protocol_sha256':file_digest(Path(protocol_dir)/'protocol.json'),
                    'instructions':instructions, 'frames':batch}
                if policy is not None:
                    task_record['annotation_policy'] = policy
                write_json(output/'tasks'/f'{name}.json', task_record)
                print(f'ready {name}: {len(batch)} frames',flush=True)
        write_json(output/f'{split}_index.json',tasks)
    write_json(output/'cache_index.json',cache_index)
    write_json(output/'errors.json',errors)
    if sealed_cache_index:
        write_json(output/'cache_source.json',{'sealed_index':str(Path(sealed_cache_index).resolve()),
                                                'sha256':file_digest(sealed_cache_index)})
    return {'frames':len(cache_index),'errors':errors}


if __name__=='__main__':
    parser=argparse.ArgumentParser()
    parser.add_argument('--protocol',default='outputs/protocol_v3')
    parser.add_argument('--annotations',default='outputs/protocol_v3/annotations_unreviewed.jsonl')
    parser.add_argument('--output',default='outputs/review_v3')
    parser.add_argument('--batch-size',type=int,default=20)
    parser.add_argument('--selection')
    parser.add_argument('--task-offset',type=int,default=0)
    parser.add_argument('--policy-file')
    parser.add_argument('--sealed-cache-index')
    args=parser.parse_args()
    print(json.dumps(prepare_batches(args.protocol,args.annotations,args.output,batch_size=args.batch_size,
                                     selection_path=args.selection,task_offset=args.task_offset,
                                     policy_file=args.policy_file,
                                     sealed_cache_index=args.sealed_cache_index)))
