package com.correx.apps.server.tasks

import com.correx.core.events.events.EventMetadata
import com.correx.core.events.events.NewEvent
import com.correx.core.events.events.SessionWorkingTaskEvent
import com.correx.core.events.stores.EventStore
import com.correx.core.events.types.EventId
import com.correx.core.events.types.SessionId
import com.correx.core.events.types.TaskId
import com.correx.infrastructure.tools.task.SessionFactRecorder
import kotlinx.datetime.Clock
import java.util.UUID

/**
 * Adapter for [SessionFactRecorder]: appends a [SessionWorkingTaskEvent] to the session's own stream
 * when it claims (or re-scopes) a task, so [com.correx.core.toolintent.SessionContextProjection]
 * folds it into the active task without scanning task streams.
 */
class EventStoreSessionFactRecorder(private val eventStore: EventStore) : SessionFactRecorder {

    override suspend fun recordWorkingTask(sessionId: SessionId, taskId: TaskId, affectedPaths: List<String>) {
        eventStore.append(
            NewEvent(
                metadata = EventMetadata(
                    eventId = EventId(UUID.randomUUID().toString()),
                    sessionId = sessionId,
                    timestamp = Clock.System.now(),
                    schemaVersion = 1,
                    causationId = null,
                    correlationId = null,
                ),
                payload = SessionWorkingTaskEvent(sessionId, taskId, affectedPaths),
            ),
        )
    }
}
