package handlers

import (
	"github.com/fiskerinc/cloud-services/services/depot/services"

	"github.com/fiskerinc/cloud-services/pkg/logger"
	"github.com/fiskerinc/cloud-services/pkg/redis"
)

func HMIDel(db *services.DB, id string) error {
	client := services.RedisClientPool().GetFromPool()
	defer client.Close()

	logger.Info().Msgf("Remove HMI session in Redis for %s", id)

	err := removeHMISession(client, id)
	if err != nil {
		logger.Warn().Str("id", id).Err(err).Send()
	}

	err = removeHMISessionID(client, id)
	if err != nil {
		logger.Warn().Str("id", id).Err(err).Send()
	}

	return nil
}

func removeHMISession(client redis.Client, id string) error {
	_, err := client.Execute("SREM", redis.HMISessionsKey(), id)
	return err
}

func removeHMISessionID(client redis.Client, id string) error {
	return client.Delete(redis.HMISessionKey(id))
}
