<?php

namespace ModulesGarden\Servers\ProxmoxVps\App\UI\Graph\Pages;

use ModulesGarden\ProxmoxAddon\App\Services\ApiService;
use ModulesGarden\ProxmoxAddon\App\Services\Vps\ProductService;
use ModulesGarden\ProxmoxAddon\Core\Models\ModuleSettings\Model;
use function ModulesGarden\Servers\ProxmoxVps\Core\Helper\sl;

trait GraphData
{
    use ProductService;
    use ApiService;

    protected $timeframe = "week";

    public function chartData()
    {
        $request = [
            "timeframe" => $this->timeframe,
            "cf"        => "MAX",
        ];
        return $this->vm()->rrdData($request);
    }

    protected function chartOptions()
    {
        $registrationDate = new \DateTime($this->getWhmcsParamByKey('model')->registrationDate->format("Y-m-d"));
        $options          = ['hour' => sl("lang")->tr("Hour")];
        $dnow             = new \DateTime();
        $dDiff            = $registrationDate->diff($dnow);
        if ($dDiff->days >= 1)
        {
            $options['day'] = sl("lang")->tr("Day");
        }
        if ($dDiff->days >= 7)
        {
            $options['week'] = sl("lang")->tr("Week");
        }
        if ($dDiff->days >= 30)
        {
            $options['month'] = sl("lang")->tr("Month");
        }
        if ($dDiff->y >= 1)
        {
            $options['year'] = sl("lang")->tr("year");
        }
        return $options;
    }

    protected function loadSettings()
    {
        $this->configChartsSettings = json_decode(Model::where('setting', $this->graphSettingsKey)->first()->value);

        if ($this->configChartsSettings)
        {
            $this->setGraphFilterInfo(null, $this->configChartsSettings->start, $this->configChartsSettings->end);
        }

        return $this;
    }
}
