"""
Application-specific exceptions for invoice extraction system.

This module defines a hierarchy of custom exceptions to provide better
error handling and debugging capabilities throughout the application.
"""


class InvoiceExtractionError(Exception):
    """Base exception for all invoice extraction errors."""

    def __init__(self, message: str, details: dict = None):
        """
        Initialize exception with message and optional details.

        Args:
            message: Human-readable error message
            details: Optional dict with additional error context
        """
        super().__init__(message)
        self.message = message
        self.details = details or {}

    def __str__(self):
        if self.details:
            details_str = ", ".join(f"{k}={v}" for k, v in self.details.items())
            return f"{self.message} ({details_str})"
        return self.message


class PDFProcessingError(InvoiceExtractionError):
    """Error during PDF processing (rendering, conversion)."""

    pass


class OCRError(InvoiceExtractionError):
    """Error during OCR processing."""

    pass


class ModelInferenceError(InvoiceExtractionError):
    """Error during YOLO model inference."""

    pass


class FieldValidationError(InvoiceExtractionError):
    """Error during field validation or normalization."""

    def __init__(self, field_name: str, value: str, reason: str, details: dict = None):
        """
        Initialize field validation error.

        Args:
            field_name: Name of the field that failed validation
            value: The invalid value
            reason: Why validation failed
            details: Additional context
        """
        message = f"Field '{field_name}' validation failed: {reason}"
        super().__init__(message, details)
        self.field_name = field_name
        self.value = value
        self.reason = reason


class DatabaseError(InvoiceExtractionError):
    """Error during database operations."""

    pass


class ConfigurationError(InvoiceExtractionError):
    """Error in application configuration."""

    pass


class PaymentLineParseError(InvoiceExtractionError):
    """Error parsing Swedish payment line format."""

    pass


class CustomerNumberParseError(InvoiceExtractionError):
    """Error parsing Swedish customer number."""

    pass


class DataLoadError(InvoiceExtractionError):
    """Error loading data from CSV or other sources."""

    pass


class AnnotationError(InvoiceExtractionError):
    """Error generating or processing YOLO annotations."""

    pass
