package ervu_dashboard.security.controller;

import java.util.stream.Collectors;

import ervu_dashboard.security.dto.SessionDto;
import ervu_dashboard.security.model.jwt.UserSession;
import ervu_dashboard.security.context.SecurityContext;
import ervu_dashboard.security.model.role.ErvuRoleAuthority;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class AuthorizationController {

  private final SecurityContext securityContext;

  public AuthorizationController(SecurityContext securityContext) {
    this.securityContext = securityContext;
  }

  @GetMapping("/session")
  public SessionDto getSession() {
    UserSession userSession = securityContext.getUserSession();
    return new SessionDto(
        userSession.name(),
        userSession.realm(),
        userSession.roles()
            .stream()
            .map(ErvuRoleAuthority::getAuthority)
            .collect(Collectors.toSet()),
        userSession.recruitmentId()
    );
  }
}
