import json


class NotifiedByError(Exception):
    """Base exception for all NotifiedBy errors."""
    pass


class NotifiedByConfigError(NotifiedByError):
    """Configuration error (missing API key, invalid settings)."""
    pass


class NotifiedByAPIError(NotifiedByError):
    """API request failed (4xx or 5xx response)."""

    def __init__(self, status_code, response_text, endpoint=None):
        self.status_code = status_code
        self.response_text = response_text
        self.endpoint = endpoint

        # Parse error message from response if possible
        try:
            error_data = json.loads(response_text)
            if isinstance(error_data, dict):
                message = error_data.get('detail', response_text)
            else:
                message = response_text
        except (json.JSONDecodeError, ValueError):
            message = response_text

        error_msg = f"NotifiedBy API error (HTTP {status_code})"
        if endpoint:
            error_msg += f" at {endpoint}"
        error_msg += f": {message}"

        super().__init__(error_msg)


class NotifiedByValidationError(NotifiedByAPIError):
    """API validation error (400 Bad Request)."""
    pass


class NotifiedByAuthError(NotifiedByAPIError):
    """Authentication/authorization error (401/403)."""
    pass


class NotifiedByNotFoundError(NotifiedByAPIError):
    """Resource not found (404)."""
    pass
