import com.correx.core.events.events.EventMetadata
import com.correx.core.events.events.NewEvent
import com.correx.core.events.events.RefinementIterationEvent
import com.correx.core.events.types.EventId
import com.correx.core.events.types.SessionId
import com.correx.core.kernel.orchestration.DefaultOrchestrationReducer
import com.correx.core.kernel.orchestration.OrchestrationProjector
import com.correx.core.sessions.projections.replay.DefaultEventReplayer
import com.correx.infrastructure.persistence.InMemoryEventStore
import kotlinx.coroutines.runBlocking
import kotlinx.datetime.Clock
import org.junit.jupiter.api.Assertions.assertEquals
import org.junit.jupiter.api.Test

class RefinementIterationReplayTest {

    private fun meta(sessionId: SessionId, id: String) =
        EventMetadata(EventId(id), sessionId, Clock.System.now(), 1, null, null)

    @Test
    fun `refinement iteration counter tracks last value per cycle`(): Unit = runBlocking {
        val sessionId = SessionId("s")
        val store = InMemoryEventStore()
        store.append(NewEvent(meta(sessionId, "i1"), RefinementIterationEvent(sessionId, "impl->review", 1, 3)))
        store.append(NewEvent(meta(sessionId, "i2"), RefinementIterationEvent(sessionId, "impl->review", 2, 3)))

        val replayer = DefaultEventReplayer(
            store,
            OrchestrationProjector(DefaultOrchestrationReducer()),
        )
        val state = replayer.rebuild(sessionId)

        assertEquals(2, state.refinementIterations["impl->review"])
    }
}
