package ru.micord.ervu.account_applications.security.controller;

import java.util.stream.Collectors;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import ru.micord.ervu.account_applications.security.context.SecurityContext;
import ru.micord.ervu.account_applications.security.dto.SessionDto;
import ru.micord.ervu.account_applications.security.model.UserSession;
import ru.micord.ervu.account_applications.security.model.role.ErvuPermissionAuthority;


@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.userId(),
        userSession.name(),
        userSession.realm(),
        userSession.permissions()
            .stream()
            .map(ErvuPermissionAuthority::getAuthority)
            .collect(Collectors.toSet()),
        userSession.domainId()
    );
  }
}
