<?php
/* * ********************************************************************
*  ProxmoxVPS Product 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.
*
*
* ******************************************************************** */

use ModulesGarden\ProxmoxAddon\App\Models\Whmcs\Product;


$hookManager->register(
    function ($args)
    {
        if(! \ModulesGarden\ProxmoxAddon\App\Models\ModuleSetting::where("setting","duplicateProduct")->count()){
            return;
        }
        if(!file_exists(\ModulesGarden\ProxmoxAddon\Core\ModuleConstants::getFullPathWhmcs('modules', 'servers','proxmoxVPS', 'core') . DIRECTORY_SEPARATOR . 'Bootstrap.php')){
            return;
        }

        $file   = \ModulesGarden\ProxmoxAddon\Core\ModuleConstants::getFullPathWhmcs('modules', 'servers','proxmoxVPS', 'core') . DIRECTORY_SEPARATOR . 'Bootstrap.php';
        if(!file_exists($file))
        {
            return;
        }

        require_once $file;

        $provider = new \ModulesGarden\Servers\ProxmoxVps\App\UI\Admin\Product\Providers\ProductProvider($args['pid']);
        /**
         * @var $setting ModuleSetting
         */
        $setting = \ModulesGarden\ProxmoxAddon\App\Models\ModuleSetting::where("setting","duplicateProduct")->firstOrFail();
        $setting->value = json_decode(  $setting->value , true);
        if (!$provider->isSupportedModule())
        {
            $setting->delete();
            return;
        }elseif(!Product::where("id", $args['pid'])->where("name", $setting->value['newproductname'])->count() ){
            return;
        }
        $setting->delete();
        $provider->replicate($setting->value['existingproduct']);
    }, 1001);
