<?php

namespace App\Events;

use App\Models\Message;
use Illuminate\Broadcasting\InteractsWithSockets;
use Illuminate\Broadcasting\PrivateChannel;
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Queue\SerializesModels;

class MessageDeleted implements ShouldBroadcast
{
    use Dispatchable, InteractsWithSockets, SerializesModels;

    public bool $afterCommit = true;
    public string $queue;

    public function __construct(public Message $message)
    {
        $this->queue = (string) config('messaging.broadcast.queue', 'broadcasts');
        $this->dontBroadcastToCurrentUser();
    }

    public function broadcastOn(): array
    {
        return [new PrivateChannel('conversation.' . $this->message->conversation_id)];
    }

    public function broadcastAs(): string
    {
        return 'message.deleted';
    }

    public function broadcastWith(): array
    {
        return [
            'event' => 'message.deleted',
            'conversation_id' => (int) $this->message->conversation_id,
            'message_id' => (int) $this->message->id,
            'uuid' => (string) $this->message->uuid,
            'deleted_at' => optional($this->message->deleted_at ?? now())?->toIso8601String(),
        ];
    }
}
