<?php

namespace App\Events;

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

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

    public $tenantId;
    public $changedTables;

    /**
     * @param string $tenantId
     * @param array $changedTables
     */
    public function __construct($tenantId, array $changedTables)
    {
        $this->tenantId = $tenantId;
        $this->changedTables = $changedTables;
    }

    public function broadcastOn(): array
    {
        return [
            new Channel('tenant.' . $this->tenantId),
        ];
    }

    public function broadcastAs(): string
    {
        return 'data.batch.updated';
    }

    public function broadcastWith(): array
    {
        return [
            'tenant_id' => $this->tenantId,
            'changed_tables' => $this->changedTables,
            'updated_at' => now()->toDateTimeString(),
        ];
    }
}
