#
# Copyright (c) 2021 Nitric Technologies Pty Ltd.
#
# This file is part of Nitric Python 3 SDK.
# See https://github.com/nitrictech/python-sdk for further info.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

# Generated by the protocol buffer compiler.  DO NOT EDIT!
# sources: nitric/proto/schedules/v1/schedules.proto
# plugin: python-betterproto
# This file has been @generated

from dataclasses import dataclass
from typing import (
    TYPE_CHECKING,
    AsyncIterable,
    AsyncIterator,
    Dict,
    Iterable,
    Optional,
    Union,
)

import betterproto
import grpclib
from betterproto.grpc.grpclib_server import ServiceBase


if TYPE_CHECKING:
    import grpclib.server
    from betterproto.grpc.grpclib_client import MetadataLike
    from grpclib.metadata import Deadline


@dataclass(eq=False, repr=False)
class ClientMessage(betterproto.Message):
    """ClientMessages are sent from the service to the nitric server"""

    id: str = betterproto.string_field(1)
    """globally unique ID of the request/response pair"""

    registration_request: "RegistrationRequest" = betterproto.message_field(
        2, group="content"
    )
    """Register new a schedule"""

    interval_response: "IntervalResponse" = betterproto.message_field(
        3, group="content"
    )
    """
    Response to a schedule interval (i.e. response from callback function)
    """


@dataclass(eq=False, repr=False)
class IntervalRequest(betterproto.Message):
    schedule_name: str = betterproto.string_field(1)


@dataclass(eq=False, repr=False)
class ServerMessage(betterproto.Message):
    """ServerMessages are sent from the nitric server to the service"""

    id: str = betterproto.string_field(1)
    """globally unique ID of the request/response pair"""

    registration_response: "RegistrationResponse" = betterproto.message_field(
        2, group="content"
    )
    """Response to a schedule subscription request"""

    interval_request: "IntervalRequest" = betterproto.message_field(3, group="content")
    """A schedule interval trigger request (i.e. call the callback)"""


@dataclass(eq=False, repr=False)
class RegistrationRequest(betterproto.Message):
    schedule_name: str = betterproto.string_field(1)
    every: "ScheduleEvery" = betterproto.message_field(10, group="cadence")
    cron: "ScheduleCron" = betterproto.message_field(11, group="cadence")


@dataclass(eq=False, repr=False)
class ScheduleEvery(betterproto.Message):
    rate: str = betterproto.string_field(1)


@dataclass(eq=False, repr=False)
class ScheduleCron(betterproto.Message):
    expression: str = betterproto.string_field(1)


@dataclass(eq=False, repr=False)
class RegistrationResponse(betterproto.Message):
    pass


@dataclass(eq=False, repr=False)
class IntervalResponse(betterproto.Message):
    pass


class SchedulesStub(betterproto.ServiceStub):
    async def schedule(
        self,
        client_message_iterator: Union[
            AsyncIterable["ClientMessage"], Iterable["ClientMessage"]
        ],
        *,
        timeout: Optional[float] = None,
        deadline: Optional["Deadline"] = None,
        metadata: Optional["MetadataLike"] = None
    ) -> AsyncIterator["ServerMessage"]:
        async for response in self._stream_stream(
            "/nitric.proto.schedules.v1.Schedules/Schedule",
            client_message_iterator,
            ClientMessage,
            ServerMessage,
            timeout=timeout,
            deadline=deadline,
            metadata=metadata,
        ):
            yield response


class SchedulesBase(ServiceBase):
    async def schedule(
        self, client_message_iterator: AsyncIterator["ClientMessage"]
    ) -> AsyncIterator["ServerMessage"]:
        raise grpclib.GRPCError(grpclib.const.Status.UNIMPLEMENTED)
        yield ServerMessage()

    async def __rpc_schedule(
        self, stream: "grpclib.server.Stream[ClientMessage, ServerMessage]"
    ) -> None:
        request = stream.__aiter__()
        await self._call_rpc_handler_server_stream(
            self.schedule,
            stream,
            request,
        )

    def __mapping__(self) -> Dict[str, grpclib.const.Handler]:
        return {
            "/nitric.proto.schedules.v1.Schedules/Schedule": grpclib.const.Handler(
                self.__rpc_schedule,
                grpclib.const.Cardinality.STREAM_STREAM,
                ClientMessage,
                ServerMessage,
            ),
        }
