package com.lingniu.ingest.inbound.mqtt.config;

import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.context.annotation.ConditionContext;
import org.springframework.core.env.MapPropertySource;
import org.springframework.core.env.StandardEnvironment;
import org.springframework.core.io.ResourceLoader;

import java.util.Map;

import static org.assertj.core.api.Assertions.assertThat;

class MqttInboundEnabledConditionTest {

    private final MqttInboundEnabledCondition condition = new MqttInboundEnabledCondition();

    @Test
    void ignoresLegacyGenericMqttEnabledEnvironmentAlias() {
        assertThat(condition.matches(context(Map.of("MQTT_ENABLED", "true")), null)).isFalse();
    }

    @Test
    void acceptsYutongMqttEnabledEnvironmentAlias() {
        assertThat(condition.matches(context(Map.of("YUTONG_MQTT_ENABLED", "true")), null)).isTrue();
    }

    @Test
    void directPropertyOverridesEnvironmentAlias() {
        assertThat(condition.matches(context(Map.of(
                "lingniu.ingest.mqtt.enabled", "false",
                "YUTONG_MQTT_ENABLED", "true")), null)).isFalse();
    }

    private static ConditionContext context(Map<String, Object> properties) {
        StandardEnvironment environment = new StandardEnvironment();
        environment.getPropertySources().addFirst(new MapPropertySource("test", properties));
        return new TestConditionContext(environment);
    }

    private record TestConditionContext(StandardEnvironment environment) implements ConditionContext {
        @Override
        public BeanDefinitionRegistry getRegistry() {
            return null;
        }

        @Override
        public ConfigurableListableBeanFactory getBeanFactory() {
            return null;
        }

        @Override
        public StandardEnvironment getEnvironment() {
            return environment;
        }

        @Override
        public ResourceLoader getResourceLoader() {
            return null;
        }

        @Override
        public ClassLoader getClassLoader() {
            return null;
        }
    }
}
