# 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.promote_to_assistant_response import PromoteToAssistantResponse
from ..types.workflow_list_response import WorkflowListResponse
from ..types.workflow_response import WorkflowResponse
from .raw_client import AsyncRawWorkflowsClient, RawWorkflowsClient

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


class WorkflowsClient:
    def __init__(self, *, client_wrapper: SyncClientWrapper):
        self._raw_client = RawWorkflowsClient(client_wrapper=client_wrapper)

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

        Returns
        -------
        RawWorkflowsClient
        """
        return self._raw_client

    def list_workflows(
        self,
        *,
        external_id: typing.Optional[str] = None,
        active_only: typing.Optional[bool] = None,
        limit: typing.Optional[int] = None,
        offset: typing.Optional[int] = None,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> WorkflowListResponse:
        """
        List workflows for the authenticated organization.

        Parameters
        ----------
        external_id : typing.Optional[str]
            Filter by external ID

        active_only : typing.Optional[bool]
            Return only active workflows

        limit : typing.Optional[int]
            Maximum items to return

        offset : typing.Optional[int]
            Number of items to skip

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

        Returns
        -------
        WorkflowListResponse
            Successful Response

        Examples
        --------
        from aitronos import Aitronos

        client = Aitronos(
            api_key="YOUR_API_KEY",
            token="YOUR_TOKEN",
        )
        client.workflows.list_workflows()
        """
        _response = self._raw_client.list_workflows(
            external_id=external_id,
            active_only=active_only,
            limit=limit,
            offset=offset,
            request_options=request_options,
        )
        return _response.data

    def create_workflow(
        self,
        *,
        name: str,
        definition: typing.Dict[str, typing.Any],
        description: typing.Optional[str] = OMIT,
        external_id: typing.Optional[str] = OMIT,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> WorkflowResponse:
        """
        Create a new workflow or update existing if external_id matches (upsert).

        Parameters
        ----------
        name : str
            Workflow name

        definition : typing.Dict[str, typing.Any]
            Flow-plate workflow JSON definition

        description : typing.Optional[str]
            Workflow description

        external_id : typing.Optional[str]
            Flow-plate internal flow ID for correlation

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

        Returns
        -------
        WorkflowResponse
            Successful Response

        Examples
        --------
        from aitronos import Aitronos

        client = Aitronos(
            api_key="YOUR_API_KEY",
            token="YOUR_TOKEN",
        )
        client.workflows.create_workflow(
            name="name",
            definition={"key": "value"},
        )
        """
        _response = self._raw_client.create_workflow(
            name=name,
            definition=definition,
            description=description,
            external_id=external_id,
            request_options=request_options,
        )
        return _response.data

    def get_workflow(
        self, workflow_id: str, *, request_options: typing.Optional[RequestOptions] = None
    ) -> WorkflowResponse:
        """
        Get a workflow by ID.

        Parameters
        ----------
        workflow_id : str

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

        Returns
        -------
        WorkflowResponse
            Successful Response

        Examples
        --------
        from aitronos import Aitronos

        client = Aitronos(
            api_key="YOUR_API_KEY",
            token="YOUR_TOKEN",
        )
        client.workflows.get_workflow(
            workflow_id="workflow_id",
        )
        """
        _response = self._raw_client.get_workflow(workflow_id, request_options=request_options)
        return _response.data

    def delete_workflow(self, workflow_id: str, *, request_options: typing.Optional[RequestOptions] = None) -> None:
        """
        Soft delete a workflow and unlink any associated assistant.

        Parameters
        ----------
        workflow_id : str

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

        Returns
        -------
        None

        Examples
        --------
        from aitronos import Aitronos

        client = Aitronos(
            api_key="YOUR_API_KEY",
            token="YOUR_TOKEN",
        )
        client.workflows.delete_workflow(
            workflow_id="workflow_id",
        )
        """
        _response = self._raw_client.delete_workflow(workflow_id, request_options=request_options)
        return _response.data

    def update_workflow(
        self,
        workflow_id: str,
        *,
        name: typing.Optional[str] = OMIT,
        description: typing.Optional[str] = OMIT,
        definition: typing.Optional[typing.Dict[str, typing.Any]] = OMIT,
        is_active: typing.Optional[bool] = OMIT,
        external_id: typing.Optional[str] = OMIT,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> WorkflowResponse:
        """
        Update a workflow.

        Parameters
        ----------
        workflow_id : str

        name : typing.Optional[str]

        description : typing.Optional[str]

        definition : typing.Optional[typing.Dict[str, typing.Any]]
            Updated workflow definition

        is_active : typing.Optional[bool]
            Active status

        external_id : typing.Optional[str]
            Flow-plate internal flow ID for correlation

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

        Returns
        -------
        WorkflowResponse
            Successful Response

        Examples
        --------
        from aitronos import Aitronos

        client = Aitronos(
            api_key="YOUR_API_KEY",
            token="YOUR_TOKEN",
        )
        client.workflows.update_workflow(
            workflow_id="workflow_id",
        )
        """
        _response = self._raw_client.update_workflow(
            workflow_id,
            name=name,
            description=description,
            definition=definition,
            is_active=is_active,
            external_id=external_id,
            request_options=request_options,
        )
        return _response.data

    def promote_workflow_to_assistant(
        self,
        workflow_id: str,
        *,
        name: typing.Optional[str] = OMIT,
        description: typing.Optional[str] = OMIT,
        primary_node_id: typing.Optional[str] = OMIT,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> PromoteToAssistantResponse:
        """
        Promote a workflow to an assistant.

        Parameters
        ----------
        workflow_id : str

        name : typing.Optional[str]
            Assistant name (defaults to workflow name)

        description : typing.Optional[str]
            Assistant description

        primary_node_id : typing.Optional[str]
            Node ID to use for settings (required if multiple FreddyLLM nodes)

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

        Returns
        -------
        PromoteToAssistantResponse
            Successful Response

        Examples
        --------
        from aitronos import Aitronos

        client = Aitronos(
            api_key="YOUR_API_KEY",
            token="YOUR_TOKEN",
        )
        client.workflows.promote_workflow_to_assistant(
            workflow_id="workflow_id",
        )
        """
        _response = self._raw_client.promote_workflow_to_assistant(
            workflow_id,
            name=name,
            description=description,
            primary_node_id=primary_node_id,
            request_options=request_options,
        )
        return _response.data


class AsyncWorkflowsClient:
    def __init__(self, *, client_wrapper: AsyncClientWrapper):
        self._raw_client = AsyncRawWorkflowsClient(client_wrapper=client_wrapper)

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

        Returns
        -------
        AsyncRawWorkflowsClient
        """
        return self._raw_client

    async def list_workflows(
        self,
        *,
        external_id: typing.Optional[str] = None,
        active_only: typing.Optional[bool] = None,
        limit: typing.Optional[int] = None,
        offset: typing.Optional[int] = None,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> WorkflowListResponse:
        """
        List workflows for the authenticated organization.

        Parameters
        ----------
        external_id : typing.Optional[str]
            Filter by external ID

        active_only : typing.Optional[bool]
            Return only active workflows

        limit : typing.Optional[int]
            Maximum items to return

        offset : typing.Optional[int]
            Number of items to skip

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

        Returns
        -------
        WorkflowListResponse
            Successful Response

        Examples
        --------
        import asyncio

        from aitronos import AsyncAitronos

        client = AsyncAitronos(
            api_key="YOUR_API_KEY",
            token="YOUR_TOKEN",
        )


        async def main() -> None:
            await client.workflows.list_workflows()


        asyncio.run(main())
        """
        _response = await self._raw_client.list_workflows(
            external_id=external_id,
            active_only=active_only,
            limit=limit,
            offset=offset,
            request_options=request_options,
        )
        return _response.data

    async def create_workflow(
        self,
        *,
        name: str,
        definition: typing.Dict[str, typing.Any],
        description: typing.Optional[str] = OMIT,
        external_id: typing.Optional[str] = OMIT,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> WorkflowResponse:
        """
        Create a new workflow or update existing if external_id matches (upsert).

        Parameters
        ----------
        name : str
            Workflow name

        definition : typing.Dict[str, typing.Any]
            Flow-plate workflow JSON definition

        description : typing.Optional[str]
            Workflow description

        external_id : typing.Optional[str]
            Flow-plate internal flow ID for correlation

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

        Returns
        -------
        WorkflowResponse
            Successful Response

        Examples
        --------
        import asyncio

        from aitronos import AsyncAitronos

        client = AsyncAitronos(
            api_key="YOUR_API_KEY",
            token="YOUR_TOKEN",
        )


        async def main() -> None:
            await client.workflows.create_workflow(
                name="name",
                definition={"key": "value"},
            )


        asyncio.run(main())
        """
        _response = await self._raw_client.create_workflow(
            name=name,
            definition=definition,
            description=description,
            external_id=external_id,
            request_options=request_options,
        )
        return _response.data

    async def get_workflow(
        self, workflow_id: str, *, request_options: typing.Optional[RequestOptions] = None
    ) -> WorkflowResponse:
        """
        Get a workflow by ID.

        Parameters
        ----------
        workflow_id : str

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

        Returns
        -------
        WorkflowResponse
            Successful Response

        Examples
        --------
        import asyncio

        from aitronos import AsyncAitronos

        client = AsyncAitronos(
            api_key="YOUR_API_KEY",
            token="YOUR_TOKEN",
        )


        async def main() -> None:
            await client.workflows.get_workflow(
                workflow_id="workflow_id",
            )


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

    async def delete_workflow(
        self, workflow_id: str, *, request_options: typing.Optional[RequestOptions] = None
    ) -> None:
        """
        Soft delete a workflow and unlink any associated assistant.

        Parameters
        ----------
        workflow_id : str

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

        Returns
        -------
        None

        Examples
        --------
        import asyncio

        from aitronos import AsyncAitronos

        client = AsyncAitronos(
            api_key="YOUR_API_KEY",
            token="YOUR_TOKEN",
        )


        async def main() -> None:
            await client.workflows.delete_workflow(
                workflow_id="workflow_id",
            )


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

    async def update_workflow(
        self,
        workflow_id: str,
        *,
        name: typing.Optional[str] = OMIT,
        description: typing.Optional[str] = OMIT,
        definition: typing.Optional[typing.Dict[str, typing.Any]] = OMIT,
        is_active: typing.Optional[bool] = OMIT,
        external_id: typing.Optional[str] = OMIT,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> WorkflowResponse:
        """
        Update a workflow.

        Parameters
        ----------
        workflow_id : str

        name : typing.Optional[str]

        description : typing.Optional[str]

        definition : typing.Optional[typing.Dict[str, typing.Any]]
            Updated workflow definition

        is_active : typing.Optional[bool]
            Active status

        external_id : typing.Optional[str]
            Flow-plate internal flow ID for correlation

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

        Returns
        -------
        WorkflowResponse
            Successful Response

        Examples
        --------
        import asyncio

        from aitronos import AsyncAitronos

        client = AsyncAitronos(
            api_key="YOUR_API_KEY",
            token="YOUR_TOKEN",
        )


        async def main() -> None:
            await client.workflows.update_workflow(
                workflow_id="workflow_id",
            )


        asyncio.run(main())
        """
        _response = await self._raw_client.update_workflow(
            workflow_id,
            name=name,
            description=description,
            definition=definition,
            is_active=is_active,
            external_id=external_id,
            request_options=request_options,
        )
        return _response.data

    async def promote_workflow_to_assistant(
        self,
        workflow_id: str,
        *,
        name: typing.Optional[str] = OMIT,
        description: typing.Optional[str] = OMIT,
        primary_node_id: typing.Optional[str] = OMIT,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> PromoteToAssistantResponse:
        """
        Promote a workflow to an assistant.

        Parameters
        ----------
        workflow_id : str

        name : typing.Optional[str]
            Assistant name (defaults to workflow name)

        description : typing.Optional[str]
            Assistant description

        primary_node_id : typing.Optional[str]
            Node ID to use for settings (required if multiple FreddyLLM nodes)

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

        Returns
        -------
        PromoteToAssistantResponse
            Successful Response

        Examples
        --------
        import asyncio

        from aitronos import AsyncAitronos

        client = AsyncAitronos(
            api_key="YOUR_API_KEY",
            token="YOUR_TOKEN",
        )


        async def main() -> None:
            await client.workflows.promote_workflow_to_assistant(
                workflow_id="workflow_id",
            )


        asyncio.run(main())
        """
        _response = await self._raw_client.promote_workflow_to_assistant(
            workflow_id,
            name=name,
            description=description,
            primary_node_id=primary_node_id,
            request_options=request_options,
        )
        return _response.data
