package user

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

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

// ListRequest struct to get all non deleted user instances.
type ListRequest struct {
	// Find by ID.
	// Required: false
	ByID string `url:"by_id,omitempty" json:"by_id,omitempty"`

	// Find by active. True or False.
	// Required: false
	Active interface{} `url:"active,omitempty" json:"active,omitempty" validate:"omitempty,isBool"`

	// Find by email.
	// Required: false
	Email string `url:"email,omitempty" json:"email,omitempty"`

	// Find by serviceaccount. True or False.
	// Required: false
	ServiceAccount interface{} `url:"serviceaccount,omitempty" json:"serviceaccount,omitempty" validate:"omitempty,isBool"`

	// Sort by one of supported fields, format +|-(field)
	// Required: false
	SortBy string `url:"sortBy,omitempty" json:"sortBy,omitempty" validate:"omitempty,sortBy"`

	// Page number.
	// Required: false
	Page uint64 `url:"page,omitempty" json:"page,omitempty"`

	// Page size, maximum - 100.
	// Required: false
	Size uint64 `url:"size,omitempty" json:"size,omitempty"`
}

// List gets all non deleted user instances as a ListUsers struct
func (u User) List(ctx context.Context, req ListRequest) (*ListUsers, error) {

	res, err := u.ListRaw(ctx, req)
	if err != nil {
		return nil, err
	}

	list := ListUsers{}

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

	return &list, nil
}

// ListRaw gets all non deleted user instances as an array of bytes
func (u User) ListRaw(ctx context.Context, req ListRequest) ([]byte, error) {

	err := validators.ValidateRequest(req)
	if err != nil {
		return nil, validators.ValidationErrors(validators.GetErrors(err))
	}

	url := "/cloudbroker/user/list"

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