<?php

namespace App\Listeners;

use App\Events\BlogPostCreated;
use App\Models\SubscriberModel as Subscriber;
use App\Mail\BlogPostPublishedMail;
use Illuminate\Support\Facades\Mail;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Queue\InteractsWithQueue;
class SendBlogPostToSubscribers implements ShouldQueue
{
    use InteractsWithQueue;

    public function handle(BlogPostCreated $event): void
    {
        $post = $event->post;
        $subscribers = Subscriber::where('status', 1)->get();

        foreach ($subscribers as $subscriber) {
            Mail::to($subscriber->email)
                ->send(new BlogPostPublishedMail($post, $subscriber));
        }
    }
}

