package auth

import (
	"context"
	"log/slog"
	"strings"

	"hyapp/pkg/appcode"
	"hyapp/pkg/logx"
	userdomain "hyapp/services/user-service/internal/domain/user"
)

func (s *Service) importIMAccountAfterCommit(ctx context.Context, user userdomain.User, meta Meta) {
	if s.imAccountImporter == nil || user.UserID <= 0 {
		return
	}
	if err := s.imAccountImporter.ImportAccount(ctx, user.UserID, strings.TrimSpace(user.Username), strings.TrimSpace(user.Avatar)); err != nil {
		// 用户创建事务已经提交；账号导入失败不能回滚用户，只能依赖 /im/usersig 兜底补偿和日志排查。
		logx.Warn(logx.With(ctx, slog.String("request_id", meta.RequestID), slog.String("app_code", appcode.Normalize(meta.AppCode))),
			"im_account_import_failed",
			slog.String("component", "user_auth"),
			slog.Int64("user_id", user.UserID),
			slog.String("error", err.Error()),
		)
	}
}
