package com.pressing.tenant;

public final class TenantContext {

    private static final ThreadLocal<Long> CURRENT_SITE = new ThreadLocal<>();
    private static final ThreadLocal<String> CURRENT_USER = new ThreadLocal<>();

    private TenantContext() {
    }

    public static void setSiteId(Long siteId) {
        CURRENT_SITE.set(siteId);
    }

    public static Long getSiteId() {
        return CURRENT_SITE.get();
    }

    public static Long requireSiteId() {
        Long siteId = CURRENT_SITE.get();
        if (siteId == null) {
            throw new IllegalStateException("Aucun site sélectionné. Envoyez l'en-tête X-Site-Id.");
        }
        return siteId;
    }

    public static void setUsername(String username) {
        CURRENT_USER.set(username);
    }

    public static String getUsername() {
        return CURRENT_USER.get();
    }

    public static void clear() {
        CURRENT_SITE.remove();
        CURRENT_USER.remove();
    }
}
