from pathlib import Path
from unittest.mock import MagicMock
from onako.task_service import create_task


def test_create_task_builds_command_without_skip():
    orch = MagicMock()
    orch.create_task.return_value = {"id": "task-abc", "status": "running"}

    result = create_task(orch, prompt="echo hello", skip_permissions=False)

    command = orch.create_task.call_args[1]["command"]
    assert command.startswith("claude --session-id ")
    assert command.endswith("'echo hello'")
    assert "--dangerously-skip-permissions" not in command
    assert result["id"] == "task-abc"


def test_create_task_builds_command_with_skip():
    orch = MagicMock()
    orch.create_task.return_value = {"id": "task-abc", "status": "running"}

    result = create_task(orch, prompt="echo hello", skip_permissions=True)

    command = orch.create_task.call_args[1]["command"]
    assert command.startswith("claude --dangerously-skip-permissions --session-id ")
    assert command.endswith("'echo hello'")
    assert result["id"] == "task-abc"


def test_create_task_passes_working_dir():
    orch = MagicMock()
    orch.create_task.return_value = {"id": "task-abc", "status": "running"}

    create_task(orch, prompt="echo hello", working_dir="/tmp/mydir")

    assert orch.create_task.call_args[1]["working_dir"] == "/tmp/mydir"


def test_create_task_passes_prompt_and_session_id():
    orch = MagicMock()
    orch.create_task.return_value = {"id": "task-abc", "status": "running"}

    create_task(orch, prompt="fix the bug")

    kwargs = orch.create_task.call_args[1]
    assert kwargs["prompt"] == "fix the bug"
    assert kwargs["claude_session_id"] is not None


def test_create_task_with_branch_and_worktree_manager():
    orch = MagicMock()
    orch.create_task.return_value = {"id": "task-abc", "status": "running"}
    wt_mgr = MagicMock()
    wt_mgr.ensure_worktree.return_value = Path("/repos/myproject--feat-auth")

    create_task(orch, prompt="add auth", branch="feat/auth", worktree_manager=wt_mgr)

    wt_mgr.ensure_worktree.assert_called_once_with("feat/auth", "main")
    kwargs = orch.create_task.call_args[1]
    assert kwargs["working_dir"] == "/repos/myproject--feat-auth"
    assert kwargs["branch"] == "feat/auth"
    assert kwargs["worktree_path"] == "/repos/myproject--feat-auth"


def test_create_task_with_branch_and_custom_base():
    orch = MagicMock()
    orch.create_task.return_value = {"id": "task-abc", "status": "running"}
    wt_mgr = MagicMock()
    wt_mgr.ensure_worktree.return_value = Path("/repos/myproject--feat-auth")

    create_task(orch, prompt="add auth", branch="feat/auth", base_branch="develop", worktree_manager=wt_mgr)

    wt_mgr.ensure_worktree.assert_called_once_with("feat/auth", "develop")


def test_create_task_branch_without_worktree_manager_ignored():
    orch = MagicMock()
    orch.create_task.return_value = {"id": "task-abc", "status": "running"}

    create_task(orch, prompt="add auth", branch="feat/auth")

    kwargs = orch.create_task.call_args[1]
    assert kwargs["branch"] is None
    assert kwargs["worktree_path"] is None
