package com.correx.core.tasks

import com.correx.core.events.events.StoredEvent
import com.correx.core.events.events.TaskEvent
import com.correx.core.sessions.projections.Projection

/**
 * Folds a per-project task stream into a [TaskBoard]. Each event is routed to its owning
 * task by [TaskEvent.taskId]; non-task payloads leave the board untouched.
 */
class TaskBoardProjector(
    private val reducer: TaskReducer,
) : Projection<TaskBoard> {

    override fun initial(): TaskBoard = emptyMap()

    override fun apply(state: TaskBoard, event: StoredEvent): TaskBoard {
        val taskId = (event.payload as? TaskEvent)?.taskId ?: return state
        val current = state[taskId] ?: TaskState()
        return state + (taskId to reducer.reduce(current, event))
    }
}
