"""
Proof: The purchasing power of the US dollar has declined by more than 90%
since the Federal Reserve was established in 1913.

Generated: 2026-03-26
"""
import json
import os
import re
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)
_REPO_ROOT = os.path.dirname(os.path.dirname(os.path.dirname(PROOF_ENGINE_ROOT)))

from datetime import date

# --- STRUCTURAL IMPORTS ---
from scripts.extract_values import parse_number_from_quote, parse_date_from_quote
from scripts.smart_extract import normalize_unicode, verify_extraction
from scripts.verify_citations import verify_all_citations, verify_data_values
from scripts.computations import compare, explain_calc

# =============================================================================
# 1. CLAIM INTERPRETATION (Rule 4)
# =============================================================================
CLAIM_NATURAL = (
    "The purchasing power of the US dollar has declined by more than 90% "
    "since the Federal Reserve was established in 1913."
)

CLAIM_FORMAL = {
    "subject": "Purchasing power of the US dollar",
    "property": "percentage decline since 1913",
    "operator": ">",
    "operator_note": (
        "'More than 90%' is interpreted as strictly greater than 90.0%. "
        "If the decline were exactly 90.0%, the claim would be FALSE. "
        "This is the natural reading of 'more than'. "
        "'Purchasing power' is operationalized via the Consumer Price Index "
        "for All Urban Consumers (CPI-U), the standard BLS measure. "
        "Decline is computed as (1 - CPI_1913 / CPI_current) * 100. "
        "'Established in 1913' refers to the Federal Reserve Act signed "
        "December 23, 1913; the CPI baseline uses 1913 annual average."
    ),
    "threshold": 90.0,
}

# =============================================================================
# 2. FACT REGISTRY
# =============================================================================
FACT_REGISTRY = {
    "B1": {
        "key": "source_a_cpi",
        "label": "Source A CPI: 1913 annual avg = 9.883, 2024 annual avg = 313.689 (BLS via rateinflation.com)",
    },
    "B2": {
        "key": "source_b_cpi",
        "label": "Source B CPI: 1913 annual avg = 9.9, 2024 annual avg = 313.689 (BLS via inflationdata.com)",
    },
    "B3": {
        "key": "source_a_fed_date",
        "label": "Federal Reserve Act signed December 23, 1913 (Wikipedia)",
    },
    "B4": {
        "key": "source_b_fed_date",
        "label": "Federal Reserve Act signed December 23, 1913 (US Senate)",
    },
    "A1": {
        "label": "Purchasing power decline computed from Source A CPI values",
        "method": None,
        "result": None,
    },
    "A2": {
        "label": "Purchasing power decline computed from Source B CPI values",
        "method": None,
        "result": None,
    },
    "A3": {
        "label": "Cross-check: Source A and B decline percentages agree within tolerance",
        "method": None,
        "result": None,
    },
    "A4": {
        "label": "Claim evaluation: decline > 90%",
        "method": None,
        "result": None,
    },
}

# =============================================================================
# 3. EMPIRICAL FACTS (Rule 1 — quotes only, NO hand-typed values)
# =============================================================================
empirical_facts = {
    "source_a_cpi": {
        "quote": (
            "The CPI for USA is calculated and issued by: "
            "U.S. Bureau of Labor Statistics. CPI data is calculated and "
            "issued monthly."
        ),
        "url": "https://www.rateinflation.com/consumer-price-index/usa-historical-cpi/",
        "source_name": "RateInflation.com (sourced from U.S. Bureau of Labor Statistics)",
        "data_values": {"cpi_1913": "9.883", "cpi_2024": "313.689"},
    },
    "source_b_cpi": {
        "quote": (
            "A CPI of 195 indicates 95% inflation since 1982"
        ),
        "url": "https://inflationdata.com/Inflation/Consumer_Price_Index/HistoricalCPI.aspx",
        "source_name": "InflationData.com (sourced from U.S. Bureau of Labor Statistics)",
        "data_values": {"cpi_1913": "9.9", "cpi_2024": "313.689"},
    },
    "source_a_fed_date": {
        "quote": (
            "Signed into law by President Woodrow Wilson on December 23, 1913"
        ),
        "url": "https://en.wikipedia.org/wiki/Federal_Reserve_Act",
        "source_name": "Wikipedia: Federal Reserve Act",
    },
    "source_b_fed_date": {
        "quote": (
            "On December 23, 1913, the Senate adopted the conference report "
            "by a vote of 43 to 25"
        ),
        "url": "https://www.senate.gov/artandhistory/history/minute/Senate_Passes_the_Federal_Reserve_Act.htm",
        "source_name": "United States Senate Historical Office",
    },
}

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

# =============================================================================
# 5. DATA VALUE VERIFICATION — confirms CPI numbers appear on source pages
# =============================================================================
print("\n" + "=" * 60)
print("DATA VALUE VERIFICATION")
print("=" * 60)

dv_results_a = verify_data_values(
    empirical_facts["source_a_cpi"]["url"],
    empirical_facts["source_a_cpi"]["data_values"],
    "B1",
)
dv_results_b = verify_data_values(
    empirical_facts["source_b_cpi"]["url"],
    empirical_facts["source_b_cpi"]["data_values"],
    "B2",
)

# =============================================================================
# 6. VALUE EXTRACTION — parsed from data_values (no verify_extraction needed)
# =============================================================================
print("\n" + "=" * 60)
print("VALUE EXTRACTION")
print("=" * 60)

# --- Source A: CPI values from data_values ---
cpi_1913_a = parse_number_from_quote(
    empirical_facts["source_a_cpi"]["data_values"]["cpi_1913"],
    r"([\d.]+)",
    "B1_cpi_1913",
)
cpi_2024_a = parse_number_from_quote(
    empirical_facts["source_a_cpi"]["data_values"]["cpi_2024"],
    r"([\d.]+)",
    "B1_cpi_2024",
)

# --- Source B: CPI values from data_values ---
cpi_1913_b = parse_number_from_quote(
    empirical_facts["source_b_cpi"]["data_values"]["cpi_1913"],
    r"([\d.]+)",
    "B2_cpi_1913",
)
cpi_2024_b = parse_number_from_quote(
    empirical_facts["source_b_cpi"]["data_values"]["cpi_2024"],
    r"([\d.]+)",
    "B2_cpi_2024",
)

# --- Federal Reserve founding date ---
fed_date_a = parse_date_from_quote(
    empirical_facts["source_a_fed_date"]["quote"], "B3_fed_date"
)
# Verify the year (computation-relevant value) appears in quote
fed_date_a_in_quote = verify_extraction(
    fed_date_a.year, empirical_facts["source_a_fed_date"]["quote"], "B3", "fed_year"
)

fed_date_b = parse_date_from_quote(
    empirical_facts["source_b_fed_date"]["quote"], "B4_fed_date"
)
fed_date_b_in_quote = verify_extraction(
    fed_date_b.year, empirical_facts["source_b_fed_date"]["quote"], "B4", "fed_year"
)

# =============================================================================
# 6. CROSS-CHECKS (Rule 6) — Independent sources must agree
# =============================================================================
print("\n" + "=" * 60)
print("CROSS-CHECKS")
print("=" * 60)

# Cross-check 1: Fed founding dates must agree
assert fed_date_a == fed_date_b, (
    f"Fed founding dates disagree: source_a={fed_date_a}, source_b={fed_date_b}"
)
print(f"Fed date cross-check: {fed_date_a} == {fed_date_b} [AGREE]")

# Cross-check 2: CPI 2024 values must agree exactly
assert cpi_2024_a == cpi_2024_b, (
    f"CPI 2024 values disagree: source_a={cpi_2024_a}, source_b={cpi_2024_b}"
)
print(f"CPI 2024 cross-check: {cpi_2024_a} == {cpi_2024_b} [AGREE]")

# Cross-check 3: CPI 1913 values should agree within rounding tolerance
# Source A: 9.883 (3 decimal places), Source B: 9.9 (1 decimal place)
# 9.9 is 9.883 rounded to 1 decimal place — check this
cpi_1913_diff = abs(cpi_1913_a - cpi_1913_b)
cpi_1913_tolerance = 0.05  # rounding tolerance
cpi_1913_agree = cpi_1913_diff < cpi_1913_tolerance
print(f"CPI 1913 cross-check: {cpi_1913_a} vs {cpi_1913_b}, "
      f"diff={cpi_1913_diff:.3f}, tolerance={cpi_1913_tolerance} "
      f"[{'AGREE' if cpi_1913_agree else 'DISAGREE'}]")
assert cpi_1913_agree, (
    f"CPI 1913 values disagree beyond tolerance: "
    f"source_a={cpi_1913_a}, source_b={cpi_1913_b}, diff={cpi_1913_diff}"
)

# =============================================================================
# 7. SYSTEM TIME (Rule 3)
# =============================================================================
PROOF_GENERATION_DATE = date(2026, 3, 26)
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"\nDate check: {date_note}")
print(f"Note: CPI data used is 2024 annual average (most recent full year).")

# =============================================================================
# 8. COMPUTATION (Rule 7) — use bundled functions, explain_calc
# =============================================================================
print("\n" + "=" * 60)
print("COMPUTATION")
print("=" * 60)

# --- Source A computation ---
print("\n--- Source A (rateinflation.com, CPI 1913=9.883, CPI 2024=313.689) ---")
ratio_a = explain_calc("cpi_1913_a / cpi_2024_a", {
    "cpi_1913_a": cpi_1913_a,
    "cpi_2024_a": cpi_2024_a,
})
decline_a = explain_calc("(1 - cpi_1913_a / cpi_2024_a) * 100", {
    "cpi_1913_a": cpi_1913_a,
    "cpi_2024_a": cpi_2024_a,
})
print(f"Source A: $1.00 in 1913 has purchasing power of ${ratio_a:.4f} in 2024 dollars")
print(f"Source A: Decline = {decline_a:.2f}%")

# --- Source B computation ---
print("\n--- Source B (inflationdata.com, CPI 1913=9.9, CPI 2024=313.689) ---")
ratio_b = explain_calc("cpi_1913_b / cpi_2024_b", {
    "cpi_1913_b": cpi_1913_b,
    "cpi_2024_b": cpi_2024_b,
})
decline_b = explain_calc("(1 - cpi_1913_b / cpi_2024_b) * 100", {
    "cpi_1913_b": cpi_1913_b,
    "cpi_2024_b": cpi_2024_b,
})
print(f"Source B: $1.00 in 1913 has purchasing power of ${ratio_b:.4f} in 2024 dollars")
print(f"Source B: Decline = {decline_b:.2f}%")

# --- Cross-check: both declines should be very close ---
decline_diff = abs(decline_a - decline_b)
print(f"\nDecline cross-check: {decline_a:.4f}% vs {decline_b:.4f}%, diff={decline_diff:.4f}%")
assert decline_diff < 0.1, f"Decline computations diverge: {decline_diff:.4f}%"

# =============================================================================
# 9. CLAIM EVALUATION (Rule 7 — use compare(), not eval())
# =============================================================================
print("\n" + "=" * 60)
print("CLAIM EVALUATION")
print("=" * 60)

# Use the more precise Source A value for the primary verdict
claim_holds_a = compare(decline_a, CLAIM_FORMAL["operator"], CLAIM_FORMAL["threshold"])
claim_holds_b = compare(decline_b, CLAIM_FORMAL["operator"], CLAIM_FORMAL["threshold"])

print(f"\nSource A: {decline_a:.2f}% > 90.0% ? {claim_holds_a}")
print(f"Source B: {decline_b:.2f}% > 90.0% ? {claim_holds_b}")
print(f"Both sources agree on verdict: {claim_holds_a == claim_holds_b}")

# Margin above threshold
margin_a = explain_calc("decline_a - 90.0", {"decline_a": decline_a})
print(f"Margin above 90% threshold: {margin_a:.2f} percentage points")

# =============================================================================
# 10. ADVERSARIAL CHECKS (Rule 5)
# =============================================================================
print("\n" + "=" * 60)
print("ADVERSARIAL CHECKS")
print("=" * 60)

adversarial_checks = [
    {
        "question": (
            "Does hedonic quality adjustment mean the CPI overstates inflation, "
            "potentially bringing the true decline below 90%?"
        ),
        "search_performed": (
            "Web search: 'CPI hedonic adjustment overstate inflation purchasing power'"
        ),
        "finding": (
            "The BLS states that hedonic adjustments currently used outside shelter "
            "and apparel have increased the annual CPI rate of change by only about "
            "0.005% per year. Even the Boskin Commission (1996), which argued CPI "
            "overstated inflation, estimated the bias at ~1.1% per year. Over 111 years, "
            "even a 1.1% annual overstatement would still leave cumulative inflation far "
            "above the 90% decline threshold. The actual decline using CPI-U is ~96.8%, "
            "so the claim holds with a ~6.8 percentage point margin."
        ),
        "breaks_proof": False,
    },
    {
        "question": (
            "Was the Federal Reserve established in 1913 (Act signed) or 1914 "
            "(Reserve Banks opened)? Does the date choice affect the result?"
        ),
        "search_performed": (
            "Web search: 'Federal Reserve established 1913 vs 1914 operational'"
        ),
        "finding": (
            "The Federal Reserve Act was signed December 23, 1913. The Reserve Banks "
            "opened for business on November 16, 1914. The CPI annual average for 1914 "
            "was 10.0 (vs 9.9 for 1913). Using 1914 baseline: "
            "(1 - 10.0/313.689) * 100 = 96.81%. The ~0.03 percentage point difference "
            "is negligible. The claim holds regardless of which date is used."
        ),
        "breaks_proof": False,
    },
    {
        "question": (
            "Could using a different price index (PCE, GDP deflator) instead of CPI-U "
            "yield a decline of 90% or less?"
        ),
        "search_performed": (
            "Web search: 'PCE vs CPI inflation 1913 to present comparison'"
        ),
        "finding": (
            "The PCE deflator typically shows slightly lower inflation than CPI-U "
            "(roughly 0.3-0.5% per year lower since the 1990s). However, PCE data only "
            "goes back to 1929, not 1913. The GDP deflator shows a similar magnitude of "
            "total inflation over the full period. No standard US price index shows a "
            "decline of less than 90% over 111 years. The margin (~6.8pp) is too large "
            "for any reasonable index choice to flip the verdict."
        ),
        "breaks_proof": False,
    },
    {
        "question": (
            "Is CPI-U the right measure? CPI-U only began in 1978. What was used "
            "before that?"
        ),
        "search_performed": (
            "Web search: 'BLS CPI history before 1978 CPI-U CPI-W comparison'"
        ),
        "finding": (
            "Before 1978, there was a single CPI series (now called CPI-W, for urban "
            "wage earners and clerical workers). The BLS retroactively linked the pre-1978 "
            "series to the CPI-U series for continuity. The Minneapolis Fed confirms the "
            "data from 1913 onward is 'generally compatible through the present day.' "
            "Using CPI-W instead of CPI-U would yield nearly identical results for the "
            "1913-2024 period."
        ),
        "breaks_proof": False,
    },
]

for i, check in enumerate(adversarial_checks):
    print(f"\nAdversarial check {i+1}: {check['question'][:80]}...")
    print(f"  Breaks proof: {check['breaks_proof']}")

# =============================================================================
# 11. VERDICT AND STRUCTURED OUTPUT
# =============================================================================
if __name__ == "__main__":
    print("\n" + "=" * 60)
    print("VERDICT DETERMINATION")
    print("=" * 60)

    any_unverified = any(
        cr["status"] != "verified" for cr in citation_results.values()
    )

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

    print(f"\nVerdict: {verdict}")
    print(f"  Decline (Source A): {decline_a:.2f}%")
    print(f"  Decline (Source B): {decline_b:.2f}%")
    print(f"  Threshold: > {CLAIM_FORMAL['threshold']}%")
    print(f"  Margin: {margin_a:.2f} percentage points")

    # --- Populate Type A method/result ---
    FACT_REGISTRY["A1"]["method"] = "(1 - CPI_1913_A / CPI_2024_A) * 100"
    FACT_REGISTRY["A1"]["result"] = f"{decline_a:.4f}%"
    FACT_REGISTRY["A2"]["method"] = "(1 - CPI_1913_B / CPI_2024_B) * 100"
    FACT_REGISTRY["A2"]["result"] = f"{decline_b:.4f}%"
    FACT_REGISTRY["A3"]["method"] = "abs(decline_A - decline_B) < 0.1"
    FACT_REGISTRY["A3"]["result"] = f"diff={decline_diff:.4f}%, within tolerance"
    FACT_REGISTRY["A4"]["method"] = f"compare({decline_a:.4f}, '>', 90.0)"
    FACT_REGISTRY["A4"]["result"] = str(claim_holds_a)

    # --- Build citation details ---
    citation_detail = {}
    for fact_id, info in FACT_REGISTRY.items():
        key = info.get("key")
        if key and key in citation_results:
            cr = citation_results[key]
            citation_detail[fact_id] = {
                "source_key": key,
                "source_name": empirical_facts[key].get("source_name", ""),
                "url": empirical_facts[key].get("url", ""),
                "quote": empirical_facts[key].get("quote", ""),
                "status": cr["status"],
                "method": cr.get("method", ""),
                "coverage_pct": cr.get("coverage_pct", None),
                "fetch_mode": cr.get("fetch_mode", ""),
                "credibility": cr.get("credibility"),
            }

    # --- Build extraction records ---
    dv_a_ok = all(v.get("found", False) for v in dv_results_a.get("results", {}).values()) if isinstance(dv_results_a, dict) else False
    dv_b_ok = all(v.get("found", False) for v in dv_results_b.get("results", {}).values()) if isinstance(dv_results_b, dict) else False
    extractions = {
        "B1": {
            "value": f"CPI_1913={cpi_1913_a}, CPI_2024={cpi_2024_a}",
            "verified_via": "verify_data_values",
            "data_values_verified": dv_a_ok,
            "quote_snippet": empirical_facts["source_a_cpi"]["quote"][:80],
        },
        "B2": {
            "value": f"CPI_1913={cpi_1913_b}, CPI_2024={cpi_2024_b}",
            "verified_via": "verify_data_values",
            "data_values_verified": dv_b_ok,
            "quote_snippet": empirical_facts["source_b_cpi"]["quote"][:80],
        },
        "B3": {
            "value": str(fed_date_a),
            "value_in_quote": fed_date_a_in_quote,
            "quote_snippet": empirical_facts["source_a_fed_date"]["quote"][:80],
        },
        "B4": {
            "value": str(fed_date_b),
            "value_in_quote": fed_date_b_in_quote,
            "quote_snippet": empirical_facts["source_b_fed_date"]["quote"][:80],
        },
    }

    # --- Build cross-check records ---
    cross_checks = [
        {
            "description": "Fed founding date: independently sourced from Wikipedia and US Senate",
            "values_compared": [str(fed_date_a), str(fed_date_b)],
            "agreement": fed_date_a == fed_date_b,
        },
        {
            "description": "CPI 2024 annual average: independently sourced from rateinflation.com and inflationdata.com",
            "values_compared": [str(cpi_2024_a), str(cpi_2024_b)],
            "agreement": cpi_2024_a == cpi_2024_b,
        },
        {
            "description": "CPI 1913 annual average: agree within rounding tolerance (9.883 vs 9.9)",
            "values_compared": [str(cpi_1913_a), str(cpi_1913_b)],
            "agreement": cpi_1913_agree,
        },
        {
            "description": "Computed decline percentages agree within 0.1%",
            "values_compared": [f"{decline_a:.4f}%", f"{decline_b:.4f}%"],
            "agreement": decline_diff < 0.1,
        },
    ]

    # --- Build JSON summary ---
    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": cross_checks,
        "adversarial_checks": adversarial_checks,
        "verdict": verdict,
        "key_results": {
            "decline_source_a": round(decline_a, 4),
            "decline_source_b": round(decline_b, 4),
            "cpi_1913_a": cpi_1913_a,
            "cpi_2024_a": cpi_2024_a,
            "cpi_1913_b": cpi_1913_b,
            "cpi_2024_b": cpi_2024_b,
            "threshold": CLAIM_FORMAL["threshold"],
            "operator": CLAIM_FORMAL["operator"],
            "claim_holds": claim_holds_a and claim_holds_b,
            "margin_above_threshold": round(margin_a, 4),
            "fed_founding_date": str(fed_date_a),
        },
        "generator": {
            "name": "proof-engine",
            "version": open(os.path.join(_REPO_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))
