import path from 'path'
import dotenv from 'dotenv'

dotenv.config({ path: path.join(__dirname, '../.env') })

import { promisify } from 'util'
import redis from 'redis'
import logger from './logger'

const redisHost = process.env.REDIS_HOST ?? '127.0.0.1'

const client = redis.createClient({
  host: redisHost,
  port: 6379,
  password: process.env.REDIS_PASSWORD,
})

client.on('error', (error) => {
  logger.error(error)
})

client.on('connect', () => {
  logger.info('Connection to Redis has been established successfully.')
})

export const asyncHEXISTS = promisify(client.HEXISTS).bind(client)
export const asyncHGET = promisify(client.HGET).bind(client)
export const asyncHSET = promisify(client.HSET).bind(client)
export const asyncHDEL = promisify(client.HDEL).bind(client)
export const asyncHGETALL = promisify(client.HGETALL).bind(client)
export const asyncDEL = promisify(client.DEL).bind(client)

export default client
