<?php

namespace App\Events;

use Illuminate\Broadcasting\Channel;
use Illuminate\Contracts\Broadcasting\ShouldBroadcastNow;
use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Queue\SerializesModels;

class QrTokenIssued implements ShouldBroadcastNow
{
    use Dispatchable, SerializesModels;

    public function __construct(
        public int $sessionId,
        public string $token,
        public int $exp,
        public int $ttl
    ) {}

    public function broadcastOn(): Channel
    {
        // Harus sama dengan yg didengarkan di JS: 'attendance.session.' + SESSION_ID
        return new Channel('attendance.session.' . $this->sessionId);
    }

    // Supaya JS bisa listen('.qr.token')
    public function broadcastAs(): string
    {
        return 'qr.token';
    }

    public function broadcastWith(): array
    {
        return [
            'token' => $this->token,
            'exp'   => $this->exp,
            'ttl'   => $this->ttl,
        ];
    }
}
