#!/usr/bin/env python3
"""Validate the frozen AA public-subset inputs and Echo output evidence."""

from __future__ import annotations

import hashlib
import json
import pathlib


ROOT = pathlib.Path(__file__).resolve().parent


def sha256(path: pathlib.Path) -> str:
    digest = hashlib.sha256()
    with path.open("rb") as handle:
        for chunk in iter(lambda: handle.read(1024 * 1024), b""):
            digest.update(chunk)
    return digest.hexdigest()


def main() -> None:
    sources = json.loads((ROOT / "sources.json").read_text(encoding="utf-8"))
    manifest = json.loads((ROOT / "manifest.json").read_text(encoding="utf-8"))
    results_path = ROOT / "echo-coreml-results.json"
    results = json.loads(results_path.read_text(encoding="utf-8"))
    scored = json.loads((ROOT / "echo-coreml-aa-public-scored.json").read_text(encoding="utf-8"))

    source_rows = sources["rows"]
    source_ids = [row["fixture_id"] for row in source_rows]
    manifest_ids = [row["id"] for row in manifest["fixtures"]]
    result_ids = [row["id"] for row in results["results"]]
    scored_ids = [row["id"] for row in scored["rows"]]
    checks = {
        "source_rows_634": len(source_rows) == 634,
        "voxpopuli_rows_628": sum(row["dataset"] == "voxpopuli" for row in source_rows) == 628,
        "earnings22_rows_6": sum(row["dataset"] == "earnings22" for row in source_rows) == 6,
        "unique_source_ids": len(set(source_ids)) == 634,
        "manifest_ids_match_sources": manifest_ids == source_ids,
        "result_ids_match_sources": result_ids == source_ids,
        "scored_ids_match_sources": scored_ids == source_ids,
        "all_transcripts_nonempty": all(
            row["rawTranscript"].strip() for row in results["results"]
        ),
        "raw_asr_only": results["pipeline"] == ["raw-asr"],
        "production_model_v3": results["model"] == "multilingualV3",
        "official_aa_wer_ineligible": scored["official_aa_wer_eligible"] is False,
        "result_hash_matches": (
            sha256(results_path) == scored["result_file"]["sha256"]
        ),
    }
    mismatched_files = []
    for row in source_rows:
        path = ROOT / row["local_audio"]
        if not path.exists() or path.stat().st_size != row["bytes"] or sha256(path) != row["sha256"]:
            mismatched_files.append(row["fixture_id"])
    checks["all_audio_hashes_match"] = not mismatched_files
    report = {
        "status": "pass" if all(checks.values()) else "fail",
        "checks": checks,
        "mismatched_files": mismatched_files,
        "measured": {
            "public_subset_composite_wer": scored["public_subset_composite_wer"],
            "voxpopuli_duration_weighted_wer": scored["dataset_scores"]["voxpopuli"][
                "duration_weighted_wer"
            ],
            "earnings22_duration_weighted_wer": scored["dataset_scores"]["earnings22"][
                "duration_weighted_wer"
            ],
            "source_duration_seconds": scored["timing"]["source_duration_seconds"],
            "whole_run_wall_seconds": scored["timing"]["whole_run_wall_seconds"],
        },
    }
    (ROOT / "validation.json").write_text(
        json.dumps(report, indent=2) + "\n",
        encoding="utf-8",
    )
    print(json.dumps(report, indent=2))
    if report["status"] != "pass":
        raise SystemExit(1)


if __name__ == "__main__":
    main()
