<?php

namespace App\Console\Commands;

use App\Facades\Settings;
use App\Modules\Stock\AutoGrab;
use Illuminate\Console\Command;

/**
 * One-off onboarding seed from the AutoGrab Backfill feed set (SFSSA-114).
 *
 * The recurring daily Reconcile is not run here: it is handled by the scheduled stock-provider
 * workflow (sync:vehicles -> RunScheduledSyncTask -> AutoGrab::runScheduledImport). This command
 * only exists to load the one-off Backfill partition when onboarding a tenant.
 */
class ImportAutoGrabFeed extends Command
{
    protected $signature = 'autograb:import-feed {--set=backfill : Feed set to process (backfill only)}';

    protected $description = 'One-off import of the AutoGrab Backfill feed set (onboarding seed)';

    public function handle(): int
    {
        if ((string) $this->option('set') !== 'backfill') {
            $this->error(
                "Only 'backfill' is available here; the daily reconcile runs in the scheduled stock sync."
            );
            return self::FAILURE;
        }

        if (!Settings::get('stock-autograb-enabled', false)) {
            $this->info('AutoGrab stock is disabled for this tenant; skipping backfill.');
            return self::SUCCESS;
        }

        $dispatched = new AutoGrab()->importBackfill();

        $this->info("AutoGrab backfill: {$dispatched} row(s) dispatched for processing on the batch queue.");

        return self::SUCCESS;
    }
}
