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

from __future__ import annotations

from dataclasses import dataclass, field

import grpc

from ai.stigmer.agentic.agentinstance.v1 import api_pb2
from ai.stigmer.agentic.agentinstance.v1 import command_pb2_grpc
from ai.stigmer.agentic.agentinstance.v1 import query_pb2_grpc
from ai.stigmer.agentic.agentinstance.v1 import io_pb2
from ai.stigmer.agentic.agentinstance.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.commons.apiresource.apiresourcekind import api_resource_kind_pb2

from ._errors import wrap_error
from ._types import ResourceRef


class AgentInstanceClient:
    """Provides operations on agentinstance resources."""

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

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

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

    def update(self, input: AgentInstanceInput) -> api_pb2.AgentInstance:
        try:
            return self._command.update(input._to_proto())
        except grpc.RpcError as e:
            raise wrap_error(e) from e

    def delete(self, id: str) -> api_pb2.AgentInstance:
        try:
            return self._command.delete(io_pb2.AgentInstanceId(value=id))
        except grpc.RpcError as e:
            raise wrap_error(e) from e

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

    def get_by_agent(self, input: io_pb2.GetAgentInstancesByAgentRequest) -> io_pb2.AgentInstanceList:
        try:
            return self._query.getByAgent(input)
        except grpc.RpcError as e:
            raise wrap_error(e) from e

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

    def list(self, input: io_pb2.ListAgentInstancesRequest) -> io_pb2.AgentInstanceList:
        try:
            return self._query.list(input)
        except grpc.RpcError as e:
            raise wrap_error(e) from e


@dataclass
class AgentInstanceInput:
    """Input for creating or updating a AgentInstance."""

    name: str
    org: str
    slug: str | None = None
    labels: dict[str, str] | None = None
    agent_id: str = ""
    description: str = ""
    environment_refs: list[ResourceRef] = field(default_factory=list)

    def _to_proto(self) -> api_pb2.AgentInstance:
        spec = spec_pb2.AgentInstanceSpec(
            agent_id=self.agent_id,
            description=self.description,
        )
        for ref in self.environment_refs:
            spec.environment_refs.append(ref._to_proto())
        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.AgentInstance(
            api_version="agentic.stigmer.ai/v1",
            kind="AgentInstance",
            metadata=metadata,
            spec=spec,
        )

