"""
Behavioral tests for base_constraint module.

Tests ConstraintType enum and Constraint dataclass logic.
"""


class TestConstraintTypeEnum:
    """Tests for ConstraintType enum values."""

    def test_property_type_exists(self):
        from local_deep_research.advanced_search_system.constraints.base_constraint import (
            ConstraintType,
        )

        assert ConstraintType.PROPERTY.value == "property"

    def test_name_pattern_type_exists(self):
        from local_deep_research.advanced_search_system.constraints.base_constraint import (
            ConstraintType,
        )

        assert ConstraintType.NAME_PATTERN.value == "name_pattern"

    def test_event_type_exists(self):
        from local_deep_research.advanced_search_system.constraints.base_constraint import (
            ConstraintType,
        )

        assert ConstraintType.EVENT.value == "event"

    def test_statistic_type_exists(self):
        from local_deep_research.advanced_search_system.constraints.base_constraint import (
            ConstraintType,
        )

        assert ConstraintType.STATISTIC.value == "statistic"

    def test_temporal_type_exists(self):
        from local_deep_research.advanced_search_system.constraints.base_constraint import (
            ConstraintType,
        )

        assert ConstraintType.TEMPORAL.value == "temporal"

    def test_location_type_exists(self):
        from local_deep_research.advanced_search_system.constraints.base_constraint import (
            ConstraintType,
        )

        assert ConstraintType.LOCATION.value == "location"

    def test_comparison_type_exists(self):
        from local_deep_research.advanced_search_system.constraints.base_constraint import (
            ConstraintType,
        )

        assert ConstraintType.COMPARISON.value == "comparison"

    def test_existence_type_exists(self):
        from local_deep_research.advanced_search_system.constraints.base_constraint import (
            ConstraintType,
        )

        assert ConstraintType.EXISTENCE.value == "existence"

    def test_has_eight_types(self):
        from local_deep_research.advanced_search_system.constraints.base_constraint import (
            ConstraintType,
        )

        assert len(ConstraintType) == 8


class TestConstraintInit:
    """Tests for Constraint dataclass initialization."""

    def test_basic_construction(self):
        from local_deep_research.advanced_search_system.constraints.base_constraint import (
            Constraint,
            ConstraintType,
        )

        c = Constraint(
            id="c1",
            type=ConstraintType.PROPERTY,
            description="test",
            value="ice age",
        )
        assert c.id == "c1"
        assert c.type == ConstraintType.PROPERTY
        assert c.description == "test"
        assert c.value == "ice age"

    def test_default_weight_is_one(self):
        from local_deep_research.advanced_search_system.constraints.base_constraint import (
            Constraint,
            ConstraintType,
        )

        c = Constraint(
            id="c1", type=ConstraintType.PROPERTY, description="test", value="v"
        )
        assert c.weight == 1.0

    def test_custom_weight(self):
        from local_deep_research.advanced_search_system.constraints.base_constraint import (
            Constraint,
            ConstraintType,
        )

        c = Constraint(
            id="c1",
            type=ConstraintType.PROPERTY,
            description="test",
            value="v",
            weight=0.5,
        )
        assert c.weight == 0.5

    def test_metadata_defaults_to_empty_dict(self):
        from local_deep_research.advanced_search_system.constraints.base_constraint import (
            Constraint,
            ConstraintType,
        )

        c = Constraint(
            id="c1", type=ConstraintType.PROPERTY, description="test", value="v"
        )
        assert c.metadata == {}

    def test_metadata_none_becomes_empty_dict(self):
        from local_deep_research.advanced_search_system.constraints.base_constraint import (
            Constraint,
            ConstraintType,
        )

        c = Constraint(
            id="c1",
            type=ConstraintType.PROPERTY,
            description="test",
            value="v",
            metadata=None,
        )
        assert c.metadata == {}

    def test_custom_metadata_preserved(self):
        from local_deep_research.advanced_search_system.constraints.base_constraint import (
            Constraint,
            ConstraintType,
        )

        meta = {"key": "val"}
        c = Constraint(
            id="c1",
            type=ConstraintType.PROPERTY,
            description="test",
            value="v",
            metadata=meta,
        )
        assert c.metadata == {"key": "val"}


class TestConstraintToSearchTerms:
    """Tests for Constraint.to_search_terms() method."""

    def test_property_returns_value_directly(self):
        from local_deep_research.advanced_search_system.constraints.base_constraint import (
            Constraint,
            ConstraintType,
        )

        c = Constraint(
            id="c1",
            type=ConstraintType.PROPERTY,
            description="d",
            value="ice age",
        )
        assert c.to_search_terms() == "ice age"

    def test_name_pattern_appends_keywords(self):
        from local_deep_research.advanced_search_system.constraints.base_constraint import (
            Constraint,
            ConstraintType,
        )

        c = Constraint(
            id="c1",
            type=ConstraintType.NAME_PATTERN,
            description="d",
            value="body part",
        )
        result = c.to_search_terms()
        assert "body part" in result
        assert "name" in result
        assert "trail" in result

    def test_event_appends_keywords(self):
        from local_deep_research.advanced_search_system.constraints.base_constraint import (
            Constraint,
            ConstraintType,
        )

        c = Constraint(
            id="c1",
            type=ConstraintType.EVENT,
            description="d",
            value="rockslide",
        )
        result = c.to_search_terms()
        assert "rockslide" in result
        assert "accident" in result or "incident" in result

    def test_statistic_appends_keywords(self):
        from local_deep_research.advanced_search_system.constraints.base_constraint import (
            Constraint,
            ConstraintType,
        )

        c = Constraint(
            id="c1",
            type=ConstraintType.STATISTIC,
            description="d",
            value="84.5x",
        )
        result = c.to_search_terms()
        assert "84.5x" in result
        assert "statistics" in result

    def test_other_types_return_str_value(self):
        from local_deep_research.advanced_search_system.constraints.base_constraint import (
            Constraint,
            ConstraintType,
        )

        c = Constraint(
            id="c1",
            type=ConstraintType.LOCATION,
            description="d",
            value="Colorado",
        )
        assert c.to_search_terms() == "Colorado"


class TestConstraintIsCritical:
    """Tests for Constraint.is_critical() method."""

    def test_name_pattern_always_critical(self):
        from local_deep_research.advanced_search_system.constraints.base_constraint import (
            Constraint,
            ConstraintType,
        )

        c = Constraint(
            id="c1",
            type=ConstraintType.NAME_PATTERN,
            description="d",
            value="v",
            weight=0.1,
        )
        assert c.is_critical() is True

    def test_high_weight_is_critical(self):
        from local_deep_research.advanced_search_system.constraints.base_constraint import (
            Constraint,
            ConstraintType,
        )

        c = Constraint(
            id="c1",
            type=ConstraintType.PROPERTY,
            description="d",
            value="v",
            weight=0.9,
        )
        assert c.is_critical() is True

    def test_low_weight_not_critical(self):
        from local_deep_research.advanced_search_system.constraints.base_constraint import (
            Constraint,
            ConstraintType,
        )

        c = Constraint(
            id="c1",
            type=ConstraintType.PROPERTY,
            description="d",
            value="v",
            weight=0.5,
        )
        assert c.is_critical() is False

    def test_weight_exactly_0_8_not_critical(self):
        from local_deep_research.advanced_search_system.constraints.base_constraint import (
            Constraint,
            ConstraintType,
        )

        c = Constraint(
            id="c1",
            type=ConstraintType.EVENT,
            description="d",
            value="v",
            weight=0.8,
        )
        assert c.is_critical() is False

    def test_weight_just_above_0_8_is_critical(self):
        from local_deep_research.advanced_search_system.constraints.base_constraint import (
            Constraint,
            ConstraintType,
        )

        c = Constraint(
            id="c1",
            type=ConstraintType.EVENT,
            description="d",
            value="v",
            weight=0.81,
        )
        assert c.is_critical() is True
