import json
import logging

from channels.generic.websocket import AsyncJsonWebsocketConsumer

from asgiref.sync import sync_to_async
from django.apps import apps
from django.core.paginator import Paginator




class NotificationConsumer(AsyncJsonWebsocketConsumer):
    async def connect(self):
        await self.channel_layer.group_add("notifications", self.channel_name)
        await self.accept()
        logging.info("notification service started")

    async def disconnect(self, close_code):
        await self.channel_layer.group_discard("notifications", self.channel_name)

    async def receive(self, text_data):
        return await self.send(text_data=json.dumps({"message": "hello"}))

    async def send_notification(self, event):
        await self.send(text_data=json.dumps({
            "type": "notification",        # ✅ fixed — was event["type"]
            "message": event["message"]
        }))
