<?php

namespace App\Console\Commands\Report;

use Illuminate\Support\Facades\Artisan;
use Log;

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

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

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

    /**
     * Execute the console command.
     *
     * @return int
     */
    public function handle()
    {
        $start = microtime(true);

        // Track ongoing carpet bombs

        Artisan::call('report-attack:carpet-ongoing');

        $time_carpet_ongoing = microtime(true) - $start;

        // Track new carpet bombs

        Artisan::call('report-attack:carpet-new');

        $time_carpet_new = microtime(true) - $start;

        // Enrich the carpet bombs

        Artisan::call('report-attack:carpet-enrich');

        $time_enrich = microtime(true) - $start;

        // Track whether carpet bombs have started, useful for notifications

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

        $time_start = microtime(true) - $start;

        // Track whether carpet bombs have finished.

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

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

            \Log::debug(
                "Carpet collection exceeded desired execution time.\n" .
                "Ongoing Carpet Collection: $time_carpet_ongoing\n" .
                "New Carpet Collection: $time_carpet_new\n" .
                "Carpet Enrichment: $time_enrich\n" .
                "Start Tracking: $time_start\n" .
                "Completion Time: $time_taken"
            );

        }

        return 0;
    }
}