package com.correx.core.critique

import com.correx.core.critique.model.CalibrationKey
import com.correx.core.critique.model.CalibrationStats
import com.correx.core.critique.model.CriticCalibrationState
import com.correx.core.events.events.CritiqueOutcome
import com.correx.core.events.events.CritiqueOutcomeCorrelatedEvent
import com.correx.core.events.events.StoredEvent

class DefaultCriticCalibrationReducer : CriticCalibrationReducer {
    override fun reduce(state: CriticCalibrationState, event: StoredEvent): CriticCalibrationState {
        val payload = event.payload as? CritiqueOutcomeCorrelatedEvent ?: return state
        val key = CalibrationKey(payload.modelHash, payload.role)
        val current = state.byKey[key] ?: CalibrationStats()
        val updated = when (payload.outcome) {
            CritiqueOutcome.UPHELD -> current.copy(upheld = current.upheld + 1)
            CritiqueOutcome.DISMISSED -> current.copy(dismissed = current.dismissed + 1)
            CritiqueOutcome.INCONCLUSIVE -> current.copy(inconclusive = current.inconclusive + 1)
        }
        return state.copy(byKey = state.byKey + (key to updated))
    }
}
