package dev.lions.user.manager.client.view;

import dev.lions.user.manager.client.api.RoleRestClient;
import dev.lions.user.manager.dto.role.RoleDTO;
import jakarta.annotation.PostConstruct;
import jakarta.faces.application.FacesMessage;
import jakarta.faces.context.FacesContext;
import jakarta.faces.view.ViewScoped;
import jakarta.inject.Inject;
import jakarta.inject.Named;
import lombok.Getter;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;
import org.eclipse.microprofile.config.inject.ConfigProperty;
import org.eclipse.microprofile.rest.client.inject.RestClient;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

@Named
@ViewScoped
@Slf4j
public class RoleView implements Serializable {

    @Inject
    @RestClient
    RoleRestClient roleRestClient;

    @ConfigProperty(name = "lions.user.manager.default.realm", defaultValue = "test-realm")
    String defaultRealm;

    @Getter
    @Setter
    private List<RoleDTO> roles;

    @Getter
    @Setter
    private RoleDTO selectedRole;

    @Getter
    @Setter
    private String selectedRealm;

    @PostConstruct
    public void init() {
        this.selectedRealm = defaultRealm;
        this.selectedRole = new RoleDTO();
        loadRoles();
    }

    public void loadRoles() {
        try {
            roles = roleRestClient.getAllRealmRoles(selectedRealm);
        } catch (Exception e) {
            log.error("Error loading roles", e);
            roles = new ArrayList<>();
            FacesContext.getCurrentInstance().addMessage(null,
                    new FacesMessage(FacesMessage.SEVERITY_ERROR, "Erreur", "Impossible de charger les rôles"));
        }
    }

    public void openNew() {
        this.selectedRole = new RoleDTO();
    }

    public void saveRole() {
        try {
            if (this.selectedRole.getId() == null) {
                // Create
                roleRestClient.createRealmRole(selectedRealm, this.selectedRole);
                FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("Succès", "Rôle créé"));
            } else {
                // Update
                roleRestClient.updateRealmRole(this.selectedRole.getName(), selectedRealm, this.selectedRole);
                FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("Succès", "Rôle mis à jour"));
            }
            loadRoles();
        } catch (Exception e) {
            log.error("Error saving role", e);
            FacesContext.getCurrentInstance().addMessage(null,
                    new FacesMessage(FacesMessage.SEVERITY_ERROR, "Erreur", e.getMessage()));
        }
    }

    public void deleteRole() {
        try {
            roleRestClient.deleteRealmRole(this.selectedRole.getName(), selectedRealm);
            this.selectedRole = null;
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("Succès", "Rôle supprimé"));
            loadRoles();
        } catch (Exception e) {
            log.error("Error deleting role", e);
            FacesContext.getCurrentInstance().addMessage(null,
                    new FacesMessage(FacesMessage.SEVERITY_ERROR, "Erreur", e.getMessage()));
        }
    }
}
