<?php

namespace App\Console\Commands\Report;

use App\Models\Goliath\GoliathAttack;
use Illuminate\Support\Carbon;

class ReportAttackEnrich extends \App\Console\Commands\BaseCommand
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'report-attack:enrich';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Correlates singular attacks against Akvorado data and builds an enriched object.';

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

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

        if(env('reporting_disabled')) {
            $this->log_warning("Not running as `reporting_disabled` is set in the .env file.", []);
            return 0;
        }

        // Track start
        $start = microtime(true);

        $max = 1;

        $pipe = [];

        $base = base_path();
            
        for ($i = 0; $i < $max; ++$i) {
            $pipe[$i] = popen("php $base/artisan report-attack:enrich-thread --pool=$max --thread=$i", 'w');
        }
        
        for ($i = 0; $i < $max; ++$i) {
            pclose($pipe[$i]);
        }

        if(($time_taken = microtime(true) - $start) && $time_taken > 300) {
            $this->log_warning("Execution time exceeded five minutes: $time_taken\s", []);
        }

        // I know this is gross given time_taken is reserved in an if above this, but it does work and it's just a hack for the journal logs.
        echo "Completed in $time_taken seconds.\n";
        return 0;
    }
}