"""
自定义异常模块
"""
from typing import Any, Dict, Optional


class BaseAPIException(Exception):
    """API异常基类"""
    
    def __init__(
        self,
        message: str = "服务器内部错误",
        code: int = 500001,
        status_code: int = 500,
        details: Optional[Dict[str, Any]] = None
    ):
        self.message = message
        self.code = code
        self.status_code = status_code
        self.details = details or {}
        super().__init__(self.message)


class ValidationError(BaseAPIException):
    """验证错误"""
    
    def __init__(
        self,
        message: str = "参数验证失败",
        details: Optional[Dict[str, Any]] = None
    ):
        super().__init__(
            message=message,
            code=100001,
            status_code=400,
            details=details
        )


class AuthenticationError(BaseAPIException):
    """认证错误"""
    
    def __init__(
        self,
        message: str = "认证失败",
        details: Optional[Dict[str, Any]] = None
    ):
        super().__init__(
            message=message,
            code=200001,
            status_code=401,
            details=details
        )


class AuthorizationError(BaseAPIException):
    """授权错误"""
    
    def __init__(
        self,
        message: str = "权限不足",
        details: Optional[Dict[str, Any]] = None
    ):
        super().__init__(
            message=message,
            code=200003,
            status_code=403,
            details=details
        )


class NotFoundError(BaseAPIException):
    """资源不存在错误"""
    
    def __init__(
        self,
        message: str = "资源不存在",
        details: Optional[Dict[str, Any]] = None
    ):
        super().__init__(
            message=message,
            code=100004,
            status_code=404,
            details=details
        )


class ConflictError(BaseAPIException):
    """冲突错误"""
    
    def __init__(
        self,
        message: str = "资源冲突",
        details: Optional[Dict[str, Any]] = None
    ):
        super().__init__(
            message=message,
            code=400001,
            status_code=409,
            details=details
        )


class RateLimitError(BaseAPIException):
    """频率限制错误"""
    
    def __init__(
        self,
        message: str = "请求过于频繁",
        details: Optional[Dict[str, Any]] = None
    ):
        super().__init__(
            message=message,
            code=100006,
            status_code=429,
            details=details
        )


class TokenExpiredError(AuthenticationError):
    """令牌过期错误"""
    
    def __init__(
        self,
        message: str = "令牌已过期",
        details: Optional[Dict[str, Any]] = None
    ):
        super().__init__(message=message, details=details)
        self.code = 200003


class TokenInvalidError(AuthenticationError):
    """令牌无效错误"""
    
    def __init__(
        self,
        message: str = "令牌无效",
        details: Optional[Dict[str, Any]] = None
    ):
        super().__init__(message=message, details=details)
        self.code = 200002


class UserNotFoundError(NotFoundError):
    """用户不存在错误"""
    
    def __init__(
        self,
        message: str = "用户不存在",
        details: Optional[Dict[str, Any]] = None
    ):
        super().__init__(message=message, details=details)


class UserInactiveError(AuthenticationError):
    """用户未激活错误"""
    
    def __init__(
        self,
        message: str = "用户未激活",
        details: Optional[Dict[str, Any]] = None
    ):
        super().__init__(message=message, details=details)


class AppNotFoundError(NotFoundError):
    """应用不存在错误"""
    
    def __init__(
        self,
        message: str = "应用不存在",
        details: Optional[Dict[str, Any]] = None
    ):
        super().__init__(message=message, details=details)
        self.code = 300001


class AppInactiveError(AuthenticationError):
    """应用未激活错误"""
    
    def __init__(
        self,
        message: str = "应用未激活",
        details: Optional[Dict[str, Any]] = None
    ):
        super().__init__(message=message, details=details)


class InvalidRedirectURIError(ValidationError):
    """无效回调URL错误"""
    
    def __init__(
        self,
        message: str = "回调URL不匹配",
        details: Optional[Dict[str, Any]] = None
    ):
        super().__init__(message=message, details=details)
        self.code = 300003


class InvalidGrantError(ValidationError):
    """无效授权错误"""
    
    def __init__(
        self,
        message: str = "无效的授权类型",
        details: Optional[Dict[str, Any]] = None
    ):
        super().__init__(message=message, details=details)


class InvalidScopeError(ValidationError):
    """无效权限范围错误"""
    
    def __init__(
        self,
        message: str = "无效的权限范围",
        details: Optional[Dict[str, Any]] = None
    ):
        super().__init__(message=message, details=details)


class DatabaseError(BaseAPIException):
    """数据库错误"""
    
    def __init__(
        self,
        message: str = "数据库操作失败",
        details: Optional[Dict[str, Any]] = None
    ):
        super().__init__(
            message=message,
            code=500003,
            status_code=500,
            details=details
        )


class ExternalServiceError(BaseAPIException):
    """外部服务错误"""
    
    def __init__(
        self,
        message: str = "外部服务调用失败",
        details: Optional[Dict[str, Any]] = None
    ):
        super().__init__(
            message=message,
            code=500004,
            status_code=502,
            details=details
        )