<?php

namespace App\Jobs;

use App\Models\NotificationDelivery;
use App\Services\Notification\NotificationManager;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Queue\Queueable;

class ProcessNotificationDelivery implements ShouldQueue
{
    use Queueable;

    public int $tries = 5;

    public function __construct(public int $deliveryId) {}

    public function backoff(): array
    {
        return [60, 300, 900, 3600];
    }

    public function handle(NotificationManager $manager): void
    {
        $delivery = NotificationDelivery::find($this->deliveryId);
        if (! $delivery || in_array($delivery->status, ['sent', 'delivered', 'read', 'cancelled', 'skipped'], true)) {
            return;
        }
        $manager->deliver($delivery);
    }
}
