<?php

namespace App\Notifications;

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

class IssueReportedNotification extends Notification implements ShouldBroadcast
{
    use Queueable;

    public function __construct(public Issue $issue) {}

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

    public function toArray($notifiable): array
    {
        return [
            'type' => 'issue_reported',
            'issue_id' => $this->issue->id,
            'project_id' => $this->issue->project_id,
            'feature_name' => $this->issue->feature?->name ?? '—',
            'priority' => $this->issue->priority,
            'message' => "Nuevo issue '{$this->issue->title}' (prioridad: {$this->issue->priority})",
        ];
    }

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