# This file was auto-generated by Fern from our API Definition.

import typing
from json.decoder import JSONDecodeError

from .... import core
from ....core.api_error import ApiError
from ....core.client_wrapper import AsyncClientWrapper, SyncClientWrapper
from ....core.http_response import AsyncHttpResponse, HttpResponse
from ....core.jsonable_encoder import jsonable_encoder
from ....core.request_options import RequestOptions
from ....core.unchecked_base_model import construct_type
from ....errors.unprocessable_entity_error import UnprocessableEntityError
from ....types.media_id import MediaId
from ....types.order_id import OrderId
from ....types.order_media_response import OrderMediaResponse
from ....types.register_media_response import RegisterMediaResponse

# this is used as the default value for optional parameters
OMIT = typing.cast(typing.Any, ...)


class RawMediaClient:
    def __init__(self, *, client_wrapper: SyncClientWrapper):
        self._client_wrapper = client_wrapper

    def register(
        self,
        order_id: OrderId,
        *,
        declared_language: str,
        media: typing.Optional[core.File] = OMIT,
        media_url: typing.Optional[str] = OMIT,
        media_url_filename: typing.Optional[str] = OMIT,
        media_url_content_type: typing.Optional[str] = OMIT,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> HttpResponse[RegisterMediaResponse]:
        """
        Registers a media file with an order, either by uploading it directly or by providing a URL to fetch it from. Exactly one of `media` or `media_url` must be provided. The registered media can then be referenced when adding order items.

        Parameters
        ----------
        order_id : OrderId
            The ID of the order to which this media will be attached.

        declared_language : str
            The language code of the media content (e.g. 'en', 'es-ES'). Must be a supported source language for some order item kind.

        media : typing.Optional[core.File]
            See core.File for more documentation

        media_url : typing.Optional[str]
            A URL to fetch the media file from. Mutually exclusive with media.

        media_url_filename : typing.Optional[str]
            The filename for URL-sourced media (e.g. 'example.mp4'). Required when using media_url.

        media_url_content_type : typing.Optional[str]
            The MIME type for URL-sourced media (e.g. 'video/mp4'). Required when using media_url.

        request_options : typing.Optional[RequestOptions]
            Request-specific configuration.

        Returns
        -------
        HttpResponse[RegisterMediaResponse]
            Successful Response
        """
        _response = self._client_wrapper.httpx_client.request(
            f"v1/productions/orders/{jsonable_encoder(order_id)}/media",
            method="POST",
            data={
                "declared_language": declared_language,
                "media_url": media_url,
                "media_url_filename": media_url_filename,
                "media_url_content_type": media_url_content_type,
            },
            files={
                **({"media": media} if media is not None else {}),
            },
            request_options=request_options,
            omit=OMIT,
            force_multipart=True,
        )
        try:
            if 200 <= _response.status_code < 300:
                _data = typing.cast(
                    RegisterMediaResponse,
                    construct_type(
                        type_=RegisterMediaResponse,  # type: ignore
                        object_=_response.json(),
                    ),
                )
                return HttpResponse(response=_response, data=_data)
            if _response.status_code == 422:
                raise UnprocessableEntityError(
                    headers=dict(_response.headers),
                    body=typing.cast(
                        typing.Any,
                        construct_type(
                            type_=typing.Any,  # type: ignore
                            object_=_response.json(),
                        ),
                    ),
                )
            _response_json = _response.json()
        except JSONDecodeError:
            raise ApiError(status_code=_response.status_code, headers=dict(_response.headers), body=_response.text)
        raise ApiError(status_code=_response.status_code, headers=dict(_response.headers), body=_response_json)

    def get(
        self, order_id: OrderId, media_id: MediaId, *, request_options: typing.Optional[RequestOptions] = None
    ) -> HttpResponse[OrderMediaResponse]:
        """
        Retrieves metadata and a time-limited download URL for a previously uploaded media file.

        Parameters
        ----------
        order_id : OrderId
            The ID of the order.

        media_id : MediaId
            The ID of the media file.

        request_options : typing.Optional[RequestOptions]
            Request-specific configuration.

        Returns
        -------
        HttpResponse[OrderMediaResponse]
            Successful Response
        """
        _response = self._client_wrapper.httpx_client.request(
            f"v1/productions/orders/{jsonable_encoder(order_id)}/media/{jsonable_encoder(media_id)}",
            method="GET",
            request_options=request_options,
        )
        try:
            if 200 <= _response.status_code < 300:
                _data = typing.cast(
                    OrderMediaResponse,
                    construct_type(
                        type_=OrderMediaResponse,  # type: ignore
                        object_=_response.json(),
                    ),
                )
                return HttpResponse(response=_response, data=_data)
            if _response.status_code == 422:
                raise UnprocessableEntityError(
                    headers=dict(_response.headers),
                    body=typing.cast(
                        typing.Any,
                        construct_type(
                            type_=typing.Any,  # type: ignore
                            object_=_response.json(),
                        ),
                    ),
                )
            _response_json = _response.json()
        except JSONDecodeError:
            raise ApiError(status_code=_response.status_code, headers=dict(_response.headers), body=_response.text)
        raise ApiError(status_code=_response.status_code, headers=dict(_response.headers), body=_response_json)


class AsyncRawMediaClient:
    def __init__(self, *, client_wrapper: AsyncClientWrapper):
        self._client_wrapper = client_wrapper

    async def register(
        self,
        order_id: OrderId,
        *,
        declared_language: str,
        media: typing.Optional[core.File] = OMIT,
        media_url: typing.Optional[str] = OMIT,
        media_url_filename: typing.Optional[str] = OMIT,
        media_url_content_type: typing.Optional[str] = OMIT,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> AsyncHttpResponse[RegisterMediaResponse]:
        """
        Registers a media file with an order, either by uploading it directly or by providing a URL to fetch it from. Exactly one of `media` or `media_url` must be provided. The registered media can then be referenced when adding order items.

        Parameters
        ----------
        order_id : OrderId
            The ID of the order to which this media will be attached.

        declared_language : str
            The language code of the media content (e.g. 'en', 'es-ES'). Must be a supported source language for some order item kind.

        media : typing.Optional[core.File]
            See core.File for more documentation

        media_url : typing.Optional[str]
            A URL to fetch the media file from. Mutually exclusive with media.

        media_url_filename : typing.Optional[str]
            The filename for URL-sourced media (e.g. 'example.mp4'). Required when using media_url.

        media_url_content_type : typing.Optional[str]
            The MIME type for URL-sourced media (e.g. 'video/mp4'). Required when using media_url.

        request_options : typing.Optional[RequestOptions]
            Request-specific configuration.

        Returns
        -------
        AsyncHttpResponse[RegisterMediaResponse]
            Successful Response
        """
        _response = await self._client_wrapper.httpx_client.request(
            f"v1/productions/orders/{jsonable_encoder(order_id)}/media",
            method="POST",
            data={
                "declared_language": declared_language,
                "media_url": media_url,
                "media_url_filename": media_url_filename,
                "media_url_content_type": media_url_content_type,
            },
            files={
                **({"media": media} if media is not None else {}),
            },
            request_options=request_options,
            omit=OMIT,
            force_multipart=True,
        )
        try:
            if 200 <= _response.status_code < 300:
                _data = typing.cast(
                    RegisterMediaResponse,
                    construct_type(
                        type_=RegisterMediaResponse,  # type: ignore
                        object_=_response.json(),
                    ),
                )
                return AsyncHttpResponse(response=_response, data=_data)
            if _response.status_code == 422:
                raise UnprocessableEntityError(
                    headers=dict(_response.headers),
                    body=typing.cast(
                        typing.Any,
                        construct_type(
                            type_=typing.Any,  # type: ignore
                            object_=_response.json(),
                        ),
                    ),
                )
            _response_json = _response.json()
        except JSONDecodeError:
            raise ApiError(status_code=_response.status_code, headers=dict(_response.headers), body=_response.text)
        raise ApiError(status_code=_response.status_code, headers=dict(_response.headers), body=_response_json)

    async def get(
        self, order_id: OrderId, media_id: MediaId, *, request_options: typing.Optional[RequestOptions] = None
    ) -> AsyncHttpResponse[OrderMediaResponse]:
        """
        Retrieves metadata and a time-limited download URL for a previously uploaded media file.

        Parameters
        ----------
        order_id : OrderId
            The ID of the order.

        media_id : MediaId
            The ID of the media file.

        request_options : typing.Optional[RequestOptions]
            Request-specific configuration.

        Returns
        -------
        AsyncHttpResponse[OrderMediaResponse]
            Successful Response
        """
        _response = await self._client_wrapper.httpx_client.request(
            f"v1/productions/orders/{jsonable_encoder(order_id)}/media/{jsonable_encoder(media_id)}",
            method="GET",
            request_options=request_options,
        )
        try:
            if 200 <= _response.status_code < 300:
                _data = typing.cast(
                    OrderMediaResponse,
                    construct_type(
                        type_=OrderMediaResponse,  # type: ignore
                        object_=_response.json(),
                    ),
                )
                return AsyncHttpResponse(response=_response, data=_data)
            if _response.status_code == 422:
                raise UnprocessableEntityError(
                    headers=dict(_response.headers),
                    body=typing.cast(
                        typing.Any,
                        construct_type(
                            type_=typing.Any,  # type: ignore
                            object_=_response.json(),
                        ),
                    ),
                )
            _response_json = _response.json()
        except JSONDecodeError:
            raise ApiError(status_code=_response.status_code, headers=dict(_response.headers), body=_response.text)
        raise ApiError(status_code=_response.status_code, headers=dict(_response.headers), body=_response_json)
