<?php

namespace App\Listeners;

use App\Actions\BackfillRouteArticlesAction;
use App\Events\RouteActivated;
use App\Models\Route;
use Illuminate\Contracts\Queue\ShouldQueue;

class BackfillRouteArticlesListener implements ShouldQueue
{
    public string $queue = 'default';

    public function __construct(
        private BackfillRouteArticlesAction $backfillRouteArticles,
    ) {}

    public function handle(RouteActivated $event): void
    {
        $route = Route::query()
            ->where('feed_id', $event->feedId)
            ->where('platform_channel_id', $event->platformChannelId)
            ->first();

        if ($route === null || ! $route->is_active) {
            return;
        }

        $this->backfillRouteArticles->execute($route);
    }
}
