"""GraphQL subscriptions for {{ model_name }}.

⚠️ AUTO-GENERATED BY PRISM - DO NOT EDIT
"""


from typing import AsyncGenerator

import strawberry
from strawberry.types import Info

from ..context import Context
from ..types.{{ snake_name }} import {{ model_name }}Type
{% if use_event_bus %}
from {{ package_name }}.streaming._generated.event_bus import event_bus
{% endif %}


@strawberry.type
class {{ model_name }}Subscriptions:
    """GraphQL subscriptions for {{ model_name }}."""

    @strawberry.subscription(description="{{ model_name }} created events")
    async def {{ camel_name }}Created(
        self,
        info: Info[Context, None],
    ) -> AsyncGenerator[{{ model_name }}Type]:
        """Subscribe to {{ model_name }} creation events."""
{% if use_event_bus %}
        async for event in event_bus.subscribe(
            model="{{ model_name }}",
            event_types=["created"],
        ):
            if event.payload:
                yield {{ model_name }}Type(**event.payload)
{% else %}
        # Implement with your pub/sub system (Redis, etc.)
        raise NotImplementedError("Implement subscription with your pub/sub system")
        yield  # type: ignore
{% endif %}

    @strawberry.subscription(description="{{ model_name }} updated events")
    async def {{ camel_name }}Updated(
        self,
        info: Info[Context, None],
        id: int | None = None,
    ) -> AsyncGenerator[{{ model_name }}Type]:
        """Subscribe to {{ model_name }} update events."""
{% if use_event_bus %}
        async for event in event_bus.subscribe(
            model="{{ model_name }}",
            event_types=["updated"],
        ):
            if id is not None and event.entity_id != id:
                continue
            if event.payload:
                yield {{ model_name }}Type(**event.payload)
{% else %}
        raise NotImplementedError("Implement subscription with your pub/sub system")
        yield  # type: ignore
{% endif %}

    @strawberry.subscription(description="{{ model_name }} deleted events")
    async def {{ camel_name }}Deleted(
        self,
        info: Info[Context, None],
    ) -> AsyncGenerator[int]:
        """Subscribe to {{ model_name }} deletion events. Returns the deleted ID."""
{% if use_event_bus %}
        async for event in event_bus.subscribe(
            model="{{ model_name }}",
            event_types=["deleted"],
        ):
            yield event.entity_id
{% else %}
        raise NotImplementedError("Implement subscription with your pub/sub system")
        yield  # type: ignore
{% endif %}


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