package solutions.tretter.githugandroid

/**
 * Port of the upstream ruby-githug `squash` 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 squashLevel(): Level = level(
    id = "squash",
    title = "Squash",
    description = "You have committed several times but would like all those changes to be one commit.",
    hints = listOf("Take a look at the `-i` flag of the rebase command."),
    commandSuggestions = listOf("git rebase -i HEAD~4"),
    setup = { RepoState(initialized = true, commits = listOf(CommitNode("1", "Initial Commit"), CommitNode("2", "Adding README"), CommitNode("3", "Updating README (squash this commit into Adding README)"), CommitNode("4", "Updating README (squash this commit into Adding README)"), CommitNode("5", "Updating README (squash this commit into Adding README)")), branches = mapOf("master" to 5)) },
    nativeSetup = {
        resetFiles()
        write(".hidden")
        addCommit("Initial Commit", ".hidden")
        write("README")
        addCommit("Adding README", "README")
        write("README", "hey there")
        addCommit("Updating README (squash this commit into Adding README)", "README")
        append("README", "\nAdding some more text")
        addCommit("Updating README (squash this commit into Adding README)", "README")
        append("README", "\neven more text")
        addCommit("Updating README (squash this commit into Adding README)", "README")
        true
    },
    validator = repoPredicate { repo -> repo.commits.size == 2 },
    testCases = listOf(
        levelTestCase(
            "interactive squash",
            "GIT_SEQUENCE_EDITOR=\"sed -i '2,\$s/^pick /squash /'\" GIT_EDITOR=true git rebase -i HEAD~4",
        ),
        levelTestCase(
            "soft reset and recommit",
            "git reset --soft HEAD~4",
            "git commit -m \"New commit message\"",
        ),
    ),
    negativeTestCases = listOf(
        levelTestCase("reset without replacement commit", "git reset --soft HEAD~4"),
    ),
)
