package dev.lions.unionflow.client.view;

import dev.lions.unionflow.client.security.JwtTokenManager;
import dev.lions.unionflow.client.security.TokenRefreshService;
import jakarta.enterprise.context.RequestScoped;
import jakarta.inject.Inject;
import jakarta.inject.Named;
import java.io.Serializable;

@Named("securityStatusBean")
@RequestScoped
public class SecurityStatusBean implements Serializable {
    
    private static final long serialVersionUID = 1L;
    
    @Inject
    private JwtTokenManager tokenManager;
    
    @Inject
    private TokenRefreshService tokenRefreshService;
    
    @Inject
    private UserSession userSession;
    
    public String getSecurityStatusIcon() {
        if (!userSession.isAuthenticated() || !tokenManager.hasValidTokens()) {
            return "pi-shield text-red-500";
        }
        
        long timeLeft = tokenManager.getTimeUntilExpiration();
        if (timeLeft < 300) { // Moins de 5 minutes
            return "pi-exclamation-triangle text-orange-500";
        } else if (timeLeft < 900) { // Moins de 15 minutes
            return "pi-clock text-yellow-500";
        } else {
            return "pi-shield text-green-500";
        }
    }
    
    public String getSecurityStatusMessage() {
        if (!userSession.isAuthenticated() || !tokenManager.hasValidTokens()) {
            return "Session expirée";
        }
        
        long timeLeft = tokenManager.getTimeUntilExpiration();
        long minutes = timeLeft / 60;
        
        if (timeLeft < 300) {
            return "Session expire dans " + minutes + " min";
        } else if (timeLeft < 900) {
            return "Session active (" + minutes + " min restantes)";
        } else {
            return "Session sécurisée";
        }
    }
    
    public String getSecurityStatusSeverity() {
        if (!userSession.isAuthenticated() || !tokenManager.hasValidTokens()) {
            return "danger";
        }
        
        long timeLeft = tokenManager.getTimeUntilExpiration();
        if (timeLeft < 300) {
            return "warning";
        } else if (timeLeft < 900) {
            return "info";
        } else {
            return "success";
        }
    }
    
    public void refreshTokenManually() {
        try {
            String sessionId = getSessionId();
            if (sessionId != null && tokenRefreshService.tryRefreshTokenNow(sessionId)) {
                // Token rafraîchi avec succès
            } else {
                // Échec du rafraîchissement
            }
        } catch (Exception e) {
            // Gestion d'erreur
        }
    }
    
    private String getSessionId() {
        try {
            jakarta.faces.context.FacesContext facesContext = 
                jakarta.faces.context.FacesContext.getCurrentInstance();
            if (facesContext != null && facesContext.getExternalContext() != null) {
                return facesContext.getExternalContext().getSessionId(false);
            }
        } catch (Exception e) {
            // Contexte non disponible
        }
        return null;
    }
    
    public boolean isTokenExpiringSoon() {
        return tokenManager.getTimeUntilExpiration() < 900; // 15 minutes
    }
    
    public boolean isTokenCritical() {
        return tokenManager.getTimeUntilExpiration() < 300; // 5 minutes
    }
    
    public int getTokenExpirationMinutes() {
        return (int) (tokenManager.getTimeUntilExpiration() / 60);
    }
}
