// Copyright 2009-present MongoDB, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#include <mongocxx/v1/insert_many_options.hpp>

//

#include <mongocxx/v1/read_concern.hpp>
#include <mongocxx/v1/write_concern.hpp>

#include <bsoncxx/test/v1/stdx/string_view.hh>
#include <bsoncxx/test/v1/types/value.hh>

#include <utility>

#include <catch2/catch_test_macros.hpp>
#include <catch2/generators/catch_generators.hpp>
#include <catch2/generators/catch_generators_adapters.hpp>

namespace mongocxx {
namespace v1 {

TEST_CASE("ownership", "[mongocxx][v1][insert_many_options]") {
    insert_many_options source;
    insert_many_options target;

    source.comment(bsoncxx::v1::types::value{"source"});
    target.comment(bsoncxx::v1::types::value{"target"});

    REQUIRE(source.comment() == bsoncxx::v1::types::b_string{"source"});
    REQUIRE(target.comment() == bsoncxx::v1::types::b_string{"target"});

    auto const source_value = source.comment();

    SECTION("move") {
        auto move = std::move(source);

        // source is in an assign-or-destroy-only state.

        CHECK(move.comment() == source_value);

        target = std::move(move);

        // source is in an assign-or-destroy-only state.

        CHECK(target.comment() == source_value);
    }

    SECTION("copy") {
        auto copy = source;

        CHECK(source.comment() == source_value);
        CHECK(copy.comment() == source_value);

        target = copy;

        CHECK(copy.comment() == source_value);
        CHECK(target.comment() == source_value);
    }
}

TEST_CASE("default", "[mongocxx][v1][insert_many_options]") {
    insert_many_options const opts;

    CHECK_FALSE(opts.bypass_document_validation().has_value());
    CHECK_FALSE(opts.read_concern().has_value());
    CHECK_FALSE(opts.write_concern().has_value());
    CHECK_FALSE(opts.ordered().has_value());
    CHECK_FALSE(opts.comment().has_value());
}

TEST_CASE("bypass_document_validation", "[mongocxx][v1][insert_many_options]") {
    auto const v = GENERATE(false, true);

    CHECK(insert_many_options{}.bypass_document_validation(v).bypass_document_validation() == v);
}

TEST_CASE("read_concern", "[mongocxx][v1][insert_many_options]") {
    using T = v1::read_concern;

    auto const v = GENERATE(values({
        T{},
        T{}.acknowledge_level(T::level::k_majority),
    }));

    CHECK(insert_many_options{}.read_concern(v).read_concern() == v);
}

TEST_CASE("write_concern", "[mongocxx][v1][insert_many_options]") {
    using T = v1::write_concern;

    auto const v = GENERATE(values({
        T{},
        T{}.acknowledge_level(T::level::k_majority),
        T{}.tag("abc"),
    }));

    CHECK(insert_many_options{}.write_concern(v).write_concern() == v);
}

TEST_CASE("ordered", "[mongocxx][v1][insert_many_options]") {
    auto const v = GENERATE(false, true);

    CHECK(insert_many_options{}.ordered(v).ordered() == v);
}

TEST_CASE("comment", "[mongocxx][v1][insert_many_options]") {
    using T = bsoncxx::v1::types::value;

    auto const v = GENERATE(values({
        T{},
        T{std::int32_t{123}},
        T{std::int64_t{456}},
        T{123.456},
        T{"abc"},
    }));

    CHECK(insert_many_options{}.comment(v).comment() == v);
}

} // namespace v1
} // namespace mongocxx
