<?php

namespace ModulesGarden\Servers\ProxmoxCloudVps\App\UI\Vms\Providers;

use MGProvision\Proxmox\v2\VmFactory;
use ModulesGarden\ProxmoxAddon\App\Models\VmModel;
use ModulesGarden\ProxmoxAddon\App\Services\CloudService;
use ModulesGarden\Servers\ProxmoxCloudVps\App\Helpers\AppParams;
use ModulesGarden\Servers\ProxmoxCloudVps\Core\UI\Interfaces\ClientArea;
use ModulesGarden\Servers\ProxmoxCloudVps\Core\UI\ResponseTemplates\HtmlDataJsonResponse;
use ModulesGarden\Servers\ProxmoxCloudVps\Core\UI\Widget\Forms\DataProviders\BaseDataProvider;

class DeleteVmProvider extends BaseDataProvider implements ClientArea
{

    public function read()
    {

        if ($this->actionElementId && $this->actionElementId != "diskDataTable")
        {
            $this->data['id'] = $this->actionElementId;
            $this->data['name'] = VmModel::where("id", $this->actionElementId)->value("name");
        }
    }

    public function update()
    {
    }

    public function delete()
    {
        //init proxmox addon params
        (new AppParams())->initFromWhmcsParams();
        //get vm model
        $vmModel = VmModel::ofHostingId($this->getWhmcsParamByKey('serviceid'))
                          ->ofId($this->formData['id'])->firstOrFail();
        //destory vm
        (new CloudService())->delete($vmModel);
        return (new HtmlDataJsonResponse())
            ->setStatusSuccess()
            ->setMessageAndTranslate('The Virtual Machine has been deleted successfully')
            ->setCallBackFunction('pmActivateCreateVMButton');
    }
}