package cache

import (
	"errors"
	"sync"

	"github.com/redis/go-redis/v9"
)

var (
	redisClient *redis.Client
	mu          sync.RWMutex
)

// SetRedis assigns the global redis client used across the application.
func SetRedis(client *redis.Client) {
	mu.Lock()
	defer mu.Unlock()
	redisClient = client
}

// Redis returns the configured redis client. It may be nil if not yet initialised.
func Redis() *redis.Client {
	mu.RLock()
	defer mu.RUnlock()
	return redisClient
}

// MustRedis returns the redis client or panics if it has not been set.
func MustRedis() *redis.Client {
	mu.RLock()
	client := redisClient
	mu.RUnlock()
	if client == nil {
		panic(errors.New("redis client not initialised"))
	}
	return client
}
