<?php

namespace App\Console\Commands;

use App\Models\Client;
use App\Models\Pro;
use App\Models\StatTree;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Http;

class InstantiateStatTree extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'stat-tree:instantiate {slug} {pro-class}';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Instantiate Stat Tree For Multiple Pros';

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

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

        // load the tree from slug and ensure it is a template
        /** @var StatTree $statTree */
        $statTree = StatTree::where('slug', $argv[2])->first();
        if(!$statTree) {
            $this->error("Tree with specified slug does not exist!");
            return 0;
        }
        if(!$statTree->is_template) {
            $this->error("Tree with specified slug is not a template!");
            return 0;
        }

        // check pro-class and get pro uids
        $this->info($argv[3]);

        $pros = Pro::where('is_active', TRUE)->whereRaw($argv[3])->get();
        $this->info(count($pros) . " pros found.");

        $this->info("Instantiating...");
        $doneCount = 0;
        if(count($pros) > 0) {
            foreach ($pros as $pro) {
                $statTree->instantiateForPro($pro);
                $doneCount++;
                if($doneCount % 50 == 0) {
                    $this->info("$doneCount / " . count($pros) . " completed.");
                }
            }
            $this->info("$doneCount / " . count($pros) . " completed.");
        }

    }

}
