"""
Proof: The 2005 Israeli disengagement from Gaza removed every settlement and military outpost
AND resulted in Hamas winning the January 2006 parliamentary elections followed by its
complete takeover of the territory in 2007.
Generated: 2026-03-27
"""
import json
from datetime import date
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 scripts.smart_extract import verify_extraction
from scripts.verify_citations import verify_all_citations, build_citation_detail
from scripts.computations import compare

# 1. CLAIM INTERPRETATION (Rule 4)
CLAIM_NATURAL = (
    "The 2005 Israeli disengagement from Gaza removed every settlement and military outpost "
    "AND resulted in Hamas winning the January 2006 parliamentary elections followed by its "
    "complete takeover of the territory in 2007."
)
CLAIM_FORMAL = {
    "subject": "2005 Israeli disengagement from Gaza and its aftermath",
    "sub_claims": [
        {
            "id": "SC1",
            "property": "All Israeli civilian settlements in Gaza were removed in 2005",
            "operator": ">=",
            "threshold": 2,
            "operator_note": (
                "Requires 2 independent sources confirming all 21 Gaza settlements were dismantled. "
                "A single settlement remaining would disprove this sub-claim."
            ),
        },
        {
            "id": "SC2",
            "property": "All Israeli military outposts/installations in Gaza were removed in 2005",
            "operator": ">=",
            "threshold": 1,
            "operator_note": (
                "Requires at least 1 authoritative source confirming all military installations "
                "were removed from Gaza soil. Retention of airspace/naval control is noted as a caveat "
                "but does not contradict removal of ground military outposts and installations."
            ),
        },
        {
            "id": "SC3",
            "property": "Hamas won the January 2006 Palestinian parliamentary elections",
            "operator": ">=",
            "threshold": 2,
            "operator_note": (
                "Requires 2 independent sources confirming Hamas won a parliamentary majority "
                "in elections held specifically in January 2006. 'Winning' means Hamas obtained "
                "enough seats to form a government (>=67 of 132 seats)."
            ),
        },
        {
            "id": "SC4",
            "property": "Hamas achieved complete takeover of Gaza territory in 2007",
            "operator": ">=",
            "threshold": 2,
            "operator_note": (
                "Requires 2 independent sources confirming Hamas seized full de facto control "
                "of Gaza in 2007. 'Complete' means all Palestinian Authority institutions in Gaza "
                "were seized; Abbas's rival West Bank PA government does not negate Gaza control."
            ),
        },
    ],
    "compound_operator": "AND",
    "operator_note": (
        "All four sub-claims must hold for the compound claim to be PROVED. "
        "The phrase 'resulted in' is interpreted as temporal sequence plus widely recognized causal "
        "context: disengagement completed Sep 2005, Hamas won elections Jan 2006, Hamas seized Gaza Jun 2007. "
        "Strict causal proof (disengagement uniquely caused Hamas win) is beyond empirical fact-checking scope "
        "and is noted as a caveat in adversarial checks."
    ),
}

# 2. FACT REGISTRY
FACT_REGISTRY = {
    "B1": {"key": "wiki_disengagement", "label": "Wikipedia: All 21 Gaza settlements dismantled in 2005 disengagement"},
    "B2": {"key": "britannica_disengagement", "label": "Britannica: Complete removal of settlers and soldiers from Gaza"},
    "B3": {"key": "adl_disengagement", "label": "ADL: All Israeli military installations removed from Gaza"},
    "B4": {"key": "wiki_2006_election", "label": "Wikipedia: Hamas won 74/132 seats in January 25, 2006 elections"},
    "B5": {"key": "globalsec_2006_election", "label": "GlobalSecurity.org: Hamas won decisive majority in Jan 25, 2006 elections"},
    "B6": {"key": "wiki_battle_gaza", "label": "Wikipedia: Hamas completed takeover of Gaza on June 15, 2007"},
    "B7": {"key": "ecf_takeover", "label": "ECF: Complete Hamas victory in June 2007 Gaza confrontation"},
    "A1": {"label": "SC1 source count: independent sources confirming all settlements removed", "method": None, "result": None},
    "A2": {"label": "SC2 source count: independent sources confirming all military outposts removed", "method": None, "result": None},
    "A3": {"label": "SC3 source count: independent sources confirming Hamas January 2006 election win", "method": None, "result": None},
    "A4": {"label": "SC4 source count: independent sources confirming complete 2007 Hamas takeover", "method": None, "result": None},
    "A5": {"label": "Compound verdict: number of sub-claims holding out of 4", "method": None, "result": None},
}

# 3. EMPIRICAL FACTS
empirical_facts = {
    "wiki_disengagement": {
        "quote": "Israel disengaged from the Gaza Strip by dismantling all 21 Israeli settlements there.",
        "url": "https://en.wikipedia.org/wiki/Israeli_disengagement_from_the_Gaza_Strip",
        "source_name": "Wikipedia: Israeli disengagement from the Gaza Strip",
    },
    "britannica_disengagement": {
        "quote": "complete removal of Israeli settlers and soldiers from the Gaza Strip",
        "url": "https://www.britannica.com/event/Israels-disengagement-from-Gaza",
        "source_name": "Britannica: Israel's disengagement from Gaza (2005)",
    },
    "adl_disengagement": {
        "quote": "removing all Israeli military installations, 25 Israeli settlements (4 in the West Bank) with over 8,000 residents",
        "url": "https://www.adl.org/resources/backgrounder/disengagement",
        "source_name": "ADL: Israeli Disengagement Background",
    },
    "wiki_2006_election": {
        "quote": "Legislative elections were held in the Palestinian territories on 25 January 2006 in order to elect the second Palestinian Legislative Council. The result was a victory for Hamas, contesting under the list name of Change and Reform, which received 44.45% of the vote and won 74 of the 132 seats.",
        "url": "https://en.wikipedia.org/wiki/2006_Palestinian_legislative_election",
        "source_name": "Wikipedia: 2006 Palestinian legislative election",
    },
    "globalsec_2006_election": {
        "quote": "In the 25 January 2006 Palestinian parliamentary elections, Hamas won a decisive majority in the Palestinian Legislative Council or Parliament. Of the 132-seat Parliament, Hamas won 74 seats, thereby ending the Fatah party's control of the Palestinian Authority.",
        "url": "https://www.globalsecurity.org/military/world/palestine/pa-elections2006.htm",
        "source_name": "GlobalSecurity.org: Palestinian Parliamentary Elections 2006",
    },
    "wiki_battle_gaza": {
        "quote": "On 15 June, Hamas completed taking control of the Gaza Strip, seizing all PNA government institutions and replacing all PNA officials in Gaza with Hamas members.",
        "url": "https://en.wikipedia.org/wiki/Battle_of_Gaza_(2007)",
        "source_name": "Wikipedia: Battle of Gaza (2007)",
    },
    "ecf_takeover": {
        "quote": "A short confrontation between Fatah and Hamas over control of the Gaza Strip, concluding with a complete victory for the latter",
        "url": "https://ecf.org.il/issues/issue/244",
        "source_name": "Economic Cooperation Foundation: Hamas Takeover of the Gaza Strip (2007)",
    },
}

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

# 5. KEYWORD EXTRACTION — Rule 1 (verify key terms appear in quotes)

# SC1: All settlements removed (2 sources required)
sc1_conf_a = verify_extraction("21", empirical_facts["wiki_disengagement"]["quote"], "B1")
sc1_conf_b = verify_extraction("removal", empirical_facts["britannica_disengagement"]["quote"], "B2")
sc1_sources = [sc1_conf_a, sc1_conf_b]
n_sc1 = sum(1 for c in sc1_sources if c)

# SC2: All military installations removed (1 source required)
sc2_conf = verify_extraction("military", empirical_facts["adl_disengagement"]["quote"], "B3")
sc2_sources = [sc2_conf]
n_sc2 = sum(1 for c in sc2_sources if c)

# SC3: Hamas won January 2006 elections (2 sources required)
sc3_conf_a = verify_extraction("25 January 2006", empirical_facts["wiki_2006_election"]["quote"], "B4")
sc3_conf_b = verify_extraction("74 seats", empirical_facts["globalsec_2006_election"]["quote"], "B5")
sc3_sources = [sc3_conf_a, sc3_conf_b]
n_sc3 = sum(1 for c in sc3_sources if c)

# SC4: Complete Hamas takeover of Gaza in 2007 (2 sources required)
sc4_conf_a = verify_extraction("completed", empirical_facts["wiki_battle_gaza"]["quote"], "B6")
sc4_conf_b = verify_extraction("complete", empirical_facts["ecf_takeover"]["quote"], "B7")
sc4_sources = [sc4_conf_a, sc4_conf_b]
n_sc4 = sum(1 for c in sc4_sources if c)

# 6. COMPOUND CLAIM EVALUATION (Rule 7 — use compare() for all comparisons)
sc1_spec = CLAIM_FORMAL["sub_claims"][0]
sc2_spec = CLAIM_FORMAL["sub_claims"][1]
sc3_spec = CLAIM_FORMAL["sub_claims"][2]
sc4_spec = CLAIM_FORMAL["sub_claims"][3]

sc1_holds = compare(n_sc1, sc1_spec["operator"], sc1_spec["threshold"])
sc2_holds = compare(n_sc2, sc2_spec["operator"], sc2_spec["threshold"])
sc3_holds = compare(n_sc3, sc3_spec["operator"], sc3_spec["threshold"])
sc4_holds = compare(n_sc4, sc4_spec["operator"], sc4_spec["threshold"])

n_holding = sum([sc1_holds, sc2_holds, sc3_holds, sc4_holds])
n_total = 4
claim_holds = compare(n_holding, "==", n_total)  # True only if ALL sub-claims hold

# 7. ADVERSARIAL CHECKS (Rule 5)
adversarial_checks = [
    {
        "question": "Did any Israeli settlement or military installation remain in Gaza after the 2005 disengagement?",
        "verification_performed": (
            "Searched 'Israeli settlement remained Gaza after disengagement 2005' and "
            "'Gaza military base retained Israel 2005 exception'. Reviewed Wikipedia, Britannica, "
            "UN reports, and news archives."
        ),
        "finding": (
            "No civilian settlement or ground military installation remained. All 21 settlements were "
            "dismantled; IDF ground forces withdrew by September 12, 2005. Israel retained aerial and "
            "naval control (airspace, coastline), which the UN and human rights bodies argue constitutes "
            "continued occupation, but no ground outposts were retained on Gaza soil."
        ),
        "breaks_proof": False,
    },
    {
        "question": "Did Hamas win a true parliamentary majority in January 2006, or only a plurality?",
        "verification_performed": (
            "Verified seat count against 132-seat threshold: Hamas won 74 seats. Majority requires 67+ seats. "
            "74 > 67, confirmed by Wikipedia and GlobalSecurity independently."
        ),
        "finding": (
            "Hamas won 74 of 132 seats (56.1%), a clear majority exceeding the 67-seat threshold. "
            "This is a majority, not merely a plurality, enabling Hamas to form a government."
        ),
        "breaks_proof": False,
    },
    {
        "question": "Was the Hamas 2007 Gaza takeover truly 'complete', or did Fatah retain any territorial control?",
        "verification_performed": (
            "Searched 'Hamas 2007 Gaza takeover incomplete Fatah retained area' and reviewed "
            "Wikipedia Battle of Gaza (2007) and ECF. Checked whether any Fatah stronghold persisted."
        ),
        "finding": (
            "Hamas seized all PA government institutions in Gaza by June 15, 2007. President Abbas "
            "dissolved the unity government and created a rival West Bank PA, but no part of Gaza "
            "territory remained under Fatah/PA control. ECF explicitly calls it 'complete victory'. "
            "Wikipedia: 'Hamas completed taking control of the Gaza Strip'."
        ),
        "breaks_proof": False,
    },
    {
        "question": "Does 'resulted in' require proving strict causation between disengagement and Hamas's election win?",
        "verification_performed": (
            "Reviewed academic literature and policy analyses on causes of Hamas's 2006 win. "
            "Searched 'Hamas 2006 election win causes disengagement Fatah corruption'."
        ),
        "finding": (
            "Strict causal proof is contested: analysts cite Fatah corruption, poor governance, and Hamas's "
            "social service network as co-causes. The temporal sequence (disengagement Sep 2005 → Hamas "
            "election Jan 2006 → Hamas takeover Jun 2007) is uncontested across all sources. The proof "
            "interprets 'resulted in' as verified temporal sequence with widely recognized causal context; "
            "no source disputes the sequence."
        ),
        "breaks_proof": False,
    },
]

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

    if any_breaks:
        verdict = "UNDETERMINED"
    elif n_holding < n_total and n_holding > 0:
        verdict = "PARTIALLY VERIFIED"
    elif claim_holds and not any_unverified:
        verdict = "PROVED"
    elif claim_holds and any_unverified:
        verdict = "PROVED (with unverified citations)"
    else:
        verdict = "UNDETERMINED"

    FACT_REGISTRY["A1"]["method"] = f"sum(sc1_sources) = {n_sc1} / threshold {sc1_spec['threshold']}"
    FACT_REGISTRY["A1"]["result"] = str(n_sc1)
    FACT_REGISTRY["A2"]["method"] = f"sum(sc2_sources) = {n_sc2} / threshold {sc2_spec['threshold']}"
    FACT_REGISTRY["A2"]["result"] = str(n_sc2)
    FACT_REGISTRY["A3"]["method"] = f"sum(sc3_sources) = {n_sc3} / threshold {sc3_spec['threshold']}"
    FACT_REGISTRY["A3"]["result"] = str(n_sc3)
    FACT_REGISTRY["A4"]["method"] = f"sum(sc4_sources) = {n_sc4} / threshold {sc4_spec['threshold']}"
    FACT_REGISTRY["A4"]["result"] = str(n_sc4)
    FACT_REGISTRY["A5"]["method"] = f"n_holding / n_total = {n_holding} / {n_total}"
    FACT_REGISTRY["A5"]["result"] = f"{n_holding}/{n_total}"

    citation_detail = build_citation_detail(FACT_REGISTRY, citation_results, empirical_facts)

    fact_keys_ordered = [
        "wiki_disengagement", "britannica_disengagement", "adl_disengagement",
        "wiki_2006_election", "globalsec_2006_election", "wiki_battle_gaza", "ecf_takeover",
    ]
    all_confirmations = sc1_sources + sc2_sources + sc3_sources + sc4_sources
    extractions = {}
    for i, (fact_key, conf) in enumerate(zip(fact_keys_ordered, all_confirmations)):
        fact_id = f"B{i + 1}"
        extractions[fact_id] = {
            "value": "keyword confirmed" if conf else "keyword not found",
            "value_in_quote": conf,
            "quote_snippet": empirical_facts[fact_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": "SC1 (all settlements removed): Wikipedia and Britannica independently confirm",
                "n_sources": len(sc1_sources),
                "n_confirming": n_sc1,
                "agreement": n_sc1 == len(sc1_sources),
            },
            {
                "description": "SC3 (Hamas Jan 2006 win): Wikipedia and GlobalSecurity independently confirm",
                "n_sources": len(sc3_sources),
                "n_confirming": n_sc3,
                "agreement": n_sc3 == len(sc3_sources),
            },
            {
                "description": "SC4 (Hamas 2007 complete takeover): Wikipedia and ECF independently confirm",
                "n_sources": len(sc4_sources),
                "n_confirming": n_sc4,
                "agreement": n_sc4 == len(sc4_sources),
            },
        ],
        "adversarial_checks": adversarial_checks,
        "verdict": verdict,
        "key_results": {
            "sc1_settlements_removed": {"n_confirming": n_sc1, "threshold": sc1_spec["threshold"], "holds": sc1_holds},
            "sc2_military_removed": {"n_confirming": n_sc2, "threshold": sc2_spec["threshold"], "holds": sc2_holds},
            "sc3_hamas_election_jan2006": {"n_confirming": n_sc3, "threshold": sc3_spec["threshold"], "holds": sc3_holds},
            "sc4_hamas_takeover_2007": {"n_confirming": n_sc4, "threshold": sc4_spec["threshold"], "holds": sc4_holds},
            "n_holding": n_holding,
            "n_total": n_total,
            "claim_holds": claim_holds,
        },
        "generator": {
            "name": "proof-engine",
            "version": "0.10.0",
            "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))
