package sep

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

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

// SharedLockStopRequest struct to stop shared locks
type SharedLockStopRequest struct {
	// Storage endpoint provider ID
	// Required: true
	SEPID uint64 `url:"sep_id" json:"sep_id" validate:"required"`

	// List of node IDs for stop locks
	// Required: false
	Nodes []uint64 `url:"nodes,omitempty" json:"nodes,omitempty"`

	// List of pool names for stop locks
	// Required: false
	VGNames []string `url:"vg_names,omitempty" json:"vg_names,omitempty"`
}

type wrapperSharedLockStopRequest struct {
	SharedLockStopRequest

	AsyncMode bool `url:"sync"`
}

// SharedLockStop stop shared locks without AsyncMode
func (s SEP) SharedLockStop(ctx context.Context, req SharedLockStopRequest) (bool, error) {
	err := validators.ValidateRequest(req)
	if err != nil {
		return false, validators.ValidationErrors(validators.GetErrors(err))
	}

	reqWrapped := wrapperSharedLockStopRequest{
		SharedLockStopRequest: req,
		AsyncMode:             false,
	}

	url := "/cloudbroker/sep/sharedLockStop"

	res, err := s.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
}

// SharedLockStopAsync stop shared locks with AsyncMode
func (s SEP) SharedLockStopAsync(ctx context.Context, req SharedLockStopRequest) (string, error) {
	err := validators.ValidateRequest(req)
	if err != nil {
		return "", validators.ValidationErrors(validators.GetErrors(err))
	}

	reqWrapped := wrapperSharedLockStopRequest{
		SharedLockStopRequest: req,
		AsyncMode:             true,
	}

	url := "/cloudbroker/sep/sharedLockStop"

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

	result := strings.ReplaceAll(string(res), "\"", "")

	return result, nil
}
