"""
Proof: Contrast therapy alternating sauna and ice bath is scientifically proven superior
       for recovery and longevity.
Generated: 2026-03-31
"""
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

from scripts.verify_citations import verify_all_citations, build_citation_detail
from scripts.computations import compare

# ---------------------------------------------------------------------------
# 1. CLAIM INTERPRETATION (Rule 4)
# ---------------------------------------------------------------------------
CLAIM_NATURAL = (
    "Contrast therapy alternating sauna and ice bath is scientifically proven superior "
    "for recovery and longevity."
)
CLAIM_FORMAL = {
    "subject": "Contrast therapy (alternating sauna and ice bath / cold water immersion)",
    "property": (
        "scientifically proven superior for both athletic/exercise recovery "
        "and longevity compared to other modalities"
    ),
    "operator": ">=",
    "operator_note": (
        "Proof direction: DISPROVE. "
        "'Scientifically proven superior' requires independent peer-reviewed meta-analyses or "
        "systematic reviews concluding contrast therapy outperforms all comparators. "
        "We count sources that REFUTE this: peer-reviewed reviews showing (a) CWT is not "
        "superior to other active recovery methods, or (b) evidence quality is insufficient "
        "to draw superiority conclusions. "
        "Threshold of 3 independent peer-reviewed sources is the minimum for 'consensus'. "
        "The claim is compound (recovery AND longevity): disproving either part disproves "
        "the whole. For recovery: CWT must be proven superior to cold water immersion (CWI) "
        "and other active modalities — existing meta-analyses contradict this. "
        "For longevity: no prospective RCTs or cohort studies specifically examining combined "
        "contrast therapy (sauna+cold) and lifespan/all-cause mortality exist. "
        "The sauna-longevity association (Laukkanen et al.) applies to sauna use alone, "
        "not contrast therapy."
    ),
    "threshold": 3,
    "proof_direction": "disprove",
}

# ---------------------------------------------------------------------------
# 2. FACT REGISTRY
# ---------------------------------------------------------------------------
FACT_REGISTRY = {
    "B1": {
        "key": "cwt_plosone_2013",
        "label": (
            "PLOS One 2013 meta-analysis (18 RCTs): CWT for exercise-induced muscle damage — "
            "no superior intervention over other active methods"
        ),
    },
    "B2": {
        "key": "cwt_team_sport_2017",
        "label": (
            "PubMed 2017 systematic review + meta-analysis: CWT vs CWI for team sport — "
            "CWI outperforms CWT for neuromuscular recovery"
        ),
    },
    "B3": {
        "key": "contrast_scoping_2025",
        "label": (
            "PMC 2025 scoping review: contrast therapy for musculoskeletal pain — "
            "insufficient evidence quality to conclude superiority over other therapies"
        ),
    },
    "A1": {
        "label": (
            "Count of independent peer-reviewed reviews confirming CWT is NOT "
            "scientifically proven superior"
        ),
        "method": None,
        "result": None,
    },
}

# ---------------------------------------------------------------------------
# 3. EMPIRICAL FACTS
# Sources that confirm the claim is FALSE (proof_direction = "disprove").
# Only sources rejecting the claim go here; supportive sources are in adversarial_checks.
# ---------------------------------------------------------------------------
empirical_facts = {
    "cwt_plosone_2013": {
        "quote": (
            "there was little evidence for a superior treatment intervention"
        ),
        "url": "https://journals.plos.org/plosone/article?id=10.1371/journal.pone.0062356",
        "source_name": (
            "PLOS One — Contrast Water Therapy and Exercise Induced Muscle Damage: "
            "A Systematic Review and Meta-Analysis (Higgins et al., 2013)"
        ),
    },
    "cwt_team_sport_2017": {
        "quote": (
            "CWI was beneficial for neuromuscular recovery 24 hours following team sport, "
            "whereas CWT was not beneficial"
        ),
        "url": "https://pubmed.ncbi.nlm.nih.gov/27398915/",
        "source_name": (
            "PubMed — Effects of Cold Water Immersion and Contrast Water Therapy for Recovery "
            "From Team Sport: A Systematic Review and Meta-analysis (Versey et al., 2017)"
        ),
    },
    "contrast_scoping_2025": {
        "quote": (
            "the modest quality of the trials does not allow the authors to draw clear "
            "conclusions about the effectiveness of CT compared with other therapies"
        ),
        "url": "https://pmc.ncbi.nlm.nih.gov/articles/PMC11900007/",
        "source_name": (
            "PMC — Mechanisms and Efficacy of Contrast Therapy for Musculoskeletal "
            "Painful Disease: A Scoping Review (2025)"
        ),
    },
}

# ---------------------------------------------------------------------------
# 4. CITATION VERIFICATION (Rule 2)
# ---------------------------------------------------------------------------
citation_results = verify_all_citations(empirical_facts, wayback_fallback=True)

# ---------------------------------------------------------------------------
# 5. COUNT SOURCES WITH VERIFIED CITATIONS
# A source counts toward the threshold if its quote was found on the page
# (status = "verified" or "partial").
# ---------------------------------------------------------------------------
COUNTABLE_STATUSES = ("verified", "partial")
n_confirmed = sum(
    1 for key in empirical_facts
    if citation_results[key]["status"] in COUNTABLE_STATUSES
)
print(f"  Confirmed refuting sources: {n_confirmed} / {len(empirical_facts)}")

# ---------------------------------------------------------------------------
# 6. CLAIM EVALUATION (Rule 7 — must use compare(), never hardcode)
# ---------------------------------------------------------------------------
claim_holds = compare(
    n_confirmed,
    CLAIM_FORMAL["operator"],
    CLAIM_FORMAL["threshold"],
    label="refuting sources >= threshold (disproof direction)",
)

# ---------------------------------------------------------------------------
# 7. ADVERSARIAL CHECKS (Rule 5)
# Search for evidence that could SUPPORT the original claim (i.e., break the disproof).
# ---------------------------------------------------------------------------
adversarial_checks = [
    {
        "question": (
            "Does any RCT or prospective cohort study show that combined contrast therapy "
            "(alternating sauna + cold immersion specifically) improves human longevity "
            "or reduces all-cause mortality?"
        ),
        "verification_performed": (
            "Searched PubMed and web for 'contrast therapy longevity lifespan mortality RCT cohort' "
            "and 'sauna ice bath alternating longevity evidence prospective study'."
        ),
        "finding": (
            "No prospective cohort studies or RCTs found that examine combined contrast therapy "
            "(hot+cold) and longevity outcomes in humans. "
            "The landmark longevity data (Laukkanen et al. 2015, JAMA Intern Med) studies "
            "sauna use ALONE in 2,315 Finnish men over 20.7 years — no cold immersion component. "
            "Cold water immersion longevity claims rely primarily on animal models (mouse studies "
            "showing ~20% lifespan extension with mild hypothermia) with very limited human data. "
            "No studies combine these two modalities specifically for longevity outcomes."
        ),
        "breaks_proof": False,
    },
    {
        "question": (
            "Is there any meta-analysis concluding that CWT is definitively superior to "
            "ALL other recovery modalities, including cold water immersion?"
        ),
        "verification_performed": (
            "Searched PubMed and web for 'contrast water therapy superior recovery meta-analysis'. "
            "Reviewed abstracts of systematic reviews from 2010–2025."
        ),
        "finding": (
            "No meta-analysis concludes CWT is superior to all comparators. "
            "The PLOS One 2013 meta-analysis explicitly finds 'little evidence for a superior "
            "treatment intervention' when comparing CWT to CWI, compression, active recovery, "
            "and stretching. The 2017 Versey meta-analysis found CWI more effective than CWT "
            "for neuromuscular recovery after team sport. "
            "A 2022 Sports Medicine systematic review (PMID 36527593) found CWI superior to "
            "contrast water therapy for muscle soreness recovery outcomes."
        ),
        "breaks_proof": False,
    },
    {
        "question": (
            "Could the claim be narrowly true — i.e., 'superior to passive rest' — "
            "satisfying 'proven superior' in a minimal sense?"
        ),
        "verification_performed": (
            "Reviewed PLOS One 2013 findings specifically on CWT vs passive rest. "
            "Analyzed whether 'superior' in the claim implies a relative or absolute comparison."
        ),
        "finding": (
            "CWT IS proven superior to passive rest for muscle soreness and strength recovery "
            "(PLOS One 2013). However, the claim asserts superiority without qualification — "
            "the natural reading is superiority over the field of recovery methods, not just "
            "doing nothing. The claim also asserts superiority for 'longevity', for which "
            "no comparative evidence for contrast therapy exists at all. "
            "On the narrowest possible reading, only recovery-vs-rest is weakly supported; "
            "recovery-vs-other-methods and the entire longevity component are not."
        ),
        "breaks_proof": False,
    },
]

# ---------------------------------------------------------------------------
# 8. VERDICT AND STRUCTURED OUTPUT
# ---------------------------------------------------------------------------
if __name__ == "__main__":
    any_unverified = any(
        cr["status"] != "verified" for cr in citation_results.values()
    )
    is_disproof = CLAIM_FORMAL.get("proof_direction") == "disprove"
    any_breaks = any(ac.get("breaks_proof") for ac in adversarial_checks)

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

    FACT_REGISTRY["A1"]["method"] = (
        f"count(verified refuting sources) = {n_confirmed}"
    )
    FACT_REGISTRY["A1"]["result"] = str(n_confirmed)

    citation_detail = build_citation_detail(FACT_REGISTRY, citation_results, empirical_facts)

    # For qualitative proofs: extractions record citation verification status
    extractions = {}
    for fid, info in FACT_REGISTRY.items():
        if not fid.startswith("B"):
            continue
        ef_key = info["key"]
        cr = citation_results.get(ef_key, {})
        extractions[fid] = {
            "value": cr.get("status", "unknown"),
            "value_in_quote": cr.get("status") in COUNTABLE_STATUSES,
            "quote_snippet": empirical_facts[ef_key]["quote"][:80],
        }

    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,
        "cross_checks": [
            {
                "description": (
                    "Three independently published peer-reviewed reviews from different research "
                    "groups and years (PLOS One 2013, J Strength Cond Res 2017, PMC 2025) all "
                    "converge on non-superiority of CWT over other active recovery methods. "
                    "Independence: separate authors, journals, study populations, and timeframes."
                ),
                "values_compared": [
                    "PLOS One 2013 (Higgins): 'little evidence for a superior treatment intervention'",
                    "J Strength Cond Res 2017 (Versey): 'CWI beneficial, CWT was not beneficial'",
                    "PMC 2025 scoping: 'modest quality ... no clear conclusions about CT vs others'",
                ],
                "agreement": True,
            },
        ],
        "adversarial_checks": adversarial_checks,
        "verdict": verdict,
        "key_results": {
            "n_confirmed_refuting_sources": n_confirmed,
            "threshold": CLAIM_FORMAL["threshold"],
            "operator": CLAIM_FORMAL["operator"],
            "claim_holds": claim_holds,
            "proof_direction": "disprove",
            "recovery_finding": (
                "CWT not superior to other active methods per 3+ independent meta-analyses"
            ),
            "longevity_finding": (
                "No prospective studies on combined contrast therapy and longevity; "
                "sauna data (Laukkanen) applies to sauna alone, not contrast therapy"
            ),
        },
        "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("\n=== PROOF SUMMARY (JSON) ===")
    print(json.dumps(summary, indent=2, default=str))
