<?php

namespace App\Modules\Network\Commands;

use Illuminate\Support\Facades\Http;
use Log;

use App\Models\Asns;
use App\Models\Organisation;


class AsnImport extends \App\Console\Commands\BaseCommand
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'asn:import';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Import any prefixes that are not associated with known and approved ASNs';

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

    /**
     * Execute the console command.
     *
     * @return int
     */
    public function handle()
    {
        $gsl_organisation_id = Organisation::where('name', '=', 'Global Secure Layer')->get()->first()->id;

        $known_asns = Asns::all()->keyBy('asn');
        $announced_asns = array_values(array_unique(collect(Http::get("http://54.215.89.235/all.active.detailed.json")->json())->pluck('asn_originator')->toArray()));

        foreach($announced_asns as $announced_asn) {
            if(!isset($known_asns[$announced_asn])) {
                Asns::make($announced_asn, null, null, $gsl_organisation_id, true);
            }
        }

        return 0;
    }
}
