<?php

namespace Modules\ProductCompat\Jobs;

use App\Product;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Modules\ProductCompat\Entities\Services\EnrichmentManager;

/**
 * Enrich one product. Queued (ShouldQueue) so a catalog run can be spread over
 * a worker; with QUEUE_CONNECTION=sync it simply runs inline.
 */
class EnrichProductJob implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

    public int $productId;

    public function __construct(int $productId)
    {
        $this->productId = $productId;
    }

    public function handle(EnrichmentManager $manager): void
    {
        $product = Product::find($this->productId);
        if ($product) {
            $manager->enrichProduct($product);
        }
    }
}
