package dev.mokksy.aimocks.a2a.notifications

import dev.mokksy.aimocks.a2a.model.TaskId
import dev.mokksy.aimocks.a2a.model.TaskUpdateEvent
import java.util.concurrent.ConcurrentLinkedDeque

public class TaskNotificationHistory(
    public val taskId: TaskId,
) {
    private val events: ConcurrentLinkedDeque<TaskUpdateEvent> =
        ConcurrentLinkedDeque<TaskUpdateEvent>()

    internal fun add(event: TaskUpdateEvent) {
        events.offer(event)
    }

    internal fun clear() {
        events.clear()
    }

    public fun events(): List<TaskUpdateEvent> = events.toList()

    internal fun isEmpty(): Boolean = events.isEmpty()

    internal fun isNotEmpty(): Boolean = !isEmpty()

    internal fun find(predicate: (TaskUpdateEvent) -> Boolean): TaskUpdateEvent? =
        events.find(predicate)

    internal fun extract(predicate: (TaskUpdateEvent) -> Boolean): List<TaskUpdateEvent> {
        val snapshot = events.toList()
        val found = snapshot.filter(predicate)
        events.removeAll(found.toSet())
        return found
    }
}
