<?php

namespace App\Modules\Network\Hooks;

use App\Helpers\Utility\GoliathBgp;

use App\Modules\Network\Models\Goliath\PrefixGoliathFlag;
use App\Modules\Network\Models\Goliath\PrefixGoliathInline;
use App\Modules\Network\Models\Goliath\PrefixGoliathFirewall;

class NetworkHooks {
    public static function on_delete($_prefix_id) {
        // Change goliath constructs - Reset it too be default, essentially. This forces a withdrawal without risky changes to the BGP emitter.
        PrefixGoliathFlag::where('prefix_id', '=', $_prefix_id)->where(function($x) {
            $x->whereNotNull('region_id')->orWhereNotNull('subregion_id')->orWhereNotNull('metro_id');
        })->delete();

        PrefixGoliathFlag::where('prefix_id', '=', $_prefix_id)->update([
            'flag_synproxy'         => 0,
            'flag_heuristics'       => 1,
            'flag_heuristics_level' => 3,
        ]);

        PrefixGoliathInline::where('prefix_id', '=', $_prefix_id)->update([
            'forced' => 0
        ]);

        PrefixGoliathFirewall::where('prefix_id', '=', $_prefix_id)->delete();

        GoliathBgp::emit_delta([$_prefix_id]);
    }
}