package com.correx.core.sessions

import com.correx.core.events.events.SessionWorkspaceBoundEvent
import com.correx.core.events.events.StageCompletedEvent
import com.correx.core.events.events.StageFailedEvent
import com.correx.core.events.events.StoredEvent
import com.correx.core.events.events.TransitionExecutedEvent

class DefaultSessionReducer : SessionReducer {

    override fun reduce(
        state: SessionState,
        event: StoredEvent
    ): SessionState {

        val payload = event.payload

        val newStatus = when (payload) {
            is StageFailedEvent ->
                SessionStatus.FAILED

            is StageCompletedEvent,
            is TransitionExecutedEvent ->
                SessionStatus.ACTIVE

            else ->
                state.status
        }

        val createdAt = state.createdAt
            ?: event.metadata.timestamp

        val boundWorkspace = when (payload) {
            is SessionWorkspaceBoundEvent ->
                BoundWorkspace(
                    workspaceRoot = payload.workspaceRoot,
                    allowedPaths = payload.allowedPaths,
                )
            else -> state.boundWorkspace
        }

        return state.copy(
            status = newStatus,
            createdAt = createdAt,
            updatedAt = event.metadata.timestamp,
            boundWorkspace = boundWorkspace,
        )
    }
}
