import os
from typing import Optional, Type

from core.get_value import get_value
from core.typing import T


class Environment:
    _environment = "production"

    @classmethod
    def get_environment(cls):
        return cls._environment

    @classmethod
    def set_environment(cls, environment: str):
        if environment not in ["development", "staging", "production"]:
            raise ValueError("Invalid environment")
        Environment._environment = environment

    @staticmethod
    def get(key: str, cast_type: Type[T], default: Optional[T] = None) -> Optional[T]:
        """
        Get an environment variable and cast it to a specified type.
        :param str key: The name of the environment variable.
        :param Type[T] cast_type: A callable to cast the variable's value.
        :param Optional[T] default: The default value to return if the variable is not found. Defaults to None.The default value to return if the variable is not found. Defaults to None.
        :return: The casted value, or None if the variable is not found.
        :rtype: Optional[T]
        """

        return get_value(dict(os.environ), key, cast_type, default)
