package dev.lions.btpxpress.view;

import jakarta.enterprise.context.RequestScoped;
import jakarta.faces.application.FacesMessage;
import jakarta.faces.context.FacesContext;
import jakarta.inject.Named;
import lombok.Getter;
import lombok.Setter;

import java.io.Serializable;

@Named("loginView")
@RequestScoped
@Getter
@Setter
public class LoginView implements Serializable {

    private static final long serialVersionUID = 1L;

    private String username;
    private String password;
    private boolean rememberMe = false;

    public String login() {
        if (username == null || username.trim().isEmpty()) {
            addErrorMessage("Le nom d'utilisateur est requis");
            return null;
        }
        
        if (password == null || password.trim().isEmpty()) {
            addErrorMessage("Le mot de passe est requis");
            return null;
        }
        
        if ("admin".equals(username) && "admin".equals(password)) {
            addInfoMessage("Connexion réussie !");
            return "/dashboard?faces-redirect=true";
        } else {
            addErrorMessage("Nom d'utilisateur ou mot de passe incorrect");
            return null;
        }
    }

    private void addErrorMessage(String message) {
        FacesContext.getCurrentInstance().addMessage(null,
            new FacesMessage(FacesMessage.SEVERITY_ERROR, "Erreur", message));
    }

    private void addInfoMessage(String message) {
        FacesContext.getCurrentInstance().addMessage(null,
            new FacesMessage(FacesMessage.SEVERITY_INFO, "Information", message));
    }
}
