"""Step definitions for resource_type_digit_start_validation.feature.

Tests that _NAMESPACED_RE in resource_type.py and _resource_type_validation.py
correctly rejects namespace/name components that start with a digit.

Spec requirement: namespace and name components must start with a letter.

Note: The step "a validation error should be raised mentioning invalid inherits
type name" is defined in resource_type_model_coverage_steps.py and reused here.
"""

from __future__ import annotations

from behave import then, when
from pydantic import ValidationError

from cleveragents.domain.models.core.resource_type import (
    ResourceKind,
    ResourceTypeSpec,
    SandboxStrategy,
)

# ---------------------------------------------------------------------------
# Helpers
# ---------------------------------------------------------------------------


def _make_custom_physical_spec(**overrides) -> dict:
    """Build a minimal valid custom (non-built-in) physical ResourceTypeSpec dict."""
    defaults = dict(
        name="myorg/my-type",
        resource_kind=ResourceKind.PHYSICAL,
        sandbox_strategy=SandboxStrategy.GIT_WORKTREE,
        built_in=False,
    )
    defaults.update(overrides)
    return defaults


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


@when('I create a custom resource type with name "{name}"')
def step_create_custom_resource_type(context, name: str) -> None:
    """Attempt to create a custom ResourceTypeSpec with the given name."""
    try:
        ResourceTypeSpec(**_make_custom_physical_spec(name=name))
        context.rt_error = None
    except ValidationError as exc:
        context.rt_error = exc


@when('I create a resource type spec with inherits set to "{inherits_name}"')
def step_create_with_inherits_digit(context, inherits_name: str) -> None:
    """Attempt to create a ResourceTypeSpec with the given inherits value."""
    try:
        ResourceTypeSpec(**_make_custom_physical_spec(inherits=inherits_name))
        context.rt_error = None
    except ValidationError as exc:
        context.rt_error = exc


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


@then("a validation error should be raised mentioning invalid resource type name")
def step_verify_invalid_name_error(context) -> None:
    """Assert a ValidationError was raised mentioning invalid resource type name."""
    assert context.rt_error is not None, (
        "Expected a ValidationError for an invalid resource type name, "
        "but no exception was raised."
    )
    assert isinstance(context.rt_error, ValidationError), (
        f"Expected ValidationError, got {type(context.rt_error).__name__}: "
        f"{context.rt_error}"
    )
    error_str = str(context.rt_error)
    assert "Invalid resource type name" in error_str, (
        f"Expected 'Invalid resource type name' in error message, got: {error_str}"
    )


@then("no validation error should be raised")
def step_verify_no_error(context) -> None:
    """Assert no ValidationError was raised."""
    assert context.rt_error is None, (
        f"Expected no ValidationError, but got: {context.rt_error}"
    )
