<?php

namespace App\Jobs;

use App\Models\User;
use App\Services\WebPushSender;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Queue\Queueable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;

class SendWebPushToUser implements ShouldQueue
{
    use InteractsWithQueue, Queueable, SerializesModels;

    public function __construct(
        public int $userId,
        public string $title,
        public string $body,
        public string $url = '/app',
        public string $tag = 'collabs',
    ) {}

    public function handle(WebPushSender $sender): void
    {
        $user = User::query()->find($this->userId);
        if ($user === null) {
            return;
        }

        $sender->sendToUser($user, $this->title, $this->body, $this->url, $this->tag);
    }
}
