"""Portable native-grid AnyLabeling correction workspace; never changes truth."""
from __future__ import annotations

import argparse
from collections import Counter, defaultdict
from concurrent.futures import ThreadPoolExecutor
from dataclasses import asdict, replace
from datetime import datetime, timezone
import hashlib
import json
from pathlib import Path
import shutil
import zipfile

import numpy as np
from PIL import Image

from pallet.annotation import annotation_digest, is_resolved, load_jsonl
from pallet.data.loader import load_frame
from pallet.data.protocol import file_digest, load_protocol, write_json
from pallet.research_log import collect_references
from pallet.review_batches import display


FORMAT_SOURCE = 'https://github.com/vietanhdev/anylabeling/blob/main/anylabeling/views/labeling/label_file.py'
FLAGS = ('human_reviewed', 'confirmed_negative', 'ignore', 'unreviewed',
         'label_conflict', 'proposal_only', 'machine_resolved')


def rectangle(box, label='pallet', *, proposal=False):
    return {'label': label, 'points': [[box.x, box.y],
                                      [box.x + box.width, box.y + box.height]],
            'group_id': None, 'shape_type': 'rectangle',
            'flags': {'proposal_only': proposal}, 'text': ''}


def label_document(frame, image_name, *, resolved, conflict=False):
    """Frame flags distinguish empty unreviewed/ignored views from negatives."""
    frame.validate()
    proposal = not resolved and bool(frame.boxes)
    shapes = [rectangle(b, proposal=proposal) for b in frame.boxes]
    shapes += [rectangle(b, '__ignore__') for b in frame.ignore_regions]
    flags = dict.fromkeys(FLAGS, False)
    flags.update(machine_resolved=resolved and not conflict,
                 confirmed_negative=resolved and frame.status == 'negative' and not conflict,
                 ignore=frame.status == 'ignore',
                 unreviewed=not resolved or conflict, label_conflict=conflict,
                 proposal_only=proposal)
    return {'version': '0.4.43', 'flags': flags, 'shapes': shapes,
            'imagePath': image_name, 'imageData': None,
            'imageHeight': frame.height, 'imageWidth': frame.width,
            'text': '', 'pallet_export': {'frame_id': frame.frame_id,
                'source_status': frame.status, 'scene': asdict(frame.scene),
                'source_anchor_hash': frame.source_anchor_hash,
                'note': 'Historical flags describe export input. human_reviewed is initially false.'}}


def choose_label(base, train_updates, references, proposal=None):
    """No predictions in val/test; reference variants stay explicit on conflict."""
    selected, source = base
    variants = []
    if selected.split in {'val', 'test'}:
        variants = references
        if variants:
            selected, source = variants[0]
        elif not is_resolved(selected, require_blind=True):
            selected = replace(selected, status='unreviewed', boxes=[], ignore_regions=[], reviews=[])
        conflict = len({annotation_digest(f) for f, _ in variants}) > 1
    else:
        conflict = False
        for frame, path in train_updates:
            if is_resolved(frame):
                selected, source = frame, path
        if not is_resolved(selected) and proposal is not None and proposal[0].boxes:
            selected, source = proposal
    resolved = is_resolved(selected, require_blind=selected.split in {'val', 'test'})
    return selected, source, resolved, conflict, variants


def discover_sources(root, protocol):
    """Capture only hash-checked frozen or explicitly ingested label sources."""
    root = Path(root).resolve()
    protocol_hash = file_digest(Path(protocol)/'protocol.json')
    sources = []
    controls = {}

    def control(path):
        path = Path(path).resolve()
        controls[str(path)] = file_digest(path)
        return json.loads(path.read_text())

    def add(path, role, expected=None):
        path = Path(path)
        if not path.is_absolute():
            path = root/path
        path = path.resolve()
        digest = file_digest(path)
        if expected and digest != expected:
            raise ValueError(f'label source fingerprint mismatch: {path}')
        if not any(s['path'] == str(path) for s in sources):
            sources.append({'path': str(path), 'sha256': digest, 'role': role})
        return str(path)

    rounds = []
    for path in sorted((root/'outputs/rounds_v5').glob('round_*/round.json')):
        state = control(path)
        if state['protocol_sha256'] != protocol_hash:
            raise ValueError(f'round protocol mismatch: {path}')
        rounds.append((path, state))
    complete = [(p, s) for p, s in rounds if s['state'] == 'complete']
    if not complete:
        raise ValueError('no completed frozen annotation round')
    parent, state = complete[-1]
    primary = add(state['reviewed_labels'], 'frozen_primary', state['reviewed_labels_sha256'])

    # Completed supplemental batches have their own accepted queue hash binding.
    queues = sorted((root/'outputs/stages').glob('full_review_queue_*/manifest.json'))
    if queues:
        queue = control(queues[-1])
        if queue['protocol_sha256'] != protocol_hash:
            raise ValueError('queue protocol mismatch')
        for path, expected in queue.get('supplemental_labels_sha256', {}).items():
            if 'train' in path:
                add(path, 'train_supplement', expected)

    # Only deltas to the frozen parent are applied; full working snapshots must
    # not undo an independently completed supplemental batch elsewhere.
    for path, state in rounds:
        if state['round_index'] <= complete[-1][1]['round_index']:
            continue
        if state['input_labels_sha256'] != file_digest(primary):
            raise ValueError('unfinished round uses a different frozen parent')
        latest = root/'outputs'/f'review_round_{state["round_index"]:03d}'/'latest.json'
        if latest.exists():
            working = control(latest)
            for dependency, expected in working.get('dependencies', {}).items():
                if dependency.endswith('.jsonl'):
                    dependency_path = Path(dependency)
                    if not dependency_path.is_absolute():
                        dependency_path = root/dependency_path
                    if file_digest(dependency_path) != expected:
                        raise ValueError('ingested review parent changed')
            add(working['annotation_path'], 'working_train_delta')
        proposal = path.parent/'proposals.jsonl'
        if proposal.exists():
            add(proposal, 'train_proposal', state['proposal_labels_sha256'])

    refs, _ = collect_references(root, protocol=protocol)
    for reference in refs:
        if not reference['verified']:
            raise ValueError(f'unverified frozen reference: {reference["path"]}')
        add(reference['path'], 'blind_reference', reference['label_sha256'])
        control(root/reference['path'].replace('.jsonl', '.manifest.json'))
    return primary, sources, controls


def _verify_identity(frame, record, split):
    if (frame.group_id != record.group_id or frame.source_anchor_hash != record.source_sha256
            or frame.split != split or (frame.height, frame.width) != tuple(record.sensor_config['grid'])):
        raise ValueError(f'label identity/grid mismatch: {frame.frame_id}')


def _render(record, frame, directory, key):
    if shutil.disk_usage(directory).free < 50*2**30:
        raise RuntimeError('below 50 GiB disk reserve')
    data = load_frame(record)
    if data.shape[:2] != (frame.height, frame.width):
        raise ValueError('native view differs from annotation grid')
    # Use depth for the editable preview only when native intensity is absent.
    intensity_available = bool(data[..., 3].any())
    primary = (display(data[..., 1], data[..., 3], log=True) if intensity_available
               else display(data[..., 0], data[..., 2], invert=True))
    split = frame.split
    images = directory/'images'/split
    aux = directory/'auxiliary'/split
    images.mkdir(parents=True, exist_ok=True)
    aux.mkdir(parents=True, exist_ok=True)
    paths = [images/f'{key}.png', aux/f'{key}.depth.png', aux/f'{key}.validity.png']
    arrays = [primary, display(data[..., 0], data[..., 2], invert=True),
              (np.stack((data[..., 2], data[..., 3], np.zeros(data.shape[:2])), axis=-1)*255).astype(np.uint8)]
    for path, array in zip(paths, arrays):
        tmp = path.with_name(path.name+'.tmp')
        Image.fromarray(array).save(tmp, format='PNG')
        tmp.replace(path)
    return {'editable_view': 'intensity' if intensity_available else 'depth_fallback',
            'intensity_available': intensity_available, 'depth_available': bool(data[..., 2].any()),
            'files': {str(p.relative_to(directory)): file_digest(p) for p in paths}}


def validate_workspace(output):
    """Check every portable pair and native bounds; allow subsequent GUI edits."""
    output = Path(output)
    manifest_path = output/'manifest.json'
    if not manifest_path.exists():
        manifest_path = output/'package_manifest.json'
    manifest = json.loads(manifest_path.read_text())
    problems = []
    frame_ids, image_paths = set(), set()
    for line in (output/'frames.jsonl').read_text().splitlines():
        item = json.loads(line)
        fid = item['frame_id']
        if fid in frame_ids or item['image'] in image_paths:
            problems.append({'frame_id': fid, 'error': 'duplicate frame/image'})
        frame_ids.add(fid)
        image_paths.add(item['image'])
        try:
            path = output/item['annotation']
            label = json.loads(path.read_text())
            image = path.parent/label['imagePath']
            if image.resolve() != (output/item['image']).resolve():
                raise ValueError('imagePath must reference paired image inside workspace')
            with Image.open(image) as decoded:
                decoded.load()
                if decoded.size != (label['imageWidth'], label['imageHeight']) or decoded.size != tuple(item['size']):
                    raise ValueError('image/annotation dimensions differ')
            for shape in label['shapes']:
                if shape['label'] not in {'pallet', '__ignore__'}:
                    raise ValueError('unexpected class')
                points = np.asarray(shape['points'], dtype=float)
                if shape['shape_type'] != 'rectangle' or points.shape != (2, 2) or not np.isfinite(points).all():
                    raise ValueError('invalid rectangle')
                x1, y1 = points.min(axis=0)
                x2, y2 = points.max(axis=0)
                if not (0 <= x1 < x2 <= label['imageWidth'] and 0 <= y1 < y2 <= label['imageHeight']):
                    raise ValueError('rectangle outside native grid')
        except Exception as error:
            problems.append({'frame_id': fid, 'error': str(error)})
    if len(frame_ids) != manifest['frames']:
        problems.append({'error': 'incomplete canonical frame coverage'})
    result = {'frames_checked': len(frame_ids), 'problems': problems, 'ok': not problems}
    write_json(output/'validation.json', result)
    return result


def export(root, output, *, workers=4):
    root, output = Path(root).resolve(), Path(output).resolve()
    from pallet.data.eligibility import active_policy
    control, _ = active_policy()
    if control and root == Path(__file__).resolve().parents[1]:
        from pallet.clean_intensity_review import safe_copy
        source = Path(control['source_export'])
        if output != source:
            for path in sorted(source.rglob('*')):
                if path.is_file():
                    safe_copy(path, output/path.relative_to(source))
        return json.loads((source/'package_manifest.json').read_text())
    protocol = root/'outputs/protocol_v5'
    records, assignments, _ = load_protocol(protocol)
    from pallet.data.eligibility import eligible_records
    records = eligible_records(records)
    primary, sources, controls = discover_sources(root, protocol)
    sealed_path = root/'outputs/cache_v5/sealed_index.json'
    sealed = json.loads(sealed_path.read_text())
    seal = json.loads((sealed_path.parent/'seal.json').read_text())
    if (file_digest(sealed_path) != seal['index_sha256']
            or file_digest(protocol/'protocol.json') != seal['protocol_sha256']):
        raise ValueError('sealed cache fingerprint/protocol mismatch')
    binding = {'sources': sources, 'controls': controls,
               'protocol_sha256': file_digest(protocol/'protocol.json'),
               'sealed_index_sha256': file_digest(sealed_path),
               'exporter_sha256': file_digest(__file__)}
    binding_hash = hashlib.sha256(json.dumps(binding, sort_keys=True).encode()).hexdigest()
    output.mkdir(parents=True, exist_ok=True)
    state_path = output/'export_state.json'
    if state_path.exists():
        if json.loads(state_path.read_text())['binding_sha256'] != binding_hash:
            raise ValueError('export source changed: use a new output version')
    elif any(output.iterdir()):
        raise ValueError('output must be empty or a matching resumable workspace')
    else:
        write_json(state_path, {'binding_sha256': binding_hash, 'binding': binding, 'state': 'rendering'})

    base = load_jsonl(primary).frames
    if set(base) != {r.frame_id for r in records}:
        raise ValueError('primary snapshot does not cover canonical frames')
    updates, refs, proposals = defaultdict(list), defaultdict(list), {}
    for source in sources:
        path, role = source['path'], source['role']
        if role == 'frozen_primary':
            continue
        for fid, frame in load_jsonl(path).frames.items():
            if fid not in base:
                raise ValueError('noncanonical label source frame')
            if role == 'blind_reference':
                refs[fid].append((frame, path))
            elif frame.split == 'train':
                if role == 'train_proposal':
                    proposals[fid] = (frame, path)
                elif (role != 'working_train_delta'
                      or annotation_digest(frame) != annotation_digest(base[fid])):
                    if is_resolved(frame):
                        updates[fid].append((frame, path))

    # Keep original label/review payloads available after GUI saves extra fields.
    copied = {}
    for index, source in enumerate(sources):
        relative = f'provenance/sources/{index:02d}_{source["role"]}.jsonl'
        target = output/relative
        target.parent.mkdir(parents=True, exist_ok=True)
        if not target.exists():
            shutil.copyfile(source['path'], target)
        if file_digest(target) != source['sha256']:
            raise ValueError('immutable source snapshot modified')
        copied[source['path']] = relative

    jobs, frame_rows, conflicts, counts = [], [], [], Counter()
    for record in records:
        fid = record.frame_id
        split = assignments[record.group_id]
        frame, source, resolved, conflict, variants = choose_label(
            (base[fid], primary), updates[fid], refs[fid], proposals.get(fid))
        _verify_identity(frame, record, split)
        for variant, _ in variants:
            _verify_identity(variant, record, split)
        key = hashlib.sha256(fid.encode()).hexdigest()[:24]
        image = f'images/{split}/{key}.png'
        annotation = f'images/{split}/{key}.json'
        document = label_document(frame, f'{key}.png', resolved=resolved, conflict=conflict)
        path = output/annotation
        # Baseline receipts precede rendering, so a render interruption does not
        # leave thousands of untracked JSON files or overwrite human edits.
        receipt = output/'provenance/receipts'/f'{key}.json'
        baseline = output/'provenance/baselines'/f'{key}.json'
        expected_json_hash = hashlib.sha256(json.dumps(document, indent=2, ensure_ascii=False).encode()).hexdigest()
        if not baseline.exists():
            if path.exists() and file_digest(path) != expected_json_hash:
                raise ValueError('untracked edited annotation exists; refusing to overwrite')
            if not path.exists():
                write_json(path, document)
            write_json(baseline, {'baseline_json_sha256': expected_json_hash})
        baseline_hash = json.loads(baseline.read_text())['baseline_json_sha256']
        if baseline_hash != expected_json_hash:
            raise ValueError('annotation baseline does not match bound source')
        if not path.exists():
            raise ValueError('tracked annotation deleted; refusing to silently recreate')
        state = ('conflict' if conflict else frame.status if resolved else
                 'proposal' if frame.boxes else 'unreviewed')
        counts[(split, state)] += 1
        row = {'frame_id': fid, 'group_id': record.group_id, 'domain': record.domain,
               'split': split, 'state': state, 'source_status': frame.status,
               'machine_resolved': resolved and not conflict,
               'image': image, 'annotation': annotation, 'size': [frame.width, frame.height],
               'depth': f'auxiliary/{split}/{key}.depth.png',
               'validity': f'auxiliary/{split}/{key}.validity.png',
               'source_anchor_hash': record.source_sha256, 'source_labels': copied[source],
               'source_annotation_digest': annotation_digest(frame),
               'baseline_json_sha256': baseline_hash,
               'reference_variants': [{'source_labels': copied[p],
                   'digest': annotation_digest(f), 'status': f.status,
                   'boxes_xywh': [asdict(b) for b in f.boxes],
                   'ignore_regions_xywh': [asdict(b) for b in f.ignore_regions]} for f, p in variants]}
        if conflict:
            conflicts.append(row)
        entry = sealed[fid]
        cached = replace(record, cache_path=entry['path'], cache_sha256=entry['sha256'])
        jobs.append((cached, frame, key, receipt, row))
        frame_rows.append(row)

    def one(job):
        record, frame, key, receipt, row = job
        if receipt.exists():
            saved = json.loads(receipt.read_text())
            if all((output/p).exists() and file_digest(output/p) == digest
                   for p, digest in saved['files'].items()):
                return saved
        rendered = _render(record, frame, output, key)
        rendered['baseline_json_sha256'] = row['baseline_json_sha256']
        write_json(receipt, rendered)
        return rendered

    with ThreadPoolExecutor(max_workers=workers) as pool:
        for index, rendered in enumerate(pool.map(one, jobs), 1):
            frame_rows[index-1].update(rendered)
            if index % 256 == 0 or index == len(jobs):
                print(f'exported {index}/{len(jobs)} native frames', flush=True)
    # Source bindings must still match after the long rendering operation.
    for path, digest in controls.items():
        if file_digest(path) != digest:
            raise ValueError('source control changed during export; create new version')
    for source in sources:
        if file_digest(source['path']) != source['sha256']:
            raise ValueError('label source changed during export')
    (output/'frames.jsonl').write_text(''.join(json.dumps(row, ensure_ascii=False)+'\n' for row in frame_rows))
    write_json(output/'conflicts.json', conflicts)
    manifest = {'created_at_utc': datetime.now(timezone.utc).isoformat(),
        'format': 'AnyLabeling native LabelMe JSON / two-corner rectangles',
        'format_source': FORMAT_SOURCE, 'frames': len(frame_rows),
        'split_counts': dict(Counter(row['split'] for row in frame_rows)),
        'states': dict(Counter(row['state'] for row in frame_rows)),
        'split_states': {s: {state: n for (split, state), n in counts.items() if split == s}
                         for s in ('train', 'val', 'test')},
        'binding_sha256': binding_hash, 'binding': binding,
        'source_copies': copied, 'human_reviewed': 0,
        'frozen_truth_modified': False,
        'not_exported': 'Noncanonical damaged/unverified files: see excluded_source_records.json',
        'frames_index_sha256': file_digest(output/'frames.jsonl')}
    write_json(output/'manifest.json', manifest)
    excluded = []
    for path in (root/'outputs/reconciliation_v5.json', protocol/'excluded.json'):
        if path.exists():
            excluded.append({'source': str(path), 'sha256': file_digest(path),
                             'records': json.loads(path.read_text())})
    write_json(output/'excluded_source_records.json', excluded)
    (output/'labels.txt').write_text('pallet\n__ignore__\n')
    (output/'anylabeling_config.yaml').write_text(
        'language: zh_CN\nstore_data: false\nkeep_prev: false\nauto_save: true\n'
        'labels: [pallet, __ignore__]\nflags:\n' + ''.join(f'  {flag}: false\n' for flag in FLAGS))
    guide = (root/'docs/anylabeling_export.md').read_text()
    (output/'README_人工修正.md').write_text(guide)
    result = validate_workspace(output)
    if not result['ok']:
        raise ValueError(f'export validation failed: {result["problems"][:3]}')
    write_json(state_path, {'binding_sha256': binding_hash, 'binding': binding, 'state': 'complete'})
    return manifest


def bundle(output):
    output = Path(output).resolve()
    path = output.with_suffix('.zip')
    if path.exists():
        raise ValueError('bundle already exists; refusing to replace')
    if shutil.disk_usage(output).free - sum(p.stat().st_size for p in output.rglob('*') if p.is_file()) < 50*2**30:
        raise RuntimeError('insufficient disk reserve for portable bundle')
    temporary = path.with_suffix('.zip.tmp')
    with zipfile.ZipFile(temporary, 'w', zipfile.ZIP_DEFLATED, compresslevel=1) as archive:
        for source in sorted(output.rglob('*')):
            if source.is_file():
                archive.write(source, str(Path(output.name)/source.relative_to(output)))
    temporary.replace(path)
    return {'path': str(path), 'sha256': file_digest(path), 'bytes': path.stat().st_size}


if __name__ == '__main__':
    parser = argparse.ArgumentParser(description=__doc__)
    parser.add_argument('--root', default='.')
    parser.add_argument('--output', required=True)
    parser.add_argument('--workers', type=int, default=4)
    parser.add_argument('--validate-only', action='store_true')
    parser.add_argument('--zip', action='store_true')
    args = parser.parse_args()
    result = validate_workspace(args.output) if args.validate_only else export(args.root, args.output, workers=args.workers)
    if args.zip:
        result['bundle'] = bundle(args.output)
    print(json.dumps(result, ensure_ascii=False, indent=2))
    if args.validate_only and not result['ok']:
        raise SystemExit(1)
