package com.unicorn.hgzero.user.config;

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

/**
 * Azure EventHub 설정
 * 사용자 인증 관련 이벤트 발행을 위한 EventHub 설정
 */
@Slf4j
@Configuration
public class EventHubConfig {

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

    @Value("${spring.cloud.azure.eventhub.name:hgzero-eventhub-name}")
    private String eventHubName;

    /**
     * EventHub Producer Client 빈 생성
     * 연결 문자열이 있을 때만 생성
     */
    @Bean
    @ConditionalOnExpression("'${spring.cloud.azure.eventhub.connection-string:}'.length() > 0")
    public EventHubProducerClient eventHubProducerClient() {
        try {
            EventHubProducerClient client = new EventHubClientBuilder()
                    .connectionString(connectionString, eventHubName)
                    .buildProducerClient();
            
            log.info("EventHub Producer Client 생성 완료: {}", eventHubName);
            return client;
        } catch (Exception e) {
            log.error("EventHub Producer Client 생성 실패: {}", e.getMessage());
            throw new RuntimeException("EventHub Producer Client 생성 실패", e);
        }
    }
}