package solutions.tretter.githugandroid

/**
 * Port of the upstream ruby-githug `stash` level.
 *
 * Setup documents the repository shape the learner explores. Evaluation is kept
 * state-based whenever the exercise changes repository objects; answer-only
 * levels intentionally validate the answer entered at the prompt.
 */
internal fun stashLevel(): Level = level(
    id = "stash",
    title = "Stash",
    description = "You've made some changes and want to work on them later. You should save them, but don't commit them.",
    hints = listOf("It's like stashing. Try finding an appropriate git command."),
    commandSuggestions = listOf("git stash", "git status"),
    setup = { RepoState(initialized = true, files = listOf(GitFile("lyrics.txt", stashLevelModifiedLyrics(), tracked = true)), commits = listOf(CommitNode("0000001", "Add some lyrics")), branches = mapOf("master" to 1)) },
    nativeSetup = {
        resetFiles()
        write("lyrics.txt", stashLevelCommittedLyrics())
        addCommit("Add some lyrics", "lyrics.txt")
        write("lyrics.txt", stashLevelModifiedLyrics())
        true
    },
    validator = repoPredicate { repo -> repo.stashes.isNotEmpty() && repo.files.none { it.staged } },
    testCases = listOf(
        levelTestCase("stash changes", "git stash"),
    ),
    setupChecks = listOf(
        levelSetupCheck(
            "upstream modified lyrics are present",
            "stash should start with upstream lyrics plus the trailing Hey! modification.",
        ) { repo ->
            repo.files.firstOrNull { it.name == "lyrics.txt" }?.content?.contains("Hear them loudly cry:\nHey!") == true
        },
    ),
)

private fun stashLevelCommittedLyrics(): String =
    """
    Down in Louisiana in that sunny clime,
    They play a class of music that is super fine,
    And it makes no difference if its rain or shine,
    You can hear that that jazz band music playing all the time.
    It sounds so peculiar cause the music's queer.
    How its sweet vibration seem to fill the air.
    Then to you the whole world seems to be in rhyme.
    You want nothing else but blues-band music all the time.

    Ev'ry one that's nigh
    Never seems to sigh,
    Hear them loudly cry:
    """.trimIndent() + "\n"

private fun stashLevelModifiedLyrics(): String = stashLevelCommittedLyrics() + "Hey!\n"
