<?php

namespace App\Console\Commands\Report;

use Illuminate\Support\Facades\Artisan;

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

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Runs attack notification lifecycle';

    /**
     * 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;
        }

        $start = microtime(true);

        Artisan::call('attack-track:start');

        $time_start = microtime(true) - $start;

        Artisan::call('attack-track:completion');

        $time_finish = microtime(true) - $start;

        Artisan::call('report-pend:singular');

        $time_singular = microtime(true) - $start;

        Artisan::call('attack-queue:state');
    
        $time_state = microtime(true) - $start;

        Artisan::call('report-queue:singular');

        $time_queue_singular = microtime(true) - $start;

        Artisan::call('hook:report-send');

        if(($time_taken = microtime(true) - $start) && $time_taken > 30) {

            $this->log_error("Command has exceeded desired time.", [
                'start'             => $time_start,
                'completion'        => $time_finish,
                'pend-singular'     => $time_singular,
                'queue-state'       => $time_state,
                'queue-singular'    => $time_queue_singular,
                'send'              => $time_taken,
                'total'             => $time_taken
            ]);

        }

        return 0;
    }
}