package node

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

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

// GetRequest struct to get detailed information about node
type GetRequest struct {
	// Node ID
	// Required: true
	NID uint64 `url:"nid" json:"nid" validate:"required"`
}

// Get gets node summary as a RecordNode struct
func (n Node) Get(ctx context.Context, req GetRequest) (*RecordNode, error) {
	res, err := n.GetRaw(ctx, req)
	if err != nil {
		return nil, err
	}

	info := RecordNode{}

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

	return &info, nil
}

// GetRaw gets node summary as an array of bytes
func (n Node) GetRaw(ctx context.Context, req GetRequest) ([]byte, error) {
	err := validators.ValidateRequest(req)
	if err != nil {
		return nil, validators.ValidationErrors(validators.GetErrors(err))
	}

	url := "/cloudbroker/node/get"

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