# 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 ..requests.workflow_step_definition import WorkflowStepDefinitionParams
from ..types.max_page_size import MaxPageSize
from ..types.next_page_token import NextPageToken
from ..types.sort_dir import SortDir
from ..types.workflow_version import WorkflowVersion
from .raw_client import AsyncRawWorkflowVersionsClient, RawWorkflowVersionsClient
from .types.workflow_versions_list_response import WorkflowVersionsListResponse

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


class WorkflowVersionsClient:
    def __init__(self, *, client_wrapper: SyncClientWrapper):
        self._raw_client = RawWorkflowVersionsClient(client_wrapper=client_wrapper)

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

        Returns
        -------
        RawWorkflowVersionsClient
        """
        return self._raw_client

    def list(
        self,
        id: str,
        *,
        next_page_token: typing.Optional[NextPageToken] = None,
        max_page_size: typing.Optional[MaxPageSize] = None,
        sort_dir: typing.Optional[SortDir] = None,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> WorkflowVersionsListResponse:
        """
        List all versions of a workflow, including the draft version. Returns a paginated list of version summaries.

        Parameters
        ----------
        id : str
            The ID of the workflow.

        next_page_token : typing.Optional[NextPageToken]

        max_page_size : typing.Optional[MaxPageSize]

        sort_dir : typing.Optional[SortDir]

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

        Returns
        -------
        WorkflowVersionsListResponse
            Successfully retrieved versions

        Examples
        --------
        from extend_ai import Extend

        client = Extend(
            token="YOUR_TOKEN",
        )
        client.workflow_versions.list(
            id="workflow_abc123",
            next_page_token="xK9mLPqRtN3vS8wF5hB2cQ==:zWvUxYjM4nKpL7aDgE9HbTcR2mAyX3/Q+CNkfBSw1dZ=",
        )
        """
        _response = self._raw_client.list(
            id,
            next_page_token=next_page_token,
            max_page_size=max_page_size,
            sort_dir=sort_dir,
            request_options=request_options,
        )
        return _response.data

    def create(
        self,
        id: str,
        *,
        name: typing.Optional[str] = OMIT,
        steps: typing.Optional[typing.Sequence[WorkflowStepDefinitionParams]] = OMIT,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> WorkflowVersion:
        """
        Deploy a new version of a workflow. The deployed version becomes available for running workflow runs.

        When `steps` is omitted, the current draft is deployed as-is. When `steps` is provided, the given steps are deployed directly without modifying the draft.

        Parameters
        ----------
        id : str
            The ID of the workflow to deploy.

        name : typing.Optional[str]
            An optional name for this deployed version.

        steps : typing.Optional[typing.Sequence[WorkflowStepDefinitionParams]]
            Optional step definitions. When provided, these steps are deployed directly without modifying the draft. When omitted, the current draft is deployed.

            All configurable steps (`EXTRACT`, `CLASSIFY`, `SPLIT`, `CONDITIONAL_EXTRACT`, `RULE_VALIDATION`, `EXTERNAL_DATA_VALIDATION`) must include `config` when deploying. Unconfigured steps are rejected with a 400.

            See the [Configuring Workflows via API guide](https://docs.extend.ai/2026-02-09/product/workflows/configuring-workflows-via-api) for step definitions, branching patterns, and examples.

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

        Returns
        -------
        WorkflowVersion
            Version deployed successfully

        Examples
        --------
        from extend_ai import Extend

        client = Extend(
            token="YOUR_TOKEN",
        )
        client.workflow_versions.create(
            id="workflow_abc123",
        )
        """
        _response = self._raw_client.create(id, name=name, steps=steps, request_options=request_options)
        return _response.data

    def retrieve(
        self, id: str, version_id: str, *, request_options: typing.Optional[RequestOptions] = None
    ) -> WorkflowVersion:
        """
        Get a specific version of a workflow, including its step definitions.

        The `versionId` parameter accepts:
        - `"draft"` — returns the current draft version
        - A version number (e.g. `"1"`, `"2"`) — returns that deployed version
        - An internal version ID (e.g. `"workflow_version_abc123"`) — returns that specific version

        Parameters
        ----------
        id : str
            The ID of the workflow.

        version_id : str
            The version to retrieve. Use `"draft"` for the draft, a number like `"1"` for a deployed version, or the internal version ID.

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

        Returns
        -------
        WorkflowVersion
            Version details

        Examples
        --------
        from extend_ai import Extend

        client = Extend(
            token="YOUR_TOKEN",
        )
        client.workflow_versions.retrieve(
            id="workflow_abc123",
            version_id="draft",
        )
        """
        _response = self._raw_client.retrieve(id, version_id, request_options=request_options)
        return _response.data


class AsyncWorkflowVersionsClient:
    def __init__(self, *, client_wrapper: AsyncClientWrapper):
        self._raw_client = AsyncRawWorkflowVersionsClient(client_wrapper=client_wrapper)

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

        Returns
        -------
        AsyncRawWorkflowVersionsClient
        """
        return self._raw_client

    async def list(
        self,
        id: str,
        *,
        next_page_token: typing.Optional[NextPageToken] = None,
        max_page_size: typing.Optional[MaxPageSize] = None,
        sort_dir: typing.Optional[SortDir] = None,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> WorkflowVersionsListResponse:
        """
        List all versions of a workflow, including the draft version. Returns a paginated list of version summaries.

        Parameters
        ----------
        id : str
            The ID of the workflow.

        next_page_token : typing.Optional[NextPageToken]

        max_page_size : typing.Optional[MaxPageSize]

        sort_dir : typing.Optional[SortDir]

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

        Returns
        -------
        WorkflowVersionsListResponse
            Successfully retrieved versions

        Examples
        --------
        import asyncio

        from extend_ai import AsyncExtend

        client = AsyncExtend(
            token="YOUR_TOKEN",
        )


        async def main() -> None:
            await client.workflow_versions.list(
                id="workflow_abc123",
                next_page_token="xK9mLPqRtN3vS8wF5hB2cQ==:zWvUxYjM4nKpL7aDgE9HbTcR2mAyX3/Q+CNkfBSw1dZ=",
            )


        asyncio.run(main())
        """
        _response = await self._raw_client.list(
            id,
            next_page_token=next_page_token,
            max_page_size=max_page_size,
            sort_dir=sort_dir,
            request_options=request_options,
        )
        return _response.data

    async def create(
        self,
        id: str,
        *,
        name: typing.Optional[str] = OMIT,
        steps: typing.Optional[typing.Sequence[WorkflowStepDefinitionParams]] = OMIT,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> WorkflowVersion:
        """
        Deploy a new version of a workflow. The deployed version becomes available for running workflow runs.

        When `steps` is omitted, the current draft is deployed as-is. When `steps` is provided, the given steps are deployed directly without modifying the draft.

        Parameters
        ----------
        id : str
            The ID of the workflow to deploy.

        name : typing.Optional[str]
            An optional name for this deployed version.

        steps : typing.Optional[typing.Sequence[WorkflowStepDefinitionParams]]
            Optional step definitions. When provided, these steps are deployed directly without modifying the draft. When omitted, the current draft is deployed.

            All configurable steps (`EXTRACT`, `CLASSIFY`, `SPLIT`, `CONDITIONAL_EXTRACT`, `RULE_VALIDATION`, `EXTERNAL_DATA_VALIDATION`) must include `config` when deploying. Unconfigured steps are rejected with a 400.

            See the [Configuring Workflows via API guide](https://docs.extend.ai/2026-02-09/product/workflows/configuring-workflows-via-api) for step definitions, branching patterns, and examples.

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

        Returns
        -------
        WorkflowVersion
            Version deployed successfully

        Examples
        --------
        import asyncio

        from extend_ai import AsyncExtend

        client = AsyncExtend(
            token="YOUR_TOKEN",
        )


        async def main() -> None:
            await client.workflow_versions.create(
                id="workflow_abc123",
            )


        asyncio.run(main())
        """
        _response = await self._raw_client.create(id, name=name, steps=steps, request_options=request_options)
        return _response.data

    async def retrieve(
        self, id: str, version_id: str, *, request_options: typing.Optional[RequestOptions] = None
    ) -> WorkflowVersion:
        """
        Get a specific version of a workflow, including its step definitions.

        The `versionId` parameter accepts:
        - `"draft"` — returns the current draft version
        - A version number (e.g. `"1"`, `"2"`) — returns that deployed version
        - An internal version ID (e.g. `"workflow_version_abc123"`) — returns that specific version

        Parameters
        ----------
        id : str
            The ID of the workflow.

        version_id : str
            The version to retrieve. Use `"draft"` for the draft, a number like `"1"` for a deployed version, or the internal version ID.

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

        Returns
        -------
        WorkflowVersion
            Version details

        Examples
        --------
        import asyncio

        from extend_ai import AsyncExtend

        client = AsyncExtend(
            token="YOUR_TOKEN",
        )


        async def main() -> None:
            await client.workflow_versions.retrieve(
                id="workflow_abc123",
                version_id="draft",
            )


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