package com.ponzischeme89.memby.ui.alerts

import androidx.compose.ui.test.junit4.createComposeRule
import androidx.compose.ui.test.onRoot
import com.github.takahirom.roborazzi.captureRoboImage
import com.ponzischeme89.memby.data.EmbyProfile
import com.ponzischeme89.memby.data.model.NotificationPreferences
import com.ponzischeme89.memby.data.model.UserNotification
import com.ponzischeme89.memby.ui.UserSwitcherOverlay
import com.ponzischeme89.memby.ui.theme.MembyTheme
import org.junit.Rule
import org.junit.Test
import org.junit.runner.RunWith
import org.robolectric.RobolectricTestRunner
import org.robolectric.annotation.Config
import org.robolectric.annotation.GraphicsMode

/**
 * Renders My Alerts to PNGs under `build/screenshots/my-alerts/`, so the page can be looked
 * at without deploying to a TV.
 *
 * ```powershell
 * .\gradlew.bat :app:testDebugUnitTest --tests "*AlertsPageScreenshotTest"
 * ```
 *
 * The empty case is the one worth keeping. This is a page whose whole job is emptying
 * itself, so what it looks like with nothing on it is the state a viewer reaches most often
 * and the only one a unit test cannot describe.
 */
@RunWith(RobolectricTestRunner::class)
@GraphicsMode(GraphicsMode.Mode.NATIVE)
@Config(sdk = [34], qualifiers = "w960dp-h540dp-television-xhdpi")
class AlertsPageScreenshotTest {

    @get:Rule
    val compose = createComposeRule()

    @Test
    fun `alerts waiting`() {
        capture("my-alerts-populated", sampleAlerts)
    }

    /** Nothing new: the "NEW" flags are gone and the rows read as a list, not as news. */
    @Test
    fun `everything already read`() {
        capture("my-alerts-all-read", sampleAlerts.map { it.copy(readAt = "2026-08-06T09:00:00Z") })
    }

    @Test
    fun `nothing waiting`() {
        capture("my-alerts-empty", emptyList())
    }

    /** Alerts switched off has its own empty wording — and both toggles read as off. */
    @Test
    fun `alerts switched off`() {
        capture(
            "my-alerts-disabled",
            emptyList(),
            NotificationPreferences(enabled = false, showReturnAlerts = false),
        )
    }

    /** A long title and a two-line message: the row's own wrapping case. */
    @Test
    fun `long wording`() {
        capture(
            "my-alerts-long-wording",
            listOf(
                UserNotification(
                    id = 1,
                    kind = "series_return",
                    title = "A Very Long Programme Title That Will Not Fit On One Line",
                    message = "Season 4 of this show returns on Thursday, and the first two " +
                        "episodes will be in Emby that morning if the download lands.",
                    eventAt = "2026-08-13T08:30:00Z",
                ),
            ),
        )
    }

    /**
     * The way in: the user menu in the user picker, with the badge that replaced the bell
     * on the launcher. Captured here rather than beside the profile switcher's own tests
     * because the badge and the page are one feature — if they disagree about what counts,
     * this is the pair that shows it.
     */
    @Test
    fun `user menu carrying the badge`() {
        capturePicker("my-alerts-user-menu", alertCount = 3)
    }

    @Test
    fun `user menu with nothing waiting`() {
        capturePicker("my-alerts-user-menu-quiet", alertCount = 0)
    }

    /** Past the cap the badge stops counting rather than widening the row. */
    @Test
    fun `user menu with a great many alerts`() {
        capturePicker("my-alerts-user-menu-many", alertCount = 42)
    }

    private fun capturePicker(name: String, alertCount: Int) {
        compose.setContent {
            MembyTheme {
                UserSwitcherOverlay(
                    profiles = listOf(
                        EmbyProfile("a", "https://memby.example", "t", "u1", "Matt"),
                        EmbyProfile("b", "https://memby.example", "t", "u2", "Charlotte"),
                    ),
                    activeProfileId = "a",
                    onProfileSelected = {},
                    onManageProfiles = {},
                    onDismiss = {},
                    alertCount = alertCount,
                    onOpenAlerts = {},
                )
            }
        }
        compose.onRoot().captureRoboImage("build/screenshots/my-alerts/$name.png")
    }

    private fun capture(
        name: String,
        notifications: List<UserNotification>,
        preferences: NotificationPreferences = NotificationPreferences(),
    ) {
        compose.setContent {
            MembyTheme {
                MyAlertsPage(
                    notifications = notifications,
                    preferences = preferences,
                    onToggleEnabled = {},
                    onToggleShowReturns = {},
                    onRead = {},
                    onDismiss = {},
                    onDismissAll = {},
                    onClose = {},
                )
            }
        }
        compose.onRoot().captureRoboImage("build/screenshots/my-alerts/$name.png")
    }

    private val sampleAlerts = listOf(
        UserNotification(
            id = 1,
            kind = "series_return",
            title = "Northbound returns on Thursday",
            message = "Season 3 starts on 13 August. Memby will add it as soon as it lands.",
            eventAt = "2026-08-13T20:30:00Z",
        ),
        UserNotification(
            id = 2,
            kind = "series_return",
            title = "The Long Dark is back",
            message = "Season 2 started yesterday and the first episode is in Emby now.",
            eventAt = "2026-08-06T20:00:00Z",
        ),
        UserNotification(
            id = 3,
            kind = "library",
            title = "24 titles added",
            message = "Memby has finished refreshing your library.",
            readAt = "2026-08-05T11:00:00Z",
        ),
    )
}
