<?php

namespace App\Modules\Network\Helpers;

// Laravel Imports
use Illuminate\Support\Facades\Log;

// Creatia Core Imports
use App\Helpers\Utility\RouteList;

// Creatia Networking Module Imports
use App\Modules\Network\Models\Networks;

class ControllerCommons {

    public static function isValidSupernetContainingSubnets($_supernet, $_subnets) {
        foreach($_subnets as $subnet) {
            if(RouteList::ip_in_block($subnet, $_supernet)) {
                return true;
            }
        }

        return false;
    }

    public static function isValidSubnetInSupernets($_subnet, $_supernets) {
        return !RouteList::ip_in_blocks($_subnet, RouteList::get_excluded_blocks()) && RouteList::ip_in_blocks($_subnet, $_supernets);
    }

    public static function isValidSubnetInSupernet($_subnet, $_supernet) {
        return !RouteList::ip_in_blocks($_subnet, RouteList::get_excluded_blocks()) && RouteList::ip_in_block($_subnet, $_supernet);
    }

    public static function isInnerNetwork(Networks $_network) {
        return $_network->member_of_id !== null;
    }

    public static function isOuterNetwork(Networks $_network) {
        return $_network->member_of_id === null;
    }

    public static function canSetInnerSense(Networks $_network) {
        // Get outer network
        $outer = Networks::find($_network->member_of_id);

        return ($outer && $outer->sensitivity_id === null);
    }

    public static function canSetOuterSense(Networks $_network) {
        // Get a number of inners with sensitivities. 
        $inner_sense_count = Networks::where('member_of_id', '=', $_network->id)->whereNotNull('sensitivity_id')->count();

        return !$inner_sense_count;
    }

    public static function countUsedInner($_organisation_id) {
        return Networks::where([['organisation_id', '=', $_organisation_id]])->whereNotNull('member_of_id')->whereRaw('(sensitivity_id is not null OR ruleset_id is not null)')->count();
    }

    public static function countUsedOuter($_organisation_id) {
        return Networks::where([['organisation_id', '=', $_organisation_id]])->whereNull('member_of_id')->whereRaw('(sensitivity_id is not null OR ruleset_id is not null)')->count();
    }

}

?>