package zone

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

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

// UpdateCPUAlignmentProfileRequest struct to update CPU alignment profile of zone
type UpdateCPUAlignmentProfileRequest struct {
	// ID of zone
	// Required: true
	ZoneID uint64 `url:"zone_id" json:"zone_id" validate:"required"`

	// Minimum percentage of hypervisors that must support the selected CPU model for the balanced profile
	// Default: 70
	// Required: false
	HypervisorSimilarityInPercentage uint64 `url:"hypervisor_similarity_in_percentage,omitempty" json:"hypervisor_similarity_in_percentage,omitempty" validate:"omitempty,max=100"`
}

// UpdateCPUAlignmentProfile updates CPU alignment profile of zone
func (e Zone) UpdateCPUAlignmentProfile(ctx context.Context, req UpdateCPUAlignmentProfileRequest) ([]CpuAlignmentProfile, error) {
	err := validators.ValidateRequest(req)
	if err != nil {
		return nil, validators.ValidationErrors(validators.GetErrors(err))
	}

	url := "/cloudbroker/zone/update_cpu_alignment_profile"

	res, err := e.client.DecortApiCallCtype(ctx, http.MethodPost, url, constants.MIMEJSON, req)
	if err != nil {
		return nil, err
	}

	var profiles []CpuAlignmentProfile

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

	return profiles, nil
}
