<?php

namespace App\Events;

use App\Models\WhatsappLog;
use Illuminate\Broadcasting\InteractsWithSockets;
use Illuminate\Broadcasting\PrivateChannel;
use Illuminate\Contracts\Broadcasting\ShouldBroadcastNow;
use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Queue\SerializesModels;

class NuevoMensajeWhatsApp implements ShouldBroadcastNow
{
    use Dispatchable, InteractsWithSockets, SerializesModels;

    public function __construct(public WhatsappLog $log) {}

    public function broadcastOn(): array
    {
        if (!$this->log->alumno_id) {
            return [];
        }

        return [
            new PrivateChannel('whatsapp-chat.' . $this->log->alumno_id),
        ];
    }

    public function broadcastWith(): array
    {
        return [
            'id'         => $this->log->id,
            'tipo'       => $this->log->tipo,
            'mensaje'    => $this->log->mensaje,
            'media_type' => $this->log->media_type,
            'alumno_id'  => $this->log->alumno_id,
            'telefono'   => $this->log->telefono,
            'created_at' => $this->log->created_at?->format('H:i'),
        ];
    }
}
