package compute

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

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

// NetQOSRequest struct for update QOS
type NetQOSRequest struct {
	// ID of compute instance
	// Required: true
	ComputeID uint64 `url:"computeId" json:"computeId" validate:"required"`

	// Network ID
	// Required: true
	NetID uint64 `url:"netId" json:"netId" validate:"required"`

	// Network type
	// Should be one of:
	//	- VINS
	//	- EXTNET
	// Required: true
	NetType string `url:"netType" json:"netType" validate:"computeNetType"`

	// Internal traffic, kbit
	// Required: false
	IngressRate uint64 `url:"ingress_rate,omitempty" json:"ingress_rate,omitempty"`

	// Internal traffic burst, kbit
	// Required: false
	IngressBurst uint64 `url:"ingress_burst,omitempty" json:"ingress_burst,omitempty"`

	// External traffic rate, kbit
	// Required: false
	EgressRate uint64 `url:"egress_rate,omitempty" json:"egress_rate,omitempty"`
}

type wrapperNetQOSRequest struct {
	NetQOSRequest

	AsyncMode bool `url:"asyncMode"`
}

// NetQOS updates compute interfaces QOS
func (c Compute) NetQOS(ctx context.Context, req NetQOSRequest) (bool, error) {
	err := validators.ValidateRequest(req)
	if err != nil {
		return false, validators.ValidationErrors(validators.GetErrors(err))
	}

	reqWrapped := wrapperNetQOSRequest{
		NetQOSRequest: req,
		AsyncMode:     false,
	}

	url := "/cloudbroker/compute/netQos"

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

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

	return result, nil
}

// NetQOSAsync updates compute interfaces QOS with AsyncMode
func (c Compute) NetQOSAsync(ctx context.Context, req NetQOSRequest) (string, error) {
	err := validators.ValidateRequest(req)
	if err != nil {
		return "", validators.ValidationErrors(validators.GetErrors(err))
	}

	reqWrapped := wrapperNetQOSRequest{
		NetQOSRequest: req,
		AsyncMode:     true,
	}

	url := "/cloudbroker/compute/netQos"

	res, err := c.client.DecortApiCall(ctx, http.MethodPost, url, reqWrapped)
	if err != nil {
		return "", err
	}

	return string(res), nil
}
