package com.ponzischeme89.memby.ui.player

import androidx.media3.common.C
import org.junit.Assert.assertEquals
import org.junit.Assert.assertFalse
import org.junit.Assert.assertTrue
import org.junit.Test

class PrerollSequenceTest {
    @Test
    fun `fresh playback shows preroll`() {
        assertTrue(shouldShowPreroll(0L))
    }

    @Test
    fun `resumed playback skips preroll`() {
        assertFalse(shouldShowPreroll(1L))
        assertFalse(shouldShowPreroll(42 * 60_000L))
    }

    @Test
    fun `server can disable preroll for fresh playback`() {
        assertFalse(shouldShowPreroll(0L, enabled = false))
        assertTrue(shouldShowPreroll(0L, enabled = true))
    }

    @Test
    fun `handoff waits for the video countdown gate`() {
        assertFalse(prerollCanHandOff(true, false, true))
        assertTrue(prerollCanHandOff(true, true, true))
    }

    @Test
    fun `handoff never starts playback in background`() {
        assertFalse(prerollCanHandOff(true, true, false))
        assertFalse(prerollCanHandOff(false, true, true))
    }

    @Test
    fun `countdown advances on a ready paused first frame`() {
        assertTrue(prerollCountdownAdvances(lifecycleStarted = true, playbackReady = true))
        assertFalse(prerollCountdownAdvances(lifecycleStarted = false, playbackReady = true))
        assertFalse(prerollCountdownAdvances(lifecycleStarted = true, playbackReady = false))
    }

    @Test
    fun `packaged clip duration replaces the configured fallback`() {
        assertEquals(4_133L, effectivePrerollDurationMs(4_133L, 7_000L))
        assertEquals(7_000L, effectivePrerollDurationMs(C.TIME_UNSET, 7_000L))
        assertEquals(7_000L, effectivePrerollDurationMs(0L, 7_000L))
    }
}
