package clickhouse

import (
	"net/http"

	"github.com/fiskerinc/cloud-services/pkg/validator"

	"github.com/gorilla/schema"
)

const PageQueryOptionsLimitMaximum = 100

type PageQueryOptions struct {
	Limit  int `json:"limit" validate:"gte=0,lte=100"`
	Offset int `json:"offset" validate:"gte=0"`
}

// ParsePageQuery parses PageQueryOptions from http request
func ParsePageQuery(r *http.Request) (PageQueryOptions, error) {
	decoder := schema.NewDecoder()
	options := PageQueryOptions{}

	decoder.SetAliasTag("json")
	decoder.Decode(&options, r.URL.Query())
	err := validator.ValidateStruct(options)
	if err == nil && options.Limit == 0 {
		options.Limit = PageQueryOptionsLimitMaximum
	}

	return options, err
}
