<?php

namespace App\Modules\Network\Helpers;

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

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

// Creatia GeoIp Module Imports
use App\Modules\Network\Models\GeoIps;
class GeoIpControllerCommons {

    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 isSubnet(GeoIps $_network) {
        return $_network->member_of_id !== null;
    }

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

    public static function countUsedSubnet($_organisation_id) {
        return GeoIps::where([['organisation_id', '=', $_organisation_id]])->whereNotNull('member_of_id')->whereNotNull('alpha2code')->count();
    }

    public static function countUsedSupernet($_organisation_id) {
        return GeoIps::where([['organisation_id', '=', $_organisation_id]])->whereNull('member_of_id')->whereNotNull('alpha2code')->count();
    }

}

?>