"""Custom exception classes for the settings module."""

from collections.abc import Sequence
from pathlib import Path
from typing import Any


class EnvSettingError(ValueError):
    """Base class for environment setting errors."""

    def __init__(self, env_var: str, message: str):
        self.env_var = env_var
        super().__init__(f"{env_var}: {message}")


class MissingEnvironmentVariableError(EnvSettingError):
    """Raised when a required environment variable is not set."""

    def __init__(self, env_var: str):
        super().__init__(env_var, "Required environment variable is not set")


class EnvironmentValueRangeError(EnvSettingError):
    """Raised when an environment variable value is out of the allowed range."""

    def __init__(
        self, env_var: str, value: Any, min_val: Any = None, max_val: Any = None
    ):
        if min_val is not None and value < min_val:
            msg = f"value {value} is below minimum {min_val}"
        elif max_val is not None and value > max_val:
            msg = f"value {value} is above maximum {max_val}"
        else:
            msg = f"value {value} is out of range"
        super().__init__(env_var, msg)


class EnvironmentPathNotFoundError(EnvSettingError):
    """Raised when a path specified in an environment variable does not exist."""

    def __init__(self, env_var: str, path: Path | str):
        super().__init__(env_var, f"Path {path} does not exist")


class InvalidEnvironmentValueError(EnvSettingError):
    """Raised when an environment variable value is not among the allowed values."""

    def __init__(self, env_var: str, value: str, allowed_values: Sequence):
        super().__init__(
            env_var, f"value '{value}' not in allowed values: {allowed_values}"
        )
