"""Re-score archived answers offline; no SDK, credentials, or network required."""
import json
import random
from collections import defaultdict
from pathlib import Path
from run import score

BASE = Path(__file__).resolve().parent
EXPECTED_TOKENS = {
    "results.json": {
        ("T1_missing", "no_context", "haiku"): 5420,
        ("T1_missing", "no_context", "sonnet"): 5730,
        ("T1_missing", "with_fact", "haiku"): 5960,
        ("T1_missing", "with_fact", "sonnet"): 6270,
        ("T2_noise", "fact_only", "haiku"): 5960,
        ("T2_noise", "fact_only", "sonnet"): 6270,
        ("T2_noise", "noise_40", "haiku"): 55000,
        ("T2_noise", "noise_40", "sonnet"): 55310,
        ("T2_noise", "noise_400", "haiku"): 493450,
        ("T2_noise", "noise_400", "sonnet"): 493760,
        ("T3_clash", "dated_new_then_old", "haiku"): 6670,
        ("T3_clash", "dated_new_then_old", "sonnet"): 6980,
        ("T3_clash", "nodate_new_then_old", "haiku"): 5980,
        ("T3_clash", "nodate_new_then_old", "sonnet"): 6290,
        ("T3_clash", "nodate_old_then_new", "haiku"): 5980,
        ("T3_clash", "nodate_old_then_new", "sonnet"): 6290,
        ("T4_limit", "more_context", "haiku"): 6760,
        ("T4_limit", "more_context", "sonnet"): 7220,
        ("T4_limit", "plain", "haiku"): 6120,
        ("T4_limit", "plain", "sonnet"): 6580,
    },
    "results-t4-800.json": {
        ("T4_limit", "more_context", "haiku"): 12064,
        ("T4_limit", "more_context", "sonnet"): 12250,
        ("T4_limit", "plain", "haiku"): 11360,
        ("T4_limit", "plain", "sonnet"): 11610,
    },
}

def verify():
    for filename, length, expected in [("results.json", 120, 200), ("results-t4-800.json", 800, 40)]:
        rows = json.loads((BASE / filename).read_text())
        assert len(rows) == expected, (filename, len(rows))
        rng = random.Random(7)
        truth = "".join(rng.choice("abcdefgh") for _ in range(length)).count("e")
        groups = defaultdict(list)
        seen = set()
        for row in rows:
            key = (row["task"], row["condition"], row["model"], row["i"])
            assert key not in seen, key
            seen.add(key)
            result = score(row["task"], row["condition"], row["answer"], truth)
            assert all(row[k] == v for k, v in result.items()), (key, result)
            groups[key[:3]].append(row)
        print(f"{filename}: {len(rows)} answers; e count={truth}")
        assert groups.keys() == EXPECTED_TOKENS[filename].keys(), (filename, groups.keys())
        for key, group in groups.items():
            assert len(group) == 10, key
            tokens = [sum(row['usage'].get(k, 0) for k in ('input_tokens', 'cache_creation_input_tokens', 'cache_read_input_tokens')) for row in group]
            assert sum(tokens) == EXPECTED_TOKENS[filename][key], (filename, key, sum(tokens))
            print(*key, f"correct={sum(r['correct'] for r in group)}/10", f"input_mean={sum(tokens)/len(tokens):.1f}")
    print("PASS: saved scores, counts, unique trials, and token totals verified offline")

if __name__ == "__main__":
    verify()
