"""
Pytest configuration and shared test fixtures.

This module provides reusable test utilities for the sync engine tests.
"""

import subprocess
from pathlib import Path

import pytest
from click.testing import CliRunner

from tests.conftest import check_git_available, init_git_repo

# =============================================================================
# Shared CLI Test Fixtures
# =============================================================================


@pytest.fixture
def runner() -> CliRunner:
    """Create Click test runner.

    Returns:
        CliRunner instance for testing Click commands.
    """
    return CliRunner()


@pytest.fixture
def mock_git_project(tmp_path: Path) -> Path:
    """Create a mock git project directory with real git init.

    Creates:
        - Initialized git repository
        - .claude/ directory
        - Basic project structure for testing

    Args:
        tmp_path: Pytest tmp_path fixture providing temporary directory.

    Returns:
        Path to the initialized git project directory.
    """
    # Initialize real git repo
    subprocess.run(["git", "init"], cwd=tmp_path, check=True, capture_output=True)

    claude_dir = tmp_path / ".claude"
    claude_dir.mkdir(parents=True)

    # Create settings.json for commands that might read it
    settings_file = claude_dir / "settings.json"
    settings_file.write_text('{"allowedTools": []}')

    return tmp_path


@pytest.fixture
def non_git_project(tmp_path: Path) -> Path:
    """Create a mock non-git project directory.

    Args:
        tmp_path: Pytest tmp_path fixture providing temporary directory.

    Returns:
        Path to a directory that is NOT a git repository.
    """
    claude_dir = tmp_path / ".claude"
    claude_dir.mkdir(parents=True)
    (claude_dir / "settings.json").write_text("{}")

    return tmp_path


@pytest.fixture
def git_project_with_subdir(tmp_path: Path) -> tuple[Path, Path]:
    """Create a git project with a subdirectory.

    Returns:
        Tuple of (git_root, subdirectory)
    """
    # Initialize real git repo
    subprocess.run(["git", "init"], cwd=tmp_path, check=True, capture_output=True)

    claude_dir = tmp_path / ".claude"
    claude_dir.mkdir(parents=True)
    (claude_dir / "settings.json").write_text("{}")

    # Create a subdirectory
    subdir = tmp_path / "src" / "components"
    subdir.mkdir(parents=True)

    return tmp_path, subdir


__all__ = ["check_git_available", "init_git_repo"]
