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

import typing
import urllib.parse
from json.decoder import JSONDecodeError

from ...core.api_error import ApiError
from ...core.client_wrapper import AsyncClientWrapper, SyncClientWrapper
from ...core.jsonable_encoder import jsonable_encoder
from ...types.sandbox_metric_input_params_request import SandboxMetricInputParamsRequest
from ...types.sandbox_scenario import SandboxScenario
from ...types.scenario_input_request import ScenarioInputRequest

try:
    import pydantic.v1 as pydantic  # type: ignore
except ImportError:
    import pydantic  # type: ignore

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


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

    def upsert_sandbox_scenario(
        self,
        id: str,
        *,
        label: typing.Optional[str] = OMIT,
        inputs: typing.List[ScenarioInputRequest],
        scenario_id: typing.Optional[str] = OMIT,
        metric_input_params: typing.Optional[SandboxMetricInputParamsRequest] = OMIT,
    ) -> SandboxScenario:
        """
        Upserts a new scenario for a sandbox, keying off of the optionally provided scenario id.

        If an id is provided and has a match, the scenario will be updated. If no id is provided or no match
        is found, a new scenario will be appended to the end.

        Note that a full replacement of the scenario is performed, so any fields not provided will be removed
        or overwritten with default values.

        Parameters:
            - id: str. A UUID string identifying this sandbox.

            - label: typing.Optional[str].

            - inputs: typing.List[ScenarioInputRequest]. The inputs for the scenario

            - scenario_id: typing.Optional[str]. The id of the scenario to update. If none is provided, an id will be generated and a new scenario will be appended.

            - metric_input_params: typing.Optional[SandboxMetricInputParamsRequest].
        ---
        from vellum import (
            ChatMessageRequest,
            ChatMessageRole,
            EvaluationParamsRequest,
            SandboxMetricInputParamsRequest,
            ScenarioInputRequest,
            ScenarioInputTypeEnum,
        )
        from vellum.client import Vellum

        client = Vellum(
            api_key="YOUR_API_KEY",
        )
        client.sandboxes.upsert_sandbox_scenario(
            id="string",
            inputs=[
                ScenarioInputRequest(
                    key="string",
                    type=ScenarioInputTypeEnum.TEXT,
                    chat_history=[
                        ChatMessageRequest(
                            role=ChatMessageRole.SYSTEM,
                        )
                    ],
                )
            ],
            metric_input_params=SandboxMetricInputParamsRequest(
                params=EvaluationParamsRequest(),
            ),
        )
        """
        _request: typing.Dict[str, typing.Any] = {"inputs": inputs}
        if label is not OMIT:
            _request["label"] = label
        if scenario_id is not OMIT:
            _request["scenario_id"] = scenario_id
        if metric_input_params is not OMIT:
            _request["metric_input_params"] = metric_input_params
        _response = self._client_wrapper.httpx_client.request(
            "POST",
            urllib.parse.urljoin(f"{self._client_wrapper.get_environment().default}/", f"v1/sandboxes/{id}/scenarios"),
            json=jsonable_encoder(_request),
            headers=self._client_wrapper.get_headers(),
            timeout=None,
        )
        if 200 <= _response.status_code < 300:
            return pydantic.parse_obj_as(SandboxScenario, _response.json())  # type: ignore
        try:
            _response_json = _response.json()
        except JSONDecodeError:
            raise ApiError(status_code=_response.status_code, body=_response.text)
        raise ApiError(status_code=_response.status_code, body=_response_json)

    def delete_sandbox_scenario(self, id: str, scenario_id: str) -> None:
        """
        Deletes an existing scenario from a sandbox, keying off of the provided scenario id.

        Parameters:
            - id: str. A UUID string identifying this sandbox.

            - scenario_id: str. An id identifying the scenario that you'd like to delete
        ---
        from vellum.client import Vellum

        client = Vellum(
            api_key="YOUR_API_KEY",
        )
        client.sandboxes.delete_sandbox_scenario(
            id="string",
            scenario_id="string",
        )
        """
        _response = self._client_wrapper.httpx_client.request(
            "DELETE",
            urllib.parse.urljoin(
                f"{self._client_wrapper.get_environment().default}/", f"v1/sandboxes/{id}/scenarios/{scenario_id}"
            ),
            headers=self._client_wrapper.get_headers(),
            timeout=None,
        )
        if 200 <= _response.status_code < 300:
            return
        try:
            _response_json = _response.json()
        except JSONDecodeError:
            raise ApiError(status_code=_response.status_code, body=_response.text)
        raise ApiError(status_code=_response.status_code, body=_response_json)


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

    async def upsert_sandbox_scenario(
        self,
        id: str,
        *,
        label: typing.Optional[str] = OMIT,
        inputs: typing.List[ScenarioInputRequest],
        scenario_id: typing.Optional[str] = OMIT,
        metric_input_params: typing.Optional[SandboxMetricInputParamsRequest] = OMIT,
    ) -> SandboxScenario:
        """
        Upserts a new scenario for a sandbox, keying off of the optionally provided scenario id.

        If an id is provided and has a match, the scenario will be updated. If no id is provided or no match
        is found, a new scenario will be appended to the end.

        Note that a full replacement of the scenario is performed, so any fields not provided will be removed
        or overwritten with default values.

        Parameters:
            - id: str. A UUID string identifying this sandbox.

            - label: typing.Optional[str].

            - inputs: typing.List[ScenarioInputRequest]. The inputs for the scenario

            - scenario_id: typing.Optional[str]. The id of the scenario to update. If none is provided, an id will be generated and a new scenario will be appended.

            - metric_input_params: typing.Optional[SandboxMetricInputParamsRequest].
        ---
        from vellum import (
            ChatMessageRequest,
            ChatMessageRole,
            EvaluationParamsRequest,
            SandboxMetricInputParamsRequest,
            ScenarioInputRequest,
            ScenarioInputTypeEnum,
        )
        from vellum.client import AsyncVellum

        client = AsyncVellum(
            api_key="YOUR_API_KEY",
        )
        await client.sandboxes.upsert_sandbox_scenario(
            id="string",
            inputs=[
                ScenarioInputRequest(
                    key="string",
                    type=ScenarioInputTypeEnum.TEXT,
                    chat_history=[
                        ChatMessageRequest(
                            role=ChatMessageRole.SYSTEM,
                        )
                    ],
                )
            ],
            metric_input_params=SandboxMetricInputParamsRequest(
                params=EvaluationParamsRequest(),
            ),
        )
        """
        _request: typing.Dict[str, typing.Any] = {"inputs": inputs}
        if label is not OMIT:
            _request["label"] = label
        if scenario_id is not OMIT:
            _request["scenario_id"] = scenario_id
        if metric_input_params is not OMIT:
            _request["metric_input_params"] = metric_input_params
        _response = await self._client_wrapper.httpx_client.request(
            "POST",
            urllib.parse.urljoin(f"{self._client_wrapper.get_environment().default}/", f"v1/sandboxes/{id}/scenarios"),
            json=jsonable_encoder(_request),
            headers=self._client_wrapper.get_headers(),
            timeout=None,
        )
        if 200 <= _response.status_code < 300:
            return pydantic.parse_obj_as(SandboxScenario, _response.json())  # type: ignore
        try:
            _response_json = _response.json()
        except JSONDecodeError:
            raise ApiError(status_code=_response.status_code, body=_response.text)
        raise ApiError(status_code=_response.status_code, body=_response_json)

    async def delete_sandbox_scenario(self, id: str, scenario_id: str) -> None:
        """
        Deletes an existing scenario from a sandbox, keying off of the provided scenario id.

        Parameters:
            - id: str. A UUID string identifying this sandbox.

            - scenario_id: str. An id identifying the scenario that you'd like to delete
        ---
        from vellum.client import AsyncVellum

        client = AsyncVellum(
            api_key="YOUR_API_KEY",
        )
        await client.sandboxes.delete_sandbox_scenario(
            id="string",
            scenario_id="string",
        )
        """
        _response = await self._client_wrapper.httpx_client.request(
            "DELETE",
            urllib.parse.urljoin(
                f"{self._client_wrapper.get_environment().default}/", f"v1/sandboxes/{id}/scenarios/{scenario_id}"
            ),
            headers=self._client_wrapper.get_headers(),
            timeout=None,
        )
        if 200 <= _response.status_code < 300:
            return
        try:
            _response_json = _response.json()
        except JSONDecodeError:
            raise ApiError(status_code=_response.status_code, body=_response.text)
        raise ApiError(status_code=_response.status_code, body=_response_json)
