<?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 EntryCreated 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 'EntryCreated';
    }

    public function broadcastWith(): array
    {
        return [
            'entry' => [
                'id'                   => $this->entry->id,
                'unit_id'              => $this->entry->unit_id,
                'entered_by'           => $this->entry->enteredBy?->only(['id', 'name']),
                'entry_date'           => $this->entry->entry_date,
                'entry_time'           => $this->entry->entry_time,
                'condition_status'     => $this->entry->condition_status,
                'issue_found'          => $this->entry->issue_found,
                'maintenance_required' => $this->entry->maintenance_required,
                'created_at'           => $this->entry->created_at,
            ],
        ];
    }
}
