package hypervisors

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

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

// UpdateDisplayNameRequest struct to update display name for a hypervisor
type UpdateDisplayNameRequest struct {
	// Current name of the hypervisor
	// Required: true
	Name string `url:"name" json:"name" validate:"required"`

	// New display name to set
	// Required: true
	DisplayName string `url:"display_name" json:"display_name" validate:"required"`
}

// UpdateDisplayName updates display name for a hypervisor
func (hv Hypervisors) UpdateDisplayName(ctx context.Context, req UpdateDisplayNameRequest) (*RecordHypervisor, error) {
	res, err := hv.UpdateDisplayNameRaw(ctx, req)
	if err != nil {
		return nil, err
	}

	info := RecordHypervisor{}

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

	return &info, nil
}

// UpdateDisplayNameRaw update display name for a hypervisor and get its information as an array of bytes
func (hv Hypervisors) UpdateDisplayNameRaw(ctx context.Context, req UpdateDisplayNameRequest) ([]byte, error) {
	err := validators.ValidateRequest(req)
	if err != nil {
		return nil, validators.ValidationErrors(validators.GetErrors(err))
	}

	url := "/sdn/hypervisor/update_display_name"

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