<?php


namespace ModulesGarden\ProxmoxAddon\App\Models;

/**
 * Class Job
 * @package ModulesGarden\ProxmoxAddon\App\Models
 * @method static $this waiting()
 * @method static $this finished()
 * @method $this ofJob($job)
 * @method $this ofHostingId($hostingId)
 * @method static $this ofParentId($parentId)
 * @method static $this ofId($parentId)
 * @method $this ofJobs($jobs)
 * @method $this ofCustomId($id)
 */
class Job extends \ModulesGarden\ProxmoxAddon\Core\Queue\Models\Job
{

    public function scopeOfJob($query, $job)
    {
        return $query->where("job", $job . '@handle');
    }

    public function scopeOfJobs($query, $jobs)
    {
        foreach ($jobs as &$job)
        {
            $job .= '@handle';
        }
        return $query->whereIn("job", $jobs);
    }

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

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

    public function scopeWaiting($query)
    {
        return $query->whereIn("status", ['waiting', "running", ""]);
    }

    public function scopeFinished($query)
    {
        return $query->where("status", "finished");
    }

    public function scopeOfParentId($query, $parentId)
    {
        return $query->where("parent_id", $parentId);
    }

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

}