"""Shared pytest fixtures for tests/unit/cli/workflows/commands/."""

from unittest.mock import patch

import pytest

from agentic_devtools.prompts import loader


@pytest.fixture
def temp_prompts_dir(tmp_path):
    """Create a temporary prompts directory with test templates."""
    prompts_dir = tmp_path / "prompts"
    prompts_dir.mkdir()
    with patch.object(loader, "get_prompts_dir", return_value=prompts_dir):
        yield prompts_dir


@pytest.fixture
def temp_output_dir(tmp_path):
    """Create a temporary output directory."""
    output_dir = tmp_path / "temp"
    output_dir.mkdir()
    with patch.object(loader, "get_temp_output_dir", return_value=output_dir):
        yield output_dir


@pytest.fixture
def clear_state_before(temp_state_dir):
    """Clear state before each test.

    Note: We only remove the state file, not the entire temp folder,
    to avoid deleting directories created by other fixtures (like temp_prompts_dir).
    """
    state_file = temp_state_dir / "state.json"
    if state_file.exists():
        state_file.unlink()
    yield


@pytest.fixture
def mock_workflow_state_clearing():
    """Mock clear_state_for_workflow_initiation and _ensure_bootstrap_identity* to be no-ops.

    Workflow initiation commands resolve bootstrap identity and reset workflow
    tracking keys (workflow, agdt_run_id) at the start.  This fixture prevents
    both operations, which is useful when tests pre-set workflow state before
    calling the command and want to avoid real filesystem/git operations.
    """
    with patch("agentic_devtools.cli.workflows.commands.clear_state_for_workflow_initiation"):
        with patch("agentic_devtools.cli.workflows.commands._ensure_bootstrap_identity"):
            with patch("agentic_devtools.cli.workflows.commands._ensure_bootstrap_identity_and_scope"):
                yield


@pytest.fixture(autouse=True)
def prevent_real_workflow_side_effects():
    """Prevent real VS Code/Copilot/background launches from command tests.

    Some workflow command tests intentionally exercise high-level control flow.
    If a branch is left unpatched in an individual test, these guards ensure
    the test suite still cannot open a VS Code window, start a Copilot session,
    or spawn real background setup tasks.
    """
    with patch(
        "agentic_devtools.cli.workflows.worktree_setup.open_vscode_workspace",
        return_value=True,
    ):
        with patch(
            "agentic_devtools.cli.workflows.worktree_setup._start_copilot_session_for_pr_review",
            return_value=False,
        ):
            with patch(
                "agentic_devtools.cli.workflows.worktree_setup._start_copilot_session_for_workflow",
                return_value=False,
            ):
                with patch(
                    "agentic_devtools.cli.workflows.worktree_setup.start_worktree_setup_background",
                    return_value="test-task-id",
                ):
                    with patch(
                        "agentic_devtools.cli.copilot.session.start_copilot_session",
                        return_value=None,
                    ):
                        yield
