<?php


namespace ModulesGarden\ProxmoxAddon\App\Services\Vps;


use MGProvision\Proxmox\v2\models\HaResource;
use ModulesGarden\ProxmoxAddon\App\Services\ApiService;
use ModulesGarden\ProxmoxAddon\Core\UI\Traits\WhmcsParams;


class HighAvailabilityClusterService
{
    use ProductService;
    use ApiService;
    use WhmcsParams;


    public function delete()
    {
        $haResurce = new HaResource();
        $haResurce->setSid($this->vm()->getVmid())
            ->setType($this->vm()->getVirtualization() == "lxc" ? "ct" : "vm");
        $haResurce->setApi($this->api());
        if ($haResurce->exist())
        {
            $haResurce->delete();
        }
        return $this;
    }

    public function create()
    {
        $haResurce = new HaResource();
        $haResurce->setSid($this->vm()->getVmid())
            ->setState($this->configuration()->getClusterState())
            ->setType($this->vm()->getVirtualization() == "lxc" ? "ct" : "vm")
            ->setGroup($this->configuration()->getClusterGroup())
            ->setMaxRelocate($this->configuration()->getClusterMaxRelocate())
            ->setMaxRestart($this->configuration()->getClusterMaxRestart())
            ->create();
    }

    public function exist(){
        $haResurce = new HaResource();
        $haResurce->setSid($this->vm()->getVmid())
            ->setType($this->vm()->getVirtualization() == "lxc" ? "ct" : "vm");
        $haResurce->setApi($this->api());
        return $haResurce->exist();
    }

    public function isConfigured(){
        return $this->configuration()->getClusterState() &&
                is_numeric($this->configuration()->getClusterMaxRelocate()) &&
                is_numeric($this->configuration()->getClusterMaxRestart());
    }
}