package logic

import (
	"context"
	"github.com/zeromicro/go-zero/core/logx"
	plantModel "sundynix-micro-go/app/plant/model"
	"sundynix-micro-go/app/plant/rpc/internal/svc"
	"sundynix-micro-go/app/plant/rpc/plant"
)

type GetMyBadgesLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewGetMyBadgesLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetMyBadgesLogic {
	return &GetMyBadgesLogic{ctx: ctx, svcCtx: svcCtx, Logger: logx.WithContext(ctx)}
}

func (l *GetMyBadgesLogic) GetMyBadges(in *plant.GetProfileReq) (*plant.UserBadgeListResp, error) {
	var userBadges []plantModel.UserBadge
	if err := l.svcCtx.DB.Where("user_id = ?", in.UserId).Find(&userBadges).Error; err != nil {
		return nil, err
	}
	list := make([]*plant.UserBadgeInfo, 0, len(userBadges))
	for _, ub := range userBadges {
		var badge plantModel.BadgeConfig
		l.svcCtx.DB.First(&badge, "id = ?", ub.BadgeID)
		list = append(list, &plant.UserBadgeInfo{
			Id: ub.ID, BadgeId: ub.BadgeID, BadgeName: badge.Name,
			Dimension: badge.Dimension, Tier: int32(badge.Tier),
			AwardedAt: ub.CreatedAt.Format("2006-01-02 15:04:05"),
		})
	}
	return &plant.UserBadgeListResp{List: list}, nil
}
