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

from __future__ import annotations

from dataclasses import dataclass

import grpc

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

from ._errors import wrap_error
from ._types import ResourceRef


class IdentityAccountClient:
    """Provides operations on identityaccount resources."""

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

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

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

    def simulate_signup_webhook(self, input: io_pb2.IdentityAccountEmail) -> None:
        try:
            self._command.simulateSignupWebhook(input)
        except grpc.RpcError as e:
            raise wrap_error(e) from e

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

    def who_am_i(self) -> api_pb2.IdentityAccount:
        try:
            return self._query.whoAmI(empty_pb2.Empty())
        except grpc.RpcError as e:
            raise wrap_error(e) from e

    def get_by_email(self, input: io_pb2.IdentityAccountEmail) -> api_pb2.IdentityAccount:
        try:
            return self._query.getByEmail(input)
        except grpc.RpcError as e:
            raise wrap_error(e) from e

    def get_by_idp_id(self, id: str) -> api_pb2.IdentityAccount:
        try:
            return self._query.getByIdpId(io_pb2.IdpId(value=id))
        except grpc.RpcError as e:
            raise wrap_error(e) from e

    def get_actor_info(self, id: str) -> io_pb2.ApiResourceAuditActor:
        try:
            return self._query.getActorInfo(io_pb2.IdentityAccountId(value=id))
        except grpc.RpcError as e:
            raise wrap_error(e) from e


@dataclass
class IdentityAccountInput:
    """Input for creating or updating a IdentityAccount."""

    name: str
    org: str
    idp_id: str
    slug: str | None = None
    labels: dict[str, str] | None = None
    email: str = ""
    first_name: str = ""
    last_name: str = ""
    picture_url: str = ""
    is_machine_account: bool = False
    provisioning_mode: int = 0
    identity_provider_ref: ResourceRef | None = None

    def _to_proto(self) -> api_pb2.IdentityAccount:
        spec = spec_pb2.IdentityAccountSpec(
            idp_id=self.idp_id,
            email=self.email,
            first_name=self.first_name,
            last_name=self.last_name,
            picture_url=self.picture_url,
            is_machine_account=self.is_machine_account,
            provisioning_mode=self.provisioning_mode,
        )
        if self.identity_provider_ref is not None:
            spec.identity_provider_ref.CopyFrom(self.identity_provider_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.IdentityAccount(
            api_version="iam.stigmer.ai/v1",
            kind="IdentityAccount",
            metadata=metadata,
            spec=spec,
        )

