package app

import (
	"context"
	"strings"

	"hyapp/pkg/userleaderboard"
	"hyapp/services/activity-service/internal/config"
)

func openUserLeaderboardStore(ctx context.Context, cfg config.UserLeaderboardWorkerConfig) (*userleaderboard.Store, func() error, error) {
	if !cfg.Enabled {
		return nil, nil, nil
	}
	redisAddr := strings.TrimSpace(cfg.RedisAddr)
	if redisAddr == "" {
		return nil, nil, userleaderboard.ErrNotConfigured
	}
	client, err := userleaderboard.NewRedisClient(ctx, redisAddr, cfg.RedisPassword, cfg.RedisDB)
	if err != nil {
		return nil, nil, err
	}
	// activity-service 只负责把 wallet_outbox 已提交事实投影成 Redis 读模型；key 命名由共享包和配置共同约束。
	return userleaderboard.NewStore(client, cfg.KeyPrefix), client.Close, nil
}
