package zone

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

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

// NodeAutoStartRequest struct to set node autostart in zone
type NodeAutoStartRequest struct {
	// ID of zone
	// Required: true
	ZoneID uint64 `url:"zone_id" json:"zone_id" validate:"required"`

	// AutoStart nodes in zone
	// Required: true
	AutoStart bool `url:"autostart" json:"autostart" validate:"required"`
}

// NodeAutoStart sets node autostart in zone
func (e Zone) NodeAutoStart(ctx context.Context, req NodeAutoStartRequest) (bool, error) {
	err := validators.ValidateRequest(req)
	if err != nil {
		return false, validators.ValidationErrors(validators.GetErrors(err))
	}

	url := "/cloudbroker/zone/node_autostart"

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

	if err != nil {
		return false, err
	}

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

	return result, nil
}
