package com.lingniu.ingest.core.config;

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

@ConfigurationProperties(prefix = "lingniu.ingest.pipeline")
public class IngestCoreProperties {

    private Disruptor disruptor = new Disruptor();
    private Dedup dedup = new Dedup();
    private RateLimit rateLimit = new RateLimit();

    public Disruptor getDisruptor() { return disruptor; }
    public void setDisruptor(Disruptor disruptor) { this.disruptor = disruptor; }

    public Dedup getDedup() { return dedup; }
    public void setDedup(Dedup dedup) { this.dedup = dedup; }

    public RateLimit getRateLimit() { return rateLimit; }
    public void setRateLimit(RateLimit rateLimit) { this.rateLimit = rateLimit; }

    public static class Disruptor {
        /** 事件总线 ring buffer 大小；生产环境应保持 2 的幂，避免 Disruptor 初始化失败。 */
        private int ringBufferSize = 131072;
        /** waiting 策略直接影响延迟和 CPU 占用，默认 yielding 偏低延迟。 */
        private String waitStrategy = "yielding";
        private String producerType = "multi";

        public int getRingBufferSize() { return ringBufferSize; }
        public void setRingBufferSize(int ringBufferSize) { this.ringBufferSize = ringBufferSize; }
        public String getWaitStrategy() { return waitStrategy; }
        public void setWaitStrategy(String waitStrategy) { this.waitStrategy = waitStrategy; }
        public String getProducerType() { return producerType; }
        public void setProducerType(String producerType) { this.producerType = producerType; }
    }

    public static class Dedup {
        private boolean enabled = true;
        /** 单实例缓存的幂等键数量上限，按 VIN 数和重发窗口估算。 */
        private int cacheSize = 200000;
        /** 幂等键保留窗口；过短会放过重发包，过长会增加内存压力。 */
        private long ttlSeconds = 600;

        public boolean isEnabled() { return enabled; }
        public void setEnabled(boolean enabled) { this.enabled = enabled; }
        public int getCacheSize() { return cacheSize; }
        public void setCacheSize(int cacheSize) { this.cacheSize = cacheSize; }
        public long getTtlSeconds() { return ttlSeconds; }
        public void setTtlSeconds(long ttlSeconds) { this.ttlSeconds = ttlSeconds; }
    }

    public static class RateLimit {
        /** 每 VIN 每秒进入 Handler 的最大帧数，默认按 32960 高频实时上报预留。 */
        private int perVinQps = 50;
        /** VIN limiter 缓存上限；覆盖活跃车辆数即可，淘汰后会重新创建 limiter。 */
        private int maxVins = 100000;

        public int getPerVinQps() { return perVinQps; }
        public void setPerVinQps(int perVinQps) { this.perVinQps = perVinQps; }
        public int getMaxVins() { return maxVins; }
        public void setMaxVins(int maxVins) { this.maxVins = maxVins; }
    }
}
