package controllers

import (
	"github.com/gofiber/fiber/v2"
	"penahub.gitlab.yandexcloud.net/backend/quiz/common.git/model"
	"strconv"
)

func extractParams(ctx *fiber.Ctx) (*model.PaginationReq, error) {
	pageStr := ctx.Query("page")
	sizeStr := ctx.Query("size")
	page := 1
	size := 25

	if pageStr != "" {
		pageNew, err := strconv.Atoi(pageStr)
		if err != nil {
			return nil, ctx.Status(fiber.StatusBadRequest).SendString("Invalid page parameter")
		}
		page = pageNew
	}

	if sizeStr != "" {
		sizeNew, err := strconv.Atoi(sizeStr)
		if err != nil {
			return nil, ctx.Status(fiber.StatusBadRequest).SendString("Invalid size parameter")
		}
		size = sizeNew
	}

	req := model.PaginationReq{
		Page: page,
		Size: int32(size),
	}

	return &req, nil
}
