package dev.castarr.tv.data

import org.junit.Assert.assertEquals
import org.junit.Assert.assertFalse
import org.junit.Assert.assertTrue
import org.junit.Test

class TeamFiltersTest {

    private val hansa = TeamFilters.byKey("hansa")!!
    private val vfb = TeamFilters.byKey("stuttgart")!!

    @Test
    fun `matches the club regardless of case and surrounding text`() {
        assertTrue(hansa.matches("3. Liga: FC Hansa Rostock - Dynamo Dresden"))
        assertTrue(hansa.matches("HANSA ROSTOCK KOMPAKT"))
        assertTrue(vfb.matches("Bundesliga: VfB Stuttgart - Bayern München"))
    }

    @Test
    fun `does not match unrelated programmes`() {
        assertFalse(hansa.matches("Tagesschau"))
        assertFalse(vfb.matches("Hansa Rostock - Saarbrücken"))
    }

    @Test
    fun `scans sport and free-to-air groups`() {
        listOf("DAZN Event", "Sky Sport", "Magenta Sport", "Amazon Prime", "Free TV / HD+", "DYN Sport")
            .forEach { assertTrue(it, TeamFilters.scansGroup(it)) }
    }

    @Test
    fun `skips NFL and unrelated groups`() {
        assertFalse(TeamFilters.scansGroup("DAZN Event NFL"))
        assertFalse(TeamFilters.scansGroup("Kids"))
        assertFalse(TeamFilters.scansGroup("Musik"))
    }

    @Test
    fun `assigns each viewer their own club by default`() {
        assertEquals(listOf("hansa"), TeamFilters.defaultKeysFor("benjamin"))
        assertEquals(listOf("stuttgart"), TeamFilters.defaultKeysFor("TobiasB"))
        assertTrue(TeamFilters.defaultKeysFor("someone-else").isEmpty())
    }

    @Test
    fun `club keys are unique and every club has needles`() {
        val keys = TeamFilters.all.map { it.key }
        assertEquals(keys.size, keys.toSet().size)
        assertTrue(TeamFilters.all.all { it.needles.isNotEmpty() })
        assertTrue(TeamFilters.all.all { c -> c.needles.all { it == it.lowercase() } })
    }

    @Test
    fun `covers all three divisions`() {
        assertTrue(TeamFilters.all.size > 50)
        listOf("bayern", "schalke", "hansa").forEach {
            assertTrue(it, TeamFilters.byKey(it) != null)
        }
    }
}
