<?php
/**********************************************************************
 * ProxmoxVPS developed. (26.03.19)
 * *
 *
 *  CREATED BY MODULESGARDEN       ->       http://modulesgarden.com
 *  CONTACT                        ->       contact@modulesgarden.com
 *
 *
 * This software is furnished under a license and may be used and copied
 * only  in  accordance  with  the  terms  of such  license and with the
 * inclusion of the above copyright notice.  This software  or any other
 * copies thereof may not be provided or otherwise made available to any
 * other person.  No title to and  ownership of the  software is  hereby
 * transferred.
 *
 *
 **********************************************************************/

namespace ModulesGarden\Servers\ProxmoxVps\App\Http\Actions;

use MGProvision\Proxmox\v2\ProxmoxApiException;
use ModulesGarden\ProxmoxAddon\App\Jobs\Vps\Agent\ChangePasswordJob;
use ModulesGarden\ProxmoxAddon\App\Jobs\Vps\RebootVmJob;
use ModulesGarden\ProxmoxAddon\App\Models\Job;
use ModulesGarden\ProxmoxAddon\App\Services\ApiService;
use ModulesGarden\ProxmoxAddon\App\Services\Vps\AgentService;
use ModulesGarden\ProxmoxAddon\App\Services\Vps\ProductService;
use ModulesGarden\Servers\ProxmoxVps\App\Enum\CustomField;
use ModulesGarden\Servers\ProxmoxVps\App\Helpers\AppParams;
use ModulesGarden\Servers\ProxmoxVps\App\Helpers\ProxmoxAddonValidator;
use ModulesGarden\Servers\ProxmoxVps\Core\App\Controllers\Instances\AddonController;
use function ModulesGarden\ProxmoxAddon\Core\Helper\queue;
use ModulesGarden\Servers\ProxmoxVps\Core\UI\Traits\WhmcsParams;

class ChangePassword extends AddonController
{
    use WhmcsParams;
    use ProductService;
    use ApiService;

    public function execute($params = null)
    {
        if(!ProxmoxAddonValidator::isInstalled()){
            return ProxmoxAddonValidator::failAsString();
        }
        (new AppParams())->initFromWhmcsParams();
        try
        {
            //cloud init
            if($this->configuration()->isCloudInit()){
                //KVM only
                $container               = [];
                if($this->getWhmcsCustomField(CustomField::CI_USER)){
                    $container['ciuser']     = $this->getWhmcsCustomField(CustomField::CI_USER);
                }else{
                    $container['ciuser']     = $this->configuration()->getCiuser() ? $this->configuration()->getCiuser() : $params['username'];
                }
                $container['cipassword'] = $params['password'];
                $this->vm()->updateConfig($container);
                //restart
                if ($this->configuration()->isRebootVmAfterChangePackage() &&
                    $this->vm()->isRunning() &&
                    !Job::waiting()->ofJob(RebootVmJob::class)->ofHostingId($this->getWhmcsParamByKey("serviceid"))->count())
                {
                    queue(RebootVmJob::class, [], null, "hosting", $this->getWhmcsParamByKey("serviceid"));
                }
            }
            if ($this->configuration()->isAgent() && $this->configuration()->isAgentServicePassword()){
                try{
                    if(!$this->vm()->isRunning()) {
                        throw new ProxmoxApiException("VM not running");
                    }
                    $agent = new AgentService();
                    $this->vm()->agent()->ping();
                    $agent->passwordUpdate();
                }catch (ProxmoxApiException $ex){
                    queue(ChangePasswordJob::class, [], null, "hosting", $this->getWhmcsParamByKey("serviceid"));
                }
            }
            return "success";
        }
        catch (\Exception $ex)
        {
            return $ex->getMessage();
        }
    }
}