package redis

import (
	"context"
	"crm-api/pkg/configs"
	"crm-api/pkg/lfshook"
	"fmt"
	"syscall"

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

var RedisInstance *redis.Client

func CreateRedisInstance() {
	RedisInstance = redis.NewClient(&redis.Options{
		Addr: fmt.Sprintf("%s:%s",
			configs.ConfigGlobal.RedisDBHost,
			configs.ConfigGlobal.RedisDBPort,
		),
		Password: configs.ConfigGlobal.RedisDBSecret,
		DB:       0, // use default DB
	})

	err := RedisInstance.Ping(context.Background()).Err()
	if err != nil {
		lfshook.NewLogger().Error("redis ping error", err)
		syscall.Kill(syscall.Getpid(), syscall.SIGINT)
	}
}
