"""Step definitions for CLI version/info/diagnostics showcase documentation tests."""

import json
from pathlib import Path

from behave import given, then, when


@given('the showcase documentation file exists at "{file_path}"')
def step_showcase_doc_file_exists(context, file_path: str) -> None:
    """Verify the showcase documentation file exists at the given path."""
    context.doc_file_path = Path(file_path)
    assert context.doc_file_path.exists(), (
        f"Showcase documentation file not found: {file_path}"
    )


@when("I read the documentation file")
def step_read_documentation_file(context) -> None:
    """Read the showcase documentation file content."""
    context.doc_content = context.doc_file_path.read_text(encoding="utf-8")


@then('the documentation should contain "{text}"')
def step_doc_should_contain_text(context, text: str) -> None:
    """Verify the documentation content contains the given text."""
    assert text in context.doc_content, (
        f"Expected text '{text}' not found in documentation file"
    )


@then('the documentation should contain the "version" command explanation')
def step_doc_contains_version_explanation(context) -> None:
    """Verify the documentation contains a version command explanation."""
    assert "version" in context.doc_content.lower(), (
        "Documentation does not contain 'version' command explanation"
    )


@then('the documentation should contain the "info" command explanation')
def step_doc_contains_info_explanation(context) -> None:
    """Verify the documentation contains an info command explanation."""
    assert "info" in context.doc_content.lower(), (
        "Documentation does not contain 'info' command explanation"
    )


@then('the documentation should contain the "diagnostics" command explanation')
def step_doc_contains_diagnostics_explanation(context) -> None:
    """Verify the documentation contains a diagnostics command explanation."""
    assert "diagnostics" in context.doc_content.lower(), (
        "Documentation does not contain 'diagnostics' command explanation"
    )


@then(
    'the documentation should contain the "Fast-Path vs Regular Command Behavior" section'
)
def step_doc_contains_fast_path_section(context) -> None:
    """Verify the documentation contains the Fast-Path vs Regular Command Behavior section."""
    assert "Fast-Path" in context.doc_content, (
        "Documentation does not contain 'Fast-Path vs Regular Command Behavior' section"
    )


@then("the documentation should contain JSON output examples")
def step_doc_contains_json_examples(context) -> None:
    """Verify the documentation contains JSON output examples."""
    assert "```json" in context.doc_content, (
        "Documentation does not contain JSON output examples (```json code block)"
    )


@then("the documentation should contain YAML output examples")
def step_doc_contains_yaml_examples(context) -> None:
    """Verify the documentation contains YAML output examples."""
    assert "```yaml" in context.doc_content, (
        "Documentation does not contain YAML output examples (```yaml code block)"
    )


@given('the showcase examples registry exists at "{file_path}"')
def step_examples_registry_exists(context, file_path: str) -> None:
    """Verify the showcase examples registry file exists at the given path."""
    context.registry_file_path = Path(file_path)
    assert context.registry_file_path.exists(), (
        f"Showcase examples registry not found: {file_path}"
    )


@when("I read the examples registry")
def step_read_examples_registry(context) -> None:
    """Read and parse the showcase examples registry JSON file."""
    content = context.registry_file_path.read_text(encoding="utf-8")
    context.registry_data = json.loads(content)
    context.registry_examples = context.registry_data.get("examples", [])


@then('the registry should contain an entry for "{title}"')
def step_registry_contains_entry(context, title: str) -> None:
    """Verify the registry contains an entry with the given title."""
    matching = [e for e in context.registry_examples if e.get("title") == title]
    assert len(matching) > 0, (
        f"No registry entry found with title '{title}'. "
        f"Available titles: {[e.get('title') for e in context.registry_examples]}"
    )
    context.current_entry = matching[0]


@then('the registry entry should have category "{category}"')
def step_entry_has_category(context, category: str) -> None:
    """Verify the current registry entry has the expected category."""
    actual = context.current_entry.get("category")
    assert actual == category, f"Expected category '{category}', got '{actual}'"


@then('the registry entry should have path "{path}"')
def step_entry_has_path(context, path: str) -> None:
    """Verify the current registry entry has the expected path."""
    actual = context.current_entry.get("path")
    assert actual == path, f"Expected path '{path}', got '{actual}'"


@then("the registry entry should list version, info, and diagnostics commands")
def step_entry_lists_commands(context) -> None:
    """Verify the current registry entry lists version, info, and diagnostics commands."""
    commands = context.current_entry.get("commands", [])
    commands_str = " ".join(commands).lower()
    assert "version" in commands_str, (
        f"'version' command not found in entry commands: {commands}"
    )
    assert "info" in commands_str, (
        f"'info' command not found in entry commands: {commands}"
    )
    assert "diagnostics" in commands_str, (
        f"'diagnostics' command not found in entry commands: {commands}"
    )


@then('the registry entry should have complexity "{complexity}"')
def step_entry_has_complexity(context, complexity: str) -> None:
    """Verify the current registry entry has the expected complexity."""
    actual = context.current_entry.get("complexity")
    assert actual == complexity, f"Expected complexity '{complexity}', got '{actual}'"


@then('the registry entry should have educational_value "{value}"')
def step_entry_has_educational_value(context, value: str) -> None:
    """Verify the current registry entry has the expected educational_value."""
    actual = context.current_entry.get("educational_value")
    assert actual == value, f"Expected educational_value '{value}', got '{actual}'"
