package com.fabledsword.minstrel.home.ui

import com.fabledsword.minstrel.models.PlaylistRef
import com.fabledsword.minstrel.models.SystemPlaylistsStatus
import org.junit.jupiter.api.Test
import kotlin.test.assertEquals
import kotlin.test.assertTrue

class BuildPlaylistsRowTest {

    private fun user(id: String, cached: Boolean) =
        PlaylistRef(id = id, userId = "u", name = id, trackCount = 1, fullyCached = cached)

    @Test
    fun `offline row leads with pools then available before greyed`() {
        val owned = listOf(user("partial", cached = false), user("full", cached = true))
        val row = buildPlaylistsRow(owned, SystemPlaylistsStatus(), offline = true)

        assertTrue(row[0] is PlaylistRowItem.OfflinePool)
        assertTrue(row[1] is PlaylistRowItem.OfflinePool)
        // Fully-cached "full" comes before the greyed "partial".
        val reals = row.filterIsInstance<PlaylistRowItem.Real>().map { it.playlist.id }
        assertEquals(listOf("full", "partial"), reals)
    }

    @Test
    fun `offline greys a refreshable system playlist even when fully cached`() {
        val forYou = PlaylistRef(
            id = "fy",
            userId = "u",
            name = "For You",
            systemVariant = "for_you",
            trackCount = 1,
            fullyCached = true,
        )
        val row = buildPlaylistsRow(
            listOf(forYou, user("u1", cached = true)),
            SystemPlaylistsStatus(),
            offline = true,
        )
        // The fully-cached user playlist is available; the refreshable system
        // mix needs the server, so it greys out and sorts after.
        val reals = row.filterIsInstance<PlaylistRowItem.Real>().map { it.playlist.id }
        assertEquals(listOf("u1", "fy"), reals)
    }

    @Test
    fun `offline row drops building-pending placeholders`() {
        val row = buildPlaylistsRow(emptyList(), SystemPlaylistsStatus(), offline = true)
        assertTrue(row.none { it is PlaylistRowItem.Placeholder })
    }

    @Test
    fun `online row has no offline pools and keeps system-slot placeholders`() {
        val row = buildPlaylistsRow(emptyList(), SystemPlaylistsStatus(), offline = false)
        assertTrue(row.none { it is PlaylistRowItem.OfflinePool })
        assertTrue(row.any { it is PlaylistRowItem.Placeholder })
    }
}
