package hypervisors

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

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

// GetRequest struct to get information about hypervisor
type GetRequest struct {
	// Name
	// Required: true
	Name string `url:"name" json:"name" validate:"required"`

	// Port info (available options: detailed, general)
	// Required: false
	PortInfo string `url:"port_info,omitempty" json:"port_info,omitempty"`
}

// Get gets current configuration of a hypervisor as a RecordHypervisor
func (hv Hypervisors) Get(ctx context.Context, req GetRequest) (*RecordHypervisor, error) {
	res, err := hv.GetRaw(ctx, req)
	if err != nil {
		return nil, err
	}

	info := RecordHypervisor{}

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

	return &info, nil
}

// GetRaw gets information about a hypervisor as an array of bytes
func (hv Hypervisors) GetRaw(ctx context.Context, req GetRequest) ([]byte, error) {
	err := validators.ValidateRequest(req)
	if err != nil {
		return nil, validators.ValidationErrors(validators.GetErrors(err))
	}

	url := "/sdn/hypervisor/get"

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