"""Current pilot, annotation-round and experiment progress from verified artifacts."""
from __future__ import annotations

from collections import Counter
import json
from pathlib import Path
import re
import shutil

from pallet.annotation import is_resolved, load_jsonl
from pallet.data.protocol import file_digest, write_json
from pallet.progress_v4 import report as previous_report


def _candidate_round(index, rounds_root="outputs/rounds_v5"):
    root = Path(rounds_root) / f"round_{index:03d}"
    state = json.loads((root / "round.json").read_text()) if (root / "round.json").exists() else None
    selection_path = root / "preselection.json"
    selection = json.loads(selection_path.read_text()) if selection_path.exists() else None
    review_dir = Path(f"outputs/review_round_{index:03d}")
    small = Path(f"outputs/review_round_{index:03d}_small")
    if (small / 'latest.json').exists():
        review_dir = small
    latest_path = review_dir / "latest.json"
    latest = json.loads(latest_path.read_text()) if latest_path.exists() else None
    review_progress = (_candidate_review_progress(index)
                       if not state or state.get("state") != "complete" else None)
    candidate = {"planned": len(state["frame_ids"]) if state else
                 len(selection["train"]) if selection else 0,
                 "teacher_proposed": state.get("proposed_frames", 0) if state else 0,
                 "read_errors": state.get("read_errors", 0) if state else 0,
                 "round_state": state.get("state") if state else "preselected" if selection else None,
                 "paired_batches": latest.get("paired_batches", 0) if latest else 0,
                 "pending_adjudication": latest.get("pending_adjudication", 0) if latest else 0,
                 "ingest_errors": latest.get("errors", []) if latest else [],
                 "review_progress": review_progress,
                 "status": {}, "resolved": 0}
    # A completed round is defined by its frozen labels in round.json. The
    # review UI's latest.json can describe a partial or stale worktree.
    frozen_path = state.get("reviewed_labels") if state and state.get("state") == "complete" else None
    label_path = frozen_path or (latest.get("annotation_path") if latest else None)
    if state and label_path and Path(label_path).exists():
        labels = load_jsonl(label_path)
        counts = Counter()
        for frame_id in state["frame_ids"]:
            frame = labels.frames[frame_id]
            counts[frame.status if is_resolved(frame) else "unresolved"] += 1
        candidate["status"] = dict(counts)
        candidate["resolved"] = sum(counts[key] for key in ("pos", "negative", "ignore"))
        candidate["label_snapshot"] = str(label_path)
        candidate["label_sha256"] = file_digest(label_path)
    if state and state.get("state") == "complete":
        candidate["frozen_round_labels"] = state["reviewed_labels"]
        candidate["frozen_round_labels_sha256"] = state["reviewed_labels_sha256"]
    return candidate


def _candidate_review_progress(index, review_root=None):
    """Summarize normalized per-task candidate reviews, including pre-ingest work."""
    root = Path(review_root or f"outputs/review_round_{index:03d}")
    tasks_dir = root / "tasks"
    reviews_dir = root / "reviews"
    tasks = {}
    for task_path in sorted(tasks_dir.glob("train_*.json")):
        if not re.fullmatch(r"train_\d{3}\.json", task_path.name):
            continue
        task = json.loads(task_path.read_text())
        frames = task.get("frames", [])
        tasks[task_path.stem] = len(frames)

    reviewers = {}
    for side in ("a", "b", "c"):
        covered = set()
        submitted_tasks = set()
        invalid_entries = 0
        unverified_files = 0
        for review_path in sorted(reviews_dir.glob(f"{side}_train_*.json")):
            match = re.fullmatch(rf"{side}_train_(\d{{3}})\.json", review_path.name)
            if not match:
                continue
            task_id = f"train_{match.group(1)}"
            if task_id not in tasks:
                invalid_entries += 1
                continue
            task_path = tasks_dir / f"{task_id}.json"
            if side == "c":
                adjudication_path = root / "adjudication_tasks" / f"{task_id}.json"
                if adjudication_path.exists():
                    task_path = adjudication_path
            manifest_paths = (review_path.with_suffix(".manifest.json"),
                              review_path.with_name(f"{review_path.stem}_manifest.json"))
            manifest_path = next((path for path in manifest_paths if path.exists()), None)
            if manifest_path is None:
                unverified_files += 1
                continue
            manifest = json.loads(manifest_path.read_text())
            review_digest = (manifest.get("normalized_review_sha256") or
                             manifest.get("review_sha256"))
            review_reference = manifest.get("review_path")
            if review_reference is None:
                review_reference = manifest.get("review_file", review_path.name)
            expected_review_reference = (str(review_path.relative_to(root))
                                         if "review_path" in manifest else review_path.name)
            if (manifest.get("task_sha256") != file_digest(task_path)
                    or review_digest != file_digest(review_path)
                    or manifest.get("task_id", task_id) != task_id
                    or review_reference != expected_review_reference):
                invalid_entries += 1
                continue
            review = json.loads(review_path.read_text())
            frames = review.get("frames", [])
            valid_indices = set()
            for frame in frames:
                frame_index = frame.get("index")
                if isinstance(frame_index, int) and 0 <= frame_index < tasks[task_id]:
                    valid_indices.add(frame_index)
                else:
                    invalid_entries += 1
            if valid_indices:
                submitted_tasks.add(task_id)
                covered.update((task_id, frame_index) for frame_index in valid_indices)
        expected = sum(tasks.values())
        reviewed = len(covered)
        reviewers[side] = {
            "tasks_submitted": len(submitted_tasks),
            "frames_reviewed": reviewed,
            "frames_expected": expected,
            "pending_frames": max(0, expected - reviewed),
            "invalid_entries": invalid_entries,
            "unverified_files": unverified_files,
        }
    return {"tasks": len(tasks), "frames_expected": sum(tasks.values()),
            "reviewers": reviewers}


def _discover_rounds(rounds_root="outputs/rounds_v5"):
    root = Path(rounds_root)
    indices = []
    if root.exists():
        for path in root.glob("round_*"):
            if (path.is_dir() and path.name[6:].isdigit() and
                    ((path / "round.json").exists() or (path / "preselection.json").exists())):
                indices.append(int(path.name[6:]))
    return {f"{index:03d}": _candidate_round(index, rounds_root)
            for index in sorted(indices)}


def _latest_full_review_queue(stages_root="outputs/stages"):
    """Summarize the newest immutable queue manifest, if one exists."""
    root = Path(stages_root)
    candidates = sorted(path for path in root.glob("full_review_queue_v*")
                       if path.is_dir() and (path / "manifest.json").exists())
    if not candidates:
        return None
    queue = candidates[-1]
    manifest_path = queue / "manifest.json"
    manifest = json.loads(manifest_path.read_text())
    return {
        "name": queue.name,
        "manifest": str(manifest_path),
        "manifest_sha256": file_digest(manifest_path),
        "protocol_sha256": manifest.get("protocol_sha256"),
        "primary_labels_sha256": manifest.get("primary_labels_sha256"),
        "batch_limit": manifest.get("batch_limit"),
        "resolved": manifest.get("resolved", {}),
        "inflight": manifest.get("inflight", {}),
        "waiting": manifest.get("waiting", {}),
        "conflicts": len(manifest.get("conflicts", [])),
        "reference_rule": manifest.get("reference_rule"),
    }


def _labels_snapshot_for_sha(labels_sha256, rounds_root="outputs/rounds_v5"):
    """Find an immutable completed-round snapshot matching a panel's parent hash."""
    root = Path(rounds_root)
    for state_path in sorted(root.glob("round_*/round.json"), reverse=True):
        state = json.loads(state_path.read_text())
        path = state.get("reviewed_labels")
        if (state.get("state") == "complete"
                and state.get("reviewed_labels_sha256") == labels_sha256
                and path and Path(path).exists()
                and file_digest(path) == labels_sha256):
            return path
    return None


def _teacher_progress(rounds_root="outputs/rounds_v5"):
    result = {}
    for directory in sorted(Path(rounds_root).glob("teacher_*")):
        run_path = directory / "run.json"
        if not run_path.exists():
            continue
        run = json.loads(run_path.read_text())
        completion_path = directory / "completed.json"
        last = None
        best_ap = None
        metrics_path = directory / "metrics.jsonl"
        if metrics_path.exists():
            with metrics_path.open() as metrics_file:
                for line in metrics_file:
                    if not line.strip():
                        continue
                    try:
                        row = json.loads(line)
                    except json.JSONDecodeError:
                        continue
                    validation = row.get("validation", {})
                    ap = validation.get("AP50_95")
                    if ap is not None and (best_ap is None or ap > best_ap):
                        best_ap = ap
                    last = row
        latest_validation = last.get("validation", {}) if last else {}
        completed = json.loads(completion_path.read_text()) if completion_path.exists() else None
        result[directory.name] = {
            "state": "complete" if completed else "incomplete",
            "recipe": run.get("recipe"),
            "family": run.get("family"),
            "seed": run.get("seed"),
            "size": run.get("size"),
            "batch_size": run.get("batch_size"),
            "effective_batch": run.get("effective_batch"),
            "target_epochs": completed.get("target_epochs") if completed else run.get("epochs"),
            "logged_epochs": (last.get("epoch", -1) + 1) if last else 0,
            "stopped_reason": completed.get("stopped_reason") if completed else None,
            "best_AP50_95": best_ap,
            "latest_validation": {key: latest_validation.get(key) for key in
                                  ("AP50", "AP75", "AP50_95", "precision",
                                   "recall_at_threshold_iou_0.5")},
            "device_uuid": run.get("device_uuid"),
            "code_sha256": run.get("code_sha256"),
            "source_snapshot_id": run.get("source_snapshot_id"),
            "git_commit": run.get("git_commit"),
            "git_dirty": run.get("git_dirty"),
            "completion_manifest_sha256": file_digest(completion_path) if completed else None,
        }
    return result


def _full_review_batch_progress(outputs_root="outputs"):
    result = {}
    for directory in sorted(Path(outputs_root).glob("full_review_*_v5")):
        splits = {}
        for split in ("train", "val", "test"):
            index_path = directory / f"{split}_index.json"
            if not index_path.exists():
                continue
            selection = json.loads(index_path.read_text())
            if not selection:
                continue
            task_paths = sorted((directory / "tasks").glob(f"{split}_*.json"))
            reviews_dir = directory / "reviews"
            reviewer_frames = {}
            for side in ("a", "b", "c"):
                frames = 0
                if reviews_dir.exists():
                    for review_path in reviews_dir.glob(f"{side}_{split}_*.json"):
                        if not re.fullmatch(rf"{side}_{split}_\d{{3}}\.json", review_path.name):
                            continue
                        review = json.loads(review_path.read_text())
                        frames += len(review.get("frames", []))
                reviewer_frames[side] = frames
            ingest_path = directory / "latest.json"
            ingest = json.loads(ingest_path.read_text()) if ingest_path.exists() else {}
            errors_path = directory / "errors.json"
            errors = json.loads(errors_path.read_text()) if errors_path.exists() else []
            references = sum(path.stat().st_size > 0 for path in directory.glob("reference_*.jsonl"))
            splits[split] = {
                "selected_frames": len(selection),
                "tasks": len(task_paths),
                "reviewer_frames": reviewer_frames,
                "consensus_frames": ingest.get("consensus_frames", 0),
                "adjudicated_frames": ingest.get("adjudicated_frames", 0),
                "pending_adjudication": ingest.get("pending_adjudication", 0),
                "read_errors": len(errors),
                "reference_snapshots": references,
            }
        if splits:
            result[directory.name] = splits
    return result


def _audit_round(index, panel_path, before_labels, *, review_dir=None):
    audit_dir = Path(review_dir or f"outputs/audit_review_round_{index:03d}")
    audit_latest = json.loads((audit_dir / "latest.json").read_text()) if (audit_dir.joinpath("latest.json").exists()) else None
    audit = {"reserved_groups": 0, "panel_frames": 0,
             "freshly_resolved": 0,
             "pending_adjudication": audit_latest.get("pending_adjudication", 0) if audit_latest else None,
             "ingest_errors": audit_latest.get("errors", []) if audit_latest else []}
    panel_path = Path(panel_path)
    if panel_path.exists():
        panel = json.loads(panel_path.read_text())
        audit["reserved_groups"] = len(panel["groups"])
        audit["panel_frames"] = len(panel["frame_ids"])
        if audit_latest:
            labels = load_jsonl(audit_latest["annotation_path"])
            old = load_jsonl(before_labels)
            from pallet.annotation.cycle import _audit_changed
            audit["freshly_resolved"] = sum(
                is_resolved(labels.frames[fid]) and
                len(labels.frames[fid].reviews) > len(old.frames[fid].reviews)
                for fid in panel["frame_ids"])
            if audit["freshly_resolved"] == audit["panel_frames"]:
                audit["substantive_changed"] = sum(_audit_changed(old.frames[fid], labels.frames[fid])
                                                    for fid in panel["frame_ids"])
                audit["substantive_change_rate"] = audit["substantive_changed"] / audit["panel_frames"]
    return audit


def report(output="outputs/progress_v5.json"):
    previous = previous_report()
    rounds = _discover_rounds()
    candidate = rounds.get("001", _candidate_round(1))
    second = rounds.get("002", _candidate_round(2))
    third = rounds.get("003", _candidate_round(3))
    audit = _audit_round(1, "outputs/stages/pilot600_v5/audit_reserve/audit_panel.json",
                         "outputs/stages/pilot600_v5/annotations.jsonl")
    audit_second = _audit_round(2, "outputs/stages/round_002/audit_panel.json",
                                "outputs/audit_review_round_001/labels/d854216609bb99b5bde3.jsonl")
    audit_third = _audit_round(3, "outputs/stages/round_003/audit_panel.json",
                               "outputs/stages/round_002/reviewed_with_paired_snapshot.jsonl",
                               review_dir="outputs/audit_review_round_003_reblind"
                               if Path("outputs/audit_review_round_003_reblind/latest.json").exists()
                               else "outputs/audit_review_round_003_small")
    training_audits = {'001':audit, '002':audit_second, '003':audit_third}
    for index in rounds:
        if index in training_audits:
            continue
        state_path = Path('outputs/rounds_v5') / f'round_{index}' / 'round.json'
        panel_path = Path('outputs/stages') / f'round_{index}' / 'audit_panel.json'
        if panel_path.exists():
            panel = json.loads(panel_path.read_text())
            if state_path.exists():
                state = json.loads(state_path.read_text())
                before_labels = state.get('input_labels')
            else:
                before_labels = _labels_snapshot_for_sha(panel.get('input_labels_sha256'))
            if before_labels:
                training_audits[index] = _audit_round(int(index), panel_path,
                                                       before_labels)

    test_manifest = Path("outputs/protocol_v5/references/pilot_test.manifest.json")
    test = json.loads(test_manifest.read_text()) if test_manifest.exists() else None
    paired_manifest = Path("outputs/protocol_v5/references/paired_val.manifest.json")
    paired = json.loads(paired_manifest.read_text()) if paired_manifest.exists() else None
    paired_train_manifest = Path("outputs/protocol_v5/references/paired_train.manifest.json")
    paired_train = json.loads(paired_train_manifest.read_text()) if paired_train_manifest.exists() else None
    paired_test_manifest = Path("outputs/protocol_v5/references/paired_test.manifest.json")
    paired_test = json.loads(paired_test_manifest.read_text()) if paired_test_manifest.exists() else None
    paired_selection = Path("outputs/paired_panel_v5_new/selection.json")
    paired_latest_path = Path("outputs/review_paired_v5/latest.json")
    paired_training = None
    if paired_selection.exists() and paired_latest_path.exists():
        selected = json.loads(paired_selection.read_text())["train"]
        current = json.loads(paired_latest_path.read_text())
        labels = load_jsonl(current["annotation_path"])
        counts = Counter(labels.frames[fid].status if is_resolved(labels.frames[fid], require_blind=True)
                         else "unresolved" for fid in selected)
        paired_training = {"selected": len(selected), "status": dict(counts),
                           "resolved": sum(counts[key] for key in ("pos", "negative", "ignore")),
                           "pending_adjudication_all_splits": current["pending_adjudication"],
                           "ingest_errors": current["errors"],
                           "label_snapshot": current["annotation_path"]}
    free_bytes = shutil.disk_usage("outputs").free
    round_audits = {}
    for index in rounds:
        state_path = Path("outputs/rounds_v5") / f"round_{index}" / "round.json"
        if not state_path.exists():
            continue
        state = json.loads(state_path.read_text())
        if state.get("state") == "complete":
            round_audits[index] = {"panel_frames": len(state["audit_frame_ids"]),
                                   "substantive_changed": round(state["audit_change_rate"] *
                                                                len(state["audit_frame_ids"])),
                                   "substantive_change_rate": state["audit_change_rate"],
                                   "reviewed_labels_sha256": state["reviewed_labels_sha256"]}
    latest_completed = next((rounds[index] for index in reversed(rounds)
                             if rounds[index]["round_state"] == "complete"), None)
    annotation_coverage = None
    if latest_completed:
        labels = load_jsonl(latest_completed["frozen_round_labels"])
        counts = Counter(frame.status if is_resolved(frame) else "unreviewed"
                         for frame in labels.frames.values())
        annotation_coverage = {"total": len(labels.frames), "status": dict(counts),
                               "resolved": len(labels.frames) - counts["unreviewed"],
                               "labels_sha256": file_digest(latest_completed["frozen_round_labels"])}
    result = {"protocol_sha256": previous["protocol_sha256"],
              "pilot_counts": previous["pilot_counts"],
              "pilot_test_reference": test,
              "paired_validation_reference": paired,
              "paired_training_panel": paired_train,
              "paired_test_reference": paired_test,
              "paired_training_review": paired_training,
              "candidate_round_001": candidate,
              "candidate_round_002": second,
              "candidate_round_003": third,
              "candidate_rounds": rounds,
              "training_audits": training_audits,
              "round_audits": round_audits,
              "full_review_queue": _latest_full_review_queue(),
              "teacher_runs": _teacher_progress(),
              "full_review_batches": _full_review_batch_progress(),
              "annotation_coverage": annotation_coverage,
              "training_audit_round_001": audit,
              "training_audit_round_002": audit_second,
              "training_audit_round_003": audit_third,
              "pilot_completed_runs": [run for run in previous["completed_runs"]
                                       if "outputs/pilot600_v5/" in run["run"]],
              "free_disk_gib": round(free_bytes / 2**30, 2),
              "full_research_complete": False,
              "note": "Proposal count is not annotation coverage; references are independent machine reviews."}
    for index,audit_result in training_audits.items():
        result[f'training_audit_round_{index}'] = audit_result
    write_json(output, result)
    return result


if __name__ == "__main__":
    value = report()
    print(json.dumps({key: value[key] for key in ("candidate_rounds",
                      "training_audits", "annotation_coverage", "free_disk_gib")},
                     ensure_ascii=False))
