package resmon

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

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

type GetByNodeRequest struct {
	// Node ID
	// Required: true
	NodeID uint64 `url:"nodeId" json:"nodeId" validate:"required"`

	// Start of time period - unixtime
	// Required: false
	StartTime uint64 `url:"starttime,omitempty" json:"starttime,omitempty"`

	// End of time period - unixtime
	// Required: true
	EndTime uint64 `url:"endtime,omitempty" json:"endtime,omitempty"`
}

// Get a grid resource monitoring for the specified time period
func (r Resmon) GetByNode(ctx context.Context, req GetByNodeRequest) (*GetByNodeData, error) {
	res, err := r.GetByNodeRaw(ctx, req)
	if err != nil {
		return nil, err
	}

	info := GetByNodeData{}

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

	url := "/cloudbroker/resmon/get_by_node"

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