package com.correx.core.tasks

import com.correx.core.events.types.SessionId
import com.correx.core.events.types.TaskId
import com.correx.core.sessions.projections.replay.EventReplayer

/**
 * Reads a project's task board by replaying its stream. Mirrors `DefaultSessionRepository`:
 * the repository is a thin read model over an [EventReplayer]; the event log is the source
 * of truth and the board is rebuilt from it.
 */
class DefaultTaskRepository(
    private val replayer: EventReplayer<TaskBoard>,
    private val streamId: SessionId,
) {

    fun board(): TaskBoard = replayer.rebuild(streamId)

    fun getTask(taskId: TaskId): Task? =
        board()[taskId]?.takeUnless { it.deleted }?.let { Task(taskId, it) }

    fun list(): List<Task> =
        board().filterValues { !it.deleted }.map { (id, state) -> Task(id, state) }

    fun count(): Int = list().size
}
