"""Audit check 6: (a) the account correlation at which the two-account 40-pip cell fails the gate,
(b) the size of the clock inconsistency between the paper's Section 14 map (14-trade clock) and
its Sections 1 and 15 (7-trade clock), on the exact chain.

    cd /root/Projects/trading-framework && python3 /tmp/arena-prime-audit/candidate-2/c2_rho_clock.py
"""
import json
import sys

import numpy as np

sys.path.insert(0, "/root/Projects/trading-framework")
sys.path.insert(0, "/tmp/arena-prime-audit/candidate-2")
from src.notebooks.reports.topics.prime_academy.modules import prime as PA  # noqa: E402

import c2_front as F  # noqa: E402  (re-runs its own checks on import; cheap enough and keeps one definition)

print("\n[a] two accounts at 40 pips, commission and spread: the gate (log growth) along the normal correlation, 2 seeds x 5,000 paths")
c40 = F.chain26(40.0, "commission and spread")
out = []
for rho in (0.50, 0.60, 0.65):
    rows = [F.wallet(c40, rho, 2, s)[1] for s in (13, 14)]
    lg = np.mean([r["log_g"] for r in rows]); ru = np.mean([r["ruin"] for r in rows])
    out.append(dict(rho=rho, rho_outcome=float(2 / np.pi * np.arcsin(rho)), log_g=float(lg), ruin=float(ru)))
    print(f"    rho_normal {rho:.2f} (outcome rho {2 / np.pi * np.arcsin(rho):.2f}): log_g {lg:+.4f} ruin {ru:.4f}")
json.dump(out, open("/tmp/arena-prime-audit/candidate-2/c2_rho.json", "w"), indent=1)

print("\n[b] the funded cell the paper's Section 14 map reads, RR 1 at 1.5%, cost 2.87%: 14-trade clock (as built) against 7 trades (the gate's)")
g = PA.cost_share(F.STOP0)
for wt in (14, 7):
    f = PA.funded_chain(0.015, g * 0.015, PA.payout_threshold(0.015, 1.0, 3000, 0.7), rr=1.0, clip_dn=True, wait=wt)
    print(f"    wait {wt:2d}: p_any {f['p_any']:.4f} e_npay {f['e_npay']:.3f} e_W {f['e_W']:.4f} med_first_pay {f['med_first_pay']} trades, e_life {f['e_life']:.1f}")
for wt in (14, 7):
    f = PA.funded_chain(0.04, g * 0.04, PA.payout_threshold(0.04, 1.0, 3000, 0.7), rr=1.0, clip_dn=True, wait=wt)
    print(f"    4% funded, wait {wt:2d}: p_any {f['p_any']:.4f} (the map's 'ridge' comparison cell)")
