package com.doctate.watch.presentation

import android.content.Intent

/**
 * Normalised entry-point commands carried via Intent extras from Tile, Complication,
 * Launcher, or internal navigation. Keeps the string-key protocol confined to
 * `fromIntent`; the rest of the code uses the sealed type.
 */
sealed interface NavCommand {
    data object ShowList : NavCommand
    data object NewCase : NavCommand
    data class ContinueCase(val caseId: String) : NavCommand

    /**
     * Carries a monotonic `seq` so Compose sees every incoming tap as a distinct value
     * — crucial when two identical commands (e.g. two "Neu" taps from the Tile) arrive
     * back-to-back; `LaunchedEffect(key)` only fires on change.
     */
    data class Pending(val seq: Long, val cmd: NavCommand)

    companion object {
        const val EXTRA_OPEN = "open"
        const val EXTRA_CASE_ID = "caseId"

        const val OPEN_LIST = "list"
        const val OPEN_NEW = "new"
        const val OPEN_CONTINUE = "continue"

        fun fromIntent(intent: Intent?): NavCommand = when (intent?.getStringExtra(EXTRA_OPEN)) {
            OPEN_NEW -> NewCase
            OPEN_CONTINUE -> intent.getStringExtra(EXTRA_CASE_ID)
                ?.takeIf { it.isNotBlank() }
                ?.let(::ContinueCase)
                ?: ShowList
            else -> ShowList
        }
    }
}
