"""Exception types for the Areteos SDK."""

from __future__ import annotations

from typing import Any


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


class ApiError(AreteosError):
    """Raised when the API returns an error response."""

    def __init__(self, status_code: int, errors: list[dict[str, Any]]) -> None:
        self.status_code = status_code
        self.errors = errors
        detail = ""
        if errors:
            first = errors[0]
            detail = first.get("detail") or first.get("title") or str(first)
        super().__init__(f"API error {status_code}: {detail}")


class TimeoutError(AreteosError):
    """Raised when a polling operation exceeds its timeout."""


class NotFoundError(ApiError):
    """Raised when a resource is not found (404)."""

    def __init__(self, errors: list[dict[str, Any]]) -> None:
        super().__init__(404, errors)


class ForbiddenError(ApiError):
    """Raised when access is denied (403)."""

    def __init__(self, errors: list[dict[str, Any]]) -> None:
        super().__init__(403, errors)


class ValidationError(ApiError):
    """Raised when the request is invalid (422)."""

    def __init__(self, errors: list[dict[str, Any]]) -> None:
        super().__init__(422, errors)


class GraphConversionError(AreteosError):
    """Raised when a PydanticAI Graph cannot be converted to an Areteos workflow.

    Common causes:
    - Graph has branching nodes (fan-out)
    - A node uses ``returns_base_node=True`` (dynamic dispatch)
    - Graph topology is not a linear chain
    """
