"""
MQTT 5.0 Reason Codes

Reason codes are used in response packets to indicate the result of an operation.
Reference: https://docs.oasis-open.org/mqtt/mqtt/v5.0/mqtt-v5.0.html
"""

from enum import IntEnum


class ReasonCode(IntEnum):
    """MQTT 5.0 Reason Codes"""
    
    # Success (0x00)
    SUCCESS = 0x00
    
    # Normal disconnection
    NORMAL_DISCONNECTION = 0x00
    DISCONNECT_WITH_WILL_MESSAGE = 0x04
    
    # CONNACK Reason Codes
    UNSPECIFIED_ERROR = 0x80
    MALFORMED_PACKET = 0x81
    PROTOCOL_ERROR = 0x82
    IMPLEMENTATION_SPECIFIC_ERROR = 0x83
    UNSUPPORTED_PROTOCOL_VERSION = 0x84
    CLIENT_IDENTIFIER_NOT_VALID = 0x85
    BAD_USER_NAME_OR_PASSWORD = 0x86
    NOT_AUTHORIZED = 0x87
    SERVER_UNAVAILABLE = 0x88
    SERVER_BUSY = 0x89
    BANNED = 0x8A
    BAD_AUTHENTICATION_METHOD = 0x8C
    TOPIC_NAME_INVALID = 0x90
    PACKET_TOO_LARGE = 0x95
    QUOTA_EXCEEDED = 0x97
    PAYLOAD_FORMAT_INVALID = 0x99
    RETAIN_NOT_SUPPORTED = 0x9A
    QOS_NOT_SUPPORTED = 0x9B
    USE_ANOTHER_SERVER = 0x9C
    SERVER_MOVED = 0x9D
    CONNECTION_RATE_EXCEEDED = 0x9F
    
    # PUBACK, PUBREC, PUBREL, PUBCOMP Reason Codes
    NO_MATCHING_SUBSCRIBERS = 0x10
    UNSPECIFIED_ERROR_PUB = 0x80
    IMPLEMENTATION_SPECIFIC_ERROR_PUB = 0x83
    NOT_AUTHORIZED_PUB = 0x87
    TOPIC_NAME_INVALID_PUB = 0x90
    PACKET_IDENTIFIER_IN_USE = 0x91
    QUOTA_EXCEEDED_PUB = 0x97
    PAYLOAD_FORMAT_INVALID_PUB = 0x99
    
    # SUBACK Reason Codes
    GRANTED_QOS_0 = 0x00
    GRANTED_QOS_1 = 0x01
    GRANTED_QOS_2 = 0x02
    UNSPECIFIED_ERROR_SUB = 0x80
    IMPLEMENTATION_SPECIFIC_ERROR_SUB = 0x83
    NOT_AUTHORIZED_SUB = 0x87
    TOPIC_FILTER_INVALID = 0x8F
    PACKET_IDENTIFIER_IN_USE_SUB = 0x91
    QUOTA_EXCEEDED_SUB = 0x97
    SHARED_SUBSCRIPTIONS_NOT_SUPPORTED = 0x9E
    SUBSCRIPTION_IDENTIFIERS_NOT_SUPPORTED = 0xA1
    WILDCARD_SUBSCRIPTIONS_NOT_SUPPORTED = 0xA2
    
    # UNSUBACK Reason Codes
    SUCCESS_UNSUB = 0x00
    NO_SUBSCRIPTION_EXISTED = 0x11
    UNSPECIFIED_ERROR_UNSUB = 0x80
    IMPLEMENTATION_SPECIFIC_ERROR_UNSUB = 0x83
    NOT_AUTHORIZED_UNSUB = 0x87
    TOPIC_FILTER_INVALID_UNSUB = 0x8F
    PACKET_IDENTIFIER_IN_USE_UNSUB = 0x91
    
    # DISCONNECT Reason Codes
    NORMAL_DISCONNECTION_DISC = 0x00
    DISCONNECT_WITH_WILL_MESSAGE_DISC = 0x04
    UNSPECIFIED_ERROR_DISC = 0x80
    MALFORMED_PACKET_DISC = 0x81
    PROTOCOL_ERROR_DISC = 0x82
    IMPLEMENTATION_SPECIFIC_ERROR_DISC = 0x83
    NOT_AUTHORIZED_DISC = 0x87
    SERVER_BUSY_DISC = 0x89
    SERVER_SHUTTING_DOWN = 0x8B
    BAD_AUTHENTICATION_METHOD_DISC = 0x8C
    KEEP_ALIVE_TIMEOUT = 0x8D
    SESSION_TAKEN_OVER = 0x8E
    TOPIC_FILTER_INVALID_DISC = 0x8F
    TOPIC_NAME_INVALID_DISC = 0x90
    RECEIVE_MAXIMUM_EXCEEDED = 0x93
    TOPIC_ALIAS_INVALID = 0x94
    PACKET_TOO_LARGE_DISC = 0x95
    MESSAGE_RATE_TOO_HIGH = 0x96
    QUOTA_EXCEEDED_DISC = 0x97
    ADMINISTRATIVE_ACTION = 0x98
    PAYLOAD_FORMAT_INVALID_DISC = 0x99
    RETAIN_NOT_SUPPORTED_DISC = 0x9A
    QOS_NOT_SUPPORTED_DISC = 0x9B
    USE_ANOTHER_SERVER_DISC = 0x9C
    SERVER_MOVED_DISC = 0x9D
    SHARED_SUBSCRIPTIONS_NOT_SUPPORTED_DISC = 0x9E
    CONNECTION_RATE_EXCEEDED_DISC = 0x9F
    MAXIMUM_CONNECT_TIME = 0xA0
    SUBSCRIPTION_IDENTIFIERS_NOT_SUPPORTED_DISC = 0xA1
    WILDCARD_SUBSCRIPTIONS_NOT_SUPPORTED_DISC = 0xA2
    
    # AUTH Reason Codes (§3.15)
    AUTH_SUCCESS = 0x00
    CONTINUE_AUTHENTICATION = 0x18
    REAUTHENTICATE = 0x19


# Compatibility mapping for MQTT 3.1.1 return codes
MQTT3_TO_MQTT5_REASON_CODE = {
    0x00: ReasonCode.SUCCESS,  # Connection accepted
    0x01: ReasonCode.UNSUPPORTED_PROTOCOL_VERSION,
    0x02: ReasonCode.CLIENT_IDENTIFIER_NOT_VALID,
    0x03: ReasonCode.SERVER_UNAVAILABLE,
    0x04: ReasonCode.BAD_USER_NAME_OR_PASSWORD,
    0x05: ReasonCode.NOT_AUTHORIZED,
}
