package grid

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

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

// SetPasswordPolicyRequest struct to set password policy for a grid
type SetPasswordPolicyRequest struct {
	// ID of the grid
	// Required: true
	GID uint64 `url:"gridId" json:"gridId" validate:"required"`

	// Available numbers in the password
	// Default value : true
	// Required: true
	Digits interface{} `url:"digits" json:"digits" validate:"isBool"`

	// Available special characters in the password
	// Default value : false
	// Required: true
	SpecialSymbols bool `url:"specialSymbols" json:"specialSymbols"`

	// Number of characters in the password
	// Default value : 9
	// Required: true
	PasswordLength uint64 `url:"passwordLength" json:"passwordLength"`

	// Capital letters in the password are available
	// Default value : true
	// Required: true
	Uppercase interface{} `url:"uppercase" json:"uppercase" validate:"isBool"`
}

// RemoveCustomBackupPath set set password policy for a grid
func (g Grid) SetPasswordPolicy(ctx context.Context, req SetPasswordPolicyRequest) (bool, error) {
	err := validators.ValidateRequest(req)
	if err != nil {
		return false, validators.ValidationErrors(validators.GetErrors(err))
	}

	if req.PasswordLength == 0 {
		req.PasswordLength = 9
	}

	if req.Digits == nil {
		req.Digits = true
	}

	if req.Uppercase == nil {
		req.Uppercase = true
	}

	url := "/cloudbroker/grid/setPasswordPolicy"

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