package solutions.tretter.githugandroid

import java.io.File

/**
 * Port of the upstream ruby-githug `push_branch` 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 pushBranchLevel(): Level = level(
    id = "push_branch",
    title = "Push Branch",
    description = "You've made some changes to a local branch and want to share it, but aren't yet ready to merge it with the 'master' branch. Push only 'test_branch' to the remote repository",
    hints = listOf("Investigate the options in `git push` using `git push --help`"),
    commandSuggestions = listOf("git push origin test_branch"),
    setup = { RepoState(initialized = true, branches = mapOf("master" to 2, "other_branch" to 3, "test_branch" to 4), remotes = mapOf("origin" to "remote"), headBranch = "master") },
    nativeSetup = {
        resetFiles()
        write("file1")
        addCommit("committed changes on master", "file1")
        val remote = siblingRepo("origin")
        git("remote", "add", "push-branch-setup-origin", File(remote, ".git").absolutePath)
        git("push", "push-branch-setup-origin", "master")
        git("remote", "remove", "push-branch-setup-origin")
        git(remote, "checkout", "-f", "master")
        write("file2")
        addCommit("If this commit gets pushed to repo, then you have lost the level :( ", "file2")
        checkoutNew("other_branch")
        write("file3")
        addCommit("If this commit gets pushed to repo, then you have lost the level :( ", "file3")
        checkoutNew("test_branch")
        write("file4")
        addCommit("committed change on test_branch", "file4")
        git("remote", "add", "origin", File(remote, ".git").absolutePath)
        git("branch", "--set-upstream-to=origin/master", "master")
        checkout("master")
        true
    },
    validator = repoPredicate { repo -> "origin/test_branch" in repo.pushedBranches && "origin/master" !in repo.pushedBranches && "origin/other_branch" !in repo.pushedBranches },
    testCases = listOf(
        levelTestCase("push named branch", "git push origin test_branch"),
        levelTestCase("push current branch refspec", "git push origin test_branch:test_branch"),
    ),
)
