package user

import (
    "context"
)

type UserStats struct {
    ItemCardCount int64 `json:"item_card_count"`
    CouponCount   int64 `json:"coupon_count"`
    PointsBalance int64 `json:"points_balance"`
}

func (s *service) GetUserStats(ctx context.Context, userID int64) (*UserStats, error) {
    var itemCardCount int64
    var couponCount int64
    var pointsBalance int64

    icq := s.readDB.UserItemCards.WithContext(ctx).ReadDB().Where(s.readDB.UserItemCards.UserID.Eq(userID), s.readDB.UserItemCards.Status.Eq(1))
    if c, err := icq.Count(); err != nil {
        return nil, err
    } else {
        itemCardCount = c
    }

    cq := s.readDB.UserCoupons.WithContext(ctx).ReadDB().Where(s.readDB.UserCoupons.UserID.Eq(userID), s.readDB.UserCoupons.Status.Eq(1))
    if c, err := cq.Count(); err != nil {
        return nil, err
    } else {
        couponCount = c
    }

    pb, err := s.GetPointsBalance(ctx, userID)
    if err != nil {
        return nil, err
    }
    pointsBalance = pb

    return &UserStats{ItemCardCount: itemCardCount, CouponCount: couponCount, PointsBalance: pointsBalance}, nil
}