package prometheus

import (
	"context"
	"encoding/json"
	"net/http"

	"repository.basistech.ru/BASIS/dynamix-golang-sdk/v14/internal/validators"
)

type ComputeMemoryUnusedRequest struct {
	// Compute ID
	// Required: true
	ComputeID uint64 `url:"computeId" json:"computeId" validate:"required"`

	// Time to loads of statistic in seconds
	// Required: false
	ForLast uint64 `url:"forLast,omitempty" json:"forLast,omitempty"`

	// The reading interval in seconds
	// Required: false
	Step uint64 `url:"step,omitempty" json:"step,omitempty"`
}

// Unused Memory
func (p Prometheus) ComputeMemoryUnused(ctx context.Context, req ComputeMemoryUnusedRequest) (*PrometheusData, error) {
	res, err := p.ComputeMemoryUnusedRaw(ctx, req)
	if err != nil {
		return nil, err
	}

	info := PrometheusData{}

	err = json.Unmarshal(res, &info)
	if err != nil {
		return nil, err
	}

	return &info, nil
}

// GetRaw gets information about compute as an array of bytes
func (p Prometheus) ComputeMemoryUnusedRaw(ctx context.Context, req ComputeMemoryUnusedRequest) ([]byte, error) {
	err := validators.ValidateRequest(req)
	if err != nil {
		return nil, validators.ValidationErrors(validators.GetErrors(err))
	}

	url := "/cloudapi/prometheus/computeMemoryUnused"

	res, err := p.client.DecortApiCall(ctx, http.MethodPost, url, req)
	return res, err
}
