#!/usr/bin/env python3
"""Validate the Q3 Belenos dataset.

This script checks:
  1. required dataset files exist;
  2. the job manifest can be read;
  3. raw Quandela exports decode successfully;
  4. the included E4f_control_no export reproduces the manuscript row.

Any manifest rows without a matching raw export are reported but do not prevent
validation of the included data.
"""

from __future__ import annotations

import csv
import math
import subprocess
from pathlib import Path


ROOT = Path(__file__).resolve().parents[1]


def require(path: str) -> Path:
    p = ROOT / path
    if not p.exists():
        raise SystemExit(f"Missing required file: {p}")
    return p


def read_manifest() -> list[dict[str, str]]:
    with require("data/manifest/job_manifest.csv").open(newline="") as fh:
        return list(csv.DictReader(fh))


def main() -> None:
    require("README.md")
    require("data/manifest/backend_metadata.json")
    require("data/processed/reported_table_values.csv")
    require("scripts/decode_quandela_results.py")

    manifest = read_manifest()
    present = [row for row in manifest if row["status"] == "present"]
    missing = [row for row in manifest if row["status"] != "present"]

    print(f"Manifest rows: {len(manifest)}")
    print(f"Present raw exports: {len(present)}")
    print(f"Missing raw exports: {len(missing)}")

    subprocess.run(
        ["python3", str(ROOT / "scripts" / "decode_quandela_results.py")],
        cwd=ROOT,
        check=True,
    )

    summary_path = require("data/processed/job_summary.csv")
    with summary_path.open(newline="") as fh:
        rows = list(csv.DictReader(fh))

    e4 = next((row for row in rows if row["job_name"] == "E4f_control_no"), None)
    if e4 is None:
        raise SystemExit("Decoded summary missing E4f_control_no")

    probability = float(e4["dump_probability"])
    low = float(e4["dump_wilson95_low"])
    high = float(e4["dump_wilson95_high"])
    if not math.isclose(probability, 0.19567827130852342, rel_tol=0, abs_tol=1e-12):
        raise SystemExit(f"Unexpected E4f dump probability: {probability}")
    if not (0.1880 <= low <= 0.1881 and 0.2035 <= high <= 0.2036):
        raise SystemExit(f"Unexpected E4f Wilson interval: [{low}, {high}]")

    print("Included raw export validates successfully.")
    if missing:
        print(f"Missing {len(missing)} raw export(s) referenced by the manifest.")
    else:
        print("Dataset contains all manifest exports.")


if __name__ == "__main__":
    main()
