"""Behave step definitions for NamespacedName TDD validation tests."""

from behave import when
from behave.runner import Context
from pydantic import ValidationError

from cleveragents.domain.models.core.plan import NamespacedName


@when('I parse the namespaced name "{full_name}" expecting an error')
def step_when_parse_namespaced_name_expecting_error(
    context: Context, full_name: str
) -> None:
    """Parse a namespaced name string and capture any error.

    Sets context.exception (generic), context.error (service_steps compat),
    and context.lsp_error (lsp_registry_steps compat).
    """
    try:
        context.namespaced_name = NamespacedName.parse(full_name)
        context.exception = None
        context.error = None
        context.lsp_error = None
    except (ValueError, ValidationError) as e:
        context.exception = e
        context.error = e
        context.lsp_error = e
        context.namespaced_name = None


@when(
    'I construct a NamespacedName with namespace "{namespace}" and name "{name}" expecting an error'
)
def step_when_construct_namespaced_name_expecting_error(
    context: Context, namespace: str, name: str
) -> None:
    """Create a NamespacedName and capture any error.

    Sets context.exception (generic), context.error (service_steps compat),
    and context.lsp_error (lsp_registry_steps compat).
    """
    try:
        context.namespaced_name = NamespacedName(namespace=namespace, name=name)
        context.exception = None
        context.error = None
        context.lsp_error = None
    except (ValueError, ValidationError) as e:
        context.exception = e
        context.error = e
        context.lsp_error = e
        context.namespaced_name = None
