<?php

namespace App\Events;

use App\Models\Incident;
use Illuminate\Broadcasting\InteractsWithSockets;
use Illuminate\Broadcasting\PrivateChannel;
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Queue\SerializesModels;

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

    public function __construct(public Incident $incident) {}

    public function broadcastOn(): array
    {
        return [new PrivateChannel('incidents')];
    }

    public function broadcastAs(): string
    {
        return 'incident.opened';
    }

    public function broadcastWith(): array
    {
        return [
            'id' => $this->incident->id,
            'public_id' => $this->incident->public_id,
            'title' => $this->incident->title,
            'severity' => $this->incident->severity,
            'status' => $this->incident->status,
            'subject_type' => class_basename($this->incident->subject_type),
            'subject_id' => $this->incident->subject_id,
            'detected_at' => $this->incident->detected_at->toIso8601String(),
        ];
    }
}
