"""Custom exceptions for the WeChat XPay SDK."""

# Error code constants
ERR_SYSTEM_ERROR = -1
ERR_INVALID_OPENID = 268490001
ERR_BAD_REQUEST_PARAMS = 268490002
ERR_SIGNATURE_ERROR = 268490003
ERR_DUPLICATE_OPERATION = 268490004
ERR_ORDER_ALREADY_REFUNDED = 268490005
ERR_INSUFFICIENT_TOKEN_BALANCE = 268490006
ERR_SENSITIVE_CONTENT = 268490007
ERR_TOKEN_NOT_PUBLISHED = 268490008
ERR_SESSION_KEY_EXPIRED = 268490009
ERR_DATA_GENERATING = 268490011
ERR_BATCH_TASK_RUNNING = 268490012
ERR_CANNOT_REFUND_VERIFIED_ORDER = 268490013
ERR_REFUND_IN_PROGRESS = 268490014
ERR_RATE_LIMIT_EXCEEDED = 268490015
ERR_LEFT_FEE_MISMATCH = 268490016
ERR_INVALID_ENV = 268447747
ERR_INVALID_APPID = 268447748
ERR_MCH_NO_PERMISSION = 268447749
ERR_USER_NOT_REGISTERED = 268500000
ERR_INVALID_SIGN_TYPE = 268500001
ERR_INVALID_BODY_PARAM = 268500002
ERR_APPID_MCHID_MISMATCH = 268500003
ERR_RESOURCE_NOT_EXIST = 268500004
ERR_SIGN_VERIFICATION_FAILED = 268500005
ERR_MCH_NOT_EXISTS = 268500006
ERR_MCH_ACCOUNT_ABNORMAL = 268500007
ERR_AMOUNT_MISMATCH = 268500008
ERR_SERVICE_NOT_ENABLED = 268500009
ERR_REFUND_FEE_EXCEEDED = 268500010
ERR_ORDER_PAID = 268500011
ERR_ORDER_CLOSED = 268500012
ERR_MCH_CONFIG_LIMITED = 268500013
ERR_REAL_NAME_REQUIRED = 268500014
ERR_BANK_CARD_NOT_SUPPORTED = 268500015
ERR_ACCOUNT_FROZEN = 268500016
ERR_ORDER_NOT_EXIST = 268500017
ERR_REFUND_NOT_EXIST = 268500018
ERR_REFUND_AMOUNT_LIMIT = 268500019
ERR_INVALID_TRANSACTION_ID = 268500020
ERR_BALANCE_NOT_ENOUGH = 268500021
ERR_REFUND_FEE_LIMIT = 268500022
ERR_SYSTEM_BUSY = 268500023
ERR_UPLOAD_FILE_FAILED = 268500024
ERR_INVALID_FILE_TYPE = 268500025
ERR_FILE_SIZE_EXCEEDED = 268500026
ERR_INVALID_GOODS_CONFIG = 268500027
ERR_GOODS_NOT_PUBLISHED = 268500028
ERR_BATCH_TASK_FAILED = 268500029
ERR_WITHDRAW_AMOUNT_LIMIT = 268500030
ERR_WITHDRAW_FEE_LIMIT = 268500031
ERR_WITHDRAW_COUNT_LIMIT = 268500032
ERR_TRANSFER_ACCOUNT_NOT_BOUND = 268500033
ERR_ADVER_FUNDS_NOT_ENOUGH = 268500034
ERR_BILL_GENERATING = 268500035
ERR_REFUND_APPLICATION_EXIST = 268500036
ERR_COMPLAINT_NOT_EXIST = 268500037
ERR_NEGOTIATION_FAILED = 268500038
ERR_INVALID_MEDIA_TYPE = 268500039
ERR_MEDIA_UPLOAD_FAILED = 268500040
ERR_IMAGE_SIZE_EXCEEDED = 268500041
ERR_IMAGE_FORMAT_NOT_SUPPORTED = 268500042
ERR_VIDEO_SIZE_EXCEEDED = 268500043
ERR_VIDEO_FORMAT_NOT_SUPPORTED = 268500044


class XPayError(Exception):
    """Base exception for all SDK errors."""


class XPayAPIError(XPayError):
    """Raised when the WeChat XPay API returns a non-zero errcode.

    Attributes:
        errcode: WeChat error code integer.
        errmsg:  Human-readable error message from the API.

    Common error codes:
        -1 (ERR_SYSTEM_ERROR)
            System error
        268490001 (ERR_INVALID_OPENID)
            Invalid openid
        268490002 (ERR_BAD_REQUEST_PARAMS)
            Bad request parameters (see errmsg)
        268490003 (ERR_SIGNATURE_ERROR)
            Signature error
        268490004 (ERR_DUPLICATE_OPERATION)
            Duplicate operation — treat as success for present_currency
        268490005 (ERR_ORDER_ALREADY_REFUNDED)
            Order already refunded via cancel_currency_pay
        268490006 (ERR_INSUFFICIENT_TOKEN_BALANCE)
            Insufficient token balance for refund/payment
        268490007 (ERR_SENSITIVE_CONTENT)
            Sensitive content in image or text
        268490008 (ERR_TOKEN_NOT_PUBLISHED)
            Token not published yet
        268490009 (ERR_SESSION_KEY_EXPIRED)
            session_key missing or expired — caller must re-login
        268490011 (ERR_DATA_GENERATING)
            Data still generating — poll again
        268490012 (ERR_BATCH_TASK_RUNNING)
            Batch task running — wait for completion
        268490013 (ERR_CANNOT_REFUND_VERIFIED_ORDER)
            Cannot refund a verified order
        268490014 (ERR_REFUND_IN_PROGRESS)
            Refund in progress — retry with same params
        268490015 (ERR_RATE_LIMIT_EXCEEDED)
            Rate limit exceeded
        268490016 (ERR_LEFT_FEE_MISMATCH)
            left_fee mismatch — query order to confirm
    """

    def __init__(self, errcode: int, errmsg: str) -> None:
        self.errcode = errcode
        self.errmsg = errmsg
        super().__init__(f"XPay API error {errcode}: {errmsg}")
