package com.correx.core.events.serialization

import com.correx.core.events.events.EventPayload
import com.correx.core.events.events.OperatorProfileBoundEvent
import com.correx.core.events.types.SessionId
import kotlin.test.Test
import kotlin.test.assertEquals
import kotlin.test.assertTrue

class OperatorProfileBoundEventSerializationTest {

    private val sample = OperatorProfileBoundEvent(
        sessionId = SessionId("sess-1"),
        about = "A seasoned backend engineer who prefers concise, idiomatic Kotlin.",
        approvalMode = "auto",
        preferredModels = listOf("llama-3-70b", "qwen2.5-72b"),
        conventions = listOf("no var", "no mutable state"),
    )

    @Test
    fun `round-trips as polymorphic EventPayload`() {
        val encoded = eventJson.encodeToString(EventPayload.serializer(), sample)
        assertTrue(encoded.contains("\"type\":\"OperatorProfileBound\""), "SerialName must be present: $encoded")
        val decoded = eventJson.decodeFromString(EventPayload.serializer(), encoded)
        assertEquals(sample, decoded)
    }

    @Test
    fun `decodes hand-written OperatorProfileBound JSON`() {
        val json = """
            {"type":"OperatorProfileBound","sessionId":"s-99","about":"senior dev","approvalMode":"suggest","preferredModels":["model-a"],"conventions":["no null"]}
        """.trimIndent()
        val decoded = eventJson.decodeFromString(EventPayload.serializer(), json)
        assertTrue(decoded is OperatorProfileBoundEvent)
        decoded as OperatorProfileBoundEvent
        assertEquals("s-99", decoded.sessionId.value)
        assertEquals("senior dev", decoded.about)
        assertEquals("suggest", decoded.approvalMode)
        assertEquals(listOf("model-a"), decoded.preferredModels)
        assertEquals(listOf("no null"), decoded.conventions)
    }

    @Test
    fun `round-trips with empty lists`() {
        val empty = OperatorProfileBoundEvent(
            sessionId = SessionId("s-0"),
            about = "",
            approvalMode = "",
            preferredModels = emptyList(),
            conventions = emptyList(),
        )
        val encoded = eventJson.encodeToString(EventPayload.serializer(), empty)
        val decoded = eventJson.decodeFromString(EventPayload.serializer(), encoded)
        assertEquals(empty, decoded)
    }
}
