package acsgroups

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

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

// ListGroupsRequest struct to get a list of access groups
type ListGroupsRequest struct {
	// Find by enabled status, true or false
	// Required: false
	Enabled interface{} `url:"enabled,omitempty" json:"enabled,omitempty" validate:"omitempty,isBool"`

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

	// Display name filter
	// Required: false
	DisplayName string `url:"display_name,omitempty" json:"display_name,omitempty"`

	// Owner display name filter
	// Required: false
	OwnerDisplayName string `url:"owner_display_name,omitempty" json:"owner_display_name,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, created_at, updated_at, deleted_at, owner_login)
	// 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 (inclusive)
	// Required: false
	CreatedTo string `url:"created_to,omitempty" json:"created_to,omitempty"`
}

// List of access groups
func (i AccessGroups) List(ctx context.Context, req ListGroupsRequest) (*AccessGroupList, error) {
	res, err := i.ListRaw(ctx, req)
	if err != nil {
		return nil, err
	}

	groups := []AccessGroup{}

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

	result := AccessGroupList{AccessGroups: groups}

	return &result, nil
}

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

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

	url := "/sdn/access_group/list"

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