from collections.abc import Generator
from pathlib import Path

import pytest


@pytest.fixture(autouse=True)
def reset_adapter_registry() -> Generator[None, None, None]:
    """Reset the AdapterRegistry singleton after every test.

    This prevents cached adapter instances from leaking between tests, which
    could cause unexpected interactions when tests monkeypatch adapter methods.
    """
    from agentmux.adapters.base import AdapterRegistry

    yield
    AdapterRegistry.reset()


def populate_setup(root: Path) -> None:
    """Create a minimal set of component files in *root* for use in integration tests."""
    (root / "agents").mkdir(exist_ok=True)
    (root / "agents" / "helper.md").write_text(
        "---\nname: Helper\ndescription: A helper\n---\nBe helpful."
    )
    (root / "commands").mkdir(exist_ok=True)
    (root / "commands" / "fix-tests.md").write_text(
        "---\nname: Fix Tests\ndescription: Fix them\n---\nFix all failing tests."
    )
    (root / "skills").mkdir(exist_ok=True)
    skill_dir = root / "skills" / "my-skill"
    skill_dir.mkdir()
    (skill_dir / "SKILL.md").write_text(
        "---\nname: My Skill\ndescription: A skill\n---\nSkill content."
    )
    (root / "rules").mkdir(exist_ok=True)
    (root / "rules" / "no-comments.md").write_text(
        "---\nname: no-comments\nalwaysApply: false\n---\nAvoid verbose comments."
    )
    (root / "instructions.md").write_text("Be concise and helpful.")
