package user

import (
	"context"

	"bindbox-game/internal/repository/mysql/model"
)

func (s *service) ListPointsLedger(ctx context.Context, userID int64, page, pageSize int) (items []*model.UserPointsLedger, total int64, err error) {
	q := s.readDB.UserPointsLedger.WithContext(ctx).ReadDB().Where(s.readDB.UserPointsLedger.UserID.Eq(userID))
	total, err = q.Count()
	if err != nil {
		return nil, 0, err
	}
	if page <= 0 {
		page = 1
	}
	if pageSize <= 0 {
		pageSize = 20
	}
	if pageSize > 100 {
		pageSize = 100
	}
	items, err = q.Order(s.readDB.UserPointsLedger.ID.Desc()).Offset((page - 1) * pageSize).Limit(pageSize).Find()
	if err != nil {
		return nil, 0, err
	}
	return items, total, nil
}
