<?php
namespace ModulesGarden\ProxmoxAddon\App\Traits\Cloud;

use MGProvision\Proxmox\v2\repository\FileRepository;
use ModulesGarden\ProxmoxAddon\App\Models\CloudInitScript;
use ModulesGarden\ProxmoxAddon\App\Models\VmModel;
use ModulesGarden\ProxmoxAddon\App\Repositories\ServerConfigurationRepository;


trait SnippetTrait
{

    public function hasSnippet(){
        $ids = $this->configuration()->getCloudInitScript();
        if(!$ids || !CloudInitScript::ofIds($ids)->count()){
            return false;
        }
        return $ids && !empty($ids);
    }

    /**
     * @return \MGProvision\Proxmox\v2\models\File|null
     */
    public function getSnippetFiles(VmModel $vmModel){

        $serverConfiguration = new ServerConfigurationRepository($this->getWhmcsParamByKey('serverid'));
        if(!$serverConfiguration->snippetDirectory || !$serverConfiguration->snippetStorage){
            return;
        }
        $fileRepository = new FileRepository();
        $fileRepository->findByNodes([$vmModel->node])
            ->findSnippetsByVmModel($vmModel)
            ->findByStorages([$serverConfiguration->snippetStorage]);
        return $fileRepository->fetch();

    }
}