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

from __future__ import annotations

from dataclasses import dataclass

import grpc

from ai.stigmer.iam.apikey.v1 import api_pb2
from ai.stigmer.iam.apikey.v1 import command_pb2_grpc
from ai.stigmer.iam.apikey.v1 import query_pb2_grpc
from ai.stigmer.iam.apikey.v1 import io_pb2
from ai.stigmer.iam.apikey.v1 import spec_pb2
from ai.stigmer.commons.apiresource import metadata_pb2
from google.protobuf import empty_pb2

from ._errors import wrap_error


class ApiKeyClient:
    """Provides operations on apikey resources."""

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

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

    def update(self, input: ApiKeyInput) -> api_pb2.ApiKey:
        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.ApiKey:
        try:
            return self._command.delete(io_pb2.ApiKeyId(value=id))
        except grpc.RpcError as e:
            raise wrap_error(e) from e

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

    def get_by_key_hash(self, input: io_pb2.ApiKeyHash) -> api_pb2.ApiKey:
        try:
            return self._query.getByKeyHash(input)
        except grpc.RpcError as e:
            raise wrap_error(e) from e

    def find_all(self) -> io_pb2.ApiKeys:
        try:
            return self._query.findAll(empty_pb2.Empty())
        except grpc.RpcError as e:
            raise wrap_error(e) from e


@dataclass
class ApiKeyInput:
    """Input for creating or updating a ApiKey."""

    name: str
    org: str
    slug: str | None = None
    labels: dict[str, str] | None = None
    key_hash: str = ""
    fingerprint: str = ""
    expires_at: str = ""
    never_expires: bool = False

    def _to_proto(self) -> api_pb2.ApiKey:
        spec = spec_pb2.ApiKeySpec(
            key_hash=self.key_hash,
            fingerprint=self.fingerprint,
            never_expires=self.never_expires,
        )
        if self.expires_at:
            spec.expires_at.FromJsonString(self.expires_at)
        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.ApiKey(
            api_version="iam.stigmer.ai/v1",
            kind="ApiKey",
            metadata=metadata,
            spec=spec,
        )

