package com.hiveops.aria.kafka;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.stereotype.Component;

@Component
@RequiredArgsConstructor
@Slf4j
public class AriaThreatProducer {

    private final KafkaTemplate<String, Object> kafkaTemplate;

    public void publishThreatEvent(AriaThreatEvent event) {
        String key = event.getDeviceAgentId() != null ? event.getDeviceAgentId() : "unknown";

        kafkaTemplate.send(AriaKafkaConfig.TOPIC_THREATS, key, event)
                .whenComplete((result, ex) -> {
                    if (ex != null) {
                        log.error("Failed to publish ARIA threat event {} to Kafka: {}",
                                event.getEventId(), ex.getMessage());
                    } else {
                        log.debug("Published ARIA threat event {} (signal={}, severity={}) to partition {}",
                                event.getEventId(), event.getSignalKey(), event.getSeverity(),
                                result.getRecordMetadata().partition());
                    }
                });
    }
}
