"""Step definitions for _DefaultHandler NotImplementedError coverage (rhscov3).

Covers lines 275, 279, 283, 287, 291, 295-296, 308, 318-319, 330, 340
in resource_handler_service.py — all the CRUD and lifecycle stubs on the
private _DefaultHandler class that raise NotImplementedError.
"""

from __future__ import annotations

from unittest.mock import MagicMock

from behave import given, then, when

from cleveragents.application.services.resource_handler_service import (
    _DefaultHandler,
)

# ---------------------------------------------------------------------------
# Given steps
# ---------------------------------------------------------------------------


@given("rhscov3 a _DefaultHandler with a dummy type spec")
def step_rhscov3_default_handler(context):
    type_spec = MagicMock()
    type_spec.name = "dummy-type"
    type_spec.sandbox_strategy = "copy_on_write"
    type_spec.handler = None
    context.rhscov3_handler = _DefaultHandler(type_spec=type_spec)
    context.rhscov3_error = None


@given("rhscov3 a dummy resource")
def step_rhscov3_dummy_resource(context):
    resource = MagicMock()
    resource.resource_id = "01ARZ3NDEKTSV4RRFFQ69G5FAV"
    resource.resource_type_name = "dummy-type"
    resource.location = "/tmp/dummy"
    resource.sandbox_strategy = None
    context.rhscov3_resource = resource


@given("rhscov3 a mock sandbox manager")
def step_rhscov3_mock_sandbox_manager(context):
    context.rhscov3_sandbox_manager = MagicMock()


# ---------------------------------------------------------------------------
# When steps — CRUD stubs
# ---------------------------------------------------------------------------


@when("rhscov3 I call read on the default handler")
def step_rhscov3_call_read(context):
    try:
        context.rhscov3_handler.read(resource=context.rhscov3_resource, path="")
    except NotImplementedError as exc:
        context.rhscov3_error = exc


@when("rhscov3 I call write on the default handler")
def step_rhscov3_call_write(context):
    try:
        context.rhscov3_handler.write(
            resource=context.rhscov3_resource, path="file.txt", data=b"data"
        )
    except NotImplementedError as exc:
        context.rhscov3_error = exc


@when("rhscov3 I call delete on the default handler")
def step_rhscov3_call_delete(context):
    try:
        context.rhscov3_handler.delete(resource=context.rhscov3_resource, path="")
    except NotImplementedError as exc:
        context.rhscov3_error = exc


@when("rhscov3 I call list_children on the default handler")
def step_rhscov3_call_list_children(context):
    try:
        context.rhscov3_handler.list_children(resource=context.rhscov3_resource)
    except NotImplementedError as exc:
        context.rhscov3_error = exc


@when("rhscov3 I call diff on the default handler")
def step_rhscov3_call_diff(context):
    try:
        context.rhscov3_handler.diff(
            resource=context.rhscov3_resource, other_location="/tmp/other"
        )
    except NotImplementedError as exc:
        context.rhscov3_error = exc


@when("rhscov3 I call discover_children on the default handler")
def step_rhscov3_call_discover_children(context):
    try:
        context.rhscov3_handler.discover_children(resource=context.rhscov3_resource)
    except NotImplementedError as exc:
        context.rhscov3_error = exc


# ---------------------------------------------------------------------------
# When steps — lifecycle stubs
# ---------------------------------------------------------------------------


@when("rhscov3 I call create_sandbox on the default handler")
def step_rhscov3_call_create_sandbox(context):
    try:
        context.rhscov3_handler.create_sandbox(
            resource=context.rhscov3_resource,
            plan_id="plan-001",
            sandbox_manager=context.rhscov3_sandbox_manager,
        )
    except NotImplementedError as exc:
        context.rhscov3_error = exc


@when("rhscov3 I call create_checkpoint on the default handler")
def step_rhscov3_call_create_checkpoint(context):
    try:
        context.rhscov3_handler.create_checkpoint(
            resource=context.rhscov3_resource,
            plan_id="plan-001",
            sandbox_manager=context.rhscov3_sandbox_manager,
            phase="execution",
        )
    except NotImplementedError as exc:
        context.rhscov3_error = exc


@when("rhscov3 I call rollback_to on the default handler")
def step_rhscov3_call_rollback_to(context):
    try:
        context.rhscov3_handler.rollback_to(
            resource=context.rhscov3_resource,
            plan_id="plan-001",
            checkpoint_id="chk-001",
            sandbox_manager=context.rhscov3_sandbox_manager,
        )
    except NotImplementedError as exc:
        context.rhscov3_error = exc


@when("rhscov3 I call project_access on the default handler")
def step_rhscov3_call_project_access(context):
    try:
        context.rhscov3_handler.project_access(
            resource=context.rhscov3_resource,
            principal="agent-1",
            action="read",
            project_id="proj-001",
        )
    except NotImplementedError as exc:
        context.rhscov3_error = exc


# ---------------------------------------------------------------------------
# Then steps
# ---------------------------------------------------------------------------


@then('rhscov3 a NotImplementedError is stored containing "{fragment}"')
def step_rhscov3_assert_not_implemented(context, fragment):
    assert context.rhscov3_error is not None, (
        "Expected a NotImplementedError but no error was raised"
    )
    assert isinstance(context.rhscov3_error, NotImplementedError), (
        f"Expected NotImplementedError, got {type(context.rhscov3_error).__name__}: "
        f"{context.rhscov3_error}"
    )
    assert fragment in str(context.rhscov3_error), (
        f"Expected '{fragment}' in error message, got: {context.rhscov3_error}"
    )
