package user

import (
	"context"
	"time"
)

func (s *service) GetPointsBalance(ctx context.Context, userID int64) (int64, error) {
	rows, err := s.readDB.UserPoints.WithContext(ctx).Where(s.readDB.UserPoints.UserID.Eq(userID)).Find()
	if err != nil {
		return 0, err
	}
	var total int64
	now := time.Now()
	for _, r := range rows {
		if !r.ValidEnd.IsZero() && r.ValidEnd.Before(now) {
			continue
		}
		total += r.Points
	}
	return total, nil
}
