<?php

namespace App\Jobs;

use App\Services\AutoGrab\AutoGrabFeedImporter;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;

/**
 * Processes one batch of AutoGrab Parquet feed rows on the shared stock batch queue.
 *
 * The provider (App\Modules\Stock\AutoGrab) reads the downloaded snapshot, splits it into
 * batches and dispatches one of these jobs per batch, so the heavy per-row upsert work runs
 * on queue workers rather than in the scheduled sync task itself.
 */
class ImportAutoGrabFeedBatch implements ShouldQueue
{
    use Dispatchable;
    use InteractsWithQueue;
    use Queueable;
    use SerializesModels;

    public int $tries = 2;

    /**
     * @param array<int, array<string, mixed>> $rows
     */
    public function __construct(private readonly array $rows)
    {
        $this->onQueue('motors-batch');
    }

    public function handle(AutoGrabFeedImporter $importer): void
    {
        $importer->importBatch($this->rows);
    }
}
