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

import typing

from ..core.client_wrapper import AsyncClientWrapper, SyncClientWrapper
from ..core.request_options import RequestOptions
from ..types.deploy_stream_event import DeployStreamEvent
from ..types.register_container_response import RegisterContainerResponse
from ..types.version_metrics_response import VersionMetricsResponse
from ..types.version_response import VersionResponse
from .raw_client import AsyncRawVersionsClient, RawVersionsClient

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


class VersionsClient:
    def __init__(self, *, client_wrapper: SyncClientWrapper):
        self._raw_client = RawVersionsClient(client_wrapper=client_wrapper)

    @property
    def with_raw_response(self) -> RawVersionsClient:
        """
        Retrieves a raw implementation of this client that returns raw responses.

        Returns
        -------
        RawVersionsClient
        """
        return self._raw_client

    def register(
        self,
        *,
        acp_url: str,
        branch_id: str,
        version_id: str,
        registration_token: typing.Optional[str] = OMIT,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> RegisterContainerResponse:
        """
        Register an agent container that has started up.

            Called by the agent container on startup to:
            1. Register its ACP URL with the platform
            2. Receive its API key for authenticating subsequent requests

            This endpoint should be called from within the container's entrypoint
            after the ACP server is ready to receive traffic.

        Parameters
        ----------
        acp_url : str
            ACP server URL (e.g., 'http://agent-main-abc123.agents.svc.cluster.local:8000')

        branch_id : str
            Branch ID

        version_id : str
            Version ID being registered

        registration_token : typing.Optional[str]
            Single-use registration token provided during deployment (required when configured)

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

        Returns
        -------
        RegisterContainerResponse
            Successful Response

        Examples
        --------
        from terminaluse import TerminalUseBase

        client = TerminalUseBase(
            agent_api_key="YOUR_AGENT_API_KEY",
            token="YOUR_TOKEN",
            base_url="https://yourhost.com/path/to/api",
        )
        client.versions.register(
            acp_url="acp_url",
            branch_id="branch_id",
            version_id="version_id",
        )
        """
        _response = self._raw_client.register(
            acp_url=acp_url,
            branch_id=branch_id,
            version_id=version_id,
            registration_token=registration_token,
            request_options=request_options,
        )
        return _response.data

    def retrieve(self, version_id: str, *, request_options: typing.Optional[RequestOptions] = None) -> VersionResponse:
        """
        Get version details by ID.

        Parameters
        ----------
        version_id : str

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

        Returns
        -------
        VersionResponse
            Successful Response

        Examples
        --------
        from terminaluse import TerminalUseBase

        client = TerminalUseBase(
            agent_api_key="YOUR_AGENT_API_KEY",
            token="YOUR_TOKEN",
            base_url="https://yourhost.com/path/to/api",
        )
        client.versions.retrieve(
            version_id="version_id",
        )
        """
        _response = self._raw_client.retrieve(version_id, request_options=request_options)
        return _response.data

    def deploy_stream(
        self, version_id: str, *, request_options: typing.Optional[RequestOptions] = None
    ) -> typing.Iterator[DeployStreamEvent]:
        """
        SSE endpoint for real-time deployment progress events.

        Parameters
        ----------
        version_id : str

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

        Yields
        ------
        typing.Iterator[DeployStreamEvent]
            SSE stream of deployment events

        Examples
        --------
        from terminaluse import TerminalUseBase

        client = TerminalUseBase(
            agent_api_key="YOUR_AGENT_API_KEY",
            token="YOUR_TOKEN",
            base_url="https://yourhost.com/path/to/api",
        )
        response = client.versions.deploy_stream(
            version_id="version_id",
        )
        for chunk in response:
            yield chunk
        """
        with self._raw_client.deploy_stream(version_id, request_options=request_options) as r:
            yield from r.data

    def metrics(
        self, version_id: str, *, request_options: typing.Optional[RequestOptions] = None
    ) -> VersionMetricsResponse:
        """
        Get deployment metrics for a specific version.

            Returns metrics related to the deployment process for this version,
            including average and P95 deployment durations queried from ClickHouse traces.

        Parameters
        ----------
        version_id : str

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

        Returns
        -------
        VersionMetricsResponse
            Successful Response

        Examples
        --------
        from terminaluse import TerminalUseBase

        client = TerminalUseBase(
            agent_api_key="YOUR_AGENT_API_KEY",
            token="YOUR_TOKEN",
            base_url="https://yourhost.com/path/to/api",
        )
        client.versions.metrics(
            version_id="version_id",
        )
        """
        _response = self._raw_client.metrics(version_id, request_options=request_options)
        return _response.data


class AsyncVersionsClient:
    def __init__(self, *, client_wrapper: AsyncClientWrapper):
        self._raw_client = AsyncRawVersionsClient(client_wrapper=client_wrapper)

    @property
    def with_raw_response(self) -> AsyncRawVersionsClient:
        """
        Retrieves a raw implementation of this client that returns raw responses.

        Returns
        -------
        AsyncRawVersionsClient
        """
        return self._raw_client

    async def register(
        self,
        *,
        acp_url: str,
        branch_id: str,
        version_id: str,
        registration_token: typing.Optional[str] = OMIT,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> RegisterContainerResponse:
        """
        Register an agent container that has started up.

            Called by the agent container on startup to:
            1. Register its ACP URL with the platform
            2. Receive its API key for authenticating subsequent requests

            This endpoint should be called from within the container's entrypoint
            after the ACP server is ready to receive traffic.

        Parameters
        ----------
        acp_url : str
            ACP server URL (e.g., 'http://agent-main-abc123.agents.svc.cluster.local:8000')

        branch_id : str
            Branch ID

        version_id : str
            Version ID being registered

        registration_token : typing.Optional[str]
            Single-use registration token provided during deployment (required when configured)

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

        Returns
        -------
        RegisterContainerResponse
            Successful Response

        Examples
        --------
        import asyncio

        from terminaluse import AsyncTerminalUseBase

        client = AsyncTerminalUseBase(
            agent_api_key="YOUR_AGENT_API_KEY",
            token="YOUR_TOKEN",
            base_url="https://yourhost.com/path/to/api",
        )


        async def main() -> None:
            await client.versions.register(
                acp_url="acp_url",
                branch_id="branch_id",
                version_id="version_id",
            )


        asyncio.run(main())
        """
        _response = await self._raw_client.register(
            acp_url=acp_url,
            branch_id=branch_id,
            version_id=version_id,
            registration_token=registration_token,
            request_options=request_options,
        )
        return _response.data

    async def retrieve(
        self, version_id: str, *, request_options: typing.Optional[RequestOptions] = None
    ) -> VersionResponse:
        """
        Get version details by ID.

        Parameters
        ----------
        version_id : str

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

        Returns
        -------
        VersionResponse
            Successful Response

        Examples
        --------
        import asyncio

        from terminaluse import AsyncTerminalUseBase

        client = AsyncTerminalUseBase(
            agent_api_key="YOUR_AGENT_API_KEY",
            token="YOUR_TOKEN",
            base_url="https://yourhost.com/path/to/api",
        )


        async def main() -> None:
            await client.versions.retrieve(
                version_id="version_id",
            )


        asyncio.run(main())
        """
        _response = await self._raw_client.retrieve(version_id, request_options=request_options)
        return _response.data

    async def deploy_stream(
        self, version_id: str, *, request_options: typing.Optional[RequestOptions] = None
    ) -> typing.AsyncIterator[DeployStreamEvent]:
        """
        SSE endpoint for real-time deployment progress events.

        Parameters
        ----------
        version_id : str

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

        Yields
        ------
        typing.AsyncIterator[DeployStreamEvent]
            SSE stream of deployment events

        Examples
        --------
        import asyncio

        from terminaluse import AsyncTerminalUseBase

        client = AsyncTerminalUseBase(
            agent_api_key="YOUR_AGENT_API_KEY",
            token="YOUR_TOKEN",
            base_url="https://yourhost.com/path/to/api",
        )


        async def main() -> None:
            response = await client.versions.deploy_stream(
                version_id="version_id",
            )
            async for chunk in response:
                yield chunk


        asyncio.run(main())
        """
        async with self._raw_client.deploy_stream(version_id, request_options=request_options) as r:
            async for _chunk in r.data:
                yield _chunk

    async def metrics(
        self, version_id: str, *, request_options: typing.Optional[RequestOptions] = None
    ) -> VersionMetricsResponse:
        """
        Get deployment metrics for a specific version.

            Returns metrics related to the deployment process for this version,
            including average and P95 deployment durations queried from ClickHouse traces.

        Parameters
        ----------
        version_id : str

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

        Returns
        -------
        VersionMetricsResponse
            Successful Response

        Examples
        --------
        import asyncio

        from terminaluse import AsyncTerminalUseBase

        client = AsyncTerminalUseBase(
            agent_api_key="YOUR_AGENT_API_KEY",
            token="YOUR_TOKEN",
            base_url="https://yourhost.com/path/to/api",
        )


        async def main() -> None:
            await client.versions.metrics(
                version_id="version_id",
            )


        asyncio.run(main())
        """
        _response = await self._raw_client.metrics(version_id, request_options=request_options)
        return _response.data
