"""Reproduce a small, explicitly synthetic performance-metrics example.

This script exists to demonstrate that public calculations can be independently
reproduced from disclosed inputs. It is NOT a backtest, live result, paper result,
or evidence that the investment strategy performs well.

The script is intentionally runnable from the reviewed public bundle without
access to the private production repository or third-party Python packages.
"""

from __future__ import annotations

import json
import sys
from pathlib import Path
from typing import Any, Dict, Iterable, List

PUBLIC_ROOT = Path(__file__).resolve().parents[1]
if str(PUBLIC_ROOT) not in sys.path:
    sys.path.insert(0, str(PUBLIC_ROOT))

from calculations import compute_core_metrics


SYNTHETIC_MONTHLY_RETURNS: List[float] = [
    0.020,
    -0.015,
    0.011,
    0.007,
    -0.028,
    0.034,
    0.009,
    0.012,
    -0.006,
    0.018,
    0.004,
    -0.021,
    0.016,
    0.010,
    -0.009,
    0.023,
    -0.014,
    0.006,
    0.019,
    -0.031,
    0.025,
    0.008,
    0.013,
    -0.005,
]

SYNTHETIC_REFERENCE_RETURNS: List[float] = [
    0.015,
    -0.010,
    0.008,
    0.006,
    -0.020,
    0.025,
    0.007,
    0.010,
    -0.004,
    0.014,
    0.003,
    -0.016,
    0.012,
    0.008,
    -0.006,
    0.017,
    -0.010,
    0.005,
    0.014,
    -0.022,
    0.019,
    0.006,
    0.010,
    -0.004,
]


def _load_policy_identity() -> Dict[str, str]:
    policy_path = PUBLIC_ROOT / "generated-policy.json"
    policy = json.loads(policy_path.read_text(encoding="utf-8"))
    version = policy.get("policy_version")
    policy_hash = policy.get("policy_hash")
    if not isinstance(version, str) or not version:
        raise ValueError("generated-policy.json is missing policy_version")
    if not isinstance(policy_hash, str) or not policy_hash:
        raise ValueError("generated-policy.json is missing policy_hash")
    return {"policy_version": version, "policy_hash": policy_hash}


def _metrics_payload(returns: Iterable[float]) -> Dict[str, Any]:
    values = list(returns)
    years = len(values) / 12.0
    metrics = compute_core_metrics(values, years=years, risk_free_rate=0.0)
    return {
        "inputs": {
            "periodicity": "monthly",
            "returns": values,
        },
        "metrics": metrics.to_dict(),
    }


def build_example() -> Dict[str, Any]:
    """Build the deterministic public demonstration payload."""

    policy_identity = _load_policy_identity()
    return {
        "schema_version": "0.2",
        "evidence_class": "synthetic_reproducibility_example",
        "not_performance_evidence": True,
        "purpose": (
            "Demonstrate reproducible return/risk calculations from fully disclosed "
            "synthetic monthly inputs."
        ),
        "warnings": [
            "These numbers are invented for calculation testing.",
            "They are not generated by the investment strategy.",
            "They are not a backtest, paper result, live result, or forecast.",
        ],
        "calculation_context": {
            **policy_identity,
            "metrics_implementation": "calculations.compute_core_metrics",
            "metrics_parity_contract": "private backtest.metrics.MetricsCalculator",
            "risk_free_rate": 0.0,
            "periods": len(SYNTHETIC_MONTHLY_RETURNS),
            "years": len(SYNTHETIC_MONTHLY_RETURNS) / 12.0,
            "runtime_dependency": "python_standard_library_only",
        },
        "synthetic_candidate": _metrics_payload(SYNTHETIC_MONTHLY_RETURNS),
        "synthetic_reference": _metrics_payload(SYNTHETIC_REFERENCE_RETURNS),
    }


def main() -> int:
    print(json.dumps(build_example(), indent=2, sort_keys=True))
    return 0


if __name__ == "__main__":
    raise SystemExit(main())
