package user

import (
	"context"
	"encoding/json"
	"net/http"

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

// GetMatchingUsernamesRequest struct for getting a list of the matching usernames for a given string.
type GetMatchingUsernamesRequest struct {
	// Regex of the usernames to searched for.
	// Required: true
	UsernameRegex string `url:"usernameregex" json:"usernameregex" validate:"required"`

	// The number of usernames to return.
	// Required: false
	Limit uint64 `url:"limit,omitempty" json:"limit,omitempty"`
}

// GetMatchingUsernames gets a list of the matching usernames for a given string.
func (u User) GetMatchingUsernames(ctx context.Context, req GetMatchingUsernamesRequest) (ListMatchingUsernames, error) {
	err := validators.ValidateRequest(req)
	if err != nil {
		return nil, validators.ValidationErrors(validators.GetErrors(err))
	}

	url := "/cloudbroker/user/getMatchingUsernames"

	list := ListMatchingUsernames{}

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

	err = json.Unmarshal(res, &list)

	if err != nil {
		return nil, err
	}

	return list, nil
}
