"""Step definitions for uncovered plan command branches."""

from __future__ import annotations

import os
from types import SimpleNamespace
from unittest.mock import MagicMock, patch

from behave import then, when
from typer.testing import CliRunner

from cleveragents.application.services.plan_service import PlanService
from cleveragents.cli.commands.plan import app as plan_app


class _PlanTellContainer:
    def __init__(self, plan_service, project_service, actor_service):
        self._plan_service = plan_service
        self._project_service = project_service
        self._actor_service = actor_service

    def plan_service(self):
        return self._plan_service

    def project_service(self):
        return self._project_service

    def actor_service(self):
        return self._actor_service


@when("I execute plan tell with testing mode disabled and no actor registry")
def step_plan_tell_testing_mode_disabled(context):
    runner = CliRunner()
    prompt = "Check branch coverage"
    plan_service = MagicMock(spec=PlanService)
    plan_service.create_plan.return_value = SimpleNamespace(
        name="coverage-plan",
        prompt=prompt,
    )
    project_service = MagicMock()
    project_service.get_current_project.return_value = SimpleNamespace(name="project")
    actor_service = MagicMock()
    container = _PlanTellContainer(plan_service, project_service, actor_service)
    env = os.environ.copy()
    env["CLEVERAGENTS_TESTING_USE_MOCK_AI"] = "false"

    with patch(
        "cleveragents.application.container.get_container", return_value=container
    ):
        result = runner.invoke(plan_app, ["tell", prompt], env=env)

    context.result = result
    context.actor_service = actor_service


@then("the mock actor should not be initialized")
def step_mock_actor_not_initialized(context):
    context.actor_service.ensure_default_mock_actor.assert_not_called()
