"""Pytest configuration and helper functions for Polytropos tests."""

import textwrap
from pathlib import Path
from typing import Any
from unittest import mock

import pytest

# Configure pytest
pytest_plugins = []


@pytest.fixture
def mock_odoo():
    """Mock Odoo package with the _odoo helper patched."""
    odoo = mock.MagicMock()
    with (
        mock.patch("polytropos.ph._odoo", return_value=odoo),
        mock.patch("polytropos.build.primitives._odoo", return_value=odoo),
    ):
        yield odoo


def build_file_tree(
    spec: dict[str, Any],
    base: Path | None = None,
    dedent: bool = True,
) -> None:
    """Builds a file tree based on the received spec.

    Params:
        spec: A mapping from filesystem paths to file contents.
        base: Base directory for the files (defaults to current dir).
        dedent: Whether to dedent string contents.
    """
    if base is None:
        base = Path.cwd()

    for path, contents in spec.items():
        full_path = base / path
        full_path.parent.mkdir(parents=True, exist_ok=True)

        if isinstance(contents, Path):
            full_path.symlink_to(contents)
        elif isinstance(contents, bytes):
            full_path.write_bytes(contents)
        else:
            if dedent:
                contents = textwrap.dedent(contents)
            full_path.write_text(contents)
