# Code generated by stigmer-codegen. DO NOT EDIT.

from __future__ import annotations

from dataclasses import dataclass, field

import grpc

from ai.stigmer.agentic.executioncontext.v1 import api_pb2
from ai.stigmer.agentic.executioncontext.v1 import command_pb2_grpc
from ai.stigmer.agentic.executioncontext.v1 import query_pb2_grpc
from ai.stigmer.agentic.executioncontext.v1 import io_pb2
from ai.stigmer.agentic.executioncontext.v1 import spec_pb2
from ai.stigmer.commons.apiresource import io_pb2 as apiresource_io_pb2
from ai.stigmer.commons.apiresource import metadata_pb2
from ai.stigmer.agentic.executioncontext.v1 import spec_pb2 as executioncontext_spec_pb2
from ai.stigmer.commons.apiresource.apiresourcekind import api_resource_kind_pb2

from ._errors import wrap_error
from ._types import DeleteResourceInput, EnvVarInput, ResourceRef


class ExecutionContextClient:
    """Provides operations on executioncontext resources."""

    def __init__(self, channel: grpc.Channel) -> None:
        self._command = command_pb2_grpc.ExecutionContextCommandControllerStub(channel)
        self._query = query_pb2_grpc.ExecutionContextQueryControllerStub(channel)

    def apply(self, input: ExecutionContextInput) -> api_pb2.ExecutionContext:
        try:
            return self._command.apply(input._to_proto())
        except grpc.RpcError as e:
            raise wrap_error(e) from e

    def create(self, input: ExecutionContextInput) -> api_pb2.ExecutionContext:
        try:
            return self._command.create(input._to_proto())
        except grpc.RpcError as e:
            raise wrap_error(e) from e

    def delete(self, input: DeleteResourceInput) -> api_pb2.ExecutionContext:
        try:
            return self._command.delete(input._to_proto())
        except grpc.RpcError as e:
            raise wrap_error(e) from e

    def get(self, id: str) -> api_pb2.ExecutionContext:
        try:
            return self._query.get(io_pb2.ExecutionContextId(value=id))
        except grpc.RpcError as e:
            raise wrap_error(e) from e

    def get_by_reference(self, ref: ResourceRef) -> api_pb2.ExecutionContext:
        try:
            proto = ref._to_proto()
            proto.kind = api_resource_kind_pb2.execution_context
            return self._query.getByReference(proto)
        except grpc.RpcError as e:
            raise wrap_error(e) from e

    def get_by_execution_id(self, input: io_pb2.ExecutionContextExecutionIdInput) -> api_pb2.ExecutionContext:
        try:
            return self._query.getByExecutionId(input)
        except grpc.RpcError as e:
            raise wrap_error(e) from e


@dataclass
class ExecutionContextInput:
    """Input for creating or updating a ExecutionContext."""

    name: str
    org: str
    slug: str | None = None
    labels: dict[str, str] | None = None
    execution_id: str = ""
    data: dict[str, EnvVarInput] = field(default_factory=dict)

    def _to_proto(self) -> api_pb2.ExecutionContext:
        spec = spec_pb2.ExecutionContextSpec(
            execution_id=self.execution_id,
        )
        for k, v in self.data.items():
            spec.data[k].CopyFrom(executioncontext_spec_pb2.ExecutionValue(
                value=v.value, is_secret=v.is_secret,
            ))
        metadata = metadata_pb2.ApiResourceMetadata(
            name=self.name,
            org=self.org,
        )
        if self.slug:
            metadata.slug = self.slug
        if self.labels:
            metadata.labels.update(self.labels)
        return api_pb2.ExecutionContext(
            api_version="agentic.stigmer.ai/v1",
            kind="ExecutionContext",
            metadata=metadata,
            spec=spec,
        )

