package com.correx.core.events.serialization

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

class ProjectProfileBoundEventSerializationTest {

    @Test
    fun `ProjectProfileBoundEvent round-trips as polymorphic EventPayload`() {
        val sample: EventPayload = ProjectProfileBoundEvent(
            sessionId = SessionId("s1"),
            workspaceRoot = "/repo",
            about = "Event-sourced orchestration kernel",
            conventions = listOf("no bare try-catch", "events are the only source of truth"),
            commands = mapOf("build" to "./gradlew build", "test" to "./gradlew check"),
        )
        val encoded = eventJson.encodeToString(EventPayload.serializer(), sample)
        assertTrue(encoded.contains("\"type\":\"ProjectProfileBound\""), "SerialName must be present: $encoded")
        val decoded = eventJson.decodeFromString(EventPayload.serializer(), encoded)
        assertEquals(sample, decoded)
    }

    @Test
    fun `AgentInstructionsBoundEvent round-trips as polymorphic EventPayload`() {
        val sample: EventPayload = AgentInstructionsBoundEvent(
            sessionId = SessionId("s1"),
            workspaceRoot = "/repo",
            sources = listOf("CLAUDE.md", "AGENTS.md"),
            content = "# CLAUDE.md\n\nbe careful\n\n# AGENTS.md\n\nuse tools",
        )
        val encoded = eventJson.encodeToString(EventPayload.serializer(), sample)
        assertTrue(encoded.contains("\"type\":\"AgentInstructionsBound\""), "SerialName must be present: $encoded")
        val decoded = eventJson.decodeFromString(EventPayload.serializer(), encoded)
        assertEquals(sample, decoded)
    }

    @Test
    fun `round-trips with empty fields`() {
        val sample: EventPayload = ProjectProfileBoundEvent(
            sessionId = SessionId("s0"),
            workspaceRoot = "/empty",
            about = "",
            conventions = emptyList(),
            commands = emptyMap(),
        )
        val encoded = eventJson.encodeToString(EventPayload.serializer(), sample)
        val decoded = eventJson.decodeFromString(EventPayload.serializer(), encoded)
        assertEquals(sample, decoded)
    }
}
