package com.correx.core.router

import com.correx.core.events.events.ChatTurnEvent
import com.correx.core.events.events.L3MemoryRetrievedEvent
import com.correx.core.events.events.OrchestrationPausedEvent
import com.correx.core.events.events.OrchestrationResumedEvent
import com.correx.core.events.events.StageCompletedEvent
import com.correx.core.events.events.StageFailedEvent
import com.correx.core.events.events.SteeringNoteAddedEvent
import com.correx.core.events.events.StoredEvent
import com.correx.core.events.events.WorkflowCompletedEvent
import com.correx.core.events.events.WorkflowFailedEvent
import com.correx.core.events.events.WorkflowStartedEvent
import com.correx.core.router.model.RouterL2Entry
import com.correx.core.router.model.RouterState
import com.correx.core.router.model.RouterTurn
import com.correx.core.router.model.StageOutcomeKind
import com.correx.core.router.model.TurnRole
import com.correx.core.router.model.WorkflowStatus
import kotlinx.datetime.Instant

interface RouterReducer {
    val initial: RouterState
    fun reduce(state: RouterState, event: StoredEvent): RouterState
}

class DefaultRouterReducer : RouterReducer {

    override val initial: RouterState = RouterState(
        sessionId = null,
        workflowStatus = WorkflowStatus.IDLE,
        currentStageId = null,
        l2Memory = emptyList(),
        conversationHistory = emptyList(),
    )

    override fun reduce(state: RouterState, event: StoredEvent): RouterState {
        return when (val payload = event.payload) {
            is WorkflowStartedEvent -> handleWorkflowStarted(state, event)
            is WorkflowCompletedEvent -> handleWorkflowCompleted(state)
            is WorkflowFailedEvent -> handleWorkflowFailed(state)
            is OrchestrationPausedEvent -> handleOrchestrationPaused(state)
            is OrchestrationResumedEvent -> handleOrchestrationResumed(state)
            is StageCompletedEvent -> handleStageCompleted(state, event)
            is StageFailedEvent -> handleStageFailed(state, event)
            is SteeringNoteAddedEvent -> handleSteeringNote(state, payload)
            is ChatTurnEvent -> handleChatTurn(state, event)
            is L3MemoryRetrievedEvent -> state.copy(lastRetrievedMemory = payload.hits)
            else -> state
        }
    }

    private fun handleWorkflowStarted(state: RouterState, event: StoredEvent): RouterState {
        val payload = event.payload as WorkflowStartedEvent
        return state.copy(
            sessionId = payload.sessionId,
            workflowStatus = WorkflowStatus.RUNNING,
            currentStageId = payload.startStageId
        )
    }

    private fun handleWorkflowCompleted(state: RouterState): RouterState {
        return state.copy(
            workflowStatus = WorkflowStatus.COMPLETED,
            currentStageId = null
        )
    }

    private fun handleWorkflowFailed(state: RouterState): RouterState {
        return state.copy(
            workflowStatus = WorkflowStatus.FAILED,
            currentStageId = null
        )
    }

    private fun handleOrchestrationPaused(state: RouterState): RouterState {
        return state.copy(
            workflowStatus = WorkflowStatus.PAUSED
        )
    }

    private fun handleOrchestrationResumed(state: RouterState): RouterState {
        return state.copy(
            workflowStatus = WorkflowStatus.RUNNING
        )
    }

    private fun handleStageCompleted(state: RouterState, event: StoredEvent): RouterState {
        val payload = event.payload as StageCompletedEvent
        val entry = RouterL2Entry(
            stageId = payload.stageId,
            summary = "Stage ${payload.stageId.value} completed",
            outcome = StageOutcomeKind.SUCCESS,
            timestamp = event.metadata.timestamp,
            steeringNotes = state.pendingSteeringNotes,
        )
        return state.copy(
            l2Memory = state.l2Memory + entry,
            pendingSteeringNotes = emptyList(),
        )
    }

    private fun handleStageFailed(state: RouterState, event: StoredEvent): RouterState {
        val payload = event.payload as StageFailedEvent
        val entry = RouterL2Entry(
            stageId = payload.stageId,
            summary = "Stage ${payload.stageId.value} failed",
            outcome = StageOutcomeKind.FAILURE,
            timestamp = event.metadata.timestamp,
            reason = payload.reason,
            steeringNotes = state.pendingSteeringNotes,
        )
        return state.copy(
            l2Memory = state.l2Memory + entry,
            currentStageId = null,
            pendingSteeringNotes = emptyList(),
        )
    }

    // adr-0003 §4: harvest user steering into the current stage's decision point.
    // Accumulate here; handleStageCompleted/Failed attach and clear. Pure field
    // extraction from the event stream — no inference.
    private fun handleSteeringNote(state: RouterState, payload: SteeringNoteAddedEvent): RouterState =
        state.copy(pendingSteeringNotes = state.pendingSteeringNotes + payload.content)

    private fun handleChatTurn(state: RouterState, event: StoredEvent): RouterState {
        val payload = event.payload as ChatTurnEvent
        val turnRole = when (payload.role) {
            com.correx.core.events.events.ChatTurnRole.USER -> TurnRole.USER
            com.correx.core.events.events.ChatTurnRole.ROUTER -> TurnRole.ROUTER
        }
        val turn = RouterTurn(
            role = turnRole,
            content = payload.content,
            timestamp = Instant.fromEpochMilliseconds(payload.timestampMs),
            turnId = payload.turnId,
        )
        return state.copy(
            conversationHistory = state.conversationHistory + turn
        )
    }
}
