package com.correx.core.events.serialization

import com.correx.core.events.events.ClarificationAnswer
import com.correx.core.events.events.ClarificationAnsweredEvent
import com.correx.core.events.events.ClarificationQuestion
import com.correx.core.events.events.ClarificationRequestedEvent
import com.correx.core.events.events.EventPayload
import com.correx.core.events.types.ClarificationRequestId
import com.correx.core.events.types.SessionId
import com.correx.core.events.types.StageId
import kotlin.test.Test
import kotlin.test.assertEquals
import kotlin.test.assertTrue

class ClarificationEventSerializationTest {

    @Test
    fun `ClarificationRequestedEvent round-trips as polymorphic EventPayload`() {
        val sample: EventPayload = ClarificationRequestedEvent(
            requestId = ClarificationRequestId("c1"),
            sessionId = SessionId("s"),
            stageId = StageId("analyst"),
            questions = listOf(
                ClarificationQuestion(
                    id = "q1",
                    prompt = "Which frontend stack?",
                    options = listOf("React", "Vue", "Svelte"),
                    multiSelect = false,
                    header = "Stack",
                ),
                ClarificationQuestion(id = "q2", prompt = "SPA or multi-page?"),
            ),
        )
        val encoded = eventJson.encodeToString(EventPayload.serializer(), sample)
        assertTrue(encoded.contains("\"type\":\"ClarificationRequested\""), "SerialName must be present: $encoded")
        val decoded = eventJson.decodeFromString(EventPayload.serializer(), encoded)
        assertEquals(sample, decoded)
    }

    @Test
    fun `ClarificationAnsweredEvent round-trips as polymorphic EventPayload`() {
        val sample: EventPayload = ClarificationAnsweredEvent(
            requestId = ClarificationRequestId("c1"),
            sessionId = SessionId("s"),
            stageId = StageId("analyst"),
            answers = listOf(
                ClarificationAnswer(questionId = "q1", value = "React"),
                ClarificationAnswer(questionId = "q2", value = "SPA"),
            ),
        )
        val encoded = eventJson.encodeToString(EventPayload.serializer(), sample)
        assertTrue(encoded.contains("\"type\":\"ClarificationAnswered\""), "SerialName must be present: $encoded")
        val decoded = eventJson.decodeFromString(EventPayload.serializer(), encoded)
        assertEquals(sample, decoded)
    }
}
