package com.liquidnet.service.adam.support;

import com.liquidnet.common.cache.redis.util.RedisUtil;
import com.liquidnet.commons.lang.core.JwtValidator;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * 用户 JTI 会话：adam:session:{jti}=uid，adam:session:user:{uid} 记录活跃 jti。
 */
@Component
public class AdamUserSessionSupport {

    @Autowired
    private RedisUtil redisUtil;
    @Autowired
    private JwtValidator jwtValidator;

    public void bindSession(String jti, String uid) {
        long sessionTtlSec = jwtValidator.getExpireTtl() * 60;
        redisUtil.set(jwtValidator.sessionKey(jti), uid, sessionTtlSec);
        redisUtil.sSetAndTime(jwtValidator.userSessionsKey(uid), sessionTtlSec, jti);
    }

    public void unbindSession(String jti, String uid) {
        if (StringUtils.isBlank(jti)) {
            return;
        }
        redisUtil.del(jwtValidator.sessionKey(jti));
        if (StringUtils.isNotBlank(uid)) {
            redisUtil.setRemove(jwtValidator.userSessionsKey(uid), jti);
        }
    }
}
