#!/usr/bin/env python3
# -*- coding: utf-8 -*-

"""
Algorithm Package Exceptions
算法包异常类定义
"""


class AlgorithmError(Exception):
    """算法包基础异常"""
    
    def __init__(self, message: str, error_code: str = None, details: dict = None):
        super().__init__(message)
        self.message = message
        self.error_code = error_code
        self.details = details or {}
    
    def __str__(self):
        if self.error_code:
            return f"[{self.error_code}] {self.message}"
        return self.message


class ConfigError(AlgorithmError):
    """配置相关异常"""
    pass


class AIServiceError(AlgorithmError):
    """AI服务异常"""
    
    def __init__(self, message: str, error_code: str = None, details: dict = None, retryable: bool = False):
        super().__init__(message, error_code, details)
        self.retryable = retryable


class ContentGenerationError(AlgorithmError):
    """内容生成异常"""
    pass


class PosterGenerationError(AlgorithmError):
    """海报生成异常"""
    pass


class DocumentProcessingError(AlgorithmError):
    """文档处理异常"""
    pass


class ResourceNotFoundError(AlgorithmError):
    """资源未找到异常"""
    pass


class ValidationError(AlgorithmError):
    """验证异常"""
    pass


class TimeoutError(AlgorithmError):
    """超时异常"""
    pass


class RateLimitError(AIServiceError):
    """API频率限制异常"""
    
    def __init__(self, message: str = "API rate limit exceeded", retry_after: int = None):
        super().__init__(message, "RATE_LIMIT", {"retry_after": retry_after}, retryable=True)
        self.retry_after = retry_after


class QuotaExceededError(AIServiceError):
    """配额超限异常"""
    
    def __init__(self, message: str = "API quota exceeded"):
        super().__init__(message, "QUOTA_EXCEEDED", retryable=False)


class ModelNotFoundError(AIServiceError):
    """模型未找到异常"""
    
    def __init__(self, model_name: str):
        message = f"AI model not found: {model_name}"
        super().__init__(message, "MODEL_NOT_FOUND", {"model": model_name}, retryable=False)


class NetworkError(AIServiceError):
    """网络异常"""
    
    def __init__(self, message: str = "Network connection failed"):
        super().__init__(message, "NETWORK_ERROR", retryable=True) 


class CrawlingError(AlgorithmError):
    """爬虫相关错误"""
    pass


class KeywordAnalysisError(ContentGenerationError):
    """关键词分析错误"""
    pass


class ContentAnalysisError(ContentGenerationError):
    """内容分析错误"""
    pass 