package cloud.nstart.terravault.ordinis.app.config;

import cloud.nstart.terravault.ordinis.notifications.dispatcher.UserDisplayPort;
import cloud.nstart.terravault.ordinis.restapi.service.UserDisplayService;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * Wires {@link UserDisplayService} (ordinis-rest-api) into the notifications module's
 * {@link UserDisplayPort} interface (ordinis-notifications).
 *
 * <p>Lives in ordinis-app because that module has compile-time visibility over
 * both ordinis-rest-api and ordinis-notifications. This avoids a circular dependency
 * (notifications → rest-api → notifications).
 *
 * <p>Gated on {@code ordinis.notifications.enabled} — no unnecessary bean creation
 * when notifications module is dormant.
 */
@Configuration
@ConditionalOnProperty(name = "ordinis.notifications.enabled", havingValue = "true", matchIfMissing = false)
public class NotificationsUserDisplayBridge {

  @Bean
  public UserDisplayPort userDisplayPort(UserDisplayService userDisplayService) {
    return userId -> userDisplayService.find(userId)
        .map(info -> new UserDisplayPort.DisplayInfo(info.sub(), info.email(), info.name()));
  }
}
