package com.ponzischeme89.memby.ui

import com.ponzischeme89.memby.data.model.BaseItem
import com.ponzischeme89.memby.data.model.UserItemData
import org.junit.Assert.assertEquals
import org.junit.Assert.assertFalse
import org.junit.Assert.assertNull
import org.junit.Assert.assertTrue
import org.junit.Test

class WatchedVisibilityTest {
    @Test
    fun `preference is disabled by default`() {
        assertFalse(com.ponzischeme89.memby.data.Settings().hideWatchedMovies)
    }

    @Test
    fun `enabled preference hides only completed movies`() {
        val watchedMovie = item("movie", "Movie", played = true)
        val unwatchedMovie = item("new-movie", "Movie")
        val watchedSeries = item("series", "Series", played = true)
        val row = row(watchedMovie, unwatchedMovie, watchedSeries)

        val filtered = applyWatchedVisibility(listOf(row), enabled = true).single()

        assertEquals(listOf("new-movie", "series"), filtered.items.map(BaseItem::id))
        assertTrue(filtered.showWatchedEpisodeCount)
    }

    @Test
    fun `disabled preference preserves row content`() {
        val row = row(item("movie", "Movie", played = true))

        val unchanged = applyWatchedVisibility(listOf(row), enabled = false).single()

        assertEquals(listOf("movie"), unchanged.items.map(BaseItem::id))
        assertFalse(unchanged.showWatchedEpisodeCount)
    }

    @Test
    fun `series progress uses aggregate episode counts`() {
        val series = BaseItem(
            id = "series",
            type = "Series",
            recursiveItemCount = 10,
            userData = UserItemData(unplayedItemCount = 2),
        )

        assertEquals("8 of 10 watched", watchedEpisodeCountLabel(series))
        assertNull(watchedEpisodeCountLabel(item("movie", "Movie")))
    }

    private fun item(id: String, type: String, played: Boolean = false) = BaseItem(
        id = id,
        type = type,
        userData = UserItemData(played = played),
    )

    private fun row(vararg items: BaseItem) = HomeBrowseRow(
        id = "row",
        title = "Row",
        items = items.toList(),
        kind = MediaRowKind.MOVIES,
        emptyMessage = "Empty",
    )
}
