package presenseredis

import (
	"context"
	"encoding/json"
	"fmt"

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

// Get Map from Redis
//
// Deprecated: there is only one map now and we know the type
func LoadRedisMap[K comparable, V any, M map[K]V](client *redis.Client, ctx context.Context, key string) M {
	redisValue, err := client.Get(ctx, key).Result()
	resMap := make(M)

	if err == redis.Nil {
		fmt.Printf("No list found for key %s, starting empty\n", key)
	} else if err != nil {
		fmt.Printf("Error in connecting to Redis: %v, key: %s returning empty map\n", err, key)
	} else {
		if err := json.Unmarshal([]byte(redisValue), &resMap); err != nil {
			fmt.Printf("Error in unmarshalling JSON for key: %s\n", key)
		}
	}

	return resMap
}

// Set Map in Redis
//
// Deprecated: hashmaps are used now
func SaveRedisMap(client *redis.Client, ctx context.Context, key string, data interface{}) {
	eData, err := json.Marshal(data)
	if err != nil {
		fmt.Println("Error in marshalling, key: ", key)
	}

	err = client.Set(ctx, key, eData, 0).Err()
	if err != nil {
		fmt.Println("Error in persisting in Redis, key: ", key)
	}
}
