package com.bytelyst.platform

import kotlinx.serialization.encodeToString
import kotlinx.serialization.json.Json
import org.junit.jupiter.api.Assertions.*
import org.junit.jupiter.api.Test

class BLTelemetryEventTest {

    private val json = Json { ignoreUnknownKeys = true; encodeDefaults = true }

    @Test
    fun `event should serialize to JSON`() {
        val event = BLTelemetryClient.TelemetryEvent(
            id = "evt-1",
            productId = "testapp",
            anonymousInstallId = "inst-1",
            sessionId = "sess-1",
            platform = "android",
            channel = "native",
            osFamily = "android",
            osVersion = "14",
            appVersion = "1.0.0",
            buildNumber = "42",
            releaseChannel = "beta",
            eventType = "info",
            module = "test",
            eventName = "unit_test",
            occurredAt = "2026-01-01T00:00:00Z",
        )
        val jsonStr = json.encodeToString(event)
        assertTrue(jsonStr.contains("\"productId\":\"testapp\""))
        assertTrue(jsonStr.contains("\"eventName\":\"unit_test\""))
    }

    @Test
    fun `event should deserialize from JSON`() {
        val jsonStr = """
            {
                "id": "evt-1",
                "productId": "testapp",
                "anonymousInstallId": "inst-1",
                "sessionId": "sess-1",
                "platform": "android",
                "channel": "native",
                "osFamily": "android",
                "osVersion": "14",
                "appVersion": "1.0.0",
                "buildNumber": "42",
                "releaseChannel": "beta",
                "eventType": "info",
                "module": "test",
                "eventName": "unit_test",
                "occurredAt": "2026-01-01T00:00:00Z"
            }
        """.trimIndent()
        val event = json.decodeFromString<BLTelemetryClient.TelemetryEvent>(jsonStr)
        assertEquals("testapp", event.productId)
        assertEquals("unit_test", event.eventName)
        assertNull(event.feature)
        assertNull(event.tags)
    }

    @Test
    fun `event with optional fields should serialize correctly`() {
        val event = BLTelemetryClient.TelemetryEvent(
            id = "evt-2",
            productId = "testapp",
            anonymousInstallId = "inst-1",
            sessionId = "sess-1",
            platform = "android",
            channel = "native",
            osFamily = "android",
            osVersion = "14",
            appVersion = "1.0.0",
            buildNumber = "42",
            releaseChannel = "beta",
            eventType = "error",
            module = "auth",
            eventName = "login_failed",
            feature = "social_login",
            message = "Token expired",
            tags = mapOf("provider" to "google"),
            metrics = mapOf("retryCount" to 3.0),
            occurredAt = "2026-01-01T00:00:00Z",
        )
        val jsonStr = json.encodeToString(event)
        assertTrue(jsonStr.contains("\"feature\":\"social_login\""))
        assertTrue(jsonStr.contains("\"provider\":\"google\""))
        assertTrue(jsonStr.contains("\"retryCount\":3.0"))
    }
}
