<?php

namespace App\Events;

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

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

    public function __construct(public readonly ChecklistEntry $entry) {}

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

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

    public function broadcastWith(): array
    {
        return [
            'entry' => [
                'id'                   => $this->entry->id,
                'unit_id'              => $this->entry->unit_id,
                'condition_status'     => $this->entry->condition_status,
                'issue_found'          => $this->entry->issue_found,
                'maintenance_required' => $this->entry->maintenance_required,
                'last_edited_by'       => $this->entry->lastEditedBy?->only(['id', 'name']),
                'last_edited_at'       => $this->entry->last_edited_at,
                'updated_at'           => $this->entry->updated_at,
            ],
        ];
    }
}
