package compute

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

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

// GuestAgentFeatureUpdateRequest struct to feature update guest agent
type GuestAgentFeatureUpdateRequest struct {
	// ID of compute instance
	// Required: true
	ComputeID uint64 `url:"compute_id" json:"compute_id" validate:"required"`
}

type wrapperGuestAgentFeatureUpdateRequest struct {
	GuestAgentFeatureUpdateRequest

	AsyncMode bool `url:"asyncMode"`
}

// Feature update guest agent
func (c Compute) GuestAgentFeatureUpdate(ctx context.Context, req GuestAgentFeatureUpdateRequest) (bool, error) {
	err := validators.ValidateRequest(req)
	if err != nil {
		return false, validators.ValidationErrors(validators.GetErrors(err))
	}

	reqWrapped := wrapperGuestAgentFeatureUpdateRequest{
		GuestAgentFeatureUpdateRequest: req,
		AsyncMode:                      false,
	}

	url := "/cloudbroker/compute/guest_agent_feature_update"

	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
}

// GuestAgentFeatureUpdateAsync feature updates guest agent with AsyncMode
func (c Compute) GuestAgentFeatureUpdateAsync(ctx context.Context, req GuestAgentFeatureUpdateRequest) (string, error) {
	err := validators.ValidateRequest(req)
	if err != nil {
		return "", validators.ValidationErrors(validators.GetErrors(err))
	}

	reqWrapped := wrapperGuestAgentFeatureUpdateRequest{
		GuestAgentFeatureUpdateRequest: req,
		AsyncMode:                      true,
	}

	url := "/cloudbroker/compute/guest_agent_feature_update"

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

	return string(res), nil
}
