package com.unicorn.hgzero.meeting.infra.config;

import com.azure.messaging.eventhubs.EventHubClientBuilder;
import com.azure.messaging.eventhubs.EventHubProducerClient;
import jakarta.annotation.PostConstruct;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * Azure EventHub 설정
 * 이벤트 발행을 위한 EventHub 설정
 */
@Configuration
@Slf4j
@org.springframework.boot.autoconfigure.condition.ConditionalOnExpression("'${eventhub.connection-string:}'.length() > 0")
public class EventHubConfig {

    @Value("${eventhub.connection-string}")
    private String connectionString;

    @Value("${eventhub.name}")
    private String eventHubName;

    @PostConstruct
    public void init() {
        log.info("Initializing Azure EventHub configuration with hub name: {}", eventHubName);
    }

    /**
     * EventHub Producer Client 생성
     */
    @Bean(name = "eventProducer")
    public EventHubProducerClient eventProducer() {
        log.info("Creating EventHub producer for hub: {}", eventHubName);
        return new EventHubClientBuilder()
                .connectionString(connectionString, eventHubName)
                .buildProducerClient();
    }
}