package com.correx.core.events.serialization

import com.correx.core.events.events.AgentInstructionsBoundEvent
import com.correx.core.events.events.ApprovalDecisionResolvedEvent
import com.correx.core.events.events.ApprovalGrantCreatedEvent
import com.correx.core.events.events.ApprovalGrantExpiredEvent
import com.correx.core.events.events.ApprovalRequestedEvent
import com.correx.core.events.events.ArtifactContentStoredEvent
import com.correx.core.events.events.ArtifactCreatedEvent
import com.correx.core.events.events.ArtifactRepairAttemptedEvent
import com.correx.core.events.events.ArtifactRepairFailedEvent
import com.correx.core.events.events.ArtifactRepairResolvedEvent
import com.correx.core.events.events.ArtifactValidatedEvent
import com.correx.core.events.events.ArtifactValidatingEvent
import com.correx.core.events.events.BriefEchoMismatchEvent
import com.correx.core.events.events.BriefGroundingCheckedEvent
import com.correx.core.events.events.ConceptPromotedEvent
import com.correx.core.events.events.StaticAnalysisCompletedEvent
import com.correx.core.events.events.LspDiagnosticsCompletedEvent
import com.correx.core.events.events.ContractGateEvaluatedEvent
import com.correx.core.events.events.PlanLintCompletedEvent
import com.correx.core.events.events.ChatSessionStartedEvent
import com.correx.core.events.events.SessionWorkingTaskEvent
import com.correx.core.events.events.ClarificationAnsweredEvent
import com.correx.core.events.events.ClarificationRequestedEvent
import com.correx.core.events.events.ChatTurnEvent
import com.correx.core.events.events.CritiqueFindingsRecordedEvent
import com.correx.core.events.events.CritiqueOutcomeCorrelatedEvent
import com.correx.core.events.events.TalkieNarrationEvent
import com.correx.core.events.events.OperatorProfileBoundEvent
import com.correx.core.events.events.ProjectProfileBoundEvent
import com.correx.core.events.events.SessionWorkspaceBoundEvent
import com.correx.core.events.events.ContextAssembledEvent
import com.correx.core.events.events.ContextTruncatedEvent
import com.correx.core.events.events.PossibleContradictionFlaggedEvent
import com.correx.core.events.events.EgressHostsGrantedEvent
import com.correx.core.events.events.EventPayload
import com.correx.core.events.events.ExecutionPlanLockedEvent
import com.correx.core.events.events.PlanCompileCheckedEvent
import com.correx.core.events.events.CapabilityGapDetectedEvent
import com.correx.core.events.events.CapabilityGapReflectedEvent
import com.correx.core.events.events.ReviewFindingsRaisedEvent
import com.correx.core.events.events.ExecutionPlanRejectedEvent
import com.correx.core.events.events.HealthDegradedEvent
import com.correx.core.events.events.HealthRestoredEvent
import com.correx.core.events.events.IdeaCapturedEvent
import com.correx.core.events.events.IdeaDiscardedEvent
import com.correx.core.events.events.IdeaPromotedEvent
import com.correx.core.events.events.JournalCompactedEvent
import com.correx.core.events.events.FileWrittenEvent
import com.correx.core.events.events.L3MemoryRetrievedEvent
import com.correx.core.events.events.LowQualityExtractionEvent
import com.correx.core.events.events.InferenceCompletedEvent
import com.correx.core.events.events.InitialIntentEvent
import com.correx.core.events.events.SessionNamedEvent
import com.correx.core.events.events.InferenceFailedEvent
import com.correx.core.events.events.InferenceStartedEvent
import com.correx.core.events.events.InferenceTimeoutEvent
import com.correx.core.events.events.ModelLoadedEvent
import com.correx.core.events.events.ModelUnloadedEvent
import com.correx.core.events.events.OrchestrationPausedEvent
import com.correx.core.events.events.OrchestrationResumedEvent
import com.correx.core.events.events.RefinementIterationEvent
import com.correx.core.events.events.RepoKnowledgeHitsFilteredEvent
import com.correx.core.events.events.RepoKnowledgeRetrievedEvent
import com.correx.core.events.events.RepoMapComputedEvent
import com.correx.core.events.events.RetryAttemptedEvent
import com.correx.core.events.events.RetrySalvageDecidedEvent
import com.correx.core.events.events.PostFailureDiagnosedEvent
import com.correx.core.events.events.FailureTicketOpenedEvent
import com.correx.core.events.events.BuildPrerequisiteBootstrapAttemptedEvent
import com.correx.core.events.events.WorkspaceVerificationObservedEvent
import com.correx.core.events.events.PlanGroundingEvaluatedEvent
import com.correx.core.events.events.OutsidePathAccessGrantedEvent
import com.correx.core.events.events.WriteScopeGrantedEvent
import com.correx.core.events.events.WorkspaceStateObservedEvent
import com.correx.core.events.events.RiskAssessedEvent
import com.correx.core.events.events.SourceFetchedEvent
import com.correx.core.events.events.StageCheckpointFailedEvent
import com.correx.core.events.events.StageCheckpointPassedEvent
import com.correx.core.events.events.StageCompletedEvent
import com.correx.core.events.events.PreemptRedirectBlockedEvent
import com.correx.core.events.events.PreemptRedirectEvent
import com.correx.core.events.events.StageFailedEvent
import com.correx.core.events.events.ContextSummarizedEvent
import com.correx.core.events.events.SteeringNoteAddedEvent
import com.correx.core.events.events.ToolCallAssessedEvent
import com.correx.core.events.events.ToolExecutionCompletedEvent
import com.correx.core.events.events.ToolExecutionFailedEvent
import com.correx.core.events.events.ToolExecutionRejectedEvent
import com.correx.core.events.events.ToolExecutionStartedEvent
import com.correx.core.events.events.ToolInvocationRequestedEvent
import com.correx.core.events.events.ToolInvokedEvent
import com.correx.core.events.events.TransitionExecutedEvent
import com.correx.core.events.events.WorkflowCompletedEvent
import com.correx.core.events.events.WorkflowFailedEvent
import com.correx.core.events.events.WorkflowProposedEvent
import com.correx.core.events.events.WorkflowStartedEvent
import com.correx.core.events.events.RunBranchPushedEvent
import com.correx.core.events.events.TaskCreatedEvent
import com.correx.core.events.events.TaskClaimedEvent
import com.correx.core.events.events.TaskReleasedEvent
import com.correx.core.events.events.TaskBlockedEvent
import com.correx.core.events.events.TaskUnblockedEvent
import com.correx.core.events.events.TaskSubmittedForReviewEvent
import com.correx.core.events.events.TaskCompletedEvent
import com.correx.core.events.events.TaskReopenedEvent
import com.correx.core.events.events.TaskCancelledEvent
import com.correx.core.events.events.TaskEditedEvent
import com.correx.core.events.events.TaskAcceptanceCriteriaSetEvent
import com.correx.core.events.events.TaskAffectedPathsSetEvent
import com.correx.core.events.events.TaskLinkedEvent
import com.correx.core.events.events.TaskUnlinkedEvent
import com.correx.core.events.events.TaskNoteAddedEvent
import com.correx.core.events.events.TaskDeletedEvent
import kotlinx.serialization.json.Json
import kotlinx.serialization.modules.SerializersModule
import kotlinx.serialization.modules.polymorphic
import kotlinx.serialization.modules.subclass

val eventModule = SerializersModule {
    polymorphic(EventPayload::class) {
        // Legacy stub event — predates the full tool lifecycle model (ToolInvocationRequestedEvent et al.)
        subclass(ToolInvokedEvent::class)
        subclass(ToolInvocationRequestedEvent::class)
        subclass(ToolExecutionStartedEvent::class)
        subclass(ToolExecutionCompletedEvent::class)
        subclass(ToolExecutionFailedEvent::class)
        subclass(ToolExecutionRejectedEvent::class)
        subclass(ToolCallAssessedEvent::class)
        subclass(FileWrittenEvent::class)
        subclass(SteeringNoteAddedEvent::class)
        subclass(ContextSummarizedEvent::class)
        subclass(PreemptRedirectEvent::class)
        subclass(PreemptRedirectBlockedEvent::class)
        subclass(InitialIntentEvent::class)
        subclass(SessionNamedEvent::class)
        subclass(StageFailedEvent::class)
        subclass(StageCompletedEvent::class)
        subclass(ConceptPromotedEvent::class)
        subclass(TransitionExecutedEvent::class)
        subclass(ApprovalRequestedEvent::class)
        subclass(ApprovalDecisionResolvedEvent::class)
        subclass(ApprovalGrantCreatedEvent::class)
        subclass(ApprovalGrantExpiredEvent::class)
        subclass(ArtifactContentStoredEvent::class)
        subclass(ArtifactRepairAttemptedEvent::class)
        subclass(ArtifactRepairResolvedEvent::class)
        subclass(ArtifactRepairFailedEvent::class)
        subclass(ArtifactCreatedEvent::class)
        subclass(ArtifactValidatingEvent::class)
        subclass(ArtifactValidatedEvent::class)
        subclass(InferenceFailedEvent::class)
        subclass(InferenceCompletedEvent::class)
        subclass(InferenceStartedEvent::class)
        subclass(InferenceTimeoutEvent::class)
        subclass(ModelLoadedEvent::class)
        subclass(ModelUnloadedEvent::class)
        subclass(OrchestrationResumedEvent::class)
        subclass(OrchestrationPausedEvent::class)
        subclass(WorkflowStartedEvent::class)
        subclass(RunBranchPushedEvent::class)
        subclass(WorkflowFailedEvent::class)
        subclass(WorkflowCompletedEvent::class)
        subclass(RetryAttemptedEvent::class)
        subclass(RetrySalvageDecidedEvent::class)
        subclass(PostFailureDiagnosedEvent::class)
        subclass(FailureTicketOpenedEvent::class)
        subclass(BuildPrerequisiteBootstrapAttemptedEvent::class)
        subclass(WorkspaceVerificationObservedEvent::class)
        subclass(PlanGroundingEvaluatedEvent::class)
        subclass(OutsidePathAccessGrantedEvent::class)
        subclass(WriteScopeGrantedEvent::class)
        subclass(RefinementIterationEvent::class)
        subclass(RepoMapComputedEvent::class)
        subclass(WorkspaceStateObservedEvent::class)
        subclass(RepoKnowledgeRetrievedEvent::class)
        subclass(RepoKnowledgeHitsFilteredEvent::class)
        subclass(BriefGroundingCheckedEvent::class)
        subclass(BriefEchoMismatchEvent::class)
        subclass(StaticAnalysisCompletedEvent::class)
        subclass(LspDiagnosticsCompletedEvent::class)
        subclass(ContractGateEvaluatedEvent::class)
        subclass(PlanLintCompletedEvent::class)
        subclass(RiskAssessedEvent::class)
        subclass(ChatSessionStartedEvent::class)
        subclass(SessionWorkingTaskEvent::class)
        subclass(ChatTurnEvent::class)
        subclass(TalkieNarrationEvent::class)
        subclass(SessionWorkspaceBoundEvent::class)
        subclass(OperatorProfileBoundEvent::class)
        subclass(ProjectProfileBoundEvent::class)
        subclass(AgentInstructionsBoundEvent::class)
        subclass(L3MemoryRetrievedEvent::class)
        subclass(ContextTruncatedEvent::class)
        subclass(ContextAssembledEvent::class)
        subclass(ExecutionPlanLockedEvent::class)
        subclass(ExecutionPlanRejectedEvent::class)
        subclass(PlanCompileCheckedEvent::class)
        subclass(CapabilityGapDetectedEvent::class)
        subclass(CapabilityGapReflectedEvent::class)
        subclass(ReviewFindingsRaisedEvent::class)
        subclass(JournalCompactedEvent::class)
        subclass(HealthDegradedEvent::class)
        subclass(HealthRestoredEvent::class)
        subclass(ClarificationRequestedEvent::class)
        subclass(ClarificationAnsweredEvent::class)
        subclass(WorkflowProposedEvent::class)
        subclass(IdeaCapturedEvent::class)
        subclass(IdeaDiscardedEvent::class)
        subclass(IdeaPromotedEvent::class)
        subclass(CritiqueOutcomeCorrelatedEvent::class)
        subclass(CritiqueFindingsRecordedEvent::class)
        subclass(StageCheckpointPassedEvent::class)
        subclass(StageCheckpointFailedEvent::class)
        subclass(PossibleContradictionFlaggedEvent::class)
        subclass(SourceFetchedEvent::class)
        subclass(LowQualityExtractionEvent::class)
        subclass(EgressHostsGrantedEvent::class)
        subclass(TaskCreatedEvent::class)
        subclass(TaskClaimedEvent::class)
        subclass(TaskReleasedEvent::class)
        subclass(TaskBlockedEvent::class)
        subclass(TaskUnblockedEvent::class)
        subclass(TaskSubmittedForReviewEvent::class)
        subclass(TaskCompletedEvent::class)
        subclass(TaskReopenedEvent::class)
        subclass(TaskCancelledEvent::class)
        subclass(TaskEditedEvent::class)
        subclass(TaskAcceptanceCriteriaSetEvent::class)
        subclass(TaskAffectedPathsSetEvent::class)
        subclass(TaskLinkedEvent::class)
        subclass(TaskUnlinkedEvent::class)
        subclass(TaskNoteAddedEvent::class)
        subclass(TaskDeletedEvent::class)
    }
}

val eventJson = Json {
    serializersModule = eventModule
    encodeDefaults = true
    ignoreUnknownKeys = true
}
