#include "test_support.h"

#include "integration/support.h"

namespace vn::integration {
namespace {

void history_log_is_topological(test::Context& t) {
    VnFixture f;
    f.init();
    f.write("base.txt", "base\n");
    f.vn({"bmk", "set", "master"});
    f.commit("base");
    f.vn({"bmk", "set", "left"});
    f.vn({"new", "left"});
    f.write("left.txt", "left\n");
    f.commit("left");
    f.vn({"bmk", "set", "right"});
    f.vn({"new", "master"});
    f.write("right.txt", "right\n");
    f.commit("right");

    const std::vector<std::string> commits = f.log_commits();
    for (std::size_t index = 0; index < commits.size(); ++index) {
        for (const std::string& parent : f.parents(commits[index])) {
            const auto found = std::find(commits.begin(), commits.end(), parent.substr(0, 12));
            if (found == commits.end())
                continue;
            const std::size_t parent_index =
                static_cast<std::size_t>(std::distance(commits.begin(), found));
            t.expect(parent_index > index);
        }
    }
}

void history_amend_rewrites_descendants(test::Context& t) {
    VnFixture f;
    f.init();
    f.write("f.txt", "one\n");
    f.commit("one");
    const std::string one = f.commit_for_message("one");
    f.write("f.txt", "two\n");
    f.commit("two");
    const std::string two = f.commit_for_message("two");
    const std::string one_change = f.metadata(one).at("change");
    const std::string two_change = f.metadata(two).at("change");
    f.vn({"amend", one, "-m", "one renamed"});
    const std::string new_one = f.commit_for_message("one renamed");
    const std::string new_two = f.commit_for_message("two");
    t.expect_ne(one.substr(0, 12), new_one);
    t.expect_ne(two.substr(0, 12), new_two);
    t.expect_eq(f.metadata(new_one).at("change"), one_change);
    t.expect_eq(f.metadata(new_two).at("change"), two_change);
    t.expect_eq(f.parents(new_two).count(new_one), 1u);
}

void history_undo_and_operation_restore(test::Context& t) {
    VnFixture f;
    f.init();
    f.write("f.txt", "one\n");
    f.commit("one");
    const std::string one = f.commit_for_message("one");
    f.write("f.txt", "two\n");
    f.commit("two");
    f.vn({"amend", one, "-m", "renamed"});
    t.expect_contains(f.vn({"log"}).out, "renamed");
    f.vn({"undo"});
    t.expect_not_contains(f.vn({"log"}).out, "renamed");
    const std::vector<std::string> ops = split_lines(f.vn({"op", "log"}).out);
    t.expect(ops.size() >= 2);
    if (ops.size() >= 2) {
        std::istringstream stream(ops[1]);
        std::string previous;
        stream >> previous;
        f.vn({"op", "restore", previous});
        t.expect_contains(f.vn({"log"}).out, "renamed");
    }
}

void history_log_default_amount(test::Context& t) {
    VnFixture f;
    f.init();
    linear_history(f, 22);
    t.expect_eq(f.log_rows().size(), 20u);
    const std::vector<LogRow> default_rows = f.log_rows();
    bool has_newest = false;
    bool has_oldest = false;
    for (const LogRow& row : default_rows) {
        if (row.message == "commit 21")
            has_newest = true;
        if (row.message == "commit 2")
            has_oldest = true;
    }
    t.expect(has_newest);
    t.expect(!has_oldest);

    f.vn({"cfg", "-global", "log.default_amount", "3"});
    t.expect_eq(f.log_rows().size(), 3u);
    t.expect_eq(split_lines(f.vn({"log", "-a", "-no-col-titles"}).out).size(), 23u);
    t.expect_eq(split_lines(f.vn({"log", "-all", "-no-col-titles"}).out).size(), 23u);
    t.expect_eq(split_lines(f.vn({"log", "-a", "-l5", "-no-col-titles"}).out).size(), 23u);
    t.expect_eq(split_lines(f.vn({"log", "-l5", "-a", "-no-col-titles"}).out).size(), 23u);
    f.vn({"cfg", "log.default_amount", "1"});
    t.expect_eq(f.log_rows().size(), 3u);
    t.expect_eq(split_lines(f.vn({"log", "-l5", "-no-col-titles"}).out).size(), 5u);
    const auto oldest = split_lines(f.vn({"log", "-f2", "-no-col-titles"}).out);
    t.expect_eq(oldest.size(), 2u);
    if (oldest.size() == 2) {
        t.expect_contains(oldest[0], "commit 0");
        t.expect_contains(oldest[1], "commit 1");
    }
    f.vn({"cfg", "-global", "log.default_amount", "not-a-number"});
    t.expect_eq(f.log_rows().size(), 20u);
}

void history_diff_path_filter(test::Context& t) {
    VnFixture f;
    f.init();
    f.write("one.txt", "one\n");
    f.write("two.txt", "two\n");
    f.commit("base");
    f.write("one.txt", "one changed\n");
    f.write("two.txt", "two changed\n");

    const std::string one_diff = f.vn({"diff", "one.txt"}).out;
    t.expect_contains(one_diff, "+one changed");
    t.expect_not_contains(one_diff, "+two changed");
    const std::string all_diff = f.vn({"diff", "-a"}).out;
    t.expect_contains(all_diff, "+one changed");
    t.expect_contains(all_diff, "+two changed");
    f.failed({"diff", "--all"}, "unknown flag");
    f.failed({"show", "--all"}, "unknown flag");

    const fs::path subdir = f.repo / "sub";
    fs::create_directories(subdir);
    const std::string relative_diff = f.vn({"diff", "../one.txt"}, subdir).out;
    t.expect_contains(relative_diff, "+one changed");
    t.expect_not_contains(relative_diff, "+two changed");

    f.vn({"go", "-m", "second"});
    const std::string second = f.commit_for_message("second");
    const std::string second_full = f.hash(second);
    const std::string revision_diff = f.vn({"diff", "-rev", second_full, "one.txt"}).out;
    t.expect_contains(revision_diff, "+one changed");
    t.expect_not_contains(revision_diff, "+two changed");
    t.expect_contains(f.vn({"diff"}).out, "no diff to show");

    // A historical revision diff must not snapshot (and therefore must not
    // rewrite) the working copy just to print its result.
    const fs::path heads = f.repo / ".veins" / "op-heads";
    const std::size_t heads_before = list_directory(heads).size();
    f.write("one.txt", "one dirty\n");
    const std::string dirty_historical = f.vn({"diff", "-rev", second_full, "one.txt"}).out;
    t.expect_contains(dirty_historical, "+one changed");
    t.expect_not_contains(dirty_historical, "+one dirty");
    t.expect_eq(list_directory(heads).size(), heads_before);

    // A bookmark that points at the working copy still includes uncommitted
    // changes, so resolving it must trigger a snapshot.
    f.vn({"bmk", "set", "master"});
    t.expect_contains(f.vn({"diff", "master"}).out, "+one dirty");
    t.expect_contains(f.vn({"help", "diff"}).out, "-a, -all");
}

void history_show_and_diff(test::Context& t) {
    VnFixture f;
    f.init();
    f.write("f.txt", "one\n");
    f.commit("one");
    const std::string one = f.commit_for_message("one");
    f.write("f.txt", "two\n");
    f.commit("two");
    const std::string diff = f.vn({"diff", one, "@"}).out;
    t.expect_contains(diff, "-one");
    t.expect_contains(diff, "+two");
    const std::string shown = f.vn({"show", one}).out;
    t.expect_contains(shown, "one");
    t.expect_contains(f.vn({"show", "-a", one}).out, "--- /dev/null");
    t.expect_contains(f.vn({"help", "show"}).out, "-a, -all");
}

} // namespace

test::Suite history_suite() {
    return {"history",
            {
                {"log_is_topological", history_log_is_topological},
                {"log_default_amount", history_log_default_amount},
                {"amend_rewrites_descendants", history_amend_rewrites_descendants},
                {"undo_and_operation_restore", history_undo_and_operation_restore},
                {"diff_path_filter", history_diff_path_filter},
                {"show_and_diff", history_show_and_diff},
            }};
}
} // namespace vn::integration
