package com.correx.core.events.events

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

/**
 * The architect's validated execution_plan was compiled into a runnable WorkflowGraph and
 * locked as the baseline for phase 2. [planArtifactId] points at the validated plan JSON in
 * CAS (referenced, not inlined); [stageIds]/[startStageId] pin the compiled shape so replay
 * rebuilds the identical graph. Soft lock: later high-priority steering can still preempt.
 */
@Serializable
@SerialName("ExecutionPlanLocked")
data class ExecutionPlanLockedEvent(
    val sessionId: SessionId,
    val planArtifactId: ArtifactId,
    val workflowId: String,
    val stageIds: List<String>,
    val startStageId: String,
) : EventPayload

/**
 * The architect's execution_plan could not become the phase-2 graph: content missing,
 * compile/validation failure, or operator rejection at the plan-review gate. Terminal for
 * the freestyle run unless re-planned; recorded so replay explains why phase 2 never ran.
 */
@Serializable
@SerialName("ExecutionPlanRejected")
data class ExecutionPlanRejectedEvent(
    val sessionId: SessionId,
    val reason: String,
    /** "compile" | "missing_content" | "operator" */
    val source: String,
) : EventPayload

/**
 * Environment observation (invariant #9): the outcome of compiling a stage's produced execution_plan
 * as a post-stage gate. Recorded at the moment the architect stage completes so replay reads the
 * recorded verdict and never re-compiles. A non-null [error] fails the producing stage retryably,
 * feeding the compiler message back so the architect self-corrects via the retry-feedback loop
 * (rather than parking the session in ACTIVE forever, as a post-planning compile failure did).
 */
@Serializable
@SerialName("PlanCompileChecked")
data class PlanCompileCheckedEvent(
    val sessionId: SessionId,
    val stageId: StageId,
    val passed: Boolean,
    /** Compiler error when [passed] is false; null on success. Kept bounded. */
    val error: String? = null,
) : EventPayload
