"""Step definitions for resource_type_validation_coverage.feature.

Targets uncovered lines in _resource_type_validation.py:
  - Lines 211-213: auto_discovery rule that is not a dict
  - Lines 217-218: auto_discovery rule.type not a non-empty string
  - Lines 224-226: auto_discovery rule.type fails both name regexes
  - Lines 246-248: auto_discovery scan_depth not a positive integer
"""

from behave import given, then, when

from cleveragents.domain.models.core._resource_type_validation import (
    BUILTIN_TYPE_NAMES,
    validate_auto_discovery,
)

# ---------------------------------------------------------------------------
# GIVEN steps
# ---------------------------------------------------------------------------


@given('rtvcov a resource name "{name}"')
def step_rtvcov_given_resource_name(context, name):
    context.rtvcov_name = name
    context.rtvcov_auto_discovery = None
    context.rtvcov_error = None


@given("rtvcov an auto_discovery config with a non-dict rule entry")
def step_rtvcov_given_non_dict_rule(context):
    context.rtvcov_auto_discovery = {
        "enabled": True,
        "scan_depth": 3,
        "rules": ["not-a-dict"],
    }


@given("rtvcov an auto_discovery config with a rule whose type is empty")
def step_rtvcov_given_rule_empty_type(context):
    context.rtvcov_auto_discovery = {
        "enabled": True,
        "scan_depth": 3,
        "rules": [{"type": "   ", "pattern": "*.py"}],
    }


@given("rtvcov an auto_discovery config with a rule whose type is an integer")
def step_rtvcov_given_rule_int_type(context):
    context.rtvcov_auto_discovery = {
        "enabled": True,
        "scan_depth": 3,
        "rules": [{"type": 42, "pattern": "*.py"}],
    }


@given('rtvcov an auto_discovery config with a rule whose type is "{bad_type}"')
def step_rtvcov_given_rule_invalid_type_name(context, bad_type):
    context.rtvcov_auto_discovery = {
        "enabled": True,
        "scan_depth": 3,
        "rules": [{"type": bad_type, "pattern": "*.py"}],
    }


@given("rtvcov an auto_discovery config with scan_depth set to {value:d}")
def step_rtvcov_given_scan_depth_int(context, value):
    context.rtvcov_auto_discovery = {
        "enabled": True,
        "scan_depth": value,
    }


@given("rtvcov an auto_discovery config with scan_depth set to a string")
def step_rtvcov_given_scan_depth_string(context):
    context.rtvcov_auto_discovery = {
        "enabled": True,
        "scan_depth": "five",
    }


# ---------------------------------------------------------------------------
# WHEN steps
# ---------------------------------------------------------------------------


@when("rtvcov validate_auto_discovery is called")
def step_rtvcov_when_validate(context):
    try:
        validate_auto_discovery(
            name=context.rtvcov_name,
            auto_discovery=context.rtvcov_auto_discovery,
            builtin_names=BUILTIN_TYPE_NAMES,
        )
    except (ValueError, TypeError) as exc:
        context.rtvcov_error = exc


# ---------------------------------------------------------------------------
# THEN steps
# ---------------------------------------------------------------------------


@then('rtvcov a ValueError is raised containing "{fragment}"')
def step_rtvcov_then_value_error_contains(context, fragment):
    assert context.rtvcov_error is not None, (
        f"Expected a ValueError containing '{fragment}', but no exception was raised."
    )
    assert isinstance(context.rtvcov_error, ValueError), (
        f"Expected ValueError, got {type(context.rtvcov_error).__name__}: "
        f"{context.rtvcov_error}"
    )
    assert fragment in str(context.rtvcov_error), (
        f"Expected '{fragment}' in error message, got: {context.rtvcov_error}"
    )
