"""
Proof: The Schwarzschild radius of the Sun calculated via rs = 2GM/c²
with 2022 CODATA values for G, solar mass, and c lies strictly between
2.95 km and 2.96 km.
Generated: 2026-03-28
"""
import json
import os
import sys

PROOF_ENGINE_ROOT = os.environ.get("PROOF_ENGINE_ROOT")
if not PROOF_ENGINE_ROOT:
    _d = os.path.dirname(os.path.abspath(__file__))
    while _d != os.path.dirname(_d):
        if os.path.isdir(os.path.join(_d, "proof-engine", "skills", "proof-engine", "scripts")):
            PROOF_ENGINE_ROOT = os.path.join(_d, "proof-engine", "skills", "proof-engine")
            break
        _d = os.path.dirname(_d)
    if not PROOF_ENGINE_ROOT:
        raise RuntimeError("PROOF_ENGINE_ROOT not set and skill dir not found via walk-up from proof.py")
sys.path.insert(0, PROOF_ENGINE_ROOT)
from datetime import date

# --- STRUCTURAL IMPORTS ---
from scripts.verify_citations import verify_all_citations, build_citation_detail, verify_data_values
from scripts.computations import compare, explain_calc, cross_check

# ============================================================================
# 1. CLAIM INTERPRETATION (Rule 4)
# ============================================================================
CLAIM_NATURAL = (
    "The Schwarzschild radius of the Sun calculated via rs = 2GM/c² "
    "with 2022 CODATA values for G, solar mass, and c lies strictly "
    "between 2.95 km and 2.96 km."
)
CLAIM_FORMAL = {
    "subject": "Schwarzschild radius of the Sun",
    "property": "rs = 2GM/c² in kilometres",
    "operator": "compound: > AND <",
    "operator_note": (
        "'strictly between 2.95 km and 2.96 km' means rs > 2.95 AND rs < 2.96 "
        "(both strict inequalities). If rs were exactly 2.95 or exactly 2.96, "
        "the claim would be FALSE. "
        "CODATA does not publish a solar mass value directly. The standard "
        "approach is to use the IAU 2015 Resolution B3 nominal solar mass "
        "parameter GM☉^N = 1.3271244 × 10²⁰ m³ s⁻² and derive "
        "M☉ = GM☉^N / G. Since rs = 2GM/c² = 2(GM☉^N)/c², the value of G "
        "cancels, but we compute both ways for cross-checking."
    ),
    "threshold_lower": 2.95,
    "threshold_upper": 2.96,
}

# ============================================================================
# 2. FACT REGISTRY
# ============================================================================
FACT_REGISTRY = {
    "B1": {"key": "nist_G", "label": "Newtonian gravitational constant G (2022 CODATA)"},
    "B2": {"key": "nist_c", "label": "Speed of light c (2022 CODATA, exact)"},
    "B3": {"key": "iau_GM_sun", "label": "Nominal solar mass parameter GM☉^N (IAU 2015 Resolution B3)"},
    "B4": {"key": "wiki_solar_mass", "label": "Solar mass cross-check (Wikipedia)"},
    "A1": {"label": "rs via separate G and M (primary)", "method": None, "result": None},
    "A2": {"label": "rs via GM☉ directly (cross-check)", "method": None, "result": None},
}

# ============================================================================
# 3. EMPIRICAL FACTS
# ============================================================================
empirical_facts = {
    "nist_G": {
        "quote": "Newtonian constant of gravitation",
        "url": "https://physics.nist.gov/cgi-bin/cuu/Value?bg",
        "source_name": "NIST 2022 CODATA Recommended Values",
        "data_values": {"G_mantissa": "6.674 30", "G_exponent": "-11"},
    },
    "nist_c": {
        "quote": "speed of light in vacuum",
        "url": "https://physics.nist.gov/cgi-bin/cuu/Value?c",
        "source_name": "NIST 2022 CODATA Recommended Values",
        "data_values": {"c_value": "299 792 458"},
    },
    "iau_GM_sun": {
        "quote": "nominal solar mass parameter",
        "url": "https://ar5iv.labs.arxiv.org/html/1510.07674",
        "source_name": "IAU 2015 Resolution B3 (Mamajek et al. 2015, arXiv:1510.07674)",
        "data_values": {"GM_sun": "1.3271244"},
    },
    "wiki_solar_mass": {
        "quote": "1.32712442099",
        "url": "https://en.wikipedia.org/wiki/Solar_mass",
        "source_name": "Wikipedia — Solar mass (TCG-compatible GM☉ estimate)",
        "data_values": {"GM_sun_tcg": "1.32712442099"},
    },
}

# ============================================================================
# 4. CITATION VERIFICATION (Rule 2)
# ============================================================================
print("=== CITATION VERIFICATION ===")
citation_results = verify_all_citations(empirical_facts, wayback_fallback=True)
for k, v in citation_results.items():
    print(f"  {k}: {v['status']}")

# ============================================================================
# 5. DATA VALUE VERIFICATION
# ============================================================================
print("\n=== DATA VALUE VERIFICATION ===")
dv_nist_G = verify_data_values(
    empirical_facts["nist_G"]["url"],
    empirical_facts["nist_G"]["data_values"],
    "B1",
)
dv_nist_c = verify_data_values(
    empirical_facts["nist_c"]["url"],
    empirical_facts["nist_c"]["data_values"],
    "B2",
)
dv_iau = verify_data_values(
    empirical_facts["iau_GM_sun"]["url"],
    empirical_facts["iau_GM_sun"]["data_values"],
    "B3",
)
dv_wiki = verify_data_values(
    empirical_facts["wiki_solar_mass"]["url"],
    empirical_facts["wiki_solar_mass"]["data_values"],
    "B4",
)

# ============================================================================
# 6. VALUE EXTRACTION (Rule 1)
# ============================================================================
print("\n=== VALUE EXTRACTION ===")

# Extract G: mantissa with spaces + exponent, as published on NIST page
G_mantissa_str = empirical_facts["nist_G"]["data_values"]["G_mantissa"]
G_exponent_str = empirical_facts["nist_G"]["data_values"]["G_exponent"]
G = float(G_mantissa_str.replace(" ", "")) * 10 ** int(G_exponent_str)
print(f"  G = {G_mantissa_str} × 10^{G_exponent_str} = {G} m³ kg⁻¹ s⁻²")

# Extract c: value with spaces as published on NIST page
c_str = empirical_facts["nist_c"]["data_values"]["c_value"]
c = float(c_str.replace(" ", ""))
print(f"  c = {c_str} = {c} m/s")

# Extract GM☉^N: mantissa from IAU page (× 10²⁰)
GM_mantissa_str = empirical_facts["iau_GM_sun"]["data_values"]["GM_sun"]
GM_sun_iau = float(GM_mantissa_str) * 1e20
print(f"  GM☉^N (IAU nominal) = {GM_mantissa_str} × 10²⁰ = {GM_sun_iau} m³ s⁻²")

# Extract GM☉ from Wikipedia (TCG-compatible, for cross-check, × 10²⁰)
GM_wiki_str = empirical_facts["wiki_solar_mass"]["data_values"]["GM_sun_tcg"]
GM_sun_wiki = float(GM_wiki_str) * 1e20
print(f"  GM☉ (Wikipedia TCG) = {GM_wiki_str} × 10²⁰ = {GM_sun_wiki} m³ s⁻²")

# ============================================================================
# 7. CROSS-CHECK: GM☉ values from independent sources (Rule 6)
# ============================================================================
print("\n=== CROSS-CHECK: GM☉ SOURCES ===")
# IAU nominal has 8 sig figs; TCG estimate has 12. They should agree to ~1e-7 relative.
cross_check(GM_sun_iau, GM_sun_wiki, tolerance=1e-5, mode="relative",
            label="GM☉ IAU nominal vs Wikipedia TCG")

# ============================================================================
# 8. COMPUTATION (Rule 7)
# ============================================================================
print("\n=== PRIMARY COMPUTATION ===")
# Primary: Use G and M separately, where M = GM☉^N / G
M_sun = explain_calc("GM_sun_iau / G", locals(), label="Solar mass M☉ = GM☉^N / G")
print(f"  M☉ = {M_sun:.6e} kg")

rs_primary_m = explain_calc("2 * G * M_sun / (c ** 2)", locals(),
                             label="rs = 2GM/c² (primary, metres)")
rs_primary_km = explain_calc("rs_primary_m / 1000", locals(),
                              label="rs in kilometres (primary)")

print("\n=== CROSS-CHECK COMPUTATION ===")
# Cross-check: Use GM☉ directly (G cancels)
rs_crosscheck_m = explain_calc("2 * GM_sun_iau / (c ** 2)", locals(),
                                label="rs = 2(GM☉)/c² (direct, metres)")
rs_crosscheck_km = explain_calc("rs_crosscheck_m / 1000", locals(),
                                 label="rs in kilometres (cross-check)")

print("\n=== CROSS-CHECK: primary vs direct ===")
cross_check(rs_primary_km, rs_crosscheck_km, tolerance=1e-10, mode="absolute",
            label="rs primary vs direct (km)")

# Additional cross-check using Wikipedia GM☉ value
rs_wiki_m = explain_calc("2 * GM_sun_wiki / (c ** 2)", locals(),
                          label="rs using Wikipedia GM☉ (metres)")
rs_wiki_km = explain_calc("rs_wiki_m / 1000", locals(),
                           label="rs using Wikipedia GM☉ (km)")

print("\n=== CROSS-CHECK: IAU nominal vs Wikipedia GM☉ ===")
cross_check(rs_crosscheck_km, rs_wiki_km, tolerance=1e-5, mode="absolute",
            label="rs IAU vs Wikipedia (km)")

# ============================================================================
# 9. CLAIM EVALUATION
# ============================================================================
print("\n=== CLAIM EVALUATION ===")
lower_holds = compare(rs_primary_km, ">", CLAIM_FORMAL["threshold_lower"],
                       label="rs > 2.95 km")
upper_holds = compare(rs_primary_km, "<", CLAIM_FORMAL["threshold_upper"],
                       label="rs < 2.96 km")
claim_holds = lower_holds and upper_holds
print(f"  Both conditions hold: {claim_holds}")

# ============================================================================
# 10. ADVERSARIAL CHECKS (Rule 5)
# ============================================================================
adversarial_checks = [
    {
        "question": "Could uncertainty in G shift rs outside [2.95, 2.96] km?",
        "verification_performed": (
            "Computed rs using G ± uncertainty (6.67430 ± 0.00015 × 10⁻¹¹). "
            "Since rs = 2(GM☉)/c² and G cancels when using the GM☉ product, "
            "G's uncertainty does not affect the result. Even computing via "
            "G×M separately, the relative uncertainty in G (2.2 × 10⁻⁵) shifts "
            "rs by only ~0.00006 km, well within the 0.01 km band."
        ),
        "finding": "G uncertainty cannot move rs outside [2.95, 2.96].",
        "breaks_proof": False,
    },
    {
        "question": "Does the choice of time coordinate (TCB vs TDB) for GM☉ matter?",
        "verification_performed": (
            "Searched for TCB vs TDB solar mass parameter values. "
            "TCG: 1.32712442099 × 10²⁰, TDB: 1.32712440041 × 10²⁰. "
            "IAU nominal: 1.3271244 × 10²⁰. Difference is in the 8th digit, "
            "yielding ~10⁻⁸ relative change in rs — negligible."
        ),
        "finding": "Time coordinate choice shifts rs by < 10⁻⁵ km. No impact.",
        "breaks_proof": False,
    },
    {
        "question": "Do any authoritative sources cite a Schwarzschild radius outside [2.95, 2.96] km?",
        "verification_performed": (
            "Searched web: 'Schwarzschild radius Sun km value'. "
            "Wikipedia cites 'approximately 3.0 km' and '2.95 × 10³ m'. "
            "NASA SpaceMath cites ~3 km. No source gives a value outside "
            "the range [2.9, 3.0] km, and all precise computations give ~2.953 km."
        ),
        "finding": "No authoritative source contradicts the [2.95, 2.96] range.",
        "breaks_proof": False,
    },
]

# ============================================================================
# 11. VERDICT AND STRUCTURED OUTPUT
# ============================================================================
if __name__ == "__main__":
    any_unverified = any(
        cr["status"] != "verified" for cr in citation_results.values()
    )

    if claim_holds and not any_unverified:
        verdict = "PROVED"
    elif claim_holds and any_unverified:
        verdict = "PROVED (with unverified citations)"
    elif not claim_holds and not any_unverified:
        verdict = "DISPROVED"
    elif not claim_holds and any_unverified:
        verdict = "DISPROVED (with unverified citations)"
    else:
        verdict = "UNDETERMINED"

    FACT_REGISTRY["A1"]["method"] = "rs = 2 * G * (GM☉/G) / c² (G and M separate)"
    FACT_REGISTRY["A1"]["result"] = f"{rs_primary_km:.6f} km"
    FACT_REGISTRY["A2"]["method"] = "rs = 2 * GM☉ / c² (direct, G cancels)"
    FACT_REGISTRY["A2"]["result"] = f"{rs_crosscheck_km:.6f} km"

    citation_detail = build_citation_detail(FACT_REGISTRY, citation_results, empirical_facts)

    extractions = {
        "B1_G": {
            "value": str(G),
            "value_in_quote": True,
            "quote_snippet": f"{G_mantissa_str} x 10^{G_exponent_str}",
        },
        "B2_c": {
            "value": str(c),
            "value_in_quote": True,
            "quote_snippet": c_str,
        },
        "B3_GM_sun": {
            "value": str(GM_sun_iau),
            "value_in_quote": True,
            "quote_snippet": f"{GM_mantissa_str} x 10^20",
        },
        "B4_GM_sun_tcg": {
            "value": str(GM_sun_wiki),
            "value_in_quote": True,
            "quote_snippet": f"{GM_wiki_str} x 10^20",
        },
    }

    data_value_verification = {
        "B1": {k: v for k, v in dv_nist_G.items()},
        "B2": {k: v for k, v in dv_nist_c.items()},
        "B3": {k: v for k, v in dv_iau.items()},
        "B4": {k: v for k, v in dv_wiki.items()},
    }

    summary = {
        "fact_registry": {
            fid: {k: v for k, v in info.items()}
            for fid, info in FACT_REGISTRY.items()
        },
        "claim_formal": CLAIM_FORMAL,
        "claim_natural": CLAIM_NATURAL,
        "citations": citation_detail,
        "extractions": extractions,
        "data_value_verification": data_value_verification,
        "cross_checks": [
            {
                "description": "GM☉ IAU nominal vs Wikipedia TCG",
                "values_compared": [str(GM_sun_iau), str(GM_sun_wiki)],
                "agreement": True,
                "tolerance": "1e-5 relative",
            },
            {
                "description": "rs primary (G×M) vs direct (GM☉/c²)",
                "values_compared": [f"{rs_primary_km:.10f}", f"{rs_crosscheck_km:.10f}"],
                "agreement": True,
                "tolerance": "1e-10 km absolute",
            },
            {
                "description": "rs IAU nominal GM☉ vs Wikipedia GM☉",
                "values_compared": [f"{rs_crosscheck_km:.6f}", f"{rs_wiki_km:.6f}"],
                "agreement": True,
                "tolerance": "1e-5 km absolute",
            },
        ],
        "adversarial_checks": adversarial_checks,
        "verdict": verdict,
        "key_results": {
            "rs_primary_km": rs_primary_km,
            "rs_crosscheck_km": rs_crosscheck_km,
            "rs_wiki_km": rs_wiki_km,
            "threshold_lower": CLAIM_FORMAL["threshold_lower"],
            "threshold_upper": CLAIM_FORMAL["threshold_upper"],
            "lower_holds": lower_holds,
            "upper_holds": upper_holds,
            "claim_holds": claim_holds,
        },
        "generator": {
            "name": "proof-engine",
            "version": open(os.path.join(PROOF_ENGINE_ROOT, "VERSION")).read().strip(),
            "repo": "https://github.com/yaniv-golan/proof-engine",
            "generated_at": date.today().isoformat(),
        },
    }

    print(f"\n=== VERDICT: {verdict} ===")
    print(f"\n=== PROOF SUMMARY (JSON) ===")
    print(json.dumps(summary, indent=2, default=str))
