<?php

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

use MGProvision\Proxmox\v2\VmFactory;
use ModulesGarden\ProxmoxAddon\App\Models\VmModel;
use ModulesGarden\ProxmoxAddon\App\Services\ApiService;
use ModulesGarden\ProxmoxAddon\App\Services\Cloud\ProductService;
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 UpdateTemplateVmProvider extends BaseDataProvider implements ClientArea
{
    use ProductService;
    use ApiService;

    public function read()
    {

        if (!$this->actionElementId)
        {
            return;
        }
        $this->data['id'] = $this->actionElementId;
        $vmModel = VmModel::ofHostingId($this->getWhmcsParamByKey('serviceid'))
            ->ofId($this->actionElementId)->firstOrFail();
        $this->data['name'] = $vmModel->name;
        $this->api();
        $vm = (new VmFactory())->fromVmModel($vmModel);
        $this->data['description'] =  $vm->config()['description'];
    }

    public function update()
    {
        //get vm model
        $vmModel = VmModel::ofHostingId($this->getWhmcsParamByKey('serviceid'))
            ->ofId($this->getFormDataValues()['id'])->firstOrFail();
        $this->api();
        $vm = (new VmFactory())->fromVmModel($vmModel);
        //update
        $vm->updateConfig(['description'=> $this->getFormDataValues()['description'] ]);
        return (new HtmlDataJsonResponse())
            ->setStatusSuccess()
            ->setMessageAndTranslate('The Template has been updated successfully');
    }

    public function delete()
    {
    }
}