# File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.

from __future__ import annotations

from typing import Dict, Optional
from typing_extensions import Literal

import httpx

from ..types import workflow_run_list_params, workflow_run_resume_params
from .._types import Body, Omit, Query, Headers, NoneType, NotGiven, omit, not_given
from .._utils import maybe_transform, async_maybe_transform
from .._compat import cached_property
from .._resource import SyncAPIResource, AsyncAPIResource
from .._response import (
    to_raw_response_wrapper,
    to_streamed_response_wrapper,
    async_to_raw_response_wrapper,
    async_to_streamed_response_wrapper,
)
from ..pagination import SyncCursor, AsyncCursor
from .._base_client import AsyncPaginator, make_request_options
from ..types.workflow_run import WorkflowRun
from ..types.shared_params.context import Context
from ..types.workflow_run_resume_response import WorkflowRunResumeResponse

__all__ = ["WorkflowRunsResource", "AsyncWorkflowRunsResource"]


class WorkflowRunsResource(SyncAPIResource):
    @cached_property
    def with_raw_response(self) -> WorkflowRunsResourceWithRawResponse:
        """
        This property can be used as a prefix for any HTTP method call to return
        the raw response object instead of the parsed content.

        For more information, see https://www.github.com/reminix-ai/reminix-python#accessing-raw-response-data-eg-headers
        """
        return WorkflowRunsResourceWithRawResponse(self)

    @cached_property
    def with_streaming_response(self) -> WorkflowRunsResourceWithStreamingResponse:
        """
        An alternative to `.with_raw_response` that doesn't eagerly read the response body.

        For more information, see https://www.github.com/reminix-ai/reminix-python#with_streaming_response
        """
        return WorkflowRunsResourceWithStreamingResponse(self)

    def retrieve(
        self,
        run_id: str,
        *,
        # Use the following arguments if you need to pass additional parameters to the API that aren't available via kwargs.
        # The extra values given here take precedence over values defined on the client or passed to this method.
        extra_headers: Headers | None = None,
        extra_query: Query | None = None,
        extra_body: Body | None = None,
        timeout: float | httpx.Timeout | None | NotGiven = not_given,
    ) -> WorkflowRun:
        """
        Get details of a specific workflow run including its full state.

        Args:
          run_id: Workflow run ID

          extra_headers: Send extra headers

          extra_query: Add additional query parameters to the request

          extra_body: Add additional JSON properties to the request

          timeout: Override the client-level default timeout for this request, in seconds
        """
        if not run_id:
            raise ValueError(f"Expected a non-empty value for `run_id` but received {run_id!r}")
        return self._get(
            f"/workflow-runs/{run_id}",
            options=make_request_options(
                extra_headers=extra_headers, extra_query=extra_query, extra_body=extra_body, timeout=timeout
            ),
            cast_to=WorkflowRun,
        )

    def list(
        self,
        *,
        agent_name: str | Omit = omit,
        cursor: str | Omit = omit,
        limit: float | Omit = omit,
        status: Literal["running", "paused", "completed", "failed"] | Omit = omit,
        # Use the following arguments if you need to pass additional parameters to the API that aren't available via kwargs.
        # The extra values given here take precedence over values defined on the client or passed to this method.
        extra_headers: Headers | None = None,
        extra_query: Query | None = None,
        extra_body: Body | None = None,
        timeout: float | httpx.Timeout | None | NotGiven = not_given,
    ) -> SyncCursor[WorkflowRun]:
        """
        List workflow runs for the project with optional filtering.

        **Filters:**

        - `agent_name`: Filter by agent name
        - `status`: Filter by run status (running, paused, completed, failed)
        - `identity.*`: Filter by identity fields (e.g., identity.user_id=user_123)

        Args:
          agent_name: Filter by agent name

          cursor: Cursor for pagination

          limit: Number of workflow runs to return

          status: Filter by run status

          extra_headers: Send extra headers

          extra_query: Add additional query parameters to the request

          extra_body: Add additional JSON properties to the request

          timeout: Override the client-level default timeout for this request, in seconds
        """
        return self._get_api_list(
            "/workflow-runs",
            page=SyncCursor[WorkflowRun],
            options=make_request_options(
                extra_headers=extra_headers,
                extra_query=extra_query,
                extra_body=extra_body,
                timeout=timeout,
                query=maybe_transform(
                    {
                        "agent_name": agent_name,
                        "cursor": cursor,
                        "limit": limit,
                        "status": status,
                    },
                    workflow_run_list_params.WorkflowRunListParams,
                ),
            ),
            model=WorkflowRun,
        )

    def delete(
        self,
        run_id: str,
        *,
        # Use the following arguments if you need to pass additional parameters to the API that aren't available via kwargs.
        # The extra values given here take precedence over values defined on the client or passed to this method.
        extra_headers: Headers | None = None,
        extra_query: Query | None = None,
        extra_body: Body | None = None,
        timeout: float | httpx.Timeout | None | NotGiven = not_given,
    ) -> None:
        """Delete a workflow run.

        This action cannot be undone.

        Args:
          run_id: Workflow run ID

          extra_headers: Send extra headers

          extra_query: Add additional query parameters to the request

          extra_body: Add additional JSON properties to the request

          timeout: Override the client-level default timeout for this request, in seconds
        """
        if not run_id:
            raise ValueError(f"Expected a non-empty value for `run_id` but received {run_id!r}")
        extra_headers = {"Accept": "*/*", **(extra_headers or {})}
        return self._delete(
            f"/workflow-runs/{run_id}",
            options=make_request_options(
                extra_headers=extra_headers, extra_query=extra_query, extra_body=extra_body, timeout=timeout
            ),
            cast_to=NoneType,
        )

    def resume(
        self,
        run_id: str,
        *,
        context: Context | Omit = omit,
        input: Dict[str, Optional[object]] | Omit = omit,
        stream: bool | Omit = omit,
        # Use the following arguments if you need to pass additional parameters to the API that aren't available via kwargs.
        # The extra values given here take precedence over values defined on the client or passed to this method.
        extra_headers: Headers | None = None,
        extra_query: Query | None = None,
        extra_body: Body | None = None,
        timeout: float | httpx.Timeout | None | NotGiven = not_given,
    ) -> WorkflowRunResumeResponse:
        """
        Resume a paused workflow run by providing the required action response.

        **Input:** Pass the action response (e.g. `{ approved: true }` for an approval
        step). The run must be in `paused` status to be resumed.

        Args:
          run_id: Workflow run ID

          context: Optional context: identity (conversation scoping), attributes (injected into
              system prompt), and pass-through for tools

          input: Action response input (e.g. { approved: true })

          stream: Enable streaming response (SSE)

          extra_headers: Send extra headers

          extra_query: Add additional query parameters to the request

          extra_body: Add additional JSON properties to the request

          timeout: Override the client-level default timeout for this request, in seconds
        """
        if not run_id:
            raise ValueError(f"Expected a non-empty value for `run_id` but received {run_id!r}")
        return self._post(
            f"/workflow-runs/{run_id}/resume",
            body=maybe_transform(
                {
                    "context": context,
                    "input": input,
                    "stream": stream,
                },
                workflow_run_resume_params.WorkflowRunResumeParams,
            ),
            options=make_request_options(
                extra_headers=extra_headers, extra_query=extra_query, extra_body=extra_body, timeout=timeout
            ),
            cast_to=WorkflowRunResumeResponse,
        )


class AsyncWorkflowRunsResource(AsyncAPIResource):
    @cached_property
    def with_raw_response(self) -> AsyncWorkflowRunsResourceWithRawResponse:
        """
        This property can be used as a prefix for any HTTP method call to return
        the raw response object instead of the parsed content.

        For more information, see https://www.github.com/reminix-ai/reminix-python#accessing-raw-response-data-eg-headers
        """
        return AsyncWorkflowRunsResourceWithRawResponse(self)

    @cached_property
    def with_streaming_response(self) -> AsyncWorkflowRunsResourceWithStreamingResponse:
        """
        An alternative to `.with_raw_response` that doesn't eagerly read the response body.

        For more information, see https://www.github.com/reminix-ai/reminix-python#with_streaming_response
        """
        return AsyncWorkflowRunsResourceWithStreamingResponse(self)

    async def retrieve(
        self,
        run_id: str,
        *,
        # Use the following arguments if you need to pass additional parameters to the API that aren't available via kwargs.
        # The extra values given here take precedence over values defined on the client or passed to this method.
        extra_headers: Headers | None = None,
        extra_query: Query | None = None,
        extra_body: Body | None = None,
        timeout: float | httpx.Timeout | None | NotGiven = not_given,
    ) -> WorkflowRun:
        """
        Get details of a specific workflow run including its full state.

        Args:
          run_id: Workflow run ID

          extra_headers: Send extra headers

          extra_query: Add additional query parameters to the request

          extra_body: Add additional JSON properties to the request

          timeout: Override the client-level default timeout for this request, in seconds
        """
        if not run_id:
            raise ValueError(f"Expected a non-empty value for `run_id` but received {run_id!r}")
        return await self._get(
            f"/workflow-runs/{run_id}",
            options=make_request_options(
                extra_headers=extra_headers, extra_query=extra_query, extra_body=extra_body, timeout=timeout
            ),
            cast_to=WorkflowRun,
        )

    def list(
        self,
        *,
        agent_name: str | Omit = omit,
        cursor: str | Omit = omit,
        limit: float | Omit = omit,
        status: Literal["running", "paused", "completed", "failed"] | Omit = omit,
        # Use the following arguments if you need to pass additional parameters to the API that aren't available via kwargs.
        # The extra values given here take precedence over values defined on the client or passed to this method.
        extra_headers: Headers | None = None,
        extra_query: Query | None = None,
        extra_body: Body | None = None,
        timeout: float | httpx.Timeout | None | NotGiven = not_given,
    ) -> AsyncPaginator[WorkflowRun, AsyncCursor[WorkflowRun]]:
        """
        List workflow runs for the project with optional filtering.

        **Filters:**

        - `agent_name`: Filter by agent name
        - `status`: Filter by run status (running, paused, completed, failed)
        - `identity.*`: Filter by identity fields (e.g., identity.user_id=user_123)

        Args:
          agent_name: Filter by agent name

          cursor: Cursor for pagination

          limit: Number of workflow runs to return

          status: Filter by run status

          extra_headers: Send extra headers

          extra_query: Add additional query parameters to the request

          extra_body: Add additional JSON properties to the request

          timeout: Override the client-level default timeout for this request, in seconds
        """
        return self._get_api_list(
            "/workflow-runs",
            page=AsyncCursor[WorkflowRun],
            options=make_request_options(
                extra_headers=extra_headers,
                extra_query=extra_query,
                extra_body=extra_body,
                timeout=timeout,
                query=maybe_transform(
                    {
                        "agent_name": agent_name,
                        "cursor": cursor,
                        "limit": limit,
                        "status": status,
                    },
                    workflow_run_list_params.WorkflowRunListParams,
                ),
            ),
            model=WorkflowRun,
        )

    async def delete(
        self,
        run_id: str,
        *,
        # Use the following arguments if you need to pass additional parameters to the API that aren't available via kwargs.
        # The extra values given here take precedence over values defined on the client or passed to this method.
        extra_headers: Headers | None = None,
        extra_query: Query | None = None,
        extra_body: Body | None = None,
        timeout: float | httpx.Timeout | None | NotGiven = not_given,
    ) -> None:
        """Delete a workflow run.

        This action cannot be undone.

        Args:
          run_id: Workflow run ID

          extra_headers: Send extra headers

          extra_query: Add additional query parameters to the request

          extra_body: Add additional JSON properties to the request

          timeout: Override the client-level default timeout for this request, in seconds
        """
        if not run_id:
            raise ValueError(f"Expected a non-empty value for `run_id` but received {run_id!r}")
        extra_headers = {"Accept": "*/*", **(extra_headers or {})}
        return await self._delete(
            f"/workflow-runs/{run_id}",
            options=make_request_options(
                extra_headers=extra_headers, extra_query=extra_query, extra_body=extra_body, timeout=timeout
            ),
            cast_to=NoneType,
        )

    async def resume(
        self,
        run_id: str,
        *,
        context: Context | Omit = omit,
        input: Dict[str, Optional[object]] | Omit = omit,
        stream: bool | Omit = omit,
        # Use the following arguments if you need to pass additional parameters to the API that aren't available via kwargs.
        # The extra values given here take precedence over values defined on the client or passed to this method.
        extra_headers: Headers | None = None,
        extra_query: Query | None = None,
        extra_body: Body | None = None,
        timeout: float | httpx.Timeout | None | NotGiven = not_given,
    ) -> WorkflowRunResumeResponse:
        """
        Resume a paused workflow run by providing the required action response.

        **Input:** Pass the action response (e.g. `{ approved: true }` for an approval
        step). The run must be in `paused` status to be resumed.

        Args:
          run_id: Workflow run ID

          context: Optional context: identity (conversation scoping), attributes (injected into
              system prompt), and pass-through for tools

          input: Action response input (e.g. { approved: true })

          stream: Enable streaming response (SSE)

          extra_headers: Send extra headers

          extra_query: Add additional query parameters to the request

          extra_body: Add additional JSON properties to the request

          timeout: Override the client-level default timeout for this request, in seconds
        """
        if not run_id:
            raise ValueError(f"Expected a non-empty value for `run_id` but received {run_id!r}")
        return await self._post(
            f"/workflow-runs/{run_id}/resume",
            body=await async_maybe_transform(
                {
                    "context": context,
                    "input": input,
                    "stream": stream,
                },
                workflow_run_resume_params.WorkflowRunResumeParams,
            ),
            options=make_request_options(
                extra_headers=extra_headers, extra_query=extra_query, extra_body=extra_body, timeout=timeout
            ),
            cast_to=WorkflowRunResumeResponse,
        )


class WorkflowRunsResourceWithRawResponse:
    def __init__(self, workflow_runs: WorkflowRunsResource) -> None:
        self._workflow_runs = workflow_runs

        self.retrieve = to_raw_response_wrapper(
            workflow_runs.retrieve,
        )
        self.list = to_raw_response_wrapper(
            workflow_runs.list,
        )
        self.delete = to_raw_response_wrapper(
            workflow_runs.delete,
        )
        self.resume = to_raw_response_wrapper(
            workflow_runs.resume,
        )


class AsyncWorkflowRunsResourceWithRawResponse:
    def __init__(self, workflow_runs: AsyncWorkflowRunsResource) -> None:
        self._workflow_runs = workflow_runs

        self.retrieve = async_to_raw_response_wrapper(
            workflow_runs.retrieve,
        )
        self.list = async_to_raw_response_wrapper(
            workflow_runs.list,
        )
        self.delete = async_to_raw_response_wrapper(
            workflow_runs.delete,
        )
        self.resume = async_to_raw_response_wrapper(
            workflow_runs.resume,
        )


class WorkflowRunsResourceWithStreamingResponse:
    def __init__(self, workflow_runs: WorkflowRunsResource) -> None:
        self._workflow_runs = workflow_runs

        self.retrieve = to_streamed_response_wrapper(
            workflow_runs.retrieve,
        )
        self.list = to_streamed_response_wrapper(
            workflow_runs.list,
        )
        self.delete = to_streamed_response_wrapper(
            workflow_runs.delete,
        )
        self.resume = to_streamed_response_wrapper(
            workflow_runs.resume,
        )


class AsyncWorkflowRunsResourceWithStreamingResponse:
    def __init__(self, workflow_runs: AsyncWorkflowRunsResource) -> None:
        self._workflow_runs = workflow_runs

        self.retrieve = async_to_streamed_response_wrapper(
            workflow_runs.retrieve,
        )
        self.list = async_to_streamed_response_wrapper(
            workflow_runs.list,
        )
        self.delete = async_to_streamed_response_wrapper(
            workflow_runs.delete,
        )
        self.resume = async_to_streamed_response_wrapper(
            workflow_runs.resume,
        )
