# -*- coding: utf-8 -*-
from __future__ import annotations

import enum
import typing
import uuid

NULL_UUID = uuid.UUID('00000000-0000-0000-0000-000000000000')


class Environment:
    def __init__(self, name: str, value: str):
        self._name = name
        self._value = value

    @property
    def name(self) -> str:
        return self._name

    @property
    def value(self) -> str:
        return self._value

    def __repr__(self) -> str:
        return f'<Env {self.name}: {self.value}>'

    def __str__(self) -> str:
        return self.name

    def to_rpc(self) -> str:
        return self.value


class Environments:
    METAL = Environment('METAL', 'metal')
    DOCKER = Environment('DOCKER', 'docker')
    DEPLOYMENT = Environment('DEPLOYMENT', 'deployment')
    AIO = Environment('AIO', 'aio')

    def _current_envs(self) -> typing.Generator[Environment, None, None]:
        for attribute in dir(self):
            candidate = getattr(self, attribute)
            if isinstance(candidate, Environment):
                yield candidate

    def __call__(self, value: str) -> Environment:
        result: Environment | None = None
        for candidate in self._current_envs():
            if candidate.value == value:
                result = candidate
                break

        if result is None:
            raise ValueError(f'Could not resolve env: `{value}`')

        return result

    def choices(self) -> list[tuple[str, str]]:
        result = []

        for env in self._current_envs():
            result.append((env.value, env.name))

        return result


Env = Environments()


class App(enum.Enum):
    ADMIN = 'admin'
    WEBAPP = 'webapp'
