"""
Proof: A mathematical model proves the world will end on a specific day in 2026.
Generated: 2026-03-28

Context: The only known mathematical model predicting a "doomsday" in 2026 is the
von Foerster et al. (1960) paper published in Science, which modeled population
growth as a hyperbolic function that approaches infinity on November 13, 2026.
This proof evaluates whether that model (or any other) constitutes a scientific
"proof" that the world will end on a specific day in 2026.
"""
import json
import os
import sys
from datetime import date

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 scripts.verify_citations import verify_all_citations, build_citation_detail
from scripts.computations import compare, explain_calc

# 1. CLAIM INTERPRETATION (Rule 4)
CLAIM_NATURAL = "A mathematical model proves the world will end on a specific day in 2026."
CLAIM_FORMAL = {
    "subject": "The von Foerster et al. (1960) mathematical model (the only known candidate)",
    "property": (
        "constitutes a scientifically credible proof that Earth / human civilization "
        "will effectively end on a specific day in 2026"
    ),
    "operator": ">=",
    "operator_note": (
        "The claim asserts a mathematical model 'proves' world-end on a specific day in 2026. "
        "We interpret 'proves' as: the model provides a scientifically valid, evidence-supported "
        "prediction of Earth's end (physical or civilizational) on a specific 2026 date. "
        "The only known candidate is von Foerster, Mora & Amiot (1960, Science), which predicted "
        "population growth would reach a mathematical singularity (infinity) on Friday, "
        "November 13, 2026. "
        "proof_direction=disprove: We collect sources that REJECT the claim. "
        "The claim is DISPROVED if >=2 independent sources confirm (a) Earth's actual "
        "lifespan is billions of years, not decades, and/or (b) the von Foerster model's "
        "core assumptions have been observably falsified. "
        "Threshold=2 (not default 3): counter-evidence is overwhelming and only a few "
        "authoritative sources exist above astrophysics and demography."
    ),
    "threshold": 2,
    "proof_direction": "disprove",
}

# 2. FACT REGISTRY
FACT_REGISTRY = {
    "B1": {
        "key": "source_future_earth",
        "label": "Wikipedia Future of Earth: Sun engulfs Earth in ~7.59 billion years",
    },
    "B2": {
        "key": "source_ladbible",
        "label": "LADbible: von Foerster model's exponential growth assumption falsified",
    },
    "B3": {
        "key": "source_livescience",
        "label": "Live Science: Earth habitable for another 1.75 billion years",
    },
    "A1": {
        "label": "Verified disproof source count vs threshold",
        "method": None,
        "result": None,
    },
}

# 3. EMPIRICAL FACTS — sources that SUPPORT the DISPROOF (contradict the claim)
#    For a disproof, empirical_facts collect evidence that the claim is FALSE.
empirical_facts = {
    "source_future_earth": {
        "quote": (
            "These effects will counterbalance the impact of mass loss by the Sun, "
            "and the Sun will likely engulf Earth in about 7.59 billion years from now."
        ),
        "url": "https://en.wikipedia.org/wiki/Future_of_Earth",
        "source_name": "Wikipedia: Future of Earth (synthesizing peer-reviewed astrophysics)",
    },
    "source_ladbible": {
        "quote": (
            "With the exponential growth of the population halted, largely because women are "
            "choosing to have fewer children in some of the world's largest countries, a 2026 "
            "apocalypse is less likely."
        ),
        "url": "https://www.ladbible.com/news/science/when-will-world-end-date-408044-20260109",
        "source_name": "LADbible: Chilling mathematical equation predicted world end date (Jan 2026)",
    },
    "source_livescience": {
        "quote": (
            "in another 1.75 billion years the planet will travel out of the solar system's "
            "habitable zone and into a hot zone that will scorch away its oceans"
        ),
        "url": "https://www.livescience.com/39775-how-long-can-earth-support-life.html",
        "source_name": "Live Science: How Much Longer Can Earth Support Life?",
    },
}

# 4. CITATION VERIFICATION (Rule 2)
print("\n--- Citation Verification ---")
citation_results = verify_all_citations(empirical_facts, wayback_fallback=True)

# 5. COUNT VERIFIED DISPROOF SOURCES
COUNTABLE_STATUSES = ("verified", "partial")
n_confirmed = sum(
    1 for key in empirical_facts
    if citation_results[key]["status"] in COUNTABLE_STATUSES
)
print(f"\n  Confirmed disproof sources: {n_confirmed} / {len(empirical_facts)}")

# 6. CLAIM EVALUATION — disproof established if n_confirmed >= threshold (Rule 7)
claim_holds = compare(
    n_confirmed,
    CLAIM_FORMAL["operator"],
    CLAIM_FORMAL["threshold"],
    label="verified disproof sources vs threshold",
)

# 7. SYSTEM TIME ANCHOR (Rule 3)
PROOF_GENERATION_DATE = date(2026, 3, 28)
today = date.today()
if today == PROOF_GENERATION_DATE:
    date_note = "System date matches proof generation date"
else:
    date_note = f"Proof generated for {PROOF_GENERATION_DATE}, running on {today}"
print(f"\n  Date anchor: {date_note}")

# Type A observational fact: days until the predicted doomsday date
predicted_end_date = date(2026, 11, 13)
days_until_prediction = explain_calc(
    "(predicted_end_date - today).days", locals(),
    label="days from today until predicted Nov 13 2026 doomsday"
)
# If positive: date hasn't arrived; model can still be evaluated on its falsified assumptions.
# If zero or negative: the predicted date passed without event — additional direct falsification.

# 8. ADVERSARIAL CHECKS (Rule 5)
adversarial_checks = [
    {
        "question": (
            "Was the von Foerster 1960 paper peer-reviewed and published in a credible journal? "
            "Could the model still be considered scientifically valid?"
        ),
        "verification_performed": (
            "Searched 'von Foerster 1960 Science paper doomsday equation'. "
            "Confirmed: von Foerster, Mora & Amiot published 'Doomsday: Friday, 13 November, A.D. 2026' "
            "in Science (Vol. 132, pp. 1291-1295, 4 Nov 1960) — a top-tier peer-reviewed journal. "
            "However, the paper was explicitly presented as a conditional extrapolation under extreme "
            "assumptions (unlimited food, no nuclear war, ever-accelerating growth), not a literal "
            "prediction. The authors framed it as a warning about what would happen IF growth continued "
            "unchecked. The title itself contained 'Doomsday' in quotation marks in some reprints."
        ),
        "finding": (
            "The paper is legitimate peer-reviewed science, but its doomsday conclusion was always "
            "conditional. By 2026 the core assumption — super-exponential population acceleration — "
            "is observably false. The UN projects population peaking at 10.3 billion in the 2080s "
            "and declining, not reaching infinity. A model whose assumptions are falsified does not "
            "'prove' its conclusion."
        ),
        "breaks_proof": False,
    },
    {
        "question": "Are there other mathematical models (besides von Foerster) predicting world end in 2026?",
        "verification_performed": (
            "Searched 'mathematical model predicts world end 2026 scientific peer reviewed'. "
            "Other 2026 'end of world' claims found: (1) Messiah Foundation International — "
            "fringe religious group predicting asteroid impact, no mathematical model; "
            "(2) Various numerology-based claims with no scientific basis. "
            "No peer-reviewed mathematical model other than von Foerster predicts world end in 2026."
        ),
        "finding": (
            "Von Foerster (1960) is the only peer-reviewed mathematical model cited in connection "
            "with a specific 2026 doomsday date. No additional credible models support the claim. "
            "The claim therefore rises or falls with von Foerster — and that model's assumptions "
            "have been falsified."
        ),
        "breaks_proof": False,
    },
    {
        "question": (
            "Could 'world will end' refer only to civilizational collapse from overpopulation "
            "rather than physical Earth destruction? Would this charitable reading rescue the claim?"
        ),
        "verification_performed": (
            "Considered whether 'world ends' = civilizational collapse from overpopulation could "
            "save the claim. The von Foerster model predicted population reaching a mathematical "
            "singularity — a feature of the equation, not a validated physical mechanism. "
            "Even under the civilizational-collapse reading: (a) population growth has slowed, "
            "not accelerated; (b) the model's mechanism (overcrowding to death) is not operational; "
            "(c) the UN projects a stable peak of 10.3 billion, not runaway growth."
        ),
        "finding": (
            "Even the charitable reading fails. A model whose driving assumption (ever-accelerating "
            "growth) is falsified does not 'prove' any version of world-end in 2026. "
            "The equation's singularity is a mathematical artifact of an invalid input assumption."
        ),
        "breaks_proof": False,
    },
]

# 9. 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 disproof citations) = {n_confirmed}"
    FACT_REGISTRY["A1"]["result"] = str(n_confirmed)

    citation_detail = build_citation_detail(FACT_REGISTRY, citation_results, empirical_facts)

    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 independent sources from different domains consulted: "
                    "astrophysics (B1, B3) and science journalism on demography (B2). "
                    "B1 and B3 independently confirm Earth's lifespan in billions of years. "
                    "B2 confirms the von Foerster model's assumptions were falsified."
                ),
                "n_sources_consulted": len(empirical_facts),
                "n_sources_verified": n_confirmed,
                "sources": {k: citation_results[k]["status"] for k in empirical_facts},
                "independence_note": (
                    "B1 (Wikipedia/astrophysics) and B3 (Live Science/habitability research) "
                    "are independent measurements of Earth's projected lifespan from different "
                    "research groups. B2 (LADbible) documents the demographic evidence separately. "
                    "All three reach the same conclusion through different mechanisms."
                ),
            }
        ],
        "adversarial_checks": adversarial_checks,
        "verdict": verdict,
        "key_results": {
            "n_confirmed_disproof_sources": n_confirmed,
            "threshold": CLAIM_FORMAL["threshold"],
            "operator": CLAIM_FORMAL["operator"],
            "claim_holds": claim_holds,
            "days_until_predicted_end_date": int(days_until_prediction),
            "predicted_end_date": str(predicted_end_date),
            "date_note": date_note,
            "earth_lifespan_years_remaining": "~7.59 billion (until solar engulfment)",
            "model_assumption_status": "FALSIFIED (population growth slowed dramatically)",
        },
        "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))
