"""Evidence-bearing display classifications; never overwrite corpus reviews.

Review tags take precedence. Regex matches are explicitly unreviewed suggestions
about words in the title/abstract, not a claim of substantive scientific relevance.
"""
import re

VERSION = "2026-09-11.1"
DIMENSIONS = {"physics": "Physics", "platform": "Platform / system", "approach": "Research approach"}
PLATFORMS = {
    "Quantum dots": ({"quantum-dot", "double-quantum-dot"}, r"\bquantum[ -]dots?\b"),
    "Nanowires": ({"nanowire"}, r"\bnanowires?\b"),
    "Graphene": ({"graphene"}, r"\bgraphene\b"),
    "Magnetic impurities & Shiba chains": ({"magnetic-impurity", "shiba-chain"}, r"\b(?:magnetic impurit(?:y|ies)|shiba chains?|magnetic adatoms?)\b"),
    "Topological insulators": ({"topological-insulator"}, r"\btopological insulators?\b"),
    "Quantum Hall systems": ({"quantum-hall"}, r"\bquantum hall\b"),
    "Superconducting circuits": ({"superconducting-qubit", "josephson-device"}, r"\b(?:superconducting (?:circuits?|qubits?)|transmons?|gatemons?|fluxoniums?)\b"),
    "Cold atoms & superfluids": ({"superfluid-analogue"}, r"\b(?:superfluid helium|helium-3|superfluid (?:3|\^3)he|ultracold|cold atoms?|bose[ -]einstein condensat\w*)\b"),
    "Two-dimensional electron gases": ({"2deg"}, r"\b(?:2deg|two[ -]dimensional electron (?:gas|gases))\b"),
}
PHYSICS_PATTERNS = {
    "Andreev reflection": r"\bandreev reflections?\b",
    "Andreev bound states": r"\bandreev (?:bound )?(?:states?|levels?)\b",
    "Josephson physics": r"\bjosephson\b",
    "Majorana physics": r"\bmajorana\b",
    "Proximity effect": r"\b(?:superconducting proximity|proximity[ -](?:induced superconduct|effect))",
    "Spectroscopy": r"\bspectroscop(?:y|ic)\b",
    "Yu–Shiba–Rusinov states": r"\b(?:yu[ –-]shiba[ –-]rusinov|shiba states?|ysr)\b",
    "Crossed Andreev reflection": r"\bcrossed andreev\b",
    "Multiple Andreev reflection": r"\bmultiple andreev\b",
    "Qubits": r"\bqubits?\b",
    "Cooper-pair splitting": r"\bcooper[ -]pair split\w*\b",
    "Parity": r"\b(?:fermion|quasiparticle|charge|ground[ -]state|fermionic) parity\b",
    "Topological superconductivity": r"\btopological superconduct\w*\b",
}
APPROACHES = {
    "Theory / computation": ({"theory"}, r"\b(?:theor(?:y|etical)|numerical|simulation|analytical)\b", r"\bwe (?:theoretically|numerically|calculate|derive|simulate|develop a theoretical)\b"),
    "Experiment": ({"experiment"}, r"\bexperimental(?:ly)?\b", r"\bwe (?:experimentally|measure\b|fabricate\b|report measurements|demonstrate experimentally)"),
    "Review / perspective": ({"review", "perspective"}, r"\b(?:review|perspective|overview|roadmap)\b", r"\b(?:we review|this review|this perspective)\b"),
}


def classify(record):
    result = []
    normalized = {label.lower() for label in record["labels"]}

    def assign(dimension, name, aliases, title_pattern=None, abstract_pattern=None):
        tags = sorted(aliases & normalized)
        if tags:
            result.append({"dimension": dimension, "name": name, "basis": "review-tag", "field": "labels", "evidence": "; ".join(tags)})
            return
        for field, pattern in [("title", title_pattern), ("abstract", abstract_pattern)]:
            if not pattern:
                continue
            text = record[field]
            match = re.search(pattern, text, re.IGNORECASE)
            if match:
                excerpt = text[max(0, match.start()-55):min(len(text), match.end()+95)]
                result.append({"dimension": dimension, "name": name, "basis": "text-suggestion", "field": field, "evidence": excerpt})
                return

    # Existing display topics already reconcile synonymous original review tags.
    for topic in record["topics"]:
        result.append({"dimension": "physics", "name": topic, "basis": "review-tag", "field": "topics", "evidence": topic})
    for name, pattern in PHYSICS_PATTERNS.items():
        if name not in record["topics"]:
            assign("physics", name, set(), pattern, pattern)
    for name, (aliases, pattern) in PLATFORMS.items():
        assign("platform", name, aliases, pattern, pattern)
    for name, (aliases, title_pattern, abstract_pattern) in APPROACHES.items():
        assign("approach", name, aliases, title_pattern, abstract_pattern)
    return result
