package solutions.tretter.githugandroid

import java.io.File

/**
 * Port of the upstream ruby-githug `submodule` 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 submoduleLevel(): Level = level(
    id = "submodule",
    title = "Submodule",
    description = "You want to include the files from the following repo: `https://github.com/jackmaney/githug-include-me` into the folder `./githug-include-me`. Do this without manually cloning the repo or copying the files from the repo into this repo.",
    hints = listOf("Take a look at `git submodule`."),
    commandSuggestions = listOf(
        "git config -f .gitmodules submodule.githug-include-me.path githug-include-me",
        "git config -f .gitmodules submodule.githug-include-me.url ../submodule-source",
        "git add .gitmodules",
    ),
    setup = { RepoState(initialized = true, branches = mapOf("master" to 0)) },
    nativeSetup = {
        val source = File(sandbox.parentFile ?: sandbox, "submodule-source")
        source.deleteRecursively()
        initRepo(source)
        writeIn(source, "README.md", "Included by submodule.\n")
        addCommitIn(source, "Initial submodule content", "README.md")
        git("config", "protocol.file.allow", "always")
        true
    },
    validator = repoPredicate { repo ->
        repo.submodules["githug-include-me"]?.isNotBlank() == true &&
            repo.files.any { it.name == ".gitmodules" && it.tracked && it.content.contains("githug-include-me") }
    },
    testCases = listOf(
        levelTestCase(
            "record submodule metadata",
            "git config -f .gitmodules submodule.githug-include-me.path githug-include-me",
            "git config -f .gitmodules submodule.githug-include-me.url ../submodule-source",
            "git add .gitmodules",
        ),
    ),
)
