package de.accso.flexinale.besucherportal.infrastructure;

import de.accso.flexinale.common.api.eventbus.EventNotification;
import de.accso.flexinale.common.api.event.Event;
import de.accso.flexinale.common.shared_kernel.Identifiable;
import org.springframework.boot.actuate.endpoint.annotation.Endpoint;
import org.springframework.boot.actuate.endpoint.annotation.ReadOperation;
import org.springframework.boot.actuate.endpoint.annotation.Selector;
import org.springframework.context.annotation.Profile;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Queue;
import java.util.concurrent.ConcurrentLinkedQueue;

@Component
@Profile({ "!testdata-backoffice & !testdata-ticketing" })
@Endpoint(id = "besucherportalEventsConsumed")
public class FlexinaleBesucherPortalActuatorEndpointEventsConsumed implements EventNotification {
    private final Queue<Event> eventsConsumed = new ConcurrentLinkedQueue<>(); // event list is ordered by consumption time

    @ReadOperation
    public List<Event> eventsConsumed() {
        return eventsConsumed.stream().toList();
    }

    @ReadOperation
    public List<Event> eventsConsumedFilteredBy(@Selector Identifiable.Id correlationId) {
        return eventsConsumed.stream().filter(event -> event.correlationId().equals(correlationId)).toList();
    }

    @Override
    public void notify(final Event event) {// might want to use a generic subscriber
        eventsConsumed.add(event);
    }
}
