package com.correx.core.events.serialization

import com.correx.core.events.events.EventPayload
import com.correx.core.events.events.LowQualityExtractionEvent
import com.correx.core.events.events.SourceFetchedEvent
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 ResearchSourceEventSerializationTest {

    private val sessionId = SessionId("s")
    private val stageId = StageId("st")

    @Test
    fun `SourceFetchedEvent round-trips as polymorphic EventPayload`() {
        val sample: EventPayload = SourceFetchedEvent(
            sessionId = sessionId,
            stageId = stageId,
            url = "https://example.com/a",
            contentSha256 = "abc123",
            quality = "OK",
            byteCount = 4096,
        )
        val encoded = eventJson.encodeToString(EventPayload.serializer(), sample)
        assertTrue(encoded.contains("\"type\":\"SourceFetched\""), "SerialName must be present: $encoded")
        val decoded = eventJson.decodeFromString(EventPayload.serializer(), encoded)
        assertEquals(sample, decoded)
    }

    @Test
    fun `LowQualityExtractionEvent round-trips as polymorphic EventPayload`() {
        val sample: EventPayload = LowQualityExtractionEvent(
            sessionId = sessionId,
            stageId = stageId,
            url = "https://example.com/spa",
            contentSha256 = "def456",
            reason = "extracted 12 chars, below minimum 200",
        )
        val encoded = eventJson.encodeToString(EventPayload.serializer(), sample)
        assertTrue(encoded.contains("\"type\":\"LowQualityExtraction\""), "SerialName must be present: $encoded")
        val decoded = eventJson.decodeFromString(EventPayload.serializer(), encoded)
        assertEquals(sample, decoded)
    }
}
