"""Step definitions for resource_type_builtins.feature."""

from __future__ import annotations

from pathlib import Path

import yaml
from behave import given, then, when

from cleveragents.domain.models.core.resource_type import ResourceTypeSpec
from cleveragents.resource.schema import ResourceTypeConfigSchema

_EXAMPLES_DIR = (
    Path(__file__).resolve().parent.parent.parent / "examples" / "resource-types"
)


# ── Given steps ──────────────────────────────────────────────


@given("the built-in git-checkout YAML file")
def step_builtin_git_checkout_yaml(context: object) -> None:
    context.builtin_yaml_path = _EXAMPLES_DIR / "git-checkout.yaml"  # type: ignore[attr-defined]


@given("the built-in fs-directory YAML file")
def step_builtin_fs_directory_yaml(context: object) -> None:
    context.builtin_yaml_path = _EXAMPLES_DIR / "fs-directory.yaml"  # type: ignore[attr-defined]


# ── When steps ───────────────────────────────────────────────


@when("I load the built-in YAML via from_yaml_file")
def step_load_builtin_yaml(context: object) -> None:
    path: Path = context.builtin_yaml_path  # type: ignore[attr-defined]
    context.builtin_schema = ResourceTypeConfigSchema.from_yaml_file(path)  # type: ignore[attr-defined]


@when("I load the built-in YAML as a ResourceTypeSpec")
def step_load_builtin_as_domain(context: object) -> None:
    path: Path = context.builtin_yaml_path  # type: ignore[attr-defined]
    raw: dict[str, object] = yaml.safe_load(path.read_text(encoding="utf-8"))
    context.builtin_domain = ResourceTypeSpec.from_config(raw)  # type: ignore[attr-defined]


# ── Then steps ───────────────────────────────────────────────


@then("the builtin schema should be valid")
def step_builtin_schema_valid(context: object) -> None:
    assert context.builtin_schema is not None  # type: ignore[attr-defined]


@then('the builtin schema name should be "{name}"')
def step_builtin_schema_name(context: object, name: str) -> None:
    assert context.builtin_schema.name == name  # type: ignore[attr-defined]


@then('the builtin schema sandbox_strategy should be "{strategy}"')
def step_builtin_schema_sandbox(context: object, strategy: str) -> None:
    assert context.builtin_schema.sandbox_strategy == strategy  # type: ignore[attr-defined]


@then('the builtin schema resource_kind should be "{kind}"')
def step_builtin_schema_kind(context: object, kind: str) -> None:
    assert context.builtin_schema.resource_kind == kind  # type: ignore[attr-defined]


@then('the builtin schema parent_types should contain "{parent}"')
def step_builtin_schema_parent(context: object, parent: str) -> None:
    assert parent in context.builtin_schema.parent_types  # type: ignore[attr-defined]


@then("the builtin schema should have empty parent_types")
def step_builtin_schema_no_parents(context: object) -> None:
    assert len(context.builtin_schema.parent_types) == 0  # type: ignore[attr-defined]


@then('the builtin schema name should not contain "/"')
def step_builtin_schema_no_slash(context: object) -> None:
    assert "/" not in context.builtin_schema.name  # type: ignore[attr-defined]


@then("the builtin schema built_in should be true")
def step_builtin_schema_builtin(context: object) -> None:
    assert context.builtin_schema.built_in is True  # type: ignore[attr-defined]


@then("the builtin capabilities should have read {value}")
def step_builtin_caps_read(context: object, value: str) -> None:
    expected = value.lower() == "true"
    assert context.builtin_schema.capabilities["read"] is expected  # type: ignore[attr-defined]


@then("the builtin capabilities should have write {value}")
def step_builtin_caps_write(context: object, value: str) -> None:
    expected = value.lower() == "true"
    assert context.builtin_schema.capabilities["write"] is expected  # type: ignore[attr-defined]


@then("the builtin capabilities should have sandbox {value}")
def step_builtin_caps_sandbox(context: object, value: str) -> None:
    expected = value.lower() == "true"
    assert context.builtin_schema.capabilities["sandbox"] is expected  # type: ignore[attr-defined]


@then("the builtin capabilities should have checkpoint {value}")
def step_builtin_caps_checkpoint(context: object, value: str) -> None:
    expected = value.lower() == "true"
    assert context.builtin_schema.capabilities["checkpoint"] is expected  # type: ignore[attr-defined]


@then("the builtin domain model should be valid")
def step_builtin_domain_valid(context: object) -> None:
    assert context.builtin_domain is not None  # type: ignore[attr-defined]


@then('the builtin domain model name should be "{name}"')
def step_builtin_domain_name(context: object, name: str) -> None:
    assert context.builtin_domain.name == name  # type: ignore[attr-defined]


@then("the builtin schema should have {count:d} cli_args")
def step_builtin_schema_cli_args_count(context: object, count: int) -> None:
    assert len(context.builtin_schema.cli_args) == count  # type: ignore[attr-defined]


@then('the builtin first cli_arg name should be "{name}"')
def step_builtin_first_cli_arg(context: object, name: str) -> None:
    assert context.builtin_schema.cli_args[0].name == name  # type: ignore[attr-defined]


@then('the builtin second cli_arg name should be "{name}"')
def step_builtin_second_cli_arg(context: object, name: str) -> None:
    assert context.builtin_schema.cli_args[1].name == name  # type: ignore[attr-defined]


@then('the builtin schema child_types should contain "{child}"')
def step_builtin_schema_child(context: object, child: str) -> None:
    assert child in context.builtin_schema.child_types  # type: ignore[attr-defined]


@then('the builtin schema handler should be "{handler}"')
def step_builtin_schema_handler(context: object, handler: str) -> None:
    assert context.builtin_schema.handler == handler  # type: ignore[attr-defined]


@then("the builtin schema auto_discovery should be enabled")
def step_builtin_schema_discovery_enabled(context: object) -> None:
    assert context.builtin_schema.auto_discovery is not None  # type: ignore[attr-defined]
    assert context.builtin_schema.auto_discovery.get("enabled") is True  # type: ignore[attr-defined]


@then("the builtin schema auto_discovery should be none")
def step_builtin_schema_discovery_none(context: object) -> None:
    assert context.builtin_schema.auto_discovery is None  # type: ignore[attr-defined]
