<?php

namespace App\Notifications;

use App\Models\Inspection;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
use Illuminate\Notifications\Notification;

class InspectionDeletedNotification extends Notification implements ShouldBroadcast
{
    use Queueable;

    public function __construct(public Inspection $inspection, public string $deletedBy) {}

    public function via($notifiable): array
    {
        return ['database', 'broadcast'];
    }

    public function toArray($notifiable): array
    {
        return [
            'type' => 'inspection_deleted',
            'inspection_id' => $this->inspection->id,
            'project_id' => $this->inspection->project_id,
            'feature_name' => $this->inspection->feature?->name ?? '—',
            'template_name' => $this->inspection->template?->name ?? '—',
            'deleted_by' => $this->deletedBy,
            'message' => "Inspección eliminada en '{$this->inspection->feature?->name}' por {$this->deletedBy}",
        ];
    }

    public function broadcastOn(): array
    {
        $userIds = $this->inspection->project->users->pluck('id')->toArray();
        return array_map(fn ($id) => new \Illuminate\Broadcasting\PrivateChannel("user.{$id}"), $userIds);
    }
}