<?php

namespace App\Console\Commands\Utility;

use App\Console\Commands\BaseCommand;

use App\Models\Prefixes;

use App\Models\Goliath\PrefixGoliathFlag;
use App\Models\Goliath\PrefixGoliathInline;
use App\Models\Goliath\PrefixGoliathLoose;

class PrefixForceOah extends BaseCommand
{
    protected $signature = 'prefix:force-oah';

    protected $description = 'Forces Origin Aware Heuristics for all prefixes that have not had any mitigation changes made for L1.';

    public function handle()
    {

        $identity_set_all = Prefixes::all()->pluck('id');

        $identity_set_a = PrefixGoliathFlag::whereIn('prefix_id', $identity_set_all)->whereNull('created_at')->whereNull('updated_at')->get()->pluck('prefix_id');
        $identity_set_b = PrefixGoliathInline::whereIn('prefix_id', $identity_set_all)->whereNull('created_at')->whereNull('updated_at')->get()->pluck('prefix_id');
        $identity_set_c = PrefixGoliathLoose::whereIn('prefix_id', $identity_set_all)->get()->pluck('prefix_id');

        $oah_inserts = [];

        foreach(Prefixes::whereIn('id', $identity_set_a)->whereIn('id', $identity_set_b)->whereNotIn('id', $identity_set_c)->get()->pluck('id') as $x) {
            $oah_inserts[] = [
                'prefix_id' => $x,
                'loose' => 1
            ];
        }

        PrefixGoliathLoose::insert($oah_inserts);

        return 0;
    }
}
