package compute

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

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

// ChangeMTURequest struct to change MTU for a compute
type ChangeMTURequest struct {
	// ID of compute instance
	// Required: true
	ComputeID uint64 `url:"compute_id" json:"compute_id" validate:"required"`

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

	// Maximum transmission unit
	// Required: true
	MTU uint64 `url:"mtu" json:"mtu" validate:"required" validate:"omitempty,mtu"`
}

type wrapperChangeMTURequest struct {
	ChangeMTURequest

	AsyncMode bool `url:"asyncMode"`
}

// ChangeMTU change MTU for compute instance
func (c Compute) ChangeMTU(ctx context.Context, req ChangeMTURequest) (bool, error) {
	err := validators.ValidateRequest(req)
	if err != nil {
		return false, validators.ValidationErrors(validators.GetErrors(err))
	}

	reqWrapped := wrapperChangeMTURequest{
		ChangeMTURequest: req,
		AsyncMode:        false,
	}

	url := "/cloudapi/compute/change_mtu"

	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
}

// ChangeMTUAsync change MTU for compute instance with AsyncMode
func (c Compute) ChangeMTUAsync(ctx context.Context, req ChangeMTURequest) (string, error) {
	err := validators.ValidateRequest(req)
	if err != nil {
		return "", validators.ValidationErrors(validators.GetErrors(err))
	}

	reqWrapped := wrapperChangeMTURequest{
		ChangeMTURequest: req,
		AsyncMode:        true,
	}

	url := "/cloudapi/compute/change_mtu"

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

	return string(res), nil
}
