<?php
namespace App\Jobs;

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

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

    protected Link $link;

    /**
     * Create a new job instance.
     */
    public function __construct(Link $link)
    {
        $this->link = $link;
    }

    /**
     * Execute the job.
     */
    public function handle(NotificationService $notifier): void
    {
        $donateur = $this->link->donateur;

        if ($donateur->email) {
            $notifier->sendEmail($this->link);
        } else {
            $this->link->update(['email_status' => 'failed']);
        }

        if ($donateur->telephone) {
            $notifier->sendWhatsapp($this->link);
        } else {
            $this->link->update(['whatsapp_status' => 'failed']);
        }
    }
}
