<?php

declare(strict_types=1);

namespace App\Console\Commands;

use App\Services\ArtworkSearchIndexer;
use Illuminate\Console\Command;

class RebuildArtworkSearchIndex extends Command
{
    protected $signature   = 'artworks:search-rebuild {--chunk=500 : Number of artworks per chunk}';
    protected $description = 'Re-queue all artworks for Meilisearch indexing (non-blocking, chunk-based).';

    public function __construct(private readonly ArtworkSearchIndexer $indexer)
    {
        parent::__construct();
    }

    public function handle(): int
    {
        $chunk = (int) $this->option('chunk');

        $this->info("Dispatching index jobs in chunks of {$chunk}…");
        $this->indexer->rebuildAll($chunk);
        $this->info('All jobs dispatched. Workers will process them asynchronously.');

        return self::SUCCESS;
    }
}
