package user

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

// SearchRequest struct for searching user's elements.
type SearchRequest struct {
	// Text to search
	// Required: true
	Text string `url:"text" json:"text" validate:"required"`
}

// Search searches for user's elements.
func (u User) Search(ctx context.Context, req SearchRequest) (*FoundElements, error) {
	url := "/cloudapi/user/search"

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

	list := FoundElements{}

	err = json.Unmarshal(res, &list)
	if err != nil {
		return nil, err
	}

	return &list, nil
}
