from abc import ABC, abstractmethod
from collections.abc import Iterator
from typing import TYPE_CHECKING, Any, Union

import httpx
from openai.types.file_deleted import FileDeleted

from litellm.proxy._types import UserAPIKeyAuth
from litellm.types.files import TwoStepFileUploadConfig
from litellm.types.llms.openai import (
    AllMessageValues,
    CreateFileRequest,
    FileContentRequest,
    FileListPage,
    OpenAICreateFileRequestOptionalParams,
    OpenAIFileObject,
)
from litellm.types.utils import LlmProviders, ModelResponse

from ..chat.transformation import BaseConfig

if TYPE_CHECKING:
    import tiktoken

    from litellm.litellm_core_utils.litellm_logging import Logging as _LiteLLMLoggingObj
    from litellm.router import Router as _Router
    from litellm.types.llms.openai import HttpxBinaryResponseContent

    LiteLLMLoggingObj = _LiteLLMLoggingObj
    Span = Any
    Router = _Router
else:
    LiteLLMLoggingObj = Any
    Span = Any
    Router = Any


class BaseFileUploadStream(ABC):
    """Re-iterable request body that yields an upload's bytes lazily.

    A provider returns one of these (inside the upload config from
    ``transform_create_file_request``) when the upload body can be produced
    incrementally; the HTTP handler then sends it in bounded chunks instead of
    buffering the whole payload, which is what exhausts memory on large uploads.

    ``iter_bytes`` must return a fresh iterator each call so the body can be
    replayed if the upload is retried.
    """

    @abstractmethod
    def iter_bytes(self) -> Iterator[bytes]: ...


class BaseFilesConfig(BaseConfig):
    @property
    @abstractmethod
    def custom_llm_provider(self) -> LlmProviders:
        pass

    @property
    def file_upload_http_method(self) -> str:
        """
        HTTP method to use for file uploads.
        Override this in provider configs if they need different methods.
        Default is POST (used by most providers like OpenAI, Anthropic).
        S3-based providers like Bedrock should return "PUT".
        """
        return "POST"

    @abstractmethod
    def get_supported_openai_params(self, model: str) -> list[OpenAICreateFileRequestOptionalParams]:
        pass

    def get_complete_file_url(
        self,
        api_base: str | None,
        api_key: str | None,
        model: str,
        optional_params: dict,
        litellm_params: dict,
        data: CreateFileRequest,
    ):
        return self.get_complete_url(
            api_base=api_base,
            api_key=api_key,
            model=model,
            optional_params=optional_params,
            litellm_params=litellm_params,
        )

    @abstractmethod
    def transform_create_file_request(
        self,
        model: str,
        create_file_data: CreateFileRequest,
        optional_params: dict,
        litellm_params: dict,
    ) -> Union[dict, str, bytes, "TwoStepFileUploadConfig"]:
        """
        Transform OpenAI-style file creation request into provider-specific format.

        Returns:
            - dict: For pre-signed single-step uploads (e.g., Bedrock S3)
            - str/bytes: For traditional file uploads
            - TwoStepFileUploadConfig: For two-step upload process (e.g., Manus, GCS)
        """

    @abstractmethod
    def transform_create_file_response(
        self,
        model: str | None,
        raw_response: httpx.Response,
        logging_obj: LiteLLMLoggingObj,
        litellm_params: dict,
    ) -> OpenAIFileObject:
        pass

    @abstractmethod
    def transform_retrieve_file_request(
        self,
        file_id: str,
        optional_params: dict,
        litellm_params: dict,
    ) -> tuple[str, dict]:
        """Transform file retrieve request into provider-specific format."""

    @abstractmethod
    def transform_retrieve_file_response(
        self,
        raw_response: httpx.Response,
        logging_obj: LiteLLMLoggingObj,
        litellm_params: dict,
    ) -> OpenAIFileObject:
        """Transform file retrieve response into OpenAI format."""

    @abstractmethod
    def transform_delete_file_request(
        self,
        file_id: str,
        optional_params: dict,
        litellm_params: dict,
    ) -> tuple[str, dict]:
        """Transform file delete request into provider-specific format."""

    @abstractmethod
    def transform_delete_file_response(
        self,
        raw_response: httpx.Response,
        logging_obj: LiteLLMLoggingObj,
        litellm_params: dict,
    ) -> "FileDeleted":
        """Transform file delete response into OpenAI format."""

    @abstractmethod
    def transform_list_files_request(
        self,
        purpose: str | None,
        optional_params: dict,
        litellm_params: dict,
    ) -> tuple[str, dict]:
        """Transform file list request into provider-specific format."""

    @abstractmethod
    def transform_list_files_response(
        self,
        raw_response: httpx.Response,
        logging_obj: LiteLLMLoggingObj,
        litellm_params: dict,
    ) -> list[OpenAIFileObject]:
        """Transform file list response into OpenAI format."""

    @abstractmethod
    def transform_file_content_request(
        self,
        file_content_request: "FileContentRequest",
        optional_params: dict,
        litellm_params: dict,
    ) -> tuple[str, dict]:
        """Transform file content request into provider-specific format."""

    @abstractmethod
    def transform_file_content_response(
        self,
        raw_response: httpx.Response,
        logging_obj: LiteLLMLoggingObj,
        litellm_params: dict,
    ) -> "HttpxBinaryResponseContent":
        """Transform file content response into OpenAI format."""

    def transform_request(
        self,
        model: str,
        messages: list[AllMessageValues],
        optional_params: dict,
        litellm_params: dict,
        headers: dict,
    ) -> dict:
        raise NotImplementedError(
            "AudioTranscriptionConfig does not need a request transformation for audio transcription models"
        )

    def transform_response(
        self,
        model: str,
        raw_response: httpx.Response,
        model_response: ModelResponse,
        logging_obj: LiteLLMLoggingObj,
        request_data: dict,
        messages: list[AllMessageValues],
        optional_params: dict,
        litellm_params: dict,
        encoding: "tiktoken.Encoding | None",
        api_key: str | None = None,
        json_mode: bool | None = None,
    ) -> ModelResponse:
        raise NotImplementedError(
            "AudioTranscriptionConfig does not need a response transformation for audio transcription models"
        )


class BaseFileEndpoints(ABC):
    @abstractmethod
    async def acreate_file(
        self,
        create_file_request: CreateFileRequest,
        llm_router: Router,
        target_model_names_list: list[str],
        litellm_parent_otel_span: Span,
        user_api_key_dict: UserAPIKeyAuth,
    ) -> OpenAIFileObject:
        pass

    @abstractmethod
    async def afile_retrieve(
        self,
        file_id: str,
        litellm_parent_otel_span: Span | None,
        llm_router: Router | None = None,
    ) -> OpenAIFileObject:
        pass

    @abstractmethod
    async def afile_list(
        self,
        purpose: str | None,
        litellm_parent_otel_span: Span | None,
        user_api_key_dict: UserAPIKeyAuth,
        limit: int | None = None,
        after: str | None = None,
        **data: dict,
    ) -> FileListPage:
        pass

    @abstractmethod
    async def afile_delete(
        self,
        file_id: str,
        litellm_parent_otel_span: Span | None,
        llm_router: Router,
        **data: dict,
    ) -> OpenAIFileObject:
        pass

    @abstractmethod
    async def afile_content(
        self,
        file_id: str,
        litellm_parent_otel_span: Span | None,
        llm_router: Router,
        **data: dict,
    ) -> "HttpxBinaryResponseContent":
        pass
