package de.krisenvorrat.app.domain.model

import org.junit.Assert.assertEquals
import org.junit.Assert.assertTrue
import org.junit.Test

class AgeGroupTest {

    @Test
    fun test_totalDailyKcal_withMixedGroups_returnsSumOfCountTimesKcal() {
        // Given
        val entries = listOf(
            AgeGroupEntry(ageGroup = AgeGroup.KLEINKIND, count = 1, kcalPerDay = 1000),
            AgeGroupEntry(ageGroup = AgeGroup.ERWACHSENER, count = 2, kcalPerDay = 2000),
            AgeGroupEntry(ageGroup = AgeGroup.JUGENDLICH, count = 0, kcalPerDay = 2400)
        )

        // When
        val result = entries.totalDailyKcal()

        // Then – 1×1000 + 2×2000 + 0×2400 = 5000
        assertEquals(5000, result)
    }

    @Test
    fun test_parseAgeGroupsFromJson_withValidJson_returnsCorrectGroups() {
        // Given
        val entries = listOf(
            AgeGroupEntry(ageGroup = AgeGroup.ERWACHSENER, count = 3, kcalPerDay = 2500)
        )
        val json = entries.toJson()

        // When
        val parsed = parseAgeGroupsFromJson(json)

        // Then
        assertEquals(1, parsed.size)
        assertEquals(AgeGroup.ERWACHSENER, parsed.first().ageGroup)
        assertEquals(3, parsed.first().count)
        assertEquals(2500, parsed.first().kcalPerDay)
    }

    @Test
    fun test_parseAgeGroupsFromJson_withNull_returnsDefaults() {
        // Given / When
        val result = parseAgeGroupsFromJson(null)

        // Then
        assertEquals(defaultAgeGroups(), result)
    }

    @Test
    fun test_defaultAgeGroups_hasErwachsenerWithCount2() {
        // Given / When
        val defaults = defaultAgeGroups()

        // Then
        val erwachsener = defaults.first { it.ageGroup == AgeGroup.ERWACHSENER }
        assertEquals(2, erwachsener.count)
        assertTrue(defaults.filter { it.ageGroup != AgeGroup.ERWACHSENER }.all { it.count == 0 })
    }
}
