package de.tsschulz.timeclock.data.api

import kotlinx.serialization.builtins.ListSerializer
import kotlinx.serialization.json.Json
import org.junit.Assert.assertEquals
import org.junit.Assert.assertFalse
import org.junit.Assert.assertTrue
import org.junit.Test

class ApiSerializationTest {
    private val json = Json {
        ignoreUnknownKeys = true
        isLenient = true
        coerceInputValues = true
    }

    @Test
    fun loginResponseMapsWebUserFields() {
        val raw = """
            {
              "success": true,
              "token": "jwt",
              "user": {
                "id": 1,
                "full_name": "Max Mustermann",
                "email": "max@example.com",
                "role": 1,
                "daily_hours": 8,
                "week_workdays": 5
              }
            }
        """.trimIndent()

        val decoded = json.decodeFromString(LoginResponse.serializer(), raw)

        assertTrue(decoded.success)
        assertEquals("jwt", decoded.token)
        assertEquals("Max Mustermann", decoded.user?.fullName)
        assertEquals(1, decoded.user?.role)
        assertEquals(5, decoded.user?.weekWorkdays)
    }

    @Test
    fun protectedIdsDecodeAsStrings() {
        val raw = """
            [
              {
                "id": "abc.def",
                "project": "Allgemein",
                "description": "Test",
                "startTime": "2026-05-14T08:00:00+02:00",
                "endTime": null,
                "duration": 3600,
                "isRunning": true
              }
            ]
        """.trimIndent()

        val decoded = json.decodeFromString(ListSerializer(TimeEntryDto.serializer()), raw)

        assertEquals("abc.def", decoded.first().id)
        assertTrue(decoded.first().isRunning)
        assertEquals(3600L, decoded.first().duration)
    }

    @Test
    fun holidayResponseKeepsFutureAndPastBuckets() {
        val raw = """
            {
              "future": [
                {
                  "id": "holiday.hash",
                  "date": "2026-10-03",
                  "hours": 8,
                  "description": "Tag der Deutschen Einheit",
                  "states": [],
                  "isFederal": true
                }
              ],
              "past": []
            }
        """.trimIndent()

        val decoded = json.decodeFromString(HolidaysResponse.serializer(), raw)

        assertEquals(1, decoded.future.size)
        assertEquals("Tag der Deutschen Einheit", decoded.future.first().description)
        assertTrue(decoded.future.first().isFederal)
        assertTrue(decoded.past.isEmpty())
    }

    @Test
    fun roleUserDecodesUserRole() {
        val raw = """
            {
              "id": "user.hash",
              "fullName": "Erika Musterfrau",
              "role": 0,
              "roleString": "user",
              "stateName": "Nordrhein-Westfalen"
            }
        """.trimIndent()

        val decoded = json.decodeFromString(RoleUserDto.serializer(), raw)

        assertEquals("user.hash", decoded.id)
        assertEquals("Erika Musterfrau", decoded.fullName)
        assertFalse(decoded.role == 1)
        assertEquals("Nordrhein-Westfalen", decoded.stateName)
    }
}
