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

from __future__ import annotations

from dataclasses import dataclass

import grpc

from ai.stigmer.tenancy.organization.v1 import api_pb2
from ai.stigmer.tenancy.organization.v1 import command_pb2_grpc
from ai.stigmer.tenancy.organization.v1 import query_pb2_grpc
from ai.stigmer.tenancy.organization.v1 import io_pb2
from ai.stigmer.tenancy.organization.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 OrganizationClient:
    """Provides operations on organization resources."""

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

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

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

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

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

    def find(self, input: io_pb2.FindApiResourcesRequest) -> io_pb2.OrganizationList:
        try:
            return self._query.find(input)
        except grpc.RpcError as e:
            raise wrap_error(e) from e

    def find_my_organizations(self) -> io_pb2.Organizations:
        try:
            return self._query.findMyOrganizations(empty_pb2.Empty())
        except grpc.RpcError as e:
            raise wrap_error(e) from e

    def get_by_external_org_id(self, input: io_pb2.OrganizationExternalLookup) -> api_pb2.Organization:
        try:
            return self._query.getByExternalOrgId(input)
        except grpc.RpcError as e:
            raise wrap_error(e) from e


@dataclass
class OrganizationInput:
    """Input for creating or updating a Organization."""

    name: str
    org: str
    slug: str | None = None
    labels: dict[str, str] | None = None
    description: str = ""
    logo_url: str = ""
    management_mode: int = 0
    identity_provider_ref: ResourceRef | None = None
    external_org_id: str = ""
    is_personal: bool = False

    def _to_proto(self) -> api_pb2.Organization:
        spec = spec_pb2.OrganizationSpec(
            description=self.description,
            logo_url=self.logo_url,
            management_mode=self.management_mode,
            external_org_id=self.external_org_id,
            is_personal=self.is_personal,
        )
        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.Organization(
            api_version="tenancy.stigmer.ai/v1",
            kind="Organization",
            metadata=metadata,
            spec=spec,
        )

