package apiaccess

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

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

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

	// Find by name apiaccess
	// Required: false
	Name string `url:"name,omitempty" json:"name,omitempty"`

	// Find by status apiaccess
	// Required: false
	Status string `url:"status,omitempty" json:"status,omitempty"`

	// Find by description
	// Required: false
	Desc string `url:"desc,omitempty" json:"desc,omitempty"`

	// Find by created after time (unix timestamp)
	// Required: false
	CreatedAfter uint64 `url:"created_after,omitempty" json:"created_after,omitempty"`

	// Find by created before time (unix timestamp)
	// Required: false
	CreatedBefore uint64 `url:"created_before,omitempty" json:"created_before,omitempty"`

	// Sort by one of supported fields, format +|-(field)
	// Required: false
	SortBy string `url:"sort_by,omitempty" json:"sort_by,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 list of all non deleted apiaccess instances as a ListAPIAccess struct
func (a APIAccess) List(ctx context.Context, req ListRequest) (*ListAPIAccess, error) {

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

	info := ListAPIAccess{}

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

	return &info, nil
}

// ListRaw gets list of all non deleted apiaccess instances as an array of bytes
func (a APIAccess) ListRaw(ctx context.Context, req ListRequest) ([]byte, error) {

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

	url := "/cloudbroker/apiaccess/list"

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