<?php


namespace ModulesGarden\ProxmoxAddon\App\Factory;


use ModulesGarden\ProxmoxAddon\App\Helper\Logger;
use ModulesGarden\ProxmoxAddon\App\Models\ModuleSettings;
use ModulesGarden\ProxmoxAddon\App\Repositories\ModuleSettingRepository;
use ModulesGarden\ProxmoxAddon\App\Repositories\ServerConfigurationRepository;
use ModulesGarden\ProxmoxAddon\App\Services\ProxyService;
use ModulesGarden\ProxmoxAddon\App\Services\RestFullApi;

class ProxyServiceFactory
{

    /**
     * @param ModuleSettingRepository $modConf
     * @return ProxyService
     */
    public function fromModuleConfiguration(ModuleSettingRepository $modConf){
        if(!$modConf->consoleHost){
            throw  new \InvalidArgumentException("Console Host is empty");
        }
        if(!$modConf->getConsoleApiKey()){
            throw  new \InvalidArgumentException("Console API Key is empty");
        }
        $host = $modConf->consoleHost;
        if(!preg_match("/http/", $host)){
            $host ="https://".$host;
        }
        $api = new RestFullApi($host);
        //debug
        if(ModuleSettings::isDebug()){
            $api->setLogger(new Logger('ProxmoxVps',[$modConf->getConsoleApiKey()]));
        }
        return new ProxyService($modConf->getConsoleApiKey(), $modConf->consoleHost, $api);

    }

}