<?php

namespace App\Http\Middleware;

use App\Services\NewsletterSchedulerService;
use App\Services\ThemeService;
use Closure;
use Illuminate\Http\Request;
use Symfony\Component\HttpFoundation\Response;

class ProcessScheduledNewsletters
{
    public function __construct(
        protected NewsletterSchedulerService $scheduler,
        protected ThemeService $themeService
    ) {}

    public function handle(Request $request, Closure $next): Response
    {
        $response = $next($request);

        if (! $this->themeService->isPluginActive('newsletter')) {
            return $response;
        }

        $this->scheduler->sendDueCampaigns();

        return $response;
    }
}
