package com.correx.core.events

import com.correx.core.events.events.EventMetadata
import com.correx.core.events.events.EventPayload
import com.correx.core.events.events.NewEvent
import com.correx.core.events.stores.EventStore
import com.correx.core.events.types.CausationId
import com.correx.core.events.types.CorrelationId
import com.correx.core.events.types.EventId
import com.correx.core.events.types.SessionId
import kotlinx.datetime.Clock
import java.util.*

class EventDispatcher(private val eventStore: EventStore) {
    suspend fun emit(
        payload: EventPayload,
        sessionId: SessionId,
        causationId: CausationId? = null,
        correlationId: CorrelationId? = null,
    ) {
        val event = NewEvent(
            metadata = EventMetadata(
                eventId = EventId(UUID.randomUUID().toString()),
                sessionId = sessionId,
                timestamp = Clock.System.now(),
                schemaVersion = 1,
                causationId = causationId,
                correlationId = correlationId,
            ),
            payload = payload
        )
        eventStore.append(event)
    }
}
