package solutions.tretter.githugandroid

import java.io.File

/**
 * Port of the upstream ruby-githug `fetch` 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 fetchLevel(): Level = level(
    id = "fetch",
    title = "Fetch",
    description = "Looks like a new branch was pushed into our remote repository. Get the changes without merging them with the local repository",
    hints = listOf("Look up the 'git fetch' command"),
    commandSuggestions = listOf("git fetch origin"),
    setup = {
        RepoState(
            initialized = true,
            branches = mapOf("master" to 1),
            remotes = mapOf("origin" to "remote"),
            fetchedBranches = setOf("origin/master"),
            fetchHeadCount = 1,
        )
    },
    nativeSetup = {
        resetFiles()
        write("master_file")
        addCommit("Commits master_file", "master_file")
        val remote = siblingRepo("origin")
        git("remote", "add", "fetch-setup-origin", File(remote, ".git").absolutePath)
        git("push", "fetch-setup-origin", "master")
        git("remote", "remove", "fetch-setup-origin")
        git(remote, "checkout", "-f", "master")
        git(remote, "checkout", "-b", "new_branch")
        writeIn(remote, "file1")
        addCommitIn(remote, "Commits file 1", "file1")
        git("remote", "add", "origin", File(remote, ".git").absolutePath)
        git("branch", "--set-upstream-to=origin/master", "master")
        true
    },
    validator = repoPredicate { repo ->
        repo.branches.keys == setOf("master") &&
            repo.fetchHeadCount >= 2 &&
            "origin/new_branch" in repo.fetchedBranches &&
            repo.files.none { it.name == "file1" && it.tracked }
    },
    testCases = listOf(
        levelTestCase("fetch origin", "git fetch origin"),
        levelTestCase("fetch default", "git fetch"),
    ),
)
