from enum import Enum
import json


class ReleasePhase(str, Enum):
    ENV = "PhaseEnv"
    CONFIG = "PhaseConfig"
    BUILD = "PhaseBuild"
    CHECK = "PhaseCheck"
    PACKAGE = "PhasePackage"
    VERIFY = "PhaseVerify"


class ProcessStep(str, Enum):
    """
    ReleaseFlow中每个阶段的几个执行步骤，顺序执行
    """

    PRE = "Pre"
    PROCESS = "Process"
    POST = "Post"


class ReleaseErr(Exception):
    """Release tool专用异常基类，包含错误信息和上下文"""

    def __init__(self, message: str, ctx: dict = None):
        self.message = message
        self.ctx = ctx or {}
        super().__init__(message)

    def __str__(self):
        return f"{self.message} (context: {self.ctx})" if self.ctx else self.message


class Dict(dict):
    """
    一个字典，支持点号访问，从xxx[xxx][xxx] 的方式转化为 xxx.xxx.xxx 的形式访问字典中的值
    如果key不存在，返回一个空的字典
    """

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        # 将字典中的所有值递归转换为 Dict
        for key, value in self.items():
            if isinstance(value, dict):
                self[key] = Dict(value)

    @classmethod
    def from_dict(cls, data: dict) -> "Dict":
        """
        从普通字典创建一个 Dict 对象
        :param data: 要转换的字典
        :return: Dict 对象
        """
        return cls(data)

    def __getattr__(self, key):
        if key in self:
            value = self[key]
            # 如果值是字典，递归转换为 Dict
            if isinstance(value, dict):
                return Dict(value)
            return value
        # 如果key不存在，返回None
        # return Dict()
        raise AttributeError(f"In object not found '{key}'")

    def __setattr__(self, key, value):
        self[key] = value

    def __str__(self):
        string = json.dumps(self, indent=4)
        return string

    def __repr__(self):
        return self.__str__()


class List(list):
    def __str__(self):
        return json.dumps(self, indent=4)

    def __repr__(self):
        return self.__str__()
