package com.iqser.red.service.persistence.management.v1.processor.service.websocket;

import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import com.fasterxml.jackson.databind.ObjectMapper;

import lombok.RequiredArgsConstructor;
import lombok.SneakyThrows;

@Service
@RequiredArgsConstructor
public class RedisSyncedWebSocketService {

    private final StringRedisTemplate redisTemplate;
    private final ObjectMapper mapper;

    @SneakyThrows
    public void convertAndSend(String topic, Object message) {
        var socketData = new WebSocketMessage(topic, mapper.writeValueAsString(message));
        String data = mapper.writeValueAsString(socketData);
        redisTemplate.convertAndSend("persistence-service-websocket-messages", data);
    }

}