"""Mock switch driver used in development and simulation modes."""

from __future__ import annotations

from dataclasses import dataclass, field


@dataclass(slots=True)
class MockSwitchDriver:
    """In-memory switch driver that validates and tracks current position."""

    name: str
    positions: int
    default_position: int = 0
    _is_open: bool = field(init=False, default=False, repr=False)
    _current_position: int = field(init=False, default=0, repr=False)

    def __post_init__(self) -> None:
        """Initialize closed driver state."""
        self._is_open = False
        self._current_position = 0

    def open(self) -> None:
        """Validate config and open driver."""
        self._validate()
        self._is_open = True
        self.switch_to(self.default_position)

    def close(self) -> None:
        """Close mock driver."""
        self._is_open = False

    def position_count(self) -> int:
        """Return number of supported positions."""
        return self.positions

    def switch_to(self, position: int) -> None:
        """Switch to requested position."""
        if not self._is_open:
            raise RuntimeError(f"Switch driver is not open for {self.name}")
        if position < 0 or position >= self.positions:
            raise ValueError(f"Position out of range for {self.name}: {position}")
        self._current_position = int(position)

    @property
    def current_position(self) -> int:
        """Return current position."""
        return self._current_position

    def _validate(self) -> None:
        """Validate mock driver parameters."""
        if self.positions <= 0:
            raise ValueError(f"Switch positions must be > 0 for {self.name}")
        if self.default_position < 0 or self.default_position >= self.positions:
            raise ValueError(f"Switch default_position out of range for {self.name}")
