#!/usr/bin/env python3
from __future__ import annotations
import html,json
from pathlib import Path
ROOT=Path(__file__).resolve().parents[1]; DATA=ROOT/'data'; SITE=ROOT/'site'; BADGES=ROOT/'badges'
PROOF=DATA/'rsi-capability-treasury-flywheel-proof.json'; SITE.mkdir(parents=True,exist_ok=True); BADGES.mkdir(parents=True,exist_ok=True)
def esc(x): return html.escape(str(x))
def main():
    proof=json.loads(PROOF.read_text(encoding='utf-8')); f=proof['final']
    cards=''.join(f"<tr><td>{esc(k)}</td><td>{v['value_capture_rate_percent']}%</td><td>{v['risk_breach_rate_percent']}%</td></tr>" for k,v in proof['baselines_and_controls'].items())
    gates=''.join(f"<tr><td>{esc(k)}</td><td>{'passed' if v else 'failed'}</td></tr>" for k,v in proof['pre_registered_gates'].items())
    html_text=f"""<!doctype html><html><head><meta charset="utf-8"><meta name="viewport" content="width=device-width,initial-scale=1"><title>SkillOS Capability Treasury Flywheel Proof</title><style>body{{margin:0;font-family:-apple-system,BlinkMacSystemFont,Segoe UI,sans-serif;background:linear-gradient(135deg,#06131f,#13243d 60%,#282a5d);color:#f5fbff}}main{{max-width:1180px;margin:auto;padding:44px 20px 80px}}h1{{font-size:clamp(42px,7vw,92px);line-height:.88;letter-spacing:-.07em}}p,td{{color:#b8c8d8;font-size:18px;line-height:1.5}}.grid{{display:grid;grid-template-columns:repeat(4,1fr);gap:16px}}.metric,.card{{background:rgba(255,255,255,.075);border:1px solid rgba(255,255,255,.16);border-radius:26px;padding:22px;margin:18px 0}}strong{{color:#7dffb0;font-size:32px;display:block}}table{{width:100%;border-collapse:collapse;background:rgba(255,255,255,.07);border-radius:16px;overflow:hidden}}td,th{{padding:12px;border-bottom:1px solid rgba(255,255,255,.12);text-align:left}}th{{color:#86f8ff}}a{{color:#86f8ff}}</style></head><body><main><p style="color:#86f8ff;text-transform:uppercase;letter-spacing:.18em;font-weight:900">MONTREAL.AI / SKILLOS</p><h1>Capability Treasury Flywheel.</h1><p>Autonomous proof that SkillOS reinvests capability-market signals into skill bounties, verifier capacity, routing upgrades, and compounding capability supply.</p><section class="grid"><div class="metric"><strong>{f['value_capture_rate_percent']}%</strong>value capture</div><div class="metric"><strong>{f['minimum_domain_value_capture_percent']}%</strong>minimum domain capture</div><div class="metric"><strong>{f['reinvestment_yield_score']}%</strong>reinvestment yield</div><div class="metric"><strong>{f['risk_breach_rate_percent']}%</strong>risk breach</div></section><div class="card"><h2>Core mechanism</h2><p>proof receipts -> value signals -> treasury allocation -> skill bounties -> verifier capacity -> routing upgrades -> reinvestment -> compounding capability supply</p></div><h2>Baselines and controls</h2><table><tr><th>System</th><th>Value capture</th><th>Risk breach</th></tr>{cards}</table><h2>Pre-registered gates</h2><table><tr><th>Gate</th><th>Status</th></tr>{gates}</table><div class="card"><p>{esc(proof['public_boundary'])}</p><p>Protocol fingerprint: {esc(proof['protocol_fingerprint_sha256'])}</p></div></main></body></html>"""
    (SITE/'rsi-capability-treasury-flywheel-proof.html').write_text(html_text,encoding='utf-8')
    (BADGES/'rsi-capability-treasury-flywheel-proof.svg').write_text('<svg xmlns="http://www.w3.org/2000/svg" width="330" height="20"><rect width="330" height="20" rx="10" fill="#14233a"/><rect x="228" width="102" height="20" rx="10" fill="#2bb673"/><text x="10" y="14" fill="#dff7ff" font-family="Verdana" font-size="11">capability treasury flywheel</text><text x="246" y="14" fill="#fff" font-family="Verdana" font-size="11">passed</text></svg>',encoding='utf-8')
    print(json.dumps({'status':'VISIBLE_OUTPUTS_WRITTEN'}, indent=2))
if __name__=='__main__': main()
