package connectors

import (
	"net/http"
	"strconv"

	"github.com/gin-gonic/gin"
)

func parsePositiveLimit(c *gin.Context, key string, defaultValue, maxValue int) (int, bool) {
	raw := c.Query(key)
	if raw == "" {
		return defaultValue, true
	}
	value, err := strconv.Atoi(raw)
	if err != nil || value <= 0 {
		c.JSON(http.StatusBadRequest, gin.H{"error": "invalid " + key})
		return 0, false
	}
	if value > maxValue {
		value = maxValue
	}
	return value, true
}

func parseOptionalNonNegativeInt(c *gin.Context, key string, defaultValue int) (int, bool) {
	raw := c.Query(key)
	if raw == "" {
		return defaultValue, true
	}
	value, err := strconv.Atoi(raw)
	if err != nil || value < 0 {
		c.JSON(http.StatusBadRequest, gin.H{"error": "invalid " + key})
		return 0, false
	}
	return value, true
}
