package node

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

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

// GetNetworkInfoRequest struct to get network information of a node
type GetNetworkInfoRequest struct {
	// Node ID
	// Required: true
	NodeID uint64 `url:"node_id" json:"node_id" validate:"required"`
}

// GetNetworkInfo gets network information of a node as a RecordNodeNetworkInfo struct
func (n Node) GetNetworkInfo(ctx context.Context, req GetNetworkInfoRequest) (*RecordNodeNetworkInfo, error) {
	res, err := n.GetNetworkInfoRaw(ctx, req)
	if err != nil {
		return nil, err
	}

	info := RecordNodeNetworkInfo{}

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

	return &info, nil
}

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

	url := "/cloudbroker/node/get_network_info"

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