<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;
use App\Jobs\IndexContent;
use App\Models\Artist;
use App\Models\Exhibition;
use App\Models\NewsItem;
use App\Models\Onboarding;
use App\Models\Location;

class ReindexSearch extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'contemporanea:reindex-content';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Reindexes all the contents for search index (may take a while)';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return int
     */
    public function handle()
    {

        $contentClasses = [
            'App\Models\Artist',
            'App\Models\Exhibition',
            'App\Models\Location',
            'App\Models\Collection',
            'App\Models\NewsItem',
            'App\Models\Onboarding',
        ];

        foreach ($contentClasses as $contentClass) {

            $this->info('Indexing items of: ' . $contentClass);
            $items = $contentClass::full()->get();

            foreach ($items as $item) {
                //to prevent rate limiting issues.
                sleep(1);
                IndexContent::dispatch($item, IndexContent::ACTION_ADD);
            }
        }
    }
}
