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

import org.springframework.boot.context.properties.ConfigurationProperties;

import java.util.ArrayList;
import java.util.List;

/**
 * MQTT 接入配置。支持多 endpoint（每个 endpoint 可对应一个车厂 / 一个 broker）。
 *
 * <p>这是 JSON/MQTT 厂商入口，不参与 GB32960 TCP 32960 端口接入；如果同一辆车同时存在
 * MQTT 和 32960 数据，需要在下游按 protocol/source 做归因。
 */
@ConfigurationProperties(prefix = "lingniu.ingest.mqtt")
public class MqttInboundProperties {

    /** 总开关。关闭时不会创建任何 MQTT 客户端连接。 */
    private boolean enabled = false;
    /** 是否随 Spring Boot 应用启动立即连接 broker。测试装配时可关闭，生产默认开启。 */
    private boolean autoStartup = true;
    /** 多 broker/多 topic 配置；每个 endpoint 独立重连、订阅并转成内部 VehicleEvent。 */
    private List<Endpoint> endpoints = new ArrayList<>();

    public boolean isEnabled() { return enabled; }
    public void setEnabled(boolean enabled) { this.enabled = enabled; }
    public boolean isAutoStartup() { return autoStartup; }
    public void setAutoStartup(boolean autoStartup) { this.autoStartup = autoStartup; }
    public List<Endpoint> getEndpoints() { return endpoints; }
    public void setEndpoints(List<Endpoint> endpoints) { this.endpoints = endpoints; }

    public static class Endpoint {
        /** 逻辑名称，用于指标与日志。 */
        private String name;
        /** 对端 URI。形如 ssl://host:port 或 tcp://host:port。 */
        private String uri;
        /** 订阅 topic，支持 + / # 通配符。 */
        private String topic;
        /** QoS 0/1/2。 */
        private int qos = 1;
        private String clientId;
        private String username;
        private String password;
        /** false 表示保留 broker 会话，重连后可继续接收离线期间的 QoS1/2 消息。 */
        private boolean cleanSession = false;
        private int keepAliveSeconds = 20;
        private int connectionTimeoutSeconds = 10;
        /** JSON 字段适配器 profile。默认 yutong。未来可支持 haipote / others。 */
        private String profile = "yutong";
        private Tls tls = new Tls();

        public String getName() { return name; }
        public void setName(String name) { this.name = name; }
        public String getUri() { return uri; }
        public void setUri(String uri) { this.uri = uri; }
        public String getTopic() { return topic; }
        public void setTopic(String topic) { this.topic = topic; }
        public int getQos() { return qos; }
        public void setQos(int qos) { this.qos = qos; }
        public String getClientId() { return clientId; }
        public void setClientId(String clientId) { this.clientId = clientId; }
        public String getUsername() { return username; }
        public void setUsername(String username) { this.username = username; }
        public String getPassword() { return password; }
        public void setPassword(String password) { this.password = password; }
        public boolean isCleanSession() { return cleanSession; }
        public void setCleanSession(boolean cleanSession) { this.cleanSession = cleanSession; }
        public int getKeepAliveSeconds() { return keepAliveSeconds; }
        public void setKeepAliveSeconds(int keepAliveSeconds) { this.keepAliveSeconds = keepAliveSeconds; }
        public int getConnectionTimeoutSeconds() { return connectionTimeoutSeconds; }
        public void setConnectionTimeoutSeconds(int connectionTimeoutSeconds) { this.connectionTimeoutSeconds = connectionTimeoutSeconds; }
        public String getProfile() { return profile; }
        public void setProfile(String profile) { this.profile = profile; }
        public Tls getTls() { return tls; }
        public void setTls(Tls tls) { this.tls = tls; }
    }

    public static class Tls {
        private String caPem;
        private String clientPem;
        private String clientKey;
        private boolean hostnameVerificationEnabled = true;
        public String getCaPem() { return caPem; }
        public void setCaPem(String caPem) { this.caPem = caPem; }
        public String getClientPem() { return clientPem; }
        public void setClientPem(String clientPem) { this.clientPem = clientPem; }
        public String getClientKey() { return clientKey; }
        public void setClientKey(String clientKey) { this.clientKey = clientKey; }
        public boolean isHostnameVerificationEnabled() { return hostnameVerificationEnabled; }
        public void setHostnameVerificationEnabled(boolean hostnameVerificationEnabled) {
            this.hostnameVerificationEnabled = hostnameVerificationEnabled;
        }
    }
}
