package de.tsschulz.tt_tagebuch.shared.api.models

import kotlinx.serialization.json.Json
import org.junit.Test
import kotlin.test.assertEquals
import kotlin.test.assertFalse
import kotlin.test.assertTrue

/**
 * Phase 14: Regression gegen API-Inkonsistenzen (Boolean als 0/1/String) – siehe [FlexibleNullableBooleanSerializer].
 */
class InternalTournamentSerializationTest {

    private val json = Json {
        ignoreUnknownKeys = true
        isLenient = true
    }

    @Test
    fun internalSummary_deserializesNumericBooleans() {
        val raw = """{"id":1,"name":"X","allowsExternal":1,"isDoublesTournament":0}"""
        val dto = json.decodeFromString(InternalTournamentSummaryDto.serializer(), raw)
        assertEquals(1, dto.id)
        assertEquals(true, dto.allowsExternal)
        assertEquals(false, dto.isDoublesTournament)
    }

    @Test
    fun internalSummary_deserializesStringBooleans() {
        val raw = """{"id":2,"allowsExternal":"true","isDoublesTournament":"no"}"""
        val dto = json.decodeFromString(InternalTournamentSummaryDto.serializer(), raw)
        assertTrue(dto.allowsExternal == true)
        assertTrue(dto.isDoublesTournament == false)
    }

    @Test
    fun internalDetail_deserializesMixedBooleanForms() {
        val raw = """{"id":3,"allowsExternal":0,"isDoublesTournament":"1"}"""
        val dto = json.decodeFromString(InternalTournamentDetailDto.serializer(), raw)
        assertFalse(dto.allowsExternal == true)
        assertTrue(dto.isDoublesTournament == true)
    }
}
