"""gRPC service implementation for {{ model_name }}.

⚠️ AUTO-GENERATED BY PRISM - DO NOT EDIT
Extend this in grpc/{{ snake_name }}_servicer.py to add custom logic.
"""

from __future__ import annotations

import logging
from typing import Any

logger = logging.getLogger(__name__)


class {{ model_name }}Servicer:
    """gRPC service implementation for {{ model_name }}.

    This is a stub implementation that demonstrates the gRPC service
    interface. Connect to your service layer to implement actual logic.

    Proto definition: {{ snake_name }}.proto
    Service: {{ service_name }}

    Methods:
{% if ops_read %}
        - Get{{ model_name }}: Fetch a single {{ model_name }} by ID
{% endif %}
{% if ops_list %}
        - List{{ model_name }}s: List {{ model_name }} records with pagination
{% endif %}
{% if ops_create %}
        - Create{{ model_name }}: Create a new {{ model_name }}
{% endif %}
{% if ops_update %}
        - Update{{ model_name }}: Update an existing {{ model_name }}
{% endif %}
{% if ops_delete %}
        - Delete{{ model_name }}: Delete a {{ model_name }}
{% endif %}
{% if streaming_methods %}
        - Watch{{ model_name }}: Stream real-time events for {{ model_name }}
        - Stream{{ model_name }}s: Server-stream all {{ model_name }} records
{% endif %}
{% if bidirectional_streaming %}
        - Sync{{ model_name }}: Bidirectional streaming for real-time sync
{% endif %}
    """

{% if ops_read %}
    async def Get{{ model_name }}(
        self,
        request: Any,
        context: Any,
    ) -> Any:
        """Get a {{ model_name }} by ID.

        Args:
            request: The gRPC request with entity ID.
            context: The gRPC context.

        Returns:
            The {{ model_name }} proto message.
        """
        # TODO: Implement with your service layer
        # service = {{ model_name }}Service(db_session)
        # entity = await service.get(request.id)
        raise NotImplementedError("Implement Get{{ model_name }} with your service layer")

{% endif %}
{% if ops_list %}
    async def List{{ model_name }}s(
        self,
        request: Any,
        context: Any,
    ) -> Any:
        """List {{ model_name }} records.

        Args:
            request: The gRPC request with pagination params.
            context: The gRPC context.

        Returns:
            The list response with items and total count.
        """
        raise NotImplementedError("Implement List{{ model_name }}s with your service layer")

{% endif %}
{% if ops_create %}
    async def Create{{ model_name }}(
        self,
        request: Any,
        context: Any,
    ) -> Any:
        """Create a new {{ model_name }}.

        Args:
            request: The gRPC request with creation data.
            context: The gRPC context.

        Returns:
            The created {{ model_name }} proto message.
        """
        raise NotImplementedError("Implement Create{{ model_name }} with your service layer")

{% endif %}
{% if ops_update %}
    async def Update{{ model_name }}(
        self,
        request: Any,
        context: Any,
    ) -> Any:
        """Update an existing {{ model_name }}.

        Args:
            request: The gRPC request with update data.
            context: The gRPC context.

        Returns:
            The updated {{ model_name }} proto message.
        """
        raise NotImplementedError("Implement Update{{ model_name }} with your service layer")

{% endif %}
{% if ops_delete %}
    async def Delete{{ model_name }}(
        self,
        request: Any,
        context: Any,
    ) -> Any:
        """Delete a {{ model_name }}.

        Args:
            request: The gRPC request with entity ID.
            context: The gRPC context.

        Returns:
            Delete response with success flag.
        """
        raise NotImplementedError("Implement Delete{{ model_name }} with your service layer")

{% endif %}
{% if streaming_methods %}
    async def Watch{{ model_name }}(
        self,
        request: Any,
        context: Any,
    ) -> Any:
        """Stream real-time {{ model_name }} events.

        Server-streaming RPC that pushes events (created, updated, deleted)
        to the client. Integrates with the streaming event bus.

        Args:
            request: The gRPC request with optional event type filters.
            context: The gRPC context.

        Yields:
            {{ model_name }}Event proto messages.
        """
        from {{ package_name }}.streaming._generated.event_bus import event_bus

        event_types = list(request.event_types) if request.event_types else None

        async for event in event_bus.subscribe(
            model="{{ model_name }}",
            event_types=event_types,
        ):
            # TODO: Convert StreamEvent to proto message
            # yield {{ model_name }}Event(
            #     event_type=event.event_type,
            #     entity_id=event.entity_id,
            #     entity=to_proto(event.payload),
            # )
            yield event  # Replace with proper proto conversion

    async def Stream{{ model_name }}s(
        self,
        request: Any,
        context: Any,
    ) -> Any:
        """Stream all {{ model_name }} records.

        Server-streaming RPC that sends records one by one.

        Args:
            request: The gRPC request with pagination params.
            context: The gRPC context.

        Yields:
            {{ model_name }} proto messages.
        """
        raise NotImplementedError("Implement Stream{{ model_name }}s with your service layer")

{% endif %}
{% if bidirectional_streaming %}
    async def Sync{{ model_name }}(
        self,
        request_iterator: Any,
        context: Any,
    ) -> Any:
        """Bidirectional streaming for real-time {{ model_name }} sync.

        Receives events from the client and sends events back.

        Args:
            request_iterator: Async iterator of incoming events.
            context: The gRPC context.

        Yields:
            {{ model_name }}Event proto messages.
        """
        raise NotImplementedError("Implement Sync{{ model_name }} with your service layer")

{% endif %}

__all__ = ["{{ model_name }}Servicer"]
