package com.bio.bio_backend.global.utils;

import java.util.concurrent.atomic.AtomicInteger;

public class OidUtil {
    private static final int MAX_SEQUENCE = 999;
    private static volatile Long lastTimestamp = null;
    private static AtomicInteger sequence = new AtomicInteger(0);
    private static final int SERVER_ID = resolveServerId();

    public static synchronized long generateOid() {
        long now = System.currentTimeMillis();

        if (lastTimestamp == null || now != lastTimestamp) {
            lastTimestamp = now;
            sequence.set(SERVER_ID);
        }

        int index = sequence.getAndAdd(2);

        if (index > MAX_SEQUENCE) {
            now += index / 1000;
            index = index % 1000;
        }

        return now * 1000L + index;
    }

    private static int resolveServerId() {
        try {
    		//	서버 이중화        	
//            String ip = InetAddress.getLocalHost().getHostAddress();
//            if (ip.contains(".162")) return 1;
//            if (ip.contains(".163")) return 0;
        } catch (Exception ignored) {}
        return 1;
    }
}
