<?php

namespace App\Jobs;

use App\Models\User;
use App\Services\OneSignalService;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;

class SendPushNotification implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

    public function __construct(
        public User $user,
        public string $title,
        public string $body,
        public array $data = []
    ) {}

    public function handle(): void
    {
        if (!($this->user->notifications_enabled ?? true)) {
            return;
        }

        $service = app(OneSignalService::class);
        $service->sendToUser(
            $this->user->id,
            $this->title,
            $this->body,
            $this->data,
        );
    }
}
