/*
 * Copyright (C) 2026 Memby contributors
 *
 * SPDX-License-Identifier: GPL-2.0-only
 */

package com.ponzischeme89.memby.ui.whatsnew

/** What a launch should do about the update notice for the build that is running. */
internal sealed interface WhatsNewDecision {
    /** Briefly announce this installed version, then record it. */
    data class Notify(val version: String) : WhatsNewDecision

    /** Record the version without showing anything. */
    data class MarkSeen(val version: String) : WhatsNewDecision

    /** Neither — this launch has nothing to say and nothing to write. */
    data object Nothing : WhatsNewDecision
}

/**
 * Decides whether a television that has just been updated should announce the new version.
 *
 * Pure, because the interesting part is which launches the toast must stay out of. Three
 * cases are deliberately not "notify":
 *
 * - **Already recorded.** The whole contract is once per update; every later launch of the
 *   same build is silent, which is what [seenVersion] exists for.
 * - **A fresh install** (no record at all, and nobody signed in yet). It has not updated
 *   from an earlier version, so it is marked seen during setup and the first launcher stays
 *   quiet.
 * - **Signed out.** A toast belongs over the launcher, so an updated television with an
 *   existing record waits until somebody signs in rather than consuming the notice early.
 *
 * Signed out with a record already present is [Nothing] rather than [MarkSeen]: the notes
 * belong over the launcher, so that launch simply waits for whoever is about to sign in.
 */
internal fun whatsNewDecision(
    installedVersion: String,
    seenVersion: String?,
    isSignedIn: Boolean,
): WhatsNewDecision {
    val version = installedVersion.trim()
    if (version.isEmpty()) return WhatsNewDecision.Nothing
    if (seenVersion?.trim() == version) return WhatsNewDecision.Nothing
    if (seenVersion == null && !isSignedIn) return WhatsNewDecision.MarkSeen(version)
    if (!isSignedIn) return WhatsNewDecision.Nothing
    return WhatsNewDecision.Notify(version)
}
