package zone

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

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

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

	// Hypervisor similarity in percentage
	// Default: 70
	// Required: false
	HypervisorSimilarityInPercentage uint64 `url:"hypervisor_similarity_in_percentage,omitempty" json:"hypervisor_similarity_in_percentage,omitempty"`
}

// AddCPUAlignmentProfile adds CPU alignment profile to zone
func (e Zone) AddCPUAlignmentProfile(ctx context.Context, req AddCPUAlignmentProfileRequest) ([]CpuAlignmentProfile, error) {
	res, err := e.AddCPUAlignmentProfileRaw(ctx, req)
	if err != nil {
		return nil, err
	}

	var profiles []CpuAlignmentProfile

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

	return profiles, nil
}

// AddCPUAlignmentProfileRaw adds CPU alignment profile to zone and returns the result as an array of bytes
func (e Zone) AddCPUAlignmentProfileRaw(ctx context.Context, req AddCPUAlignmentProfileRequest) ([]byte, error) {
	err := validators.ValidateRequest(req)
	if err != nil {
		return nil, validators.ValidationErrors(validators.GetErrors(err))
	}

	url := "/cloudbroker/zone/add_cpu_alignment_profile"

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