"""Live seat-map consumer (FR-10 real-time seat status).

Read-only broadcast channel - anyone viewing the booking page for a trip may
subscribe. All *mutations* (lock/release/book) go through the authenticated
HTTP endpoints; this socket only tells viewers "seat X changed to status Y"
so their grid updates instantly.
"""
import json

from channels.generic.websocket import AsyncWebsocketConsumer


class SeatMapConsumer(AsyncWebsocketConsumer):
    async def connect(self):
        self.schedule_id = self.scope['url_route']['kwargs']['schedule_id']
        self.group = f'seats_{self.schedule_id}'
        await self.channel_layer.group_add(self.group, self.channel_name)
        await self.accept()

    async def disconnect(self, code):
        await self.channel_layer.group_discard(self.group, self.channel_name)

    async def seat_update(self, event):
        await self.send(text_data=json.dumps(event['payload']))
