package cn.reghao.bnt.web.admin.security.session;

import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import jakarta.servlet.http.HttpSession;
import jakarta.servlet.http.HttpSessionEvent;
import jakarta.servlet.http.HttpSessionListener;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * HttpSession 事件监听, 引入 spring-session 后不再使用
 * 由于 MySQL 不像 Redis 那样拥有 pubsub 的接口, 因此 spring-session-jdbc 不支持 session 事件, spring-session-redis 是支持的
 *
 * @author reghao
 * @date 2025-10-01 22:10:35
 */
@Slf4j
//@Configuration
@Deprecated
public class HttpSessionManager {
    private static final Map<String, HttpSession> sessions = new HashMap<>();

    public List<HttpSession> getActiveSessions() {
        return new ArrayList<>(sessions.values());
    }

    public HttpSession getHttpSession(String sessionId) {
        return sessions.get(sessionId);
    }

    //@Bean
    public HttpSessionListener httpSessionListener() {
        return new HttpSessionListener() {
            @Override
            public void sessionCreated(HttpSessionEvent hse) {
                log.info("session created...");
                sessions.put(hse.getSession().getId(), hse.getSession());
            }

            @Override
            public void sessionDestroyed(HttpSessionEvent hse) {
                log.info("session destroyed...");
                sessions.remove(hse.getSession().getId());
            }
        };
    }
}
