<?php

namespace App\Console\Commands;

use App\Libs\Facades\Localization;
use App\Libs\Services\SearchService;
use App\Models\Website;
use Illuminate\Console\Command;

class RebuildSearch extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'search:rebuild {websiteId}';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Rebuilds website search';

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

    /**
     * Execute the console command.
     *
     * @return int
     */
    public function handle(SearchService $service)
    {
        $websiteId = $this->argument('websiteId');
        if (!$websiteId) {
            $this->error('You have to specify the website Id');
            return 0;
        }
        $website = Website::find($websiteId);
        if (!$website) {
            $this->error('Website not found!');
            return 0;
        }

        Localization::commandInit($website);

        $service->rebuild($website);
        return 0;
    }
}
