package com.correx.apps.server.tasks

import com.correx.core.events.stores.EventStore
import com.correx.core.events.types.SessionId
import com.correx.core.toolintent.SessionContextProjection
import com.correx.infrastructure.tools.task.SessionWrites

/**
 * Adapter for the [SessionWrites] port: folds a session's events through
 * [SessionContextProjection] and returns its writes (the recorded receipt.affectedEntities).
 * Powers the cite-before-claim gate. Replay-safe (reads the log, never live state).
 */
class EventStoreSessionWrites(private val eventStore: EventStore) : SessionWrites {

    override fun pathsWritten(sessionId: SessionId): Set<String> {
        val projection = SessionContextProjection(sessionId)
        return eventStore.read(sessionId)
            .fold(projection.initial()) { acc, event -> projection.apply(acc, event) }
            .writes
    }
}
