package compute

import (
	"context"
	"net/http"
	"strconv"

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

type ChangeQoSPoliciesRequest struct {
	// Compute ID
	// Required: true
	ComputeID uint64 `url:"compute_id" json:"compute_id" validate:"required"`

	// Interface IP or MAC address
	// Required: true
	Interface string `url:"interface" json:"interface" validate:"required"`

	// List of QoS Policy IDs to assign to this interface
	// Required: false
	QoSPolicyIDs []uint64 `url:"qos_policy_ids,omitempty" json:"qos_policy_ids,omitempty"`
}

type wrapperChangeQoSPoliciesRequest struct {
	ChangeQoSPoliciesRequest

	AsyncMode bool `url:"asyncMode"`
}

// ChangeQoSPolicies change qos policies for compute
func (c Compute) ChangeQoSPolicies(ctx context.Context, req ChangeQoSPoliciesRequest) (bool, error) {
	err := validators.ValidateRequest(req)
	if err != nil {
		return false, validators.ValidationErrors(validators.GetErrors(err))
	}

	reqWrapped := wrapperChangeQoSPoliciesRequest{
		ChangeQoSPoliciesRequest: req,
		AsyncMode:                false,
	}

	url := "/cloudbroker/compute/change_qos_policies"

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

	result, err := strconv.ParseBool(string(res))
	if err != nil {
		return false, err
	}

	return result, nil
}

// ChangeQoSPolicies change qos policies for compute with AsyncMode
func (c Compute) ChangeQoSPoliciesAsync(ctx context.Context, req ChangeQoSPoliciesRequest) (bool, error) {
	err := validators.ValidateRequest(req)
	if err != nil {
		return false, validators.ValidationErrors(validators.GetErrors(err))
	}

	reqWrapped := wrapperChangeQoSPoliciesRequest{
		ChangeQoSPoliciesRequest: req,
		AsyncMode:                true,
	}

	url := "/cloudbroker/compute/change_qos_policies"

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

	result, err := strconv.ParseBool(string(res))
	if err != nil {
		return false, err
	}

	return result, nil
}
