package dev.kaizen.app

import dev.kaizen.app.db.Converters
import dev.kaizen.app.net.Attachment
import dev.kaizen.app.net.SearchSource
import org.junit.Assert.assertEquals
import org.junit.Assert.assertTrue
import org.junit.Test

class ConvertersTest {

    private val converters = Converters()

    @Test
    fun emptyListRoundTrip() {
        val json = converters.attachmentsToJson(emptyList())
        val result = converters.jsonToAttachments(json)
        assertTrue(result.isEmpty())
    }

    @Test
    fun singleAttachmentRoundTrip() {
        val original = listOf(
            Attachment(url = "https://cdn.kaizen.dev/img.png", kind = "image", name = "img.png", mimeType = "image/png", size = 2048)
        )
        val json = converters.attachmentsToJson(original)
        val result = converters.jsonToAttachments(json)

        assertEquals(1, result.size)
        assertEquals("https://cdn.kaizen.dev/img.png", result[0].url)
        assertEquals("image", result[0].kind)
        assertEquals("img.png", result[0].name)
        assertEquals("image/png", result[0].mimeType)
        assertEquals(2048L, result[0].size)
    }

    @Test
    fun multipleAttachmentsRoundTrip() {
        val original = listOf(
            Attachment(url = "https://a.com/1.pdf", kind = "document", name = "1.pdf"),
            Attachment(url = "https://a.com/2.mp4", kind = "video", name = "2.mp4", size = 999999),
        )
        val json = converters.attachmentsToJson(original)
        val result = converters.jsonToAttachments(json)

        assertEquals(2, result.size)
        assertEquals("1.pdf", result[0].name)
        assertEquals("2.mp4", result[1].name)
        assertEquals(999999L, result[1].size)
    }

    @Test
    fun malformedJsonReturnsEmptyList() {
        val result = converters.jsonToAttachments("not valid json")
        assertTrue(result.isEmpty())
    }

    @Test
    fun unknownFieldsAreIgnored() {
        val json = """[{"url":"https://x.com/f","kind":"image","name":"f","mimeType":"","unknownField":42}]"""
        val result = converters.jsonToAttachments(json)
        assertEquals(1, result.size)
        assertEquals("https://x.com/f", result[0].url)
    }

    // --- SearchSource converter tests ---

    @Test
    fun emptySourcesRoundTrip() {
        val json = converters.sourcesToJson(emptyList())
        val result = converters.jsonToSources(json)
        assertTrue(result.isEmpty())
    }

    @Test
    fun singleSourceRoundTrip() {
        val original = listOf(
            SearchSource(title = "Wikipedia", url = "https://en.wikipedia.org/wiki/Test", snippet = "A test article")
        )
        val json = converters.sourcesToJson(original)
        val result = converters.jsonToSources(json)

        assertEquals(1, result.size)
        assertEquals("Wikipedia", result[0].title)
        assertEquals("https://en.wikipedia.org/wiki/Test", result[0].url)
        assertEquals("A test article", result[0].snippet)
    }

    @Test
    fun multipleSourcesRoundTrip() {
        val original = listOf(
            SearchSource(title = "Source 1", url = "https://a.com"),
            SearchSource(title = "Source 2", url = "https://b.com", snippet = "Details"),
        )
        val json = converters.sourcesToJson(original)
        val result = converters.jsonToSources(json)

        assertEquals(2, result.size)
        assertEquals("Source 1", result[0].title)
        assertEquals("Source 2", result[1].title)
        assertEquals("Details", result[1].snippet)
    }

    @Test
    fun malformedSourcesJsonReturnsEmptyList() {
        val result = converters.jsonToSources("{broken")
        assertTrue(result.isEmpty())
    }

    @Test
    fun sourcesWithUnknownFieldsIgnored() {
        val json = """[{"title":"T","url":"https://x.com","snippet":"","futureField":"v"}]"""
        val result = converters.jsonToSources(json)
        assertEquals(1, result.size)
        assertEquals("T", result[0].title)
    }
}
