package com.correx.core.events.events

import com.correx.core.events.types.ClarificationRequestId
import com.correx.core.events.types.SessionId
import com.correx.core.events.types.StageId
import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable

/**
 * One open question a stage (e.g. the analyst) raised about the request, surfaced to the operator
 * before the stage's output is considered settled. [options] empty → free-text answer; non-empty →
 * the client renders selectable choices. [multiSelect] permits more than one choice. [header] is a
 * short chip label for the question.
 */
@Serializable
data class ClarificationQuestion(
    val id: String,
    val prompt: String,
    val options: List<String> = emptyList(),
    val multiSelect: Boolean = false,
    val header: String = "",
)

/** The operator's answer to a single [ClarificationQuestion], keyed by [questionId]. */
@Serializable
data class ClarificationAnswer(
    val questionId: String,
    val value: String,
)

/**
 * A stage produced an artifact carrying open questions; the orchestrator paused the stage and is
 * awaiting operator answers. The parsed [questions] are recorded at the moment they are observed
 * (invariant #9), so replay reads the recorded list rather than re-parsing artifact content.
 */
@Serializable
@SerialName("ClarificationRequested")
data class ClarificationRequestedEvent(
    val requestId: ClarificationRequestId,
    val sessionId: SessionId,
    val stageId: StageId,
    val questions: List<ClarificationQuestion>,
) : EventPayload

/**
 * The operator answered a [ClarificationRequestedEvent]. The orchestrator injects these answers as
 * context and re-runs the producing stage. Terminal for the clarification request.
 */
@Serializable
@SerialName("ClarificationAnswered")
data class ClarificationAnsweredEvent(
    val requestId: ClarificationRequestId,
    val sessionId: SessionId,
    val stageId: StageId,
    val answers: List<ClarificationAnswer>,
) : EventPayload
