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

import json
from pathlib import Path


def load_json(path: Path):
    with open(path, "r", encoding="utf-8") as f:
        return json.load(f)


def main():
    root = Path(__file__).resolve().parents[1]
    out_dir = root / "outputs/reproduced"
    out_dir.mkdir(parents=True, exist_ok=True)
    env = load_json(out_dir / "environment_check.json") if (out_dir / "environment_check.json").exists() else {}
    integrity = load_json(out_dir / "integrity_check.json") if (out_dir / "integrity_check.json").exists() else {}
    sparc = load_json(out_dir / "original_sparc_summary.json") if (out_dir / "original_sparc_summary.json").exists() else {}
    lam = load_json(out_dir / "lambda_event_band_summary.json") if (out_dir / "lambda_event_band_summary.json").exists() else {}

    combined = {
        "environment": env,
        "integrity": integrity,
        "original_sparc_summary": sparc,
        "lambda_event_band_summary": lam,
    }
    (out_dir / "reproduction_summary.json").write_text(json.dumps(combined, indent=2), encoding="utf-8")

    report = f"""# RBFL Reproduction Report

This report was generated by `scripts/05_write_reproduction_report.py`.

## Integrity

- All required files present: `{integrity.get('all_required_files_present')}`

## Original SPARC package summary

| Quantity | Value |
|---|---:|
| Clean radial rows | {sparc.get('sparc_clean_radial_rows')} |
| Clean galaxies | {sparc.get('sparc_clean_galaxies')} |
| Phase tuner galaxies | {sparc.get('phase_tuner_galaxies')} |
| Strict corridor count | {sparc.get('strict_corridor_count')} |
| Soft corridor count | {sparc.get('soft_corridor_count')} |
| Median chi2/dof | {sparc.get('median_chi2_dof')} |
| Reference gauge | {sparc.get('reference_gauge')} |

## Current lambda event-band detector summary

| Quantity | Value |
|---|---:|
| Best event-band center | {lam.get('best_center')} |
| Best event-band sigma | {lam.get('best_sigma')} |
| Best model label | {lam.get('best_model')} |
| Training phase match | {lam.get('train_phase_match')} |
| Holdout phase match | {lam.get('holdout_phase_match')} |
| Holdout velocity pass rate | {lam.get('holdout_velocity_pass_rate')} |
| Holdout combined velocity + phase | {lam.get('holdout_combined_velocity_phase')} |
| Baseline prior holdout phase match | {lam.get('baseline_holdout_multichannel_phase_match')} |
| Number of train galaxies | {lam.get('n_train')} |
| Number of holdout galaxies | {lam.get('n_holdout')} |

## Interpretation

The locked acceleration law was not modified by the detector.  The event-band parameter is a detection-stage proxy for projected event geometry and missing 3D field-shape information.  It improves phase-state classification in the included holdout split, while the locked-law velocity pass remains limited by the current projected-data and proxy-input pipeline.

## Generated figures

- `figures/holdout_detector_states_reproduced.png`
- `figures/lambda_event_band_sweep_reproduced.png`
- `figures/lambda_band_split_summary_reproduced.png`

"""
    (out_dir / "REPRODUCTION_REPORT.md").write_text(report, encoding="utf-8")
    print(report)


if __name__ == "__main__":
    main()
