<?php

namespace App\Console\Commands\Network;

use App\Console\Commands\BaseCommand;
use App\Helpers\Utility\Arista;
use App\Models\BgpReroute;
use Carbon\Carbon;

class AristaUnroute extends BaseCommand
{

    protected $signature = 'arista:unroute';

    protected $description = 'Generate ACL and push to Arista\'s.';

    protected $notEnterprise = true;

    public function handle()
    {

        // Select routes to eliminate - These are non permanent routes 
        $expired = BgpReroute::select(
            'id',
            'address',
            'expiration_at',
            'approved_at',
            'city',
            'provider',
            'router'
        )->where('expiration_at', '<', Carbon::now())->whereNull('approved_at')->get();

        foreach($expired as $x) {
            Arista::route_remove($x->address, $x->city, $x->provider, $x->router);
        }

        BgpReroute::whereIn('id', $expired->pluck('id'))->delete();

        return 0;
    }
}