package user

import (
	"context"

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

// GetProfile 获取用户个人资料
func (s *service) GetProfile(ctx context.Context, userID int64) (*model.Users, error) {
	return s.writeDB.Users.WithContext(ctx).Where(s.writeDB.Users.ID.Eq(userID)).First()
}

// UpdateProfile 更新用户个人资料
func (s *service) UpdateProfile(ctx context.Context, userID int64, nickname *string, avatar *string) (*model.Users, error) {
	updater := s.writeDB.Users.WithContext(ctx).Where(s.writeDB.Users.ID.Eq(userID))
	set := map[string]any{}
	if nickname != nil {
		set["nickname"] = *nickname
	}
	if avatar != nil {
		set["avatar"] = *avatar
	}
	if len(set) > 0 {
		if _, err := updater.Updates(set); err != nil {
			return nil, err
		}
	}
	return s.readDB.Users.WithContext(ctx).Where(s.readDB.Users.ID.Eq(userID)).First()
}
