#!/usr/bin/env python3
from __future__ import annotations

import hashlib
import json
from pathlib import Path


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


def main():
    root = Path(__file__).resolve().parents[1]
    out_dir = root / "outputs/reproduced"
    out_dir.mkdir(parents=True, exist_ok=True)
    required = [
        "original_package/RBFL_SPARC_Reproducibility_Verified_PRO/outputs/01_clean_sparc/sparc_merged_clean.csv",
        "original_package/RBFL_SPARC_Reproducibility_Verified_PRO/outputs/01_clean_sparc/sparc_galaxy_summary_clean.csv",
        "original_package/RBFL_SPARC_Reproducibility_Verified_PRO/outputs/02_phase_tuner/phase_tuner_results.csv",
        "data/current_detector/reference_gauge_multichannel/reference_gauge_multichannel_galaxy_detection.csv",
        "data/current_detector/lambda_event_band/rbfl_lambda_band_detector_results.csv",
        "docs/RBFL_EQUATIONS_DICTIONARY.md",
    ]
    results = []
    all_ok = True
    for rel in required:
        path = root / rel
        exists = path.exists()
        all_ok = all_ok and exists
        results.append({
            "path": rel,
            "exists": exists,
            "bytes": path.stat().st_size if exists else None,
            "sha256": sha256(path) if exists and path.is_file() else None,
        })
    payload = {"all_required_files_present": all_ok, "files": results}
    (out_dir / "integrity_check.json").write_text(json.dumps(payload, indent=2), encoding="utf-8")
    print(json.dumps(payload, indent=2))


if __name__ == "__main__":
    main()
