"""Benchmark configuration and shared fixtures."""

import pytest

try:
    import pytest_benchmark  # noqa: F401

    _HAS_BENCHMARK = True
except ImportError:
    _HAS_BENCHMARK = False


def pytest_collect_file(parent, file_path):
    """Collect bench_*.py files only if pytest-benchmark is installed."""
    if not _HAS_BENCHMARK:
        return None
    if file_path.suffix == ".py" and file_path.name.startswith("bench_"):
        return pytest.Module.from_parent(parent, path=file_path)
    return None


def pytest_configure(config):
    """Register benchmark markers."""
    config.addinivalue_line("markers", "benchmark: benchmark tests")


@pytest.fixture
def small_dag():
    """Small DAG for benchmarking (5 nodes)."""
    return {
        "nodes": ["A", "B", "C", "D", "E"],
        "edges": [("A", "B"), ("A", "C"), ("B", "D"), ("C", "D"), ("D", "E")],
    }


@pytest.fixture
def medium_dag():
    """Medium DAG for benchmarking (20 nodes)."""
    import random

    random.seed(42)
    nodes = [f"N{i}" for i in range(20)]
    edges = []
    for i in range(len(nodes)):
        for j in range(i + 1, len(nodes)):
            if random.random() < 0.15:
                edges.append((nodes[i], nodes[j]))
    return {"nodes": nodes, "edges": edges}


@pytest.fixture
def large_dag():
    """Large DAG for benchmarking (100 nodes)."""
    import random

    random.seed(42)
    nodes = [f"N{i}" for i in range(100)]
    edges = []
    for i in range(len(nodes)):
        for j in range(i + 1, min(i + 10, len(nodes))):
            if random.random() < 0.3:
                edges.append((nodes[i], nodes[j]))
    return {"nodes": nodes, "edges": edges}
