package com.gnx.telemedicine.config;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.core.session.SessionRegistry;
import org.springframework.security.core.session.SessionRegistryImpl;

/**
 * Session management configuration.
 * Configures session timeout and concurrent session limits.
 */
@Configuration
@EnableWebSecurity
@Slf4j
public class SessionManagementConfig {

    @Value("${security.session.timeout-seconds:1800}")
    private int sessionTimeoutSeconds; // Default: 30 minutes

    @Value("${security.session.max-concurrent-sessions:1}")
    private int maxConcurrentSessions; // Default: 1 session per user

    @Value("${security.session.max-sessions-prevents-login:false}")
    private boolean maxSessionsPreventsLogin; // If true, prevents login when max sessions reached

    /**
     * Session registry bean for tracking active sessions.
     * 
     * @return SessionRegistry instance
     */
    public SessionRegistry sessionRegistry() {
        return new SessionRegistryImpl();
    }

    /**
     * Gets session timeout in seconds.
     * 
     * @return Session timeout in seconds
     */
    public int getSessionTimeoutSeconds() {
        return sessionTimeoutSeconds;
    }

    /**
     * Gets maximum concurrent sessions allowed per user.
     * 
     * @return Maximum concurrent sessions
     */
    public int getMaxConcurrentSessions() {
        return maxConcurrentSessions;
    }

    /**
     * Checks if max sessions prevents login.
     * 
     * @return true if login is prevented when max sessions reached
     */
    public boolean isMaxSessionsPreventsLogin() {
        return maxSessionsPreventsLogin;
    }
}

