"""Tests for the recipes CLI commands."""

from unittest.mock import MagicMock, patch

from click.testing import CliRunner

from corpo.cli import cli


@patch("corpo.cli.get_client")
@patch("corpo.cli.load_auth_config")
def test_recipes_list(mock_auth, mock_client_cls):
    mock_auth.return_value = {"api_url": "http://localhost:8080", "api_key": "test"}
    client = MagicMock()
    client.list_recipes.return_value = [
        {"id": "startup-kit", "name": "Startup Kit", "description": "Get started", "steps": [
            {"action_type": "hire_ceo", "description": "Hire CEO", "optional": False}
        ], "tags": ["startup"]},
    ]
    mock_client_cls.return_value = client

    runner = CliRunner()
    result = runner.invoke(cli, ["recipes", "list"])
    assert result.exit_code == 0
    assert "Startup Kit" in result.output


@patch("corpo.cli.get_client")
@patch("corpo.cli.load_auth_config")
def test_recipes_show(mock_auth, mock_client_cls):
    mock_auth.return_value = {"api_url": "http://localhost:8080", "api_key": "test"}
    client = MagicMock()
    client.get_recipe.return_value = {
        "id": "startup-kit", "name": "Startup Kit", "description": "Get started",
        "category": "startup", "version": "1.0.0",
        "steps": [{"action_type": "hire_ceo", "description": "Hire CEO", "optional": False}],
    }
    mock_client_cls.return_value = client

    runner = CliRunner()
    result = runner.invoke(cli, ["recipes", "show", "startup-kit"])
    assert result.exit_code == 0
    assert "Startup Kit" in result.output


@patch("corpo.cli.get_client")
@patch("corpo.cli.load_auth_config")
def test_recipes_run(mock_auth, mock_client_cls):
    mock_auth.return_value = {"api_url": "http://localhost:8080", "api_key": "testkey"}
    client = MagicMock()
    client.get_recipe.return_value = {
        "id": "startup-kit", "name": "Startup Kit", "description": "Get started",
        "steps": [{"action_type": "hire_ceo", "description": "Hire CEO", "optional": False}],
    }
    client.execute_recipe.return_value = {
        "recipe_id": "startup-kit", "entity_id": "ent-123", "status": "completed",
        "step_results": [{"step_index": 0, "action_type": "hire_ceo", "action_id": "act-1", "status": "initiated"}],
    }
    mock_client_cls.return_value = client

    runner = CliRunner()
    result = runner.invoke(cli, ["recipes", "run", "ent-123", "startup-kit", "--yes"])
    assert result.exit_code == 0
    assert "completed" in result.output
