package solutions.tretter.githugandroid

/**
 * Port of the upstream ruby-githug `blame` 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 blameLevel(): Level = level(
    id = "blame",
    title = "Blame",
    description = "Identify who put a password inside the file `config.rb`.",
    hints = listOf("You want to research the `git blame` command."),
    commandSuggestions = listOf("git blame config.rb"),
    setup = {
        RepoState(
            initialized = true,
            files = listOf(GitFile("config.rb", blameLevelFinalConfigRb(), tracked = true)),
            commits = listOf(
                CommitNode("0000001", "added more options (no really)"),
                CommitNode("0000002", "added more options"),
                CommitNode("0000003", "added password"),
                CommitNode("0000004", "added options"),
                CommitNode("0000005", "added config with name"),
            ),
            branches = mapOf("master" to 5),
        )
    },
    nativeSetup = {
        resetFiles()
        write(
            "config.rb",
            """
            class Config
              def initialize(name)
                @name = name
              end
            end
            """.trimIndent() + "\n",
        )
        addCommit("added config with name", "config.rb", author = "Gary Rennie <webmaster@gazler.com>")
        write(
            "config.rb",
            """
            class Config
              def initialize(name, options = {})
                @name = name

                if options[:downcase]
                  @name.downcase!
                end

              end
            end
            """.trimIndent() + "\n",
        )
        addCommit("added options", "config.rb", author = "Spider Man <spidey-sense@tingling.com>")
        write(
            "config.rb",
            """
            class Config
              attr_accessor :name, :password
              def initialize(name, password = nil, options = {})
                @name = name

                if options[:downcase]
                  @name.downcase!
                end

              end
            end
            """.trimIndent() + "\n",
        )
        addCommit("added password", "config.rb", author = "Bruce Banner <hulk@smash.com>")
        write(
            "config.rb",
            """
            class Config
              attr_accessor :name, :password
              def initialize(name, password = nil, options = {})
                @name = name
                @password = password || "i<3evil"

                if options[:downcase]
                  @name.downcase!
                end

              end
            end
            """.trimIndent() + "\n",
        )
        addCommit("added more options", "config.rb", author = "Spider Man <spidey-sense@tingling.com>")
        write("config.rb", blameLevelFinalConfigRb())
        addCommit("added more options (no really)", "config.rb", author = "Gary Rennie <webmaster@gazler.com>")
        true
    },
    validator = commandAnswer("Spider Man"),
    testCases = listOf(
        levelTestCase("answer author", "Spider Man"),
    ),
    setupChecks = listOf(
        levelSetupCheck(
            "upstream password line is present",
            "blame should start with upstream config.rb password line.",
        ) { repo ->
            repo.files.firstOrNull { it.name == "config.rb" }?.content?.contains("@password = password || \"i<3evil\"") == true
        },
    ),
)

private fun blameLevelFinalConfigRb(): String =
    """
    class Config
      attr_accessor :name, :password
      def initialize(name, password = nil, options = {})
        @name = name
        @password = password || "i<3evil"

        if options[:downcase]
          @name.downcase!
        end

        if options[:upcase]
          @name.upcase!
        end

      end
    end
    """.trimIndent() + "\n"
