package de.accso.flexinale.ticketing.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.stereotype.Component;

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

@Component
@Endpoint(id = "ticketingEventsPublished")
public class FlexinaleTicketingActuatorEndpointEventsPublished implements EventNotification {
    private final Queue<Event> eventsPublished = new ConcurrentLinkedQueue<>(); // event list is ordered by production time

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

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

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