package group

import (
	"fmt"

	"github.com/perfect-panel/server/internal/model/node"
	"gorm.io/gorm"
)

const (
	defaultPageSize = 20
	maxPageSize     = 100
)

func normalizePagination(page, size int) (int, int) {
	if page <= 0 {
		page = 1
	}
	if size <= 0 {
		size = defaultPageSize
	}
	if size > maxPageSize {
		size = maxPageSize
	}
	return page, size
}

func countNodesInGroup(db *gorm.DB, nodeGroupId int64) (int, error) {
	var count int64
	err := db.Model(&node.Node{}).
		Where("JSON_CONTAINS(node_group_ids, ?)", fmt.Sprintf("[%d]", nodeGroupId)).
		Count(&count).Error
	if err != nil {
		return 0, err
	}
	return int(count), nil
}
