package utils

import "penahub.gitlab.yandexcloud.net/pena-services/customer/internal/models"

func DeterminePagination(page, limit interface{}) *models.Pagination {
	determinePage := func() int64 {
		if page == nil {
			return models.DefaultPageNumber
		}

		pageNumber, isPageNumberOK := page.(int64)

		if !isPageNumberOK || pageNumber < 1 {
			return models.DefaultPageNumber
		}

		return pageNumber
	}

	determineLimit := func() int64 {
		if limit == nil {
			return models.DefaultLimit
		}

		limitNumber, isLimitNumberOK := limit.(int64)

		if !isLimitNumberOK || limitNumber > models.DefaultLimit || limitNumber < 1 {
			return models.DefaultLimit
		}

		return limitNumber
	}

	return &models.Pagination{
		Page:  determinePage(),
		Limit: determineLimit(),
	}
}
