package echohelpers

import (
	"strconv"

	"github.com/labstack/echo/v4"
)

// ParsePagination extracts limit and offset from query parameters with bounded defaults.
// maxLimit caps the maximum page size to prevent unbounded queries.
func ParsePagination(c echo.Context, maxLimit int) (limit, offset int) {
	const defaultLimit = 50

	limit = defaultLimit
	if l := c.QueryParam("limit"); l != "" {
		if parsed, err := strconv.Atoi(l); err == nil && parsed > 0 {
			limit = parsed
		}
	}
	if limit > maxLimit {
		limit = maxLimit
	}

	offset = 0
	if o := c.QueryParam("offset"); o != "" {
		if parsed, err := strconv.Atoi(o); err == nil && parsed >= 0 {
			offset = parsed
		}
	}

	return limit, offset
}
