"""Lightweight configuration helpers for the trimmed plume_nav_sim test suite."""

from __future__ import annotations

from dataclasses import dataclass
from typing import Dict, Iterable, Optional, cast

REPRODUCIBILITY_SEEDS: Iterable[int] = (0, 1, 2, 3)


def _base_config(**overrides: object) -> Dict[str, object]:
    config = {
        "grid_size": (16, 16),
        "source_location": (8, 8),
        "max_steps": 100,
        "goal_radius": 0.5,
        "render_mode": "rgb_array",
    }
    config.update(overrides)
    return config


def create_unit_test_config(**overrides: object) -> Dict[str, object]:
    return _base_config(test_profile="unit", **overrides)


def create_integration_test_config(**overrides: object) -> Dict[str, object]:
    return _base_config(test_profile="integration", **overrides)


def create_performance_test_config(**overrides: object) -> Dict[str, object]:
    return _base_config(test_profile="performance", **overrides)


def create_reproducibility_test_config(**overrides: object) -> Dict[str, object]:
    seeds = tuple(cast(Iterable[int], overrides.pop("seeds", REPRODUCIBILITY_SEEDS)))
    config = _base_config(test_profile="reproducibility", **overrides)
    config["seeds"] = seeds
    return config


def create_edge_case_test_config(**overrides: object) -> Dict[str, object]:
    return _base_config(test_profile="edge_case", **overrides)


@dataclass
class _TestConfigFactory:
    """Simplified stand-in for the production configuration factory."""

    auto_optimize: bool = False
    _system_capabilities: Optional[Dict[str, object]] = None
    _auto_optimize: bool = False
    _configuration_cache: Optional[Dict[str, object]] = None

    def __post_init__(self) -> None:
        if self._system_capabilities is None:
            self._system_capabilities = {}
        if self._configuration_cache is None:
            self._configuration_cache = {}
        self._auto_optimize = self.auto_optimize

    def detect_system_capabilities(
        self, force_refresh: bool = False
    ) -> Dict[str, object]:
        if force_refresh or not self._system_capabilities:
            self._system_capabilities = {"memory_gb": 4, "cpu_count": 2}
        # mypy: _system_capabilities is Optional; ensured above
        return cast(Dict[str, object], self._system_capabilities)

    def create_config(self, profile: str, **overrides: object) -> Dict[str, object]:
        factory_map = {
            "unit": create_unit_test_config,
            "integration": create_integration_test_config,
            "performance": create_performance_test_config,
            "reproducibility": create_reproducibility_test_config,
            "edge_case": create_edge_case_test_config,
        }
        try:
            builder = factory_map[profile]
        except KeyError as exc:
            raise ValueError(f"Unknown test configuration profile: {profile}") from exc
        return builder(**overrides)


# Preserve public API while avoiding pytest collection on helper class name
TestConfigFactory = _TestConfigFactory
