<?php namespace Tps\Birzha\Console;

use Illuminate\Console\Command;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Input\InputArgument;
use TPS\Birzha\Models\Product;
use TPS\Birzha\Models\Category;
use Illuminate\Support\Facades\Log;


class AddKeywordToProducts extends Command
{
    /**
     * @var string The console command name.
     */
    protected $name = 'birzha:addkeywordtoproducts';

    /**
     * @var string The console command description.
     */
    protected $description = 'Adding keyword to old products using their category,name and slugs';

    /**
     * Execute the console command.
     * @return void
     */
    public function handle()
    {
        $products = Product::orderBy('id', 'asc')->get();
        foreach($products as $product){
            $keyword = $product->name.' '.$product->slug;
            foreach($product->categories as $category){
                $keyword = $keyword.' '.$category->name.' '.$category->slug;
                if($category->primary_key > 0){
                    $keyword = $keyword.' '.$category->parent->name.' '.$category->parent->slug;

                    if($category->parent->primary_key > 0){
                        $keyword = $keyword.' '.$category->parent->parent->name.' '.$category->parent->parent->slug;
                    }
                }
            }
            $product->keyword = $keyword;
            $product->save();
        }
    }

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

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