<?php

namespace App\Events;

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

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

    public function __construct(public readonly Unit $unit) {}

    public function broadcastOn(): array
    {
        return [
            new Channel('units'),
        ];
    }

    public function broadcastAs(): string
    {
        return 'UnitUpdated';
    }

    public function broadcastWith(): array
    {
        return [
            'unit' => [
                'id'        => $this->unit->id,
                'unit_name' => $this->unit->unit_name,
                'unit_code' => $this->unit->unit_code,
                'status'    => $this->unit->status,
                'location'  => $this->unit->location,
                'updated_at'=> $this->unit->updated_at,
            ],
        ];
    }
}
