import json
from enum import Enum


class ErrorCode(Enum):
    """Enumeration of error codes used in the SRU Lint tool."""

    CHANGELOG_INVALID_DISTRIBUTION = "CHANGELOG001"
    CHANGELOG_BUG_NOT_TARGETED = "CHANGELOG002"
    CHANGELOG_VERSION_ORDER = "CHANGELOG003"

    PATCH_DEP3_FORMAT = "PATCH001"
    PATCH_DEP3_MISSING_DESCRIPTION = "PATCH002"
    PATCH_DEP3_EMPTY_DESCRIPTION = "PATCH003"
    PATCH_DEP3_MISSING_ORIGIN_AUTHOR = "PATCH004"
    PATCH_DEP3_INVALID_DATE = "PATCH005"
    PATCH_DEP3_INVALID_FORWARDED = "PATCH006"

    PUBLISHING_HISTORY_ALREADY_PUBLISHED = "PUBHIST001"
    PUBLISHING_HISTORY_API_ERROR = "PUBHIST002"
    PUBLISHING_HISTORY_PARSE_ERROR = "PUBHIST003"
    PUBLISHING_HISTORY_NEWER_VERSION_EXISTS = "PUBHIST003"

    UPLOAD_QUEUE_API_ERROR = "UPLOADQ001"
    UPLOAD_QUEUE_PARSE_ERROR = "UPLOADQ002"
    UPLOAD_QUEUE_ALREADY_QUEUED = "UPLOADQ003"
    UPLOAD_QUEUE_UNRELEASED = "UPLOADQ004"

    SRU_TEMPLATE_MISSING = "LPBUG001"
    SRU_LP_API_ERROR = "LPBUG002"
    SRU_NO_BUGS_REFERENCED = "LPBUG003"

    CONTROL_MAINTAINER_NOT_UPDATED = "CTRL001"


class ErrorEnumEncoder(json.JSONEncoder):
    """Custom JSON encoder that handles Enum serialization."""

    def default(self, obj):
        if isinstance(obj, Enum):
            return obj.value
        return super().default(obj)
