"""Calibrate proposal thresholds on teacher-unseen source audit groups."""
from __future__ import annotations

import argparse
import json
from pathlib import Path

import torch

from pallet.annotation.round_diagnostics import match_counts
from pallet.annotation.cycle import _teacher_access
from pallet.annotation.workflow import is_resolved, load_jsonl
from pallet.cli import _attach_cache
from pallet.data.loader import NormalizationStats, apply_normalization, load_frame
from pallet.data.protocol import file_digest, load_protocol, write_json
from pallet.devices import device_metadata, resolve_device
from pallet.experiments.dataset import SampleMeta, letterbox_float
from pallet.experiments.evaluate import predict_boxes
from pallet.export import load_detector_checkpoint


THRESHOLDS = (.05, .1, .2, .3, .5, .7)


def _validate_audit_access(records, assignments, run, groups, protocol_sha256):
    if run.get('protocol_sha256') != protocol_sha256:
        raise ValueError('teacher was trained on another protocol')
    return _teacher_access(records, assignments, run, groups, run.get('holdout_domain'))


def calibrate(checkpoint, protocol, panel_path, audit_labels, output, *, device):
    checkpoint = Path(checkpoint)
    run = json.loads((checkpoint.parent / "run.json").read_text())
    completed = json.loads((checkpoint.parent / "completed.json").read_text())
    if file_digest(checkpoint) != completed["best_checkpoint_sha256"]:
        raise ValueError("teacher checkpoint is not completed best")
    panel = json.loads(Path(panel_path).read_text())
    records, assignments, _ = load_protocol(protocol)
    if panel["protocol_sha256"] != file_digest(Path(protocol) / "protocol.json"):
        raise ValueError("audit panel belongs to another protocol")
    groups = {group["group_id"] for group in panel["groups"]}
    _validate_audit_access(records, assignments, run, groups,
                           file_digest(Path(protocol) / 'protocol.json'))
    by_id = {row.frame_id: row for row in _attach_cache(records, "outputs/cache_v5/sealed_index.json")}
    labels = load_jsonl(audit_labels)
    model, config = load_detector_checkpoint(checkpoint)
    device = resolve_device(device)
    model.eval().to(device)
    state = torch.load(checkpoint, map_location="cpu", weights_only=False)
    stats = NormalizationStats(**state["normalization"])
    observations = []
    for fid in panel["frame_ids"]:
        row = by_id[fid]
        frame = labels.frames[fid]
        if (assignments[row.group_id] != "train" or row.group_id not in groups
                or frame.group_id != row.group_id or frame.source_anchor_hash != row.source_sha256
                or not is_resolved(frame, require_blind=True)):
            raise ValueError(f"audit frame is not a bound source reference: {fid}")
        if any(p.source.lower() in {'detector','fewshot','sam3','model','segmentation'}
               for p in frame.provenance):
            raise ValueError(f'audit reference includes model-assisted provenance: {fid}')
        if frame.status == "ignore" or frame.ignore_regions:
            continue
        raw = load_frame(row)
        image, scale, padding = letterbox_float(apply_normalization(raw, stats), config["size"])
        meta = SampleMeta(fid, raw.shape[:2], scale, padding)
        boxes = predict_boxes(model, image.unsqueeze(0).to(device), meta,
                              conf=min(THRESHOLDS), size=config["size"])
        observations.append((fid, [(b.x, b.y, b.width, b.height) for b in frame.boxes], boxes))
    curve = []
    for threshold in THRESHOLDS:
        tp = fp = fn = 0
        for _, reference, candidates in observations:
            predicted = [(box[0], box[1], box[2] - box[0], box[3] - box[1])
                         for box, score in candidates if score >= threshold]
            a, b, c = match_counts(reference, predicted)
            tp += a; fp += b; fn += c
        curve.append({"confidence_threshold": threshold, "true_positives": tp,
                      "false_positives": fp, "missed": fn,
                      "recall": tp / (tp + fn) if tp + fn else None,
                      "false_positives_per_frame": fp / len(observations) if observations else None})
    eligible = [row for row in curve if row["recall"] is not None and row["recall"] >= .95]
    result = {"scope": "teacher-unseen source-training audit only",
              "teacher_checkpoint_sha256": file_digest(checkpoint),
              "audit_labels_sha256": file_digest(audit_labels),
              "panel_sha256": file_digest(panel_path),
              "audit_groups": sorted(groups), "scored_frames": len(observations),
              "device": device_metadata(device), "curve": curve,
              "high_recall_target": .95,
              "selected_candidate_threshold": max((row["confidence_threshold"] for row in eligible),
                                                  default=None),
              "automatic_acceptance": False,
              "note": "Threshold is for proposal triage only; every candidate needs independent review."}
    write_json(output, result)
    return result


if __name__ == "__main__":
    parser = argparse.ArgumentParser()
    parser.add_argument("--checkpoint", required=True)
    parser.add_argument("--protocol", default="outputs/protocol_v5")
    parser.add_argument("--panel", required=True)
    parser.add_argument("--audit-labels", required=True)
    parser.add_argument("--output", required=True)
    parser.add_argument("--device", required=True)
    args = parser.parse_args()
    result = calibrate(args.checkpoint, args.protocol, args.panel,
                       args.audit_labels, args.output, device=args.device)
    print(json.dumps({"scored_frames": result["scored_frames"],
                      "selected_candidate_threshold": result["selected_candidate_threshold"],
                      "curve": result["curve"]}))
