<?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\ProxmoxCloudVps\App\Http\Actions;

use MGProvision\Proxmox\v2\models\Kvm;
use MGProvision\Proxmox\v2\models\User;
use MGProvision\Proxmox\v2\VmFactory;
use ModulesGarden\ProxmoxAddon\App\Services\ApiService;
use ModulesGarden\ProxmoxAddon\App\Services\Cloud\ProductService;
use ModulesGarden\ProxmoxAddon\App\Services\Cloud\UserService;
use ModulesGarden\ProxmoxAddon\App\Services\CloudService;
use ModulesGarden\Servers\ProxmoxCloudVps\App\Helpers\AppParams;
use ModulesGarden\Servers\ProxmoxCloudVps\App\Helpers\ProxmoxAddonValidator;
use ModulesGarden\Servers\ProxmoxCloudVps\Core\App\Controllers\Instances\AddonController;
use ModulesGarden\Servers\ProxmoxCloudVps\Core\UI\Traits\WhmcsParams;

class SuspendAccount extends AddonController
{
    use ProductService;
    use ApiService;
    use UserService;
    use WhmcsParams;

    public function execute($params = null)
    {
        if(!ProxmoxAddonValidator::isInstalled()){
            return ProxmoxAddonValidator::failAsString();
        }
        try
        {
            (new AppParams())->initFromParams($params);
            $cloud = new CloudService();
            //User suspend
            $user        = $this->getUser();
            $userService = new User("{$user->username}@{$user->realm}");
            $userService->setApi($this->api());
            $permissions = $userService->permissions();
            //get vmids
            $vmids = $cloud->getVmids();
            foreach ($vmids as $vmid){
                foreach ($permissions as $p)
                {
                    if ($p['path'] == "/vms/" . $vmid && $p['ugid'] == $userService->getUserid())
                    {
                        $userService->updatePermission($vmid, $p['roleid'], 1);
                    }
                }
            }
            //disable user
            $userService->disable();
            //VM suspend
            $suspensionAction = $this->configuration()->getSuspensionAction();
            foreach ($cloud->getVmModels() as $vmModel){
                $vm = (new VmFactory())->fromVmModel($vmModel);
                $vm->updateConfig(["onboot" => 0]);
                if (!$vm->isRunning())
                {
                    continue;
                }
                if ($suspensionAction && method_exists($vm, $suspensionAction))
                {
                    $vm->{$suspensionAction}();
                    continue;
                }
                if ($vm instanceof Kvm)
                {
                    $vm->suspend();
                    continue;
                }
                if ($vm->isRunning())
                {
                    $vm->stop();
                }
            }
            return "success";

        }
        catch (\Exception $ex)
        {
            return $ex->getMessage();
        }
    }
}