package ru.micord.ervu.audit.service.impl;


import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Conditional;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.stereotype.Service;
import ru.micord.ervu.audit.config.AuditEnabledCondition;
import ru.micord.ervu.audit.service.AuditKafkaPublisher;

/**
 * @author Adel Kalimullin
 */
@Service
@Conditional(AuditEnabledCondition.class)
public class BaseAuditKafkaPublisher implements AuditKafkaPublisher {
  private static final Logger LOGGER = LoggerFactory.getLogger(BaseAuditKafkaPublisher.class);
  private final KafkaTemplate<String, String> kafkaTemplate;

  public BaseAuditKafkaPublisher(
      @Qualifier("auditTemplate") KafkaTemplate<String, String> kafkaTemplate) {
    this.kafkaTemplate = kafkaTemplate;
  }

  @Override
  public void publishEvent(String topic, String message) {
    kafkaTemplate.send(topic, message)
        .addCallback(
            result -> {
            },
            ex -> LOGGER.error("Failed to send message to topic {}: {}", topic, ex.getMessage(),
                ex
            )
        );
  }
}
