<?php


namespace ModulesGarden\ProxmoxAddon\App\Models;


use ModulesGarden\ProxmoxAddon\Core\Models\ExtendedEloquentModel;

/**
 * @property int $id
 * @property string $node
 * @property string $group_id
 * @method static ServerGroupItem  ofGroupId($id)
 * @method static ServerGroupItem  ofGroupIdsAndServerId($ids, $serverId)
 * @method ServerGroupItem ofNodeNotIn(array $nodes)
 * @method ServerGroupItem ofNode($node)
 */
class ServerGroupItem extends ExtendedEloquentModel
{

    /** @var string */
    protected $table = 'ServerGroupItem';
    protected $fillable   = ['id','node','group_id'];
    public $timestamps = false;


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

    /**
     * @param $query
     * @param $ids
     * @param $serverId
     * @return mixed
     */
    public function scopeOfGroupIdsAndServerId($query, $ids, $serverId){
        $sg = (new ServerGroup())->getTable();
        $sgi = (new ServerGroupItem())->getTable();
        $query->select("{$sgi}.*")
            ->leftJoin($sg,"{$sg}.id","=","{$sgi}.group_id")
            ->whereIn("{$sgi}.group_id", $ids)
            ->where("{$sg}.server_id", $serverId);
        return $query;
    }

    public function scopeOfNodeNotIn($query, array $nodes){
        $query->whereNotIn("node", $nodes);
    }

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