package com.psg.dlsysinfo.dl_sysinfo_server.security;

import org.springframework.core.convert.converter.Converter;
import org.springframework.security.oauth2.jwt.Jwt;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
public class JwtToCurrentUserConverter implements Converter<Jwt, CurrentUser> {

    @Override
    public CurrentUser convert(Jwt jwt) {
        // 👇 Try to get the "roles" claim as a list of strings
        var roles = jwt.getClaimAsStringList("roles");

        return new CurrentUser(
                jwt.getSubject(),              // username
                jwt.getClaim("displayname"),  // display name
                jwt.getClaim("idauth"),       // clientIdentifier
                jwt.getClaim("userId"),       // userId
                roles != null ? roles : List.of()  // roles fallback to empty list
        );
    }

}
