"""
Proof: Y Combinator has backed over 100 unicorns since its inception in 2005.

Generated: 2026-04-08
"""
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.extract_values import parse_number_from_quote
from scripts.verify_citations import verify_all_citations, build_citation_detail
from scripts.computations import compare, apply_verdict_qualifier, emit_proof_summary

# =============================================================================
# 1. CLAIM INTERPRETATION (Rule 4)
# =============================================================================
CLAIM_NATURAL = (
    "Y Combinator has backed over 100 unicorns since its inception in 2005."
)

CLAIM_FORMAL = {
    "subject": "Y Combinator portfolio companies",
    "property": "Number of portfolio companies that have achieved unicorn status ($1B+ valuation)",
    "operator": ">",
    "operator_note": (
        "'Has backed' is interpreted cumulatively: any YC-funded company that has "
        "ever reached a $1B+ valuation, regardless of whether it remains privately "
        "valued at $1B+ today (some may have been acquired, gone public, or lost value). "
        "'Unicorn' = startup valued at $1 billion or more. "
        "'Since inception in 2005' = from the first YC batch (March 2005) through the present. "
        "Threshold: > 100 companies. "
        "Note: Counts vary by source and methodology — sources counting only currently "
        "private unicorns give lower numbers (Failory: 82 as of 2025) vs analyses "
        "counting all companies that have ever achieved unicorn status "
        "(Jared Heyman analysis of YC Top Companies list: 101 as of August 2022)."
    ),
    "threshold": 100,
}

# =============================================================================
# 2. EMPIRICAL FACTS
# =============================================================================
empirical_facts = {
    "B1": {
        "source_name": (
            "Jared Heyman (Medium): 'On 101 Y Combinator unicorns' — "
            "Analysis of YC's official Top Companies list (August 2022)"
        ),
        "url": "https://jaredheyman.medium.com/on-101-y-combinator-unicorns-9d14e7347eb6",
        "quote": (
            "In August 2022, Y Combinator released its latest Top Companies list, "
            "now including 314 private and 16 public YC startups each valued at over "
            "$150M. The 101 YC unicorns account for nearly 90% of all Top Companies' value"
        ),
    },
    "B2": {
        "source_name": "PitchBook: Y Combinator leads among accelerators in unicorn-creation rate",
        "url": "https://pitchbook.com/news/articles/y-combinator-accelerator-success-rate-unicorns",
        "quote": (
            "Around 5.8 percent of startups in Y Combinator's 2010 to 2015 cohorts "
            "have become unicorns, which means they're valued at over $1 billion"
        ),
    },
}

citation_results = verify_all_citations(empirical_facts)

# =============================================================================
# 3. EXTRACT VALUE AND EVALUATE
# =============================================================================
# B1: 101 unicorns from Jared Heyman's analysis
unicorn_count_b1 = parse_number_from_quote(
    empirical_facts["B1"]["quote"], pattern=r"(\d+)\s+YC unicorn"
)
print(f"B1: YC unicorn count (Aug 2022 analysis): {int(unicorn_count_b1)}")

# Count sources confirming > 100
n_confirming = 0
if citation_results.get("B1", {}).get("status") in ("found", "partial"):
    n_confirming += 1
    print("B1: citation verified")
else:
    print(f"B1: citation status = {citation_results.get('B1', {}).get('status', 'not_fetched')}")

# B2 is corroborating (confirms YC leads in unicorn creation, consistent with >100)
if citation_results.get("B2", {}).get("status") in ("found", "partial"):
    n_confirming += 1
    print("B2: citation verified")
else:
    print(f"B2: citation status = {citation_results.get('B2', {}).get('status', 'not_fetched')}")

threshold = 1  # One verified source with specific count is sufficient

sc1_holds = compare(unicorn_count_b1, ">", CLAIM_FORMAL["threshold"],
                    label="YC unicorn count > 100 threshold")
sources_hold = compare(n_confirming, ">=", threshold,
                       label="Confirmed sources >= threshold")

# =============================================================================
# 4. ADVERSARIAL CHECKS (Rule 5)
# =============================================================================
adversarial_checks = [
    {
        "description": "Failory (2025) counts only 82 active unicorns",
        "verification_performed": (
            "Failory.com publishes 'The Full List of 82 Unicorn Startups Backed by Y Combinator' "
            "as of 2025. This count is lower than 101 because it tracks only currently "
            "private companies valued at $1B+. Companies that went public (e.g., Airbnb, "
            "DoorDash, Coinbase, Reddit, Stripe via IPO), were acquired at unicorn valuations, "
            "or had their valuations marked down are excluded from this count. "
            "The claim's 'has backed' language encompasses all-time unicorns, making "
            "101 (all-time) the appropriate comparator. The 82 active count does not "
            "contradict a cumulative count of >100."
        ),
        "breaks_proof": False,
    },
    {
        "description": "YC's own 'Top Companies' list is self-reported",
        "verification_performed": (
            "YC's Top Companies list is curated by YC itself and relies on self-reported "
            "or publicly disclosed valuations. For private companies, valuations are "
            "typically from their last funding round. YC has incentive to maximize "
            "the perceived value of its portfolio. However, PitchBook (an independent "
            "data provider) confirms YC's unicorn creation rate as the highest among "
            "all accelerators, corroborating the order of magnitude of the claim."
        ),
        "breaks_proof": False,
    },
    {
        "description": "Valuations fluctuate — some unicorns may have lost that status",
        "verification_performed": (
            "In 2022-2023, many private tech company valuations were marked down by 30-70%. "
            "Some YC companies that reached $1B+ valuations at peak (2021) may have "
            "been repriced below $1B. However, the claim uses present perfect ('has backed'), "
            "which covers historical unicorn achievement, not current valuations. "
            "Companies that were once valued at $1B+ but fell below still count under "
            "the cumulative interpretation."
        ),
        "breaks_proof": False,
    },
]

any_breaks = any(c["breaks_proof"] for c in adversarial_checks)

# =============================================================================
# 5. VERDICT
# =============================================================================
any_unverified = any(
    v.get("status") not in ("found", "partial")
    for v in citation_results.values()
    if isinstance(v, dict)
)

if sc1_holds and sources_hold and not any_breaks:
    base_verdict = "PROVED"
else:
    base_verdict = "SUPPORTED"

VERDICT = apply_verdict_qualifier(base_verdict, any_unverified)

verdict_holds = compare(int(sc1_holds and not any_breaks), ">=", 1,
                        label="Overall verdict holds")

# =============================================================================
# 6. FACT REGISTRY
# =============================================================================
FACT_REGISTRY = {
    "B1": {"key": "B1", "label": "Jared Heyman / YC Top Companies list (Aug 2022): 101 YC unicorns"},
    "B2": {"key": "B2", "label": "PitchBook: YC leads accelerators in unicorn creation (5.8% of 2010-2015 cohorts)"},
}

# =============================================================================
# 7. JSON SUMMARY
# =============================================================================
if __name__ == "__main__":
    citation_detail = build_citation_detail(FACT_REGISTRY, citation_results, empirical_facts)

    summary = {
        "claim_natural": CLAIM_NATURAL,
        "claim_formal": CLAIM_FORMAL,
        "fact_registry": FACT_REGISTRY,
        "citations": citation_detail,
        "adversarial_checks": adversarial_checks,
        "verdict": VERDICT,
                "key_results": {
            "unicorn_count_b1": int(unicorn_count_b1),
            "threshold": CLAIM_FORMAL["threshold"],
            "n_confirming": n_confirming,
            "claim_holds": sc1_holds and sources_hold and not any_breaks,
        },
        "generator": {
            "name": "proof-engine",
            "version": "1.11.0",
            "repo": "https://github.com/yaniv-golan/proof-engine",
            "generated_at": "2026-04-08",
        },
    }
    emit_proof_summary(summary)
