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

import datetime as dt
import typing
from json.decoder import JSONDecodeError

from ..core.api_error import ApiError
from ..core.client_wrapper import AsyncClientWrapper, SyncClientWrapper
from ..core.datetime_utils import serialize_datetime
from ..core.http_response import AsyncHttpResponse, HttpResponse
from ..core.jsonable_encoder import jsonable_encoder
from ..core.pydantic_utilities import parse_obj_as
from ..core.request_options import RequestOptions
from ..errors.unprocessable_entity_error import UnprocessableEntityError
from ..types.feature_collection import FeatureCollection
from ..types.http_validation_error import HttpValidationError
from ..types.time_series_feature_collection_model import TimeSeriesFeatureCollectionModel


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

    def features(
        self,
        portfolio_id: str,
        *,
        x: typing.Optional[int] = None,
        y: typing.Optional[int] = None,
        z: typing.Optional[int] = None,
        country: typing.Optional[str] = None,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> HttpResponse[FeatureCollection]:
        """
        Return a GeoJSON FeatureCollection of features for a portfolio.

        Parameters
        ----------
        portfolio_id : str

        x : typing.Optional[int]

        y : typing.Optional[int]

        z : typing.Optional[int]

        country : typing.Optional[str]

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

        Returns
        -------
        HttpResponse[FeatureCollection]
            Successful Response
        """
        _response = self._client_wrapper.httpx_client.request(
            f"portfolios/{jsonable_encoder(portfolio_id)}/features",
            method="GET",
            params={
                "x": x,
                "y": y,
                "z": z,
                "country": country,
            },
            request_options=request_options,
        )
        try:
            if 200 <= _response.status_code < 300:
                _data = typing.cast(
                    FeatureCollection,
                    parse_obj_as(
                        type_=FeatureCollection,  # 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(
                        HttpValidationError,
                        parse_obj_as(
                            type_=HttpValidationError,  # 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 forecasts(
        self,
        portfolio_id: str,
        *,
        init_time: dt.datetime,
        id: typing.Optional[typing.Union[str, typing.Sequence[str]]] = None,
        variables: typing.Optional[typing.Union[str, typing.Sequence[str]]] = None,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> HttpResponse[TimeSeriesFeatureCollectionModel]:
        """
        Return a GeoJSON FeatureCollection of forecasts for a portfolio.

        Parameters
        ----------
        portfolio_id : str

        init_time : dt.datetime

        id : typing.Optional[typing.Union[str, typing.Sequence[str]]]

        variables : typing.Optional[typing.Union[str, typing.Sequence[str]]]

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

        Returns
        -------
        HttpResponse[TimeSeriesFeatureCollectionModel]
            Successful Response
        """
        _response = self._client_wrapper.httpx_client.request(
            f"portfolios/{jsonable_encoder(portfolio_id)}/forecasts",
            method="GET",
            params={
                "id": id,
                "variables": variables,
                "init_time": serialize_datetime(init_time),
            },
            request_options=request_options,
        )
        try:
            if 200 <= _response.status_code < 300:
                _data = typing.cast(
                    TimeSeriesFeatureCollectionModel,
                    parse_obj_as(
                        type_=TimeSeriesFeatureCollectionModel,  # 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(
                        HttpValidationError,
                        parse_obj_as(
                            type_=HttpValidationError,  # 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 observations(
        self,
        portfolio_id: str,
        *,
        valid_time_start: dt.datetime,
        valid_time_end: dt.datetime,
        id: typing.Optional[typing.Union[str, typing.Sequence[str]]] = None,
        variables: typing.Optional[typing.Union[str, typing.Sequence[str]]] = None,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> HttpResponse[TimeSeriesFeatureCollectionModel]:
        """
        Return a GeoJSON FeatureCollection of observations for a portfolio.

        Parameters
        ----------
        portfolio_id : str

        valid_time_start : dt.datetime

        valid_time_end : dt.datetime

        id : typing.Optional[typing.Union[str, typing.Sequence[str]]]

        variables : typing.Optional[typing.Union[str, typing.Sequence[str]]]

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

        Returns
        -------
        HttpResponse[TimeSeriesFeatureCollectionModel]
            Successful Response
        """
        _response = self._client_wrapper.httpx_client.request(
            f"portfolios/{jsonable_encoder(portfolio_id)}/observations",
            method="GET",
            params={
                "id": id,
                "variables": variables,
                "valid_time_start": serialize_datetime(valid_time_start),
                "valid_time_end": serialize_datetime(valid_time_end),
            },
            request_options=request_options,
        )
        try:
            if 200 <= _response.status_code < 300:
                _data = typing.cast(
                    TimeSeriesFeatureCollectionModel,
                    parse_obj_as(
                        type_=TimeSeriesFeatureCollectionModel,  # 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(
                        HttpValidationError,
                        parse_obj_as(
                            type_=HttpValidationError,  # 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 init_time(
        self, portfolio_id: str, *, time: dt.datetime, request_options: typing.Optional[RequestOptions] = None
    ) -> HttpResponse[dt.datetime]:
        """
        Return the latest initialization time for a portfolio.

        Parameters
        ----------
        portfolio_id : str

        time : dt.datetime

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

        Returns
        -------
        HttpResponse[dt.datetime]
            Successful Response
        """
        _response = self._client_wrapper.httpx_client.request(
            f"portfolios/{jsonable_encoder(portfolio_id)}/init_time",
            method="GET",
            params={
                "time": serialize_datetime(time),
            },
            request_options=request_options,
        )
        try:
            if 200 <= _response.status_code < 300:
                _data = typing.cast(
                    dt.datetime,
                    parse_obj_as(
                        type_=dt.datetime,  # 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(
                        HttpValidationError,
                        parse_obj_as(
                            type_=HttpValidationError,  # 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 AsyncRawPortfoliosClient:
    def __init__(self, *, client_wrapper: AsyncClientWrapper):
        self._client_wrapper = client_wrapper

    async def features(
        self,
        portfolio_id: str,
        *,
        x: typing.Optional[int] = None,
        y: typing.Optional[int] = None,
        z: typing.Optional[int] = None,
        country: typing.Optional[str] = None,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> AsyncHttpResponse[FeatureCollection]:
        """
        Return a GeoJSON FeatureCollection of features for a portfolio.

        Parameters
        ----------
        portfolio_id : str

        x : typing.Optional[int]

        y : typing.Optional[int]

        z : typing.Optional[int]

        country : typing.Optional[str]

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

        Returns
        -------
        AsyncHttpResponse[FeatureCollection]
            Successful Response
        """
        _response = await self._client_wrapper.httpx_client.request(
            f"portfolios/{jsonable_encoder(portfolio_id)}/features",
            method="GET",
            params={
                "x": x,
                "y": y,
                "z": z,
                "country": country,
            },
            request_options=request_options,
        )
        try:
            if 200 <= _response.status_code < 300:
                _data = typing.cast(
                    FeatureCollection,
                    parse_obj_as(
                        type_=FeatureCollection,  # 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(
                        HttpValidationError,
                        parse_obj_as(
                            type_=HttpValidationError,  # 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 forecasts(
        self,
        portfolio_id: str,
        *,
        init_time: dt.datetime,
        id: typing.Optional[typing.Union[str, typing.Sequence[str]]] = None,
        variables: typing.Optional[typing.Union[str, typing.Sequence[str]]] = None,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> AsyncHttpResponse[TimeSeriesFeatureCollectionModel]:
        """
        Return a GeoJSON FeatureCollection of forecasts for a portfolio.

        Parameters
        ----------
        portfolio_id : str

        init_time : dt.datetime

        id : typing.Optional[typing.Union[str, typing.Sequence[str]]]

        variables : typing.Optional[typing.Union[str, typing.Sequence[str]]]

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

        Returns
        -------
        AsyncHttpResponse[TimeSeriesFeatureCollectionModel]
            Successful Response
        """
        _response = await self._client_wrapper.httpx_client.request(
            f"portfolios/{jsonable_encoder(portfolio_id)}/forecasts",
            method="GET",
            params={
                "id": id,
                "variables": variables,
                "init_time": serialize_datetime(init_time),
            },
            request_options=request_options,
        )
        try:
            if 200 <= _response.status_code < 300:
                _data = typing.cast(
                    TimeSeriesFeatureCollectionModel,
                    parse_obj_as(
                        type_=TimeSeriesFeatureCollectionModel,  # 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(
                        HttpValidationError,
                        parse_obj_as(
                            type_=HttpValidationError,  # 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 observations(
        self,
        portfolio_id: str,
        *,
        valid_time_start: dt.datetime,
        valid_time_end: dt.datetime,
        id: typing.Optional[typing.Union[str, typing.Sequence[str]]] = None,
        variables: typing.Optional[typing.Union[str, typing.Sequence[str]]] = None,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> AsyncHttpResponse[TimeSeriesFeatureCollectionModel]:
        """
        Return a GeoJSON FeatureCollection of observations for a portfolio.

        Parameters
        ----------
        portfolio_id : str

        valid_time_start : dt.datetime

        valid_time_end : dt.datetime

        id : typing.Optional[typing.Union[str, typing.Sequence[str]]]

        variables : typing.Optional[typing.Union[str, typing.Sequence[str]]]

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

        Returns
        -------
        AsyncHttpResponse[TimeSeriesFeatureCollectionModel]
            Successful Response
        """
        _response = await self._client_wrapper.httpx_client.request(
            f"portfolios/{jsonable_encoder(portfolio_id)}/observations",
            method="GET",
            params={
                "id": id,
                "variables": variables,
                "valid_time_start": serialize_datetime(valid_time_start),
                "valid_time_end": serialize_datetime(valid_time_end),
            },
            request_options=request_options,
        )
        try:
            if 200 <= _response.status_code < 300:
                _data = typing.cast(
                    TimeSeriesFeatureCollectionModel,
                    parse_obj_as(
                        type_=TimeSeriesFeatureCollectionModel,  # 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(
                        HttpValidationError,
                        parse_obj_as(
                            type_=HttpValidationError,  # 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 init_time(
        self, portfolio_id: str, *, time: dt.datetime, request_options: typing.Optional[RequestOptions] = None
    ) -> AsyncHttpResponse[dt.datetime]:
        """
        Return the latest initialization time for a portfolio.

        Parameters
        ----------
        portfolio_id : str

        time : dt.datetime

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

        Returns
        -------
        AsyncHttpResponse[dt.datetime]
            Successful Response
        """
        _response = await self._client_wrapper.httpx_client.request(
            f"portfolios/{jsonable_encoder(portfolio_id)}/init_time",
            method="GET",
            params={
                "time": serialize_datetime(time),
            },
            request_options=request_options,
        )
        try:
            if 200 <= _response.status_code < 300:
                _data = typing.cast(
                    dt.datetime,
                    parse_obj_as(
                        type_=dt.datetime,  # 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(
                        HttpValidationError,
                        parse_obj_as(
                            type_=HttpValidationError,  # 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)
