package prometheus

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

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

const (
	ComputeCPULoad         = "computeCPUload"
	ComputeMemoryUsage     = "computeMemoryUsage"
	ComputeMemoryUsable    = "computeMemoryUsable"
	ComputeMemoryUnused    = "computeMemoryUnused"
	ComputeMemoryUsed      = "computeMemoryUsed"
	ComputeMemoryAvailable = "computeMemoryAvailable"
	ComputeReadBytes       = "computeReadBytes"
	ComputeReadRequests    = "computeReadRequests"
	ComputeReceiveBytes    = "computeReceiveBytes"
	ComputeTransmitBytes   = "computeTransmitBytes"
	ComputeTransmitPackets = "computeTransmitPackets"
	ComputeWriteBytes      = "computeWriteBytes"
	ComputeWriteRequests   = "computeWriteRequests"
)

type ComputesRequest struct {
	// List of compute IDs to fetch metrics for
	// Required: true
	ComputeIDs []uint64 `url:"computeIds" json:"computeIds" validate:"required"`

	// List of compute IDs to fetch metrics for
	// Required: true
	MetricIDs []string `url:"metricIds" json:"metricIds" 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"`

	// Number of zeros after the decimal point
	// Required: false
	DecimalPlaces uint64 `url:"decimalPlaces,omitempty" json:"decimalPlaces,omitempty"`
}

// Get multiple metrics for multiple compute instances
func (p Prometheus) Computes(ctx context.Context, req ComputesRequest) (*ComputesData, error) {
	res, err := p.ComputesRaw(ctx, req)
	if err != nil {
		return nil, err
	}

	info := ComputesData{}

	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) ComputesRaw(ctx context.Context, req ComputesRequest) ([]byte, error) {
	err := validators.ValidateRequest(req)
	if err != nil {
		return nil, validators.ValidationErrors(validators.GetErrors(err))
	}

	url := "/cloudbroker/prometheus/computes"

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