<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;
use Elasticsearch\Client;
use App\Modules\Stock\Stock;
use App\Console\Commands\AbstractCommand;
use App\Models\ClientBuilder;

class StockSearchCommand extends AbstractCommand
{
    /**
     * The console command name.
     *
     * @var string
     */
    protected $name = 'stock:search';

    protected $command_title = 'StockSearch';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Clear the stock cache table and reindex them again.';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
        $this->elasticsearch = \Elasticsearch\ClientBuilder::create()
            ->setHosts(['localhost:9200'])
            ->build();
    }

    /**
     * Execute the console command.
     *
     * @return void
     */
    public function fire()
    {
        $this->info('Emptying the stock index and adding all the entries in the stock table.');

        // Delete all the entries in the stock index
        $this->elasticsearch->indices()->delete(['index' => 'stock']);
        $this->elasticsearch->indices()->create([
                'index' => 'stock',
                'body'  => [
                    'settings' => [
                        'analysis' => [
                            'filter' => [
                                'nGram_filter'     => [
                                        'type'        => 'nGram',
                                        'min_gram'    => 1,
                                        'max_gram'    => 20,
                                        'token_chars' => [
                                            'letter',
                                            'digit',
                                            'punctuation',
                                            'symbol',
                                        ],
                                ],
                            ],
                            'analyzer' => [
                                'nGram_analyzer' => [
                                    'type'         => 'custom',
                                    'tokenizer'    => 'whitespace',
                                    'filter'       => [
                                            'standard',
                                            'lowercase',
                                            'stop',
                                            'kstem',
                                            'asciifolding',
                                            'nGram_filter',
                                    ],
                                ],
                                'whitespace_analyzer' => [
                                   'type'          => 'custom',
                                   'tokenizer'     => 'whitespace',
                                   'filter'        => [
                                        'lowercase',
                                        'asciifolding',
                                   ],
                                ],
                            ],
                        ],
                    ],

                    'mappings' => [
                        'item' => [
                            '_all' => [
                                'index_analyzer'  => 'nGram_analyzer',
                                'search_analyzer' => 'whitespace_analyzer',
                            ],
                            'properties' => [
                                'name' => [
                                        'type'  => 'string',
                                        'index' => 'not_analyzed',
                                ],
                                'model_number' => [
                                        'type'  => 'string',
                                        'index' => 'not_analyzed',
                                ],
                            ],

                        ],
                    ],
                ],
            ]);
        Stock::chunk(200, function ($stock) {
            foreach ($stock as $s) {
                $this->elasticsearch->index([
                    'index' => 'stock',
                    'type'  => 'item',
                    'id'    => $s->id,
                    'body'  => [
                        'name'            => $s->name,
                        'model_number'    => $s->model_number,
                    ],
                ]);
            }
        });

        $this->info('Done!');
    }

    /**
     * Get the console command arguments.
     *
     * @return array
     */
    protected function getArguments()
    {
        return [

        ];
    }

    /**
     * Get the console command options.
     *
     * @return array
     */
    protected function getOptions()
    {
        return [

        ];
    }
}
