package com.ruoyi.config;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.Properties;

@Configuration
public class KafkaConfig {

    private String bootstrapServers = "kafka.hikparking.com:30090,kafka.hikparking.com:30091,kafka.hikparking.com:30092";

    private String groupId = "gp_32856UK2Rr9BT";

    private String topic = "topic_pass32856ik8M5V";

    private String username = "103251113CU2C82TCA6M9";

    private String password = "ZR56NoW%qxov$Gl4";

    /**
     * 创建Kafka消费者配置
     */
    @Bean
    public Properties kafkaConsumerProperties() {
        Properties properties = new Properties();

        // 基础配置
        properties.setProperty("bootstrap.servers", bootstrapServers);
        properties.setProperty("group.id", groupId);
        properties.setProperty("key.deserializer",
                "org.apache.kafka.common.serialization.StringDeserializer");
        properties.setProperty("value.deserializer",
                "org.apache.kafka.common.serialization.StringDeserializer");
        properties.setProperty("auto.offset.reset", "latest");
        properties.setProperty("enable.auto.commit", "true");
        properties.setProperty("auto.commit.interval.ms", "1000");
        properties.setProperty("session.timeout.ms", "30000");
        properties.setProperty("heartbeat.interval.ms", "10000");
        properties.setProperty("max.poll.records", "500");

        // SASL认证配置 - 特别注意：SCRAM-SHA-256（不是你截图中的SCRAM-SMA-256）
        properties.put("security.protocol", "SASL_PLAINTEXT");
        properties.put("sasl.mechanism", "SCRAM-SHA-256");  // 修正为SCRAM-SHA-256

        // 注意：正确的类名是scram不是scran
        String jaasConfig = String.format(
                "org.apache.kafka.common.security.scram.ScramLoginModule required " +
                        "username=\"%s\" password=\"%s\";",
                username, password
        );
        properties.put("sasl.jaas.config", jaasConfig);

        return properties;
    }
}