package acsgroups

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

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

// UsersListRequest struct to get a list of users
type UsersListRequest struct {
	// Access group identifier
	// Required: true
	AccessGroupID string `url:"access_group_id" json:"access_group_id" validate:"required"`

	// Filter by global role
	// Required: false
	GlobalRole string `url:"global_role,omitempty" json:"global_role,omitempty"`

	// Filter by access group role
	// Required: false
	AccessGroupRole string `url:"access_group_role,omitempty" json:"access_group_role,omitempty"`

	// Filter by enabled status (true/false)
	// Required: false
	Enabled interface{} `url:"enabled,omitempty" json:"enabled,omitempty" validate:"omitempty,isBool"`

	// Filter by deleted status (true/false)
	// Required: false
	Deleted interface{} `url:"deleted,omitempty" json:"deleted,omitempty" validate:"omitempty,isBool"`

	// Filter by display name
	// Required: false
	DisplayName string `url:"display_name,omitempty" json:"display_name,omitempty"`

	// Filter by user login
	// Required: false
	Login string `url:"login,omitempty" json:"login,omitempty"`

	// Filter by creator login
	// Required: false
	CreatedBy string `url:"created_by,omitempty" json:"created_by,omitempty"`

	// Filter by last updater login
	// Required: false
	UpdatedBy string `url:"updated_by,omitempty" json:"updated_by,omitempty"`

	// Filter by deleter login
	// Required: false
	DeletedBy string `url:"deleted_by,omitempty" json:"deleted_by,omitempty"`

	// Filter by disabler login
	// Required: false
	DisabledBy string `url:"disabled_by,omitempty" json:"disabled_by,omitempty"`

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

	// Number of results per page
	// Required: false
	PerPage uint64 `url:"per_page,omitempty" json:"per_page,omitempty"`

	// Field to sort by (display_name, email, phone, created_at, updated_at, deleted_at)
	// Required: false
	SortBy string `url:"sort_by,omitempty" json:"sort_by,omitempty"`

	// Sort order (asc/desc)
	// Required: false
	SortOrder string `url:"sort_order,omitempty" json:"sort_order,omitempty"`

	// Creation date lower bound (inclusive)
	// Required: false
	CreatedFrom string `url:"created_from,omitempty" json:"created_from,omitempty"`

	// Creation date upper bound (exclusive)
	// Required: false
	CreatedTo string `url:"created_to,omitempty" json:"created_to,omitempty"`
}

// List of access groups
func (i AccessGroups) UsersList(ctx context.Context, req UsersListRequest) (*UsersList, error) {

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

	users := []User{}

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

	result := UsersList{Users: users}

	return &result, nil
}

// ListRaw gets a list of all users as an array of bytes
func (a AccessGroups) UserListRaw(ctx context.Context, req UsersListRequest) ([]byte, error) {

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

	url := "/sdn/access_group/user_list"

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