<?php


namespace ModulesGarden\ProxmoxAddon\App\Models;


use ModulesGarden\ProxmoxAddon\Core\Models\ExtendedEloquentModel;

/**
 * Class VmModel
 * @package ModulesGarden\ProxmoxAddon\App\Models
 * @property  int $id
 * @property int $hosting_id
 * @property string $node
 * @property int $vmid
 * @property string $virtualization
 * @property string $name
 * @property string $password
 * @property int $cores
 * @property int $sockets
 * @property int $vcpus
 * @property string $cpulimit
 * @property int $cpuunits
 * @property int $memory
 * @property int $swap
 * @property int $netin
 * @property int $netout
 * @property int $template
 * @property int $disk
 * @property int $disks
 * @property string $updated_at
 * @property string $created_at
 * @method static $this ofHostingId($hostingId)
 * @method $this ofVmid($vmid)
 * @method $this ofNode($node)
 * @method $this ofId($id)
 * @method $this notVmid($vmid)
 * @method $this notIdIn($ids)
 * @property VirtualInterface[] $virtualInterfaces
 * @property VmIpAddress[] $ipv4Addresses
 * @property VmIpAddress[] $ipv6Addresses
 * @property KeyPair $keyPair
 * @method $this ofTemplate()
 * @method $this notTemplate()
 */
class VmModel extends ExtendedEloquentModel
{

    /** @var string */
    protected $table = 'Vm';
    protected $fillable = [ 'hosting_id','node', 'vmid','virtualization', 'name', 'password', 'cores','sockets',
        'vcpus','cpulimit','cpuunits','memory','disk','swap','netin','netout','template', 'data',
        'disks','created_at',
        'updated_at'
    ];

    /**
     * The attributes that should be cast to native types.
     *
     * @var array
     */
    protected $casts = [
        'data' => 'array',
    ];

    public function setPassword($password)
    {
        $this->password = encrypt($password);
        return $this;
    }

    public function getPassword()
    {
        return decrypt($this->password);
    }

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

    public function scopeOfVmid($query, $vmid)
    {
        return $query->where("vmid", $vmid);
    }

    public function scopeNotVmid($query, $vmid)
    {
        return $query->where("vmid", "!=", $vmid);
    }

    public function scopeOfNode($query, $node)
    {
        return $query->where("node", $node);
    }

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

    public function virtualInterfaces()
    {
        return $this->hasMany(VirtualInterface::class, "vm_id");
    }

    public function keyPair(){
        return $this->hasOne(KeyPair::class, "vm_id");
    }

    public function ipv4Addresses(){
        return $this->hasMany(VmIpAddress::class, "vm_id")->ofIp4();
    }

    public function ipv6Addresses(){
        return $this->hasMany(VmIpAddress::class, "vm_id")->ofIp6();
    }

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

    public function scopeOfTemplate($query)
    {
        return $query->where("template", 1);
    }

    public function scopeNotTemplate($query)
    {
        return $query->where("template", 0);

    }
}