"""
CAF/MSC benchmark hierarchy configuration.

This module defines an ontology configuration for stromal benchmarking, tailored to
CAF/MSC-rich datasets where within-stroma states are the primary target of evaluation.

Major lineage (used for coarse penalties/partial credit)
--------------------------------------------------------
- Fibroblast (stromal/mesenchymal, including PVL/pericyte continuum)
- Endothelial (vascular lineage)
- Immune (non-stromal immune cells; treated as clearly incorrect for stromal clusters)
- Tumor (epithelial/carcinoma; treated as clearly incorrect for stromal clusters)

States (used for confusion matrices and primary scoring)
--------------------------------------------------------
- iCAF, myCAF, PVL, Cycling, Endothelial

Scoring intent
--------------
- State is emphasized (w_state=0.7) to distinguish CAF programs.
- Major lineage still contributes (w_lineage=0.3) and enforces hard penalties against
  Immune/Tumor miscalls.
- Fibroblast ↔ Endothelial receives partial lineage credit to tolerate vascular-niche
  ambiguity while still rewarding correct state calls when possible.

This configuration is deterministic and intended for reviewer-facing reproducible
benchmarking.
"""

from .hierarchical_scoring import HierarchyConfig
from typing import Set, FrozenSet

CAF_HIER_CFG = HierarchyConfig(
    # 1) Prediction-side lineage parsing
    lineage_aliases={
        "Fibroblast": [
            # fibroblast / stromal lineage
            "fibroblast",
            "fibroblasts",
            "fibrocyte",
            "fibrocytes",
            "caf",
            "cancer-associated fibroblast",
            "cancer associated fibroblast",
            "stromal",
            "stromal cell",
            "stromal cells",
            "stroma",
            "mesenchymal",
            "mesenchymal stem cell",
            "mesenchymal stem cells",
            "mesenchymal stromal cell",
            "mesenchymal stromal cells",
            # perivascular cells are mesenchymal lineage
            "pvl",
            "perivascular",
            "pericyte",
            "pericytes",
            "mural",
        ],
        "Immune": [
            "immune",
            "t cell",
            "t-cell",
            "b cell",
            "b-cell",
            "macrophage",
            "myeloid",
            "lymphocyte",
        ],
        "Tumor": [
            "tumor",
            "cancer cell",
            "epithelial",
            "carcinoma",
        ],
        # explicit endothelial lineage
        "Endothelial": [
            "endothelial",
            "endothelium",
            "blood vessel",
            "blood vessels",
            "vascular",
            "capillary",
            "arterial",
            "venous",
            "vessel",
            "vessels",
        ],
        "Other": [],
    },

    # 2) Prediction-side state parsing
    #    This is what goes into the confusion matrices for the MSC benchmark.
    state_aliases={
        # inflammatory / MSC-like CAFs
        "iCAF": [
            "icaf",
            "inflammatory",
            "chemokine",
            "il6",
            "il-6",
            "msc",
            "msc-like",
            "mesenchymal stem cell",
            "mesenchymal stem cells",
            "mesenchymal stromal cell",
            "mesenchymal stromal cells",
            "lipofibroblast",
            "lipofibroblasts",
            "alveolar fibroblast",
            "alveolar fibroblasts",
        ],
        # myofibroblastic / activated CAFs
        "myCAF": [
            "mycaf",
            "myofibroblast",
            "myofibroblasts",
            "contractile",
            "acta2",
            "acta-2",
            "alpha-sma",
            "α-sma",
            "αsma",
            "activated fibroblast",
            "activated fibroblasts",
            "activated caf",
            "activated stromal cell",
            "activated stromal cells",
        ],
        # perivascular lineage (PVL / pericytes / smooth muscle continuum)
        "PVL": [
            "pvl",
            "perivascular",
            "pericyte",
            "pericytes",
            "rgs5",
            "mural",
            "mural cell",
            "mural cells",
            "smooth muscle",
            "smooth muscle cell",
            "smooth muscle cells",
            "vascular smooth muscle",
            "vascular smooth muscle cell",
            "vascular smooth muscle cells",
            "vsmc",
            "vascular pericyte",
            "vascular pericytes",
        ],
        "Cycling": [
            "cycling",
            "proliferating",
            "proliferation",
            "dividing",
            "cell cycle",
            "mki67",
            "top2a",
        ],
        # Endothelial state for MSC benchmark
        "Endothelial": [
            "endothelial",
            "endothelium",
            "blood vessel",
            "blood vessels",
            "vascular",
            "vessel",
            "vessels",
            "capillary",
            "arterial",
            "venous",
        ],
        "Other": [],
    },

    # 3) GT Rules: Ground_Truth → (major, state)
    #    NOTE: "Endothelial" GT now maps to a real state, so it is not filtered out.
    gt_rules=[
        ("endothelial", ("Endothelial", "Endothelial")),
        ("cycling", ("Fibroblast", "Cycling")),
        ("icaf", ("Fibroblast", "iCAF")),
        ("mycaf", ("Fibroblast", "myCAF")),
        ("pvl", ("Fibroblast", "PVL")),
    ],

    # 4) Logic: penalties & weights
    major_penalties={
        "Fibroblast": {"Tumor", "Immune"},
        "Endothelial": {"Tumor", "Immune"},
    },

    # 5) Near-lineage pairs
    # Fibroblast ↔ Endothelial: award partial lineage credit (0.5) to tolerate vascular-niche ambiguity.
    near_lineage_pairs={
        frozenset({"Fibroblast", "Endothelial"}),
    },

    failure_keywords=[
        "error",
        "unknown",
        "indeterminate",
        "undetermined",
        "unclassified",
        "ambiguous",
    ],

    # For CAF / MSC we care primarily about state (iCAF/myCAF/PVL/Cycling/Endothelial),
    # but we still award partial credit for getting the mesenchymal vs immune/tumor
    # lineage direction correct.
    w_lineage=0.3,
    w_state=0.7,

    default_major="Fibroblast",
    default_state="Other",
)
