package com.rssuper.models

import com.squareup.moshi.Moshi
import com.squareup.moshi.kotlin.reflect.KotlinJsonAdapterFactory
import org.junit.Assert.assertEquals
import org.junit.Assert.assertNotNull
import org.junit.Assert.assertNull
import org.junit.Before
import org.junit.Test
import java.util.Date

class FeedItemTest {

    private lateinit var moshi: Moshi
    private lateinit var adapter: com.squareup.moshi.JsonAdapter<FeedItem>

    @Before
    fun setup() {
        moshi = Moshi.Builder()
            .add(KotlinJsonAdapterFactory())
            .build()
        adapter = moshi.adapter(FeedItem::class.java)
    }

    @Test
    fun testSerialization() {
        val feedItem = FeedItem(
            id = "item-1",
            title = "Test Article",
            link = "https://example.com/article",
            description = "Short description",
            content = "Full content here",
            author = "John Doe",
            published = Date(1672531200000),
            categories = listOf("Tech", "News"),
            guid = "guid-123",
            subscriptionTitle = "Tech News"
        )

        val json = adapter.toJson(feedItem)
        assertNotNull(json)
    }

    @Test
    fun testDeserialization() {
        val json = """{
            "id": "item-1",
            "title": "Test Article",
            "link": "https://example.com/article",
            "description": "Short description",
            "author": "John Doe",
            "published": 1672531200000,
            "categories": ["Tech", "News"],
            "guid": "guid-123",
            "subscriptionTitle": "Tech News"
        }"""

        val feedItem = adapter.fromJson(json)
        assertNotNull(feedItem)
        assertEquals("item-1", feedItem?.id)
        assertEquals("Test Article", feedItem?.title)
        assertEquals("John Doe", feedItem?.author)
    }

    @Test
    fun testOptionalFieldsNull() {
        val json = """{
            "id": "item-1",
            "title": "Test Article"
        }"""

        val feedItem = adapter.fromJson(json)
        assertNotNull(feedItem)
        assertNull(feedItem?.link)
        assertNull(feedItem?.description)
        assertNull(feedItem?.author)
    }

    @Test
    fun testEnclosureSerialization() {
        val feedItem = FeedItem(
            id = "item-1",
            title = "Podcast Episode",
            enclosure = Enclosure(
                url = "https://example.com/episode.mp3",
                type = "audio/mpeg",
                length = 12345678
            )
        )

        val json = adapter.toJson(feedItem)
        assertNotNull(json)
    }

    @Test
    fun testCopy() {
        val original = FeedItem(
            id = "item-1",
            title = "Original Title",
            author = "Original Author"
        )

        val modified = original.copy(title = "Modified Title")

        assertEquals("item-1", modified.id)
        assertEquals("Modified Title", modified.title)
        assertEquals("Original Author", modified.author)
    }

    @Test
    fun testEqualsAndHashCode() {
        val item1 = FeedItem(id = "item-1", title = "Test")
        val item2 = FeedItem(id = "item-1", title = "Test")
        val item3 = FeedItem(id = "item-2", title = "Test")

        assertEquals(item1, item2)
        assertEquals(item1.hashCode(), item2.hashCode())
        assert(item1 != item3)
    }

    @Test
    fun testToString() {
        val feedItem = FeedItem(
            id = "item-1",
            title = "Test Article",
            author = "John Doe"
        )

        val toString = feedItem.toString()
        assertNotNull(toString)
        assert(toString.contains("id=item-1"))
        assert(toString.contains("title=Test Article"))
    }
}
