package mx.gob.slp.baseadminweb.web.support;

import mx.gob.slp.baseadminweb.configuration.security.AuthenticatedWebUser;
import mx.gob.slp.baseadminweb.domain.model.AppUser;
import org.springframework.security.core.Authentication;

public record WebUserView(String username, String displayName) {

    public static WebUserView from(Authentication authentication) {
        if (authentication == null) {
            return new WebUserView("usuario", "Usuario");
        }

        Object principal = authentication.getPrincipal();
        if (principal instanceof AppUser appUser) {
            return new WebUserView(appUser.getUsername(), appUser.getDisplayName());
        }
        if (principal instanceof AuthenticatedWebUser webUser) {
            return new WebUserView(webUser.username(), webUser.displayName());
        }

        String name = authentication.getName();
        return new WebUserView(name, name);
    }
}