"""Tests for kernel types — Entity, CausalEdge, enums, DimensionDef."""

from orxaq.kernel.types import (
    CausalEdge,
    CausalRole,
    CycleError,
    DimensionDef,
    Entity,
    SensitivityLevel,
    VariableType,
)


class TestEnums:
    def test_causal_role_values(self):
        assert CausalRole.TREATMENT.value == "treatment"
        assert CausalRole.OUTCOME.value == "outcome"
        assert CausalRole.CONFOUNDER.value == "confounder"
        assert CausalRole.MEDIATOR.value == "mediator"
        assert CausalRole.COLLIDER.value == "collider"
        assert CausalRole.INSTRUMENT.value == "instrument"
        assert CausalRole.MODERATOR.value == "moderator"
        assert CausalRole.EXOGENOUS.value == "exogenous"

    def test_variable_type_values(self):
        assert VariableType.STATIC.value == "static"
        assert VariableType.DYNAMIC.value == "dynamic"
        assert VariableType.MACRO.value == "macro"
        assert VariableType.DERIVED.value == "derived"

    def test_sensitivity_level_values(self):
        assert SensitivityLevel.PUBLIC.value == "public"
        assert SensitivityLevel.INTERNAL.value == "internal"
        assert SensitivityLevel.CONFIDENTIAL.value == "confidential"
        assert SensitivityLevel.RESTRICTED.value == "restricted"


class TestEntity:
    def test_create_entity_all_fields(self):
        entity = Entity(
            entity_id="gdp",
            name="GDP Growth",
            description="Quarterly GDP growth rate",
            causal_role=CausalRole.EXOGENOUS,
            variable_type=VariableType.MACRO,
            sensitivity=SensitivityLevel.PUBLIC,
            domain="macroeconomic",
            units="percent",
            bounds=(-10.0, 20.0),
            regulatory_tags=("CCAR", "CECL"),
            metadata=(("source", "FRED"),),
        )
        assert entity.entity_id == "gdp"
        assert entity.name == "GDP Growth"
        assert entity.causal_role == CausalRole.EXOGENOUS
        assert entity.bounds == (-10.0, 20.0)
        assert "CCAR" in entity.regulatory_tags

    def test_fingerprint_determinism(self):
        e1 = Entity(entity_id="x", name="X", domain="test")
        e2 = Entity(entity_id="x", name="X", domain="test")
        assert e1.fingerprint == e2.fingerprint

    def test_fingerprint_changes_with_causal_role(self):
        e1 = Entity(entity_id="x", name="X", causal_role=CausalRole.TREATMENT)
        e2 = Entity(entity_id="x", name="X", causal_role=CausalRole.OUTCOME)
        assert e1.fingerprint != e2.fingerprint

    def test_entity_defaults(self):
        entity = Entity(entity_id="a", name="A")
        assert entity.causal_role == CausalRole.EXOGENOUS
        assert entity.variable_type == VariableType.STATIC
        assert entity.sensitivity == SensitivityLevel.INTERNAL


class TestCausalEdge:
    def test_edge_id_format(self):
        edge = CausalEdge(source="A", target="B")
        assert edge.edge_id == "A\u2192B"

    def test_edge_defaults(self):
        edge = CausalEdge(source="X", target="Y")
        assert edge.confidence == 0.5
        assert edge.soliton_score == 0.5
        assert edge.is_immutable is False
        assert edge.edge_type == "causal"
        assert edge.temporal_lag == 0.0


class TestDimensionDef:
    def test_dimension_with_regulatory_tags(self):
        dim = DimensionDef(
            name="audit",
            description="Audit logging",
            domain="compliance",
            regulatory_tags=("SR_11_7",),
        )
        assert dim.name == "audit"
        assert "SR_11_7" in dim.regulatory_tags
        assert dim.status == "seam-spec"


class TestCycleError:
    def test_cycle_error_is_exception(self):
        assert issubclass(CycleError, Exception)
