<?php


namespace ModulesGarden\ProxmoxAddon\App\Models;


use ModulesGarden\ProxmoxAddon\Core\Models\ExtendedEloquentModel;

/**
 * @property int $id
 * @property int $hosting_id
 * @property int $vn_id
 * @property int $vm_id
 * @property string $ip
 * @property int $ip_long
 * @property string $net
 * @property VirtualNetwork $virtualNetwork
 * @property VmModel $vmModel
 * @method static $this ofHostingId($hostingId)
 * @method $this ofVnId($hostingId)
 * @method $this ofVmId($id)
 * @method $this ofIp($ip)
 * @method $this notId($ids)
 * @method $this ofNetEmpty($ids)
 * @method $this ofNet($net)
 * @method $this ofVmIds($ids)
 * @method $this notPublic()
 */
class VirtualInterface extends ExtendedEloquentModel
{

    /** @var string */
    protected $table = 'VirtualInterface';
    protected $fillable = [ 'hosting_id','vn_id', 'vm_id', 'ip', 'ip_long','net',
    ];

    public $timestamps = false;

    public function scopeOfHostingId($query, $hostingId)
    {
        return $query->where("hosting_id", $hostingId);
    }

    public function scopeOfVirtualNetworkId($query, $networkId)
    {
        return $query->where("vn_id", $networkId);
    }

    public function scopeOfVmId($query, $id)
    {
        return $query->where("vm_id", $id);
    }

    public function scopeOfVnId($query, $id)
    {
        return $query->where("vn_id", $id);
    }

    public function scopeOfIp($query, $ip)
    {
        return $query->where("ip", $ip);
    }

    public function virtualNetwork()
    {
        return $this->belongsTo(VirtualNetwork::class, "vn_id");
    }

    public function vmModel()
    {
        return $this->belongsTo(VmModel::class, "vm_id");
    }

    public function scopeNotId($query, $ips)
    {
        return $query->whereNotIn("id", $ips);
    }

    public function scopeOfNetEmpty($query)
    {
        return $query->where("net", "");
    }

    public function scopeOfNet($query , $net)
    {
        return $query->where("net", $net);
    }

    public function scopeOfVmIds($query, $ids)
    {
        return $query->whereIn("vm_id", $ids);
    }

    public function scopeNotPublic($query)
    {
        return $query->where("vn_id", "!=", "0");
    }
}