# 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.branch_list_response import BranchListResponse
from ...types.branch_response import BranchResponse
from ...types.rollback_response import RollbackResponse
from .raw_client import AsyncRawBranchesClient, RawBranchesClient

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


class BranchesClient:
    def __init__(self, *, client_wrapper: SyncClientWrapper):
        self._raw_client = RawBranchesClient(client_wrapper=client_wrapper)

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

        Returns
        -------
        RawBranchesClient
        """
        return self._raw_client

    def retrieve(
        self,
        namespace_slug: str,
        agent_name: str,
        *,
        branch: str,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> BranchResponse:
        """
        Get a specific branch by agent name and git branch.

            The git branch name will be normalized (lowercased, slashes converted to hyphens).
            For example, "feature/new-tool" becomes "feature-new-tool".

        Parameters
        ----------
        namespace_slug : str

        agent_name : str

        branch : str
            Target branch (e.g., 'main', 'feature/login')

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

        Returns
        -------
        BranchResponse
            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.agents.branches.retrieve(
            namespace_slug="namespace_slug",
            agent_name="agent_name",
            branch="branch",
        )
        """
        _response = self._raw_client.retrieve(
            namespace_slug, agent_name, branch=branch, request_options=request_options
        )
        return _response.data

    def list(
        self,
        namespace_slug: str,
        agent_name: str,
        *,
        include_retired: typing.Optional[bool] = None,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> BranchListResponse:
        """
        List all branches for an agent.

            Each branch represents a git branch (e.g., main, staging, feature-x).
            By default, retired branches are excluded.

        Parameters
        ----------
        namespace_slug : str

        agent_name : str

        include_retired : typing.Optional[bool]
            Include retired branches in results

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

        Returns
        -------
        BranchListResponse
            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.agents.branches.list(
            namespace_slug="namespace_slug",
            agent_name="agent_name",
        )
        """
        _response = self._raw_client.list(
            namespace_slug, agent_name, include_retired=include_retired, request_options=request_options
        )
        return _response.data

    def rollback(
        self,
        namespace_slug: str,
        agent_name: str,
        branch: str,
        *,
        target_version_id: typing.Optional[str] = OMIT,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> RollbackResponse:
        """
        Roll back a branch to a previous version by namespace, agent, and git branch name.

            Key behavior:
            - EnvVar table is NOT modified (remains pending state)
            - Uses Version.secrets_snapshot as deployed state

        Parameters
        ----------
        namespace_slug : str

        agent_name : str

        branch : str

        target_version_id : typing.Optional[str]
            Version ID to rollback to (defaults to previous version if not specified)

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

        Returns
        -------
        RollbackResponse
            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.agents.branches.rollback(
            namespace_slug="namespace_slug",
            agent_name="agent_name",
            branch="branch",
        )
        """
        _response = self._raw_client.rollback(
            namespace_slug, agent_name, branch, target_version_id=target_version_id, request_options=request_options
        )
        return _response.data


class AsyncBranchesClient:
    def __init__(self, *, client_wrapper: AsyncClientWrapper):
        self._raw_client = AsyncRawBranchesClient(client_wrapper=client_wrapper)

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

        Returns
        -------
        AsyncRawBranchesClient
        """
        return self._raw_client

    async def retrieve(
        self,
        namespace_slug: str,
        agent_name: str,
        *,
        branch: str,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> BranchResponse:
        """
        Get a specific branch by agent name and git branch.

            The git branch name will be normalized (lowercased, slashes converted to hyphens).
            For example, "feature/new-tool" becomes "feature-new-tool".

        Parameters
        ----------
        namespace_slug : str

        agent_name : str

        branch : str
            Target branch (e.g., 'main', 'feature/login')

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

        Returns
        -------
        BranchResponse
            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.agents.branches.retrieve(
                namespace_slug="namespace_slug",
                agent_name="agent_name",
                branch="branch",
            )


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

    async def list(
        self,
        namespace_slug: str,
        agent_name: str,
        *,
        include_retired: typing.Optional[bool] = None,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> BranchListResponse:
        """
        List all branches for an agent.

            Each branch represents a git branch (e.g., main, staging, feature-x).
            By default, retired branches are excluded.

        Parameters
        ----------
        namespace_slug : str

        agent_name : str

        include_retired : typing.Optional[bool]
            Include retired branches in results

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

        Returns
        -------
        BranchListResponse
            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.agents.branches.list(
                namespace_slug="namespace_slug",
                agent_name="agent_name",
            )


        asyncio.run(main())
        """
        _response = await self._raw_client.list(
            namespace_slug, agent_name, include_retired=include_retired, request_options=request_options
        )
        return _response.data

    async def rollback(
        self,
        namespace_slug: str,
        agent_name: str,
        branch: str,
        *,
        target_version_id: typing.Optional[str] = OMIT,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> RollbackResponse:
        """
        Roll back a branch to a previous version by namespace, agent, and git branch name.

            Key behavior:
            - EnvVar table is NOT modified (remains pending state)
            - Uses Version.secrets_snapshot as deployed state

        Parameters
        ----------
        namespace_slug : str

        agent_name : str

        branch : str

        target_version_id : typing.Optional[str]
            Version ID to rollback to (defaults to previous version if not specified)

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

        Returns
        -------
        RollbackResponse
            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.agents.branches.rollback(
                namespace_slug="namespace_slug",
                agent_name="agent_name",
                branch="branch",
            )


        asyncio.run(main())
        """
        _response = await self._raw_client.rollback(
            namespace_slug, agent_name, branch, target_version_id=target_version_id, request_options=request_options
        )
        return _response.data
