<?php


namespace ModulesGarden\ProxmoxAddon\App\Services;


use ModulesGarden\ProxmoxAddon\App\Enum\Vps\CustomField;
use ModulesGarden\ProxmoxAddon\App\Models\IpLog;
use ModulesGarden\ProxmoxAddon\Core\UI\Traits\WhmcsParams;

use function ModulesGarden\ProxmoxAddon\Core\Helper\sl;

class IpLogService
{

    use WhmcsParams;

    public function assigned(array  $ipAddresses){
        $ipLog = new IpLog();
        $ipLog->hosting_id =  $this->getWhmcsParamByKey('serviceid');
        $msg = count($ipAddresses) >1 ?  "IP Addresses: %s have been assigned, Client ID #%s, Hosting ID #%s" :  "IP Address: %s has been assigned, Client ID #%s, Hosting ID #%s";
        $ipLog->message = sprintf($msg, implode(", ", $ipAddresses), $this->getWhmcsParamByKey('userid') ,$ipLog->hosting_id );
        if($this->getWhmcsCustomField(CustomField::VMID)){
            $ipLog->message .= ", VMID: ".$this->getWhmcsCustomField(CustomField::VMID);
        }elseif(sl('Vm')->hasVmModel() && sl('Vm')->getVmModel()->vmid){
            $ipLog->message .= ", VMID: ".sl('Vm')->getVmModel()->vmid;
        }
        $ipLog->created_at = date("Y-m-d H:i:s");
        $ipLog->save();
        return $ipLog;
    }

    public function unassigned(array $ipAddresses){
        $ipLog = new IpLog();
        $ipLog->hosting_id =  $this->getWhmcsParamByKey('serviceid');
        $msg = count($ipAddresses) >1 ?  "IP Addresses: %s have been unassigned, Client ID #%s, Hosting ID#%s" :  "IP Address: %s has been unassigned, Client ID #%s, Hosting ID #%s";
        $ipLog->message = sprintf($msg, implode(", ", $ipAddresses ), $this->getWhmcsParamByKey('userid') ,$ipLog->hosting_id);
        if($this->getWhmcsCustomField(CustomField::VMID)){
            $ipLog->message .= ", VMID: ".$this->getWhmcsCustomField(CustomField::VMID);
        }
        elseif(sl('Vm')->hasVmModel() && sl('Vm')->getVmModel()->vmid){
            $ipLog->message .= ", VMID: ".sl('Vm')->getVmModel()->vmid;
        }
        $ipLog->created_at = date("Y-m-d H:i:s");
        $ipLog->save();
        return $ipLog;
    }
}