package dev.lions.unionflow.client.converter;

import jakarta.enterprise.context.ApplicationScoped;
import jakarta.faces.component.UIComponent;
import jakarta.faces.context.FacesContext;
import jakarta.faces.convert.Converter;
import jakarta.faces.convert.FacesConverter;
import jakarta.inject.Named;
import dev.lions.unionflow.client.view.DemandesBean.Membre;
import java.util.UUID;

@Named
@ApplicationScoped
@FacesConverter(value = "membreConverter", managed = true)
public class MembreConverter implements Converter<Membre> {

    @Override
    public Membre getAsObject(FacesContext context, UIComponent component, String value) {
        if (value == null || value.trim().isEmpty()) {
            return null;
        }
        
        try {
            // Parse the membre ID from the string value (UUID)
            UUID membreId = UUID.fromString(value);
            
            // Create a simple Membre object with just the ID
            // In a real implementation, you would fetch from database
            Membre membre = new Membre();
            membre.setId(membreId);
            return membre;
        } catch (IllegalArgumentException e) {
            return null;
        }
    }

    @Override
    public String getAsString(FacesContext context, UIComponent component, Membre value) {
        if (value == null || value.getId() == null) {
            return "";
        }
        return value.getId().toString();
    }
}
