#!/usr/bin/env python3
"""
Hopf Ingest Bridge — Automated problem classification via the Hopf Portability Criterion.

Accepts problem metadata as JSON, runs the 6-condition check (A-F from
hopf_portability_criterion.md), and emits a classification receipt.

Uses the Cartan-DNA bridge (cartan_dna_bridge.py) as the computation engine
for conditions B-F when the problem is quaternionic (n=8).
"""
import json, sys
from pathlib import Path
from typing import Dict, List, Optional

SILVER = Path(__file__).resolve().parent.parent

# ── Condition A: Strand Decomposition ───────────────────────────────
def check_strand_decomposition(meta: dict) -> tuple[bool, str]:
    """Check if problem admits n independent Sidon-labelable channels."""
    n = meta.get("channel_count", 0)
    sidon = meta.get("sidon_labels", [])
    yb = meta.get("yang_baxter_holds", False)
    eig = meta.get("eigensolid_exists", False)
    
    if n not in (2, 4, 8, 16):
        return False, f"channel_count {n} not in valid Hopf dimensions (2,4,8,16)"
    if len(sidon) != n:
        return False, f"sidon_labels has {len(sidon)} labels, expected {n}"
    if not all(sidon[i] == 2**i for i in range(n)):
        return False, "sidon_labels not powers of 2"
    if not yb:
        return False, "Yang-Baxter not verified"
    if not eig:
        return False, "eigensolid convergence not verified"
    return True, f"n={n} channels, Sidon-valid, YB-OK, eigensolid-OK"

# ── Condition B: Cartan Spectrum ─────────────────────────────────────
def check_cartan_spectrum(meta: dict, n: int) -> tuple[bool, float, str]:
    """Compute σ = tr(Cartan)/2ⁿ. Returns (pass, sigma, msg)."""
    a = meta.get("cartan_integer", 0)
    if a <= 0:
        return False, 0, "cartan_integer not provided"
    
    denom = 2**n
    sigma = a / denom
    return True, sigma, f"σ = {a}/{denom} = {sigma:.6f}"

# ── Condition C: Sidon Threshold ─────────────────────────────────────
def check_sidon_threshold(n: int) -> tuple[float, str]:
    """τ = 1/(n-1)."""
    tau = 1 / (n-1)
    return tau, f"τ = 1/{n-1} = {tau:.6f}"

# ── Condition D: Spectral Gap ────────────────────────────────────────
def check_spectral_gap(sigma: float, tau: float, n: int) -> tuple[bool, int, int, float, str]:
    """∆ = σ - τ > 0, expressible as p/D where D = lcm(2ⁿ, n-1)."""
    gap = sigma - tau
    D = 2**n * (n-1)  # lcm for odd n-1
    p = round(gap * D)
    
    if gap <= 0:
        return False, 0, D, gap, f"gap = {gap:.6f} ≤ 0 (not positive)"
    return True, p, D, gap, f"∆ = {p}/{D} = {gap:.6f}"

# ── Condition E: Hopf Fibration Fit ───────────────────────────────────
def check_hopf_fit(n: int) -> tuple[int, str, str]:
    """n = 2f+2 for fiber dimension f. Returns (f, hopf_map, structure_group)."""
    f = (n - 2) // 2
    maps = {0: ("S¹→S¹", "ℤ₂"), 1: ("S³→S²", "U(1)"), 
            3: ("S⁷→S⁴", "SU(2)≅Sp(1)"), 7: ("S¹⁵→S⁸", "none (non-associative)")}
    hopf = maps.get(f, (f"S^(2*{f}+1)→S^{f+1}", "unknown"))
    is_ceiling = (f == 3)
    return f, hopf[0], f"{hopf[1]}{' (CEILING — maximal group encoding)' if is_ceiling else ''}"

# ── Condition F: Regime Bound ─────────────────────────────────────────
def check_regime_bound(n: int, f: int) -> tuple[int, str]:
    """R = (n-1) × c where c = fiber_representation_classes(f)."""
    c = {0: 2, 1: 2, 3: 4, 7: 8}.get(f, 2)
    R = (n-1) * c
    return R, f"R = (n-1)×c = {n-1}×{c} = {R}"

# ── Domain Matching ───────────────────────────────────────────────────
DOMAINS = {
    (8, 3): ["topological_insulators", "anyons_tqc", "qubo_spin_glasses", 
              "ads4_cft3", "exponential_sums", "elliptic_curves_qm",
              "crystalline_cohomology", "spin_systems_o3", "class_field_theory"],
    (4, 1): ["phase_dynamics", "complex_spin_systems"],
    (2, 0): ["binary_decisions", "ising_basic"],
    (16, 7): ["octonionic_limited"],
}

# ── Main Classifier ───────────────────────────────────────────────────
def classify(problem: dict) -> dict:
    """Run the full 6-condition Hopf portability check."""
    n = problem.get("channel_count", 0)
    fiber_hint = problem.get("hint_fiber_type", "")
    domain = problem.get("domain", "unknown")
    
    results = {}
    
    # A
    a_ok, a_msg = check_strand_decomposition(problem)
    results["A"] = {"pass": a_ok, "detail": a_msg}
    if not a_ok:
        return _fail("A", a_msg, problem)
    
    # B
    b_ok, sigma, b_msg = check_cartan_spectrum(problem, n)
    results["B"] = {"pass": b_ok, "detail": b_msg}
    if not b_ok:
        return _fail("B", b_msg, problem)
    
    # C
    tau, c_msg = check_sidon_threshold(n)
    results["C"] = {"pass": True, "detail": c_msg}
    
    # D
    d_ok, p, D, gap, d_msg = check_spectral_gap(sigma, tau, n)
    results["D"] = {"pass": d_ok, "detail": d_msg}
    if not d_ok:
        return _fail("D", d_msg, problem)
    
    # E
    f, hopf_map, structure = check_hopf_fit(n)
    results["E"] = {"pass": True, "detail": f"fiber f={f}, {hopf_map}, group={structure}"}
    
    # F
    R, f_msg = check_regime_bound(n, f)
    results["F"] = {"pass": True, "detail": f_msg}
    
    # All conditions pass
    at_ceiling = (n == 8 and f == 3)
    matching_domains = DOMAINS.get((n, f), [])
    port_quality = "strong" if (n, f) in DOMAINS else "moderate"
    
    return {
        "schema": "hopf_ingest_receipt_v1",
        "problem_id": problem.get("problem_id", "unknown"),
        "hopf_portable": True,
        "conditions_passed": [results[k]["pass"] for k in "ABCDEF"],
        "fingerprint": {
            "n": n, "sigma": f"{problem.get('cartan_integer',0)}/{2**n}",
            "sigma_float": sigma, "tau": f"1/{n-1}", "tau_float": tau,
            "denominator_D": D, "gap": f"{p}/{D}", "gap_float": gap,
            "regimes_R": R, "fiber_type": {0:"real",1:"complex",3:"quaternionic",7:"octonionic"}.get(f),
            "hopf_map": hopf_map
        },
        "classification": {
            "regime_class": f"ℤ_{R}" if f in (0,1,3) else f"non-group (R={R})",
            "port_quality": port_quality,
            "domain_analogs": matching_domains,
            "maximal_encoding": at_ceiling,
            "at_ceiling": at_ceiling
        },
        "results": results
    }

def _fail(condition: str, reason: str, problem: dict) -> dict:
    return {
        "schema": "hopf_ingest_receipt_v1",
        "problem_id": problem.get("problem_id", "unknown"),
        "hopf_portable": False,
        "failed_condition": condition,
        "reason": reason
    }

# ── CLI ───────────────────────────────────────────────────────────────
if __name__ == "__main__":
    # Example: classify a quaternionic problem
    example = {
        "problem_id": "braidstorm-8strand",
        "domain": "braid_topology",
        "channel_count": 8,
        "sidon_labels": [1,2,4,8,16,32,64,128],
        "yang_baxter_holds": True,
        "eigensolid_exists": True,
        "cartan_integer": 39,
        "hint_fiber_type": "quaternionic"
    }
    
    result = classify(example)
    print(json.dumps(result, indent=2))
