#include "universal_container/adaptive_sequence.hpp"
#include "universal_container/ring_block.hpp"

#include <algorithm>
#include <cstdint>
#include <cstdlib>
#include <iostream>
#include <optional>
#include <random>
#include <stdexcept>
#include <string>
#include <vector>

namespace {

int failures = 0;

void expect(bool condition, const char* expression, int line) {
    if (!condition) {
        std::cerr << "FAIL line " << line << ": " << expression << '\n';
        ++failures;
    }
}

#define EXPECT(expression) expect(static_cast<bool>(expression), #expression, __LINE__)

struct DeferredTransitionPolicy {
    uc::TieredConfig target{8, 4, 2};
    bool ready = false;
    std::size_t decisions = 0;
    std::size_t transitions = 0;
    uc::StorageMode last_from = uc::StorageMode::vector;
    uc::StorageMode last_to = uc::StorageMode::vector;

    void observe(const uc::OperationSample& sample) noexcept {
        if (sample.kind == uc::OperationKind::insert) {
            ready = true;
        }
    }

    [[nodiscard]] bool decision_ready() const noexcept { return ready; }

    [[nodiscard]] std::optional<uc::AdaptationDecision>
    recommended_decision(uc::StorageMode current,
                         std::size_t,
                         uc::TieredConfig) noexcept {
        if (!ready) {
            return std::nullopt;
        }
        ready = false;
        ++decisions;
        if (current == uc::StorageMode::vector) {
            return uc::AdaptationDecision{uc::StorageMode::tiered, target, 1.0};
        }
        return std::nullopt;
    }

    void on_transition(uc::StorageMode from,
                       uc::StorageMode to,
                       uc::TieredConfig active) noexcept {
        ++transitions;
        last_from = from;
        last_to = to;
        target = active;
        ready = false;
    }

    void reset() noexcept {
        ready = false;
        decisions = 0;
        transitions = 0;
    }
};

template <class Sequence, class Reference>
void expect_equal(const Sequence& sequence, const Reference& reference) {
    EXPECT(sequence.size() == reference.size());
    for (std::size_t i = 0; i < reference.size() && i < sequence.size(); ++i) {
        EXPECT(sequence[i] == reference[i]);
    }
}

void ring_block_offsets() {
    uc::detail::RingBlock<int> block(8);
    block.push_back(2);
    block.push_back(3);
    block.insert(0, 1);
    block.insert(0, 0);
    block.insert(2, 9);
    EXPECT(block.offset() != 0);
    const std::vector<int> expected{0, 1, 9, 2, 3};
    for (std::size_t i = 0; i < expected.size(); ++i) {
        EXPECT(block[i] == expected[i]);
    }
    EXPECT(block.erase(2) == 9);
    EXPECT(block.erase(0) == 0);
    EXPECT(block.size() == 3);
    EXPECT(block[0] == 1 && block[1] == 2 && block[2] == 3);
}

void randomized_differential() {
    uc::TieredConfig config{16, 4, 4};
    uc::AdaptiveSequence<std::uint32_t> actual(config);
    actual.force_tiered_mode();
    std::vector<std::uint32_t> expected;
    std::mt19937_64 random(0xC0FFEEULL);

    for (std::size_t step = 0; step < 30'000; ++step) {
        const auto operation = static_cast<unsigned>(random() % 5);
        if (expected.empty() || operation == 0) {
            const auto value = static_cast<std::uint32_t>(random());
            const auto index = expected.empty() ? 0 : static_cast<std::size_t>(random() % (expected.size() + 1));
            expected.insert(expected.begin() + static_cast<std::ptrdiff_t>(index), value);
            actual.insert(index, value);
        } else if (operation == 1) {
            const auto index = static_cast<std::size_t>(random() % expected.size());
            expected.erase(expected.begin() + static_cast<std::ptrdiff_t>(index));
            actual.erase(index);
        } else if (operation == 2) {
            const auto value = static_cast<std::uint32_t>(random());
            expected.push_back(value);
            actual.push_back(value);
        } else if (operation == 3) {
            const auto index = static_cast<std::size_t>(random() % expected.size());
            const auto value = static_cast<std::uint32_t>(random());
            expected[index] = value;
            actual.set(index, value);
        } else {
            const auto index = static_cast<std::size_t>(random() % expected.size());
            EXPECT(actual[index] == expected[index]);
        }

        if (step % 251 == 0) {
            expect_equal(actual, expected);
            actual.force_vector_mode();
            expect_equal(actual, expected);
            actual.force_tiered_mode();
        }
    }
    expect_equal(actual, expected);
}

void indexed_duplicates_and_ids() {
    uc::AdaptiveSequence<std::uint32_t, true> values({8, 4, 3});
    values.push_back(5);
    values.push_back(8);
    values.push_back(5);
    values.push_back(3);
    values.push_back(5);

    const auto middle_id = values.id_at(2);
    EXPECT(values.contains(5));
    EXPECT(values.find_all(5) == std::vector<std::size_t>({0, 2, 4}));

    values.force_tiered_mode();
    values.insert(1, 5);
    EXPECT(values.find_all(5) == std::vector<std::size_t>({0, 1, 3, 5}));
    EXPECT(values.id_alive(middle_id));
    values.force_tiered_mode({16, 4, 3});
    EXPECT(values.find_all(5) == std::vector<std::size_t>({0, 1, 3, 5}));
    EXPECT(values.id_alive(middle_id));

    values[3] = 42;
    EXPECT(values.find_all(5) == std::vector<std::size_t>({0, 1, 5}));
    EXPECT(values.contains(42));
    values.erase_by_id(middle_id);
    EXPECT(!values.id_alive(middle_id));
    EXPECT(!values.contains(42));

    EXPECT(values.erase_all(5) == 3);
    EXPECT(!values.contains(5));
    EXPECT(values.size() == 2);
}

void indexed_proxy_detects_structural_invalidation() {
    uc::AdaptiveSequence<std::uint32_t, true> values({8, 4, 2});
    values.push_back(10);
    values.push_back(20);
    auto stale = values[1];

    values.insert(0, 5);
    bool invalidation_detected = false;
    try {
        stale = 99;
    } catch (const std::logic_error&) {
        invalidation_detected = true;
    }

    EXPECT(invalidation_detected);
    const auto& read_only = values;
    EXPECT(read_only[0] == 5);
    EXPECT(read_only[1] == 10);
    EXPECT(read_only[2] == 20);
}

void iterator_and_contiguous_contract() {
    uc::AdaptiveSequence<int> values({8, 4, 3});
    for (int i = 0; i < 100; ++i) {
        values.push_back(i);
    }
    values.force_tiered_mode();
    EXPECT(values.data() == nullptr);

    std::int64_t sum = 0;
    values.for_each([&](int value) { sum += value; });
    EXPECT(sum == 4'950);

    const auto contiguous = values.make_contiguous();
    EXPECT(values.mode() == uc::StorageMode::vector);
    EXPECT(contiguous.size() == 100);
    EXPECT(contiguous[37] == 37);

    auto iterator = values.begin();
    EXPECT(*iterator == 0);
    values.push_back(100);
    bool invalidation_detected = false;
    try {
        (void)*iterator;
    } catch (const std::logic_error&) {
        invalidation_detected = true;
    }
    EXPECT(invalidation_detected);
}

struct NonTrivial {
    std::string text;
    std::uint64_t marker = 0;

    NonTrivial(std::string value, std::uint64_t number)
        : text(std::move(value)), marker(number) {}
    NonTrivial(const NonTrivial&) = default;
    NonTrivial(NonTrivial&&) noexcept = default;
    NonTrivial& operator=(const NonTrivial&) = default;
    NonTrivial& operator=(NonTrivial&&) noexcept = default;
    ~NonTrivial() { marker ^= 0; }

    friend bool operator==(const NonTrivial&, const NonTrivial&) = default;
};

void non_trivial_values() {
    uc::AdaptiveSequence<NonTrivial> values({4, 4, 2});
    values.push_back(NonTrivial{"alpha", 1});
    values.push_back(NonTrivial{"gamma", 3});
    values.force_tiered_mode();
    values.insert(1, NonTrivial{"beta", 2});
    EXPECT(values[0].text == "alpha");
    EXPECT(values[1].text == "beta");
    EXPECT(values[2].text == "gamma");
    values.erase(0);
    EXPECT(values[0].marker == 2);
}

void tiered_shape_rebuild_preserves_order() {
    uc::AdaptiveSequence<std::uint32_t> values({8, 4, 2});
    std::vector<std::uint32_t> expected;
    for (std::uint32_t i = 0; i < 257; ++i) {
        values.push_back(i * 3 + 1);
        expected.push_back(i * 3 + 1);
    }

    values.force_tiered_mode();
    values.force_tiered_mode({64, 8, 3});
    EXPECT(values.mode() == uc::StorageMode::tiered);
    EXPECT(values.tiered_config() == (uc::TieredConfig{64, 8, 3}));
    expect_equal(values, expected);

    values.insert(129, 0xfeedu);
    expected.insert(expected.begin() + 129, 0xfeedu);
    values.force_tiered_mode({16, 4, 4});
    EXPECT(values.tiered_config() == (uc::TieredConfig{16, 4, 4}));
    expect_equal(values, expected);

    values.force_tiered_mode({16, 4, 1});
    values.force_tiered_mode({16, 4, 4});
    expect_equal(values, expected);
    EXPECT(values.policy().telemetry().tiered_rebuilds == 4);
    EXPECT(values.policy().telemetry().tiered_leaf_rebuilds == 2);
    EXPECT(values.policy().telemetry().tiered_directory_rebuilds == 2);
}

void adaptation_is_deferred_until_a_safe_boundary() {
    using sequence_type = uc::AdaptiveSequence<
        std::uint32_t, false, DeferredTransitionPolicy>;

    sequence_type explicit_maintenance({8, 4, 2}, DeferredTransitionPolicy{});
    for (std::uint32_t value = 0; value < 4; ++value) {
        explicit_maintenance.push_back(value);
    }
    explicit_maintenance.insert(2, 99);
    EXPECT(explicit_maintenance.mode() == uc::StorageMode::vector);
    EXPECT(explicit_maintenance.policy().decision_ready());
    EXPECT(explicit_maintenance.policy().transitions == 0);
    EXPECT(explicit_maintenance.adapt_now());
    EXPECT(explicit_maintenance.mode() == uc::StorageMode::tiered);
    EXPECT(explicit_maintenance.policy().decisions == 1);
    EXPECT(explicit_maintenance.policy().transitions == 1);
    EXPECT(explicit_maintenance.policy().last_from == uc::StorageMode::vector);
    EXPECT(explicit_maintenance.policy().last_to == uc::StorageMode::tiered);
    EXPECT(!explicit_maintenance.adapt_now());
    EXPECT(!explicit_maintenance.adapt_now());
    EXPECT(explicit_maintenance.policy().decisions == 1);
    EXPECT(explicit_maintenance.policy().transitions == 1);
    expect_equal(explicit_maintenance,
                 std::vector<std::uint32_t>({0, 1, 99, 2, 3}));

    sequence_type next_mutation({8, 4, 2}, DeferredTransitionPolicy{});
    for (std::uint32_t value = 0; value < 4; ++value) {
        next_mutation.push_back(value);
    }
    next_mutation.insert(1, 77);
    EXPECT(next_mutation.mode() == uc::StorageMode::vector);
    EXPECT(next_mutation.policy().transitions == 0);

    // The pending recommendation is applied before this append; it is not
    // applied after the preceding, already committed insertion.
    next_mutation.push_back(4);
    EXPECT(next_mutation.mode() == uc::StorageMode::tiered);
    EXPECT(next_mutation.policy().decisions == 1);
    EXPECT(next_mutation.policy().transitions == 1);
    expect_equal(next_mutation,
                 std::vector<std::uint32_t>({0, 77, 1, 2, 3, 4}));
}

std::optional<uc::AdaptationDecision> shape_decision(
    std::size_t n,
    uc::TieredConfig current,
    bool localized) {
    uc::AdaptationConfig config;
    config.evaluation_interval = 64;
    config.minimum_observations = 64;
    config.minimum_edit_observations = 64;
    config.minimum_residency_operations = 0;
    config.minimum_shape_residency_operations = 0;
    config.minimum_shape_edit_fraction = 0.0;
    config.required_confirmation_windows = 1;
    config.minimum_tiered_size = 1;
    config.forecast_operations = 1'000'000;
    config.tiered_rebuild_safety = 0.0;
    config.minimum_shape_improvement = 0.0;
    config.vector_move_unit = 1.0;
    uc::CostModelPolicy policy(config, current);
    policy.on_transition(uc::StorageMode::vector,
                         uc::StorageMode::tiered,
                         current);

    for (std::size_t i = 0; i < 64; ++i) {
        const auto position = localized
            ? n / 2 + i % 17
            : (i * 104'729 + 17) % n;
        policy.observe({uc::OperationKind::insert, n, position, 1,
                        sizeof(std::uint32_t)});
    }
    return policy.recommended_decision(uc::StorageMode::tiered, n, current);
}

void forecast_horizon_is_bounded_by_observed_evidence() {
    uc::AdaptationConfig config;
    config.evaluation_interval = 64;
    config.minimum_observations = 64;
    config.minimum_residency_operations = 0;
    config.required_confirmation_windows = 1;
    config.forecast_operations = 10'000;
    config.minimum_forecast_operations = 1;
    config.forecast_growth_factor = 8.0;

    const uc::TieredConfig tiered{64, 64, 4};
    uc::CostModelPolicy policy(config, tiered);
    for (std::size_t i = 0; i < 64; ++i) {
        policy.observe({uc::OperationKind::insert, 10'000, i, 1,
                        sizeof(std::uint32_t)});
    }
    (void)policy.recommended_decision(uc::StorageMode::vector, 10'000, tiered);
    EXPECT(policy.telemetry().last_forecast_operations == 512);
}

void confirmation_requires_consecutive_supporting_windows() {
    uc::AdaptationConfig config;
    config.evaluation_interval = 16;
    config.edit_evaluation_interval = 8;
    config.minimum_observations = 16;
    config.minimum_edit_observations = 8;
    config.minimum_residency_operations = 0;
    config.minimum_tiered_size = 1;
    config.required_confirmation_windows = 2;
    config.forecast_operations = 1'000'000;
    config.minimum_forecast_operations = 1;
    config.vector_to_tiered_safety = 0.0;
    config.vector_move_unit = 1.0;
    config.tiered_move_unit = 0.01;
    config.tiered_directory_unit = 0.0;
    config.tiered_read_base = 2.0;
    config.ewma_alpha = 1.0;
    config.tiered_entry_edit_fraction = 0.50;

    const uc::TieredConfig tiered{64, 64, 4};
    uc::CostModelPolicy policy(config, tiered);
    constexpr std::size_t n = 10'000;
    auto edit_window = [&] {
        for (std::size_t i = 0; i < 8; ++i) {
            policy.observe({uc::OperationKind::insert, n, n / 2 + i,
                            1, sizeof(std::uint32_t)});
        }
    };

    edit_window();
    for (std::size_t i = 0; i < 16; ++i) {
        policy.observe({uc::OperationKind::random_read, n, i,
                        1, sizeof(std::uint32_t)});
    }
    edit_window();

    // Three windows accumulated, but the middle one contradicted the tiered
    // proposal, so the final edit window starts a new streak of one.
    EXPECT(!policy.recommended_decision(
        uc::StorageMode::vector, n, tiered).has_value());

    edit_window();
    const auto confirmed = policy.recommended_decision(
        uc::StorageMode::vector, n, tiered);
    EXPECT(confirmed.has_value());
    if (confirmed) {
        EXPECT(confirmed->target == uc::StorageMode::tiered);
    }
}

void stationary_phases_do_not_thrash() {
    constexpr std::size_t operations = 30'000;

    {
        // This is the v12 failure shape: a stationary uniform edit phase at
        // N=10k used to alternate vector/tiered dozens of times.
        uc::AdaptationConfig config;
        const uc::TieredConfig initial{512, 64, 4};
        uc::CostModelPolicy policy(config, initial);
        uc::StorageMode mode = uc::StorageMode::vector;
        auto active = initial;
        std::mt19937_64 random(0x7c4a'9e13'51d2'08b7ULL);

        for (std::size_t observed = 0; observed < operations; observed += 4) {
            const auto position = static_cast<std::size_t>(random() % 10'000);
            policy.observe({uc::OperationKind::insert, 10'000, position,
                            4, sizeof(std::uint64_t)});
            if (!policy.decision_ready()) {
                continue;
            }
            const auto decision = policy.recommended_decision(mode, 10'000, active);
            if (!decision) {
                continue;
            }
            const auto previous = mode;
            mode = decision->target;
            if (mode == uc::StorageMode::tiered) {
                active = decision->tiered_config;
            }
            policy.on_transition(previous, mode, active);
        }

        const auto& telemetry = policy.telemetry();
        EXPECT(telemetry.vector_to_tiered + telemetry.tiered_to_vector <= 1);
    }

    {
        // Starting tiered isolates shape selection.  A stationary uniform
        // phase may select a better leaf once, but must not bounce thereafter.
        uc::AdaptationConfig config;
        const uc::TieredConfig initial{512, 64, 4};
        uc::CostModelPolicy policy(config, initial);
        uc::StorageMode mode = uc::StorageMode::tiered;
        auto active = initial;
        policy.on_transition(uc::StorageMode::vector, mode, active);
        std::mt19937_64 random(0xd1b5'4a32'09fc'77e1ULL);

        for (std::size_t observed = 0; observed < operations; observed += 4) {
            const auto position = static_cast<std::size_t>(random() % 100'000);
            policy.observe({uc::OperationKind::insert, 100'000, position,
                            4, sizeof(std::uint64_t)});
            if (!policy.decision_ready()) {
                continue;
            }
            const auto decision = policy.recommended_decision(mode, 100'000, active);
            if (!decision) {
                continue;
            }
            const auto previous = mode;
            mode = decision->target;
            if (mode == uc::StorageMode::tiered) {
                active = decision->tiered_config;
            }
            policy.on_transition(previous, mode, active);
        }

        const auto& telemetry = policy.telemetry();
        EXPECT(telemetry.tiered_to_vector == 0);
        EXPECT(telemetry.tiered_leaf_rebuilds <= 1);
    }
}

void policy_changes_shape_with_scale_and_locality() {
    const auto small_uniform = shape_decision(10'000, {512, 64, 4}, false);
    EXPECT(small_uniform.has_value());
    EXPECT(small_uniform->target == uc::StorageMode::tiered);
    EXPECT(small_uniform->tiered_config.leaf_capacity <= 128);

    const auto large_uniform = shape_decision(1'000'000, {64, 64, 4}, false);
    EXPECT(large_uniform.has_value());
    EXPECT(large_uniform->target == uc::StorageMode::tiered);
    EXPECT(large_uniform->tiered_config.leaf_capacity >= 512);

    const auto large_local = shape_decision(1'000'000, {1024, 64, 4}, true);
    EXPECT(large_local.has_value());
    EXPECT(large_local->target == uc::StorageMode::tiered);
    EXPECT(large_local->tiered_config.leaf_capacity
           < large_uniform->tiered_config.leaf_capacity);

    uc::AdaptationConfig depth_config;
    depth_config.evaluation_interval = 64;
    depth_config.minimum_observations = 64;
    depth_config.minimum_residency_operations = 0;
    depth_config.minimum_shape_residency_operations = 0;
    depth_config.minimum_shape_edit_fraction = 0.0;
    depth_config.required_confirmation_windows = 1;
    depth_config.forecast_operations = 1'000'000;
    depth_config.tiered_rebuild_safety = 0.0;
    depth_config.minimum_shape_improvement = 0.0;
    depth_config.vector_read = 100.0;
    depth_config.tiered_leaf_candidates.fill(64);
    uc::CostModelPolicy depth_policy(depth_config, {64, 64, 2});
    depth_policy.on_transition(uc::StorageMode::vector,
                               uc::StorageMode::tiered,
                               {64, 64, 2});
    for (std::size_t i = 0; i < 64; ++i) {
        depth_policy.observe({uc::OperationKind::random_read, 1'000'000, i,
                              1, sizeof(std::uint32_t)});
    }
    const auto depth = depth_policy.recommended_decision(
        uc::StorageMode::tiered, 1'000'000, {64, 64, 2});
    EXPECT(depth.has_value());
    EXPECT(depth->target == uc::StorageMode::tiered);
    EXPECT(depth->tiered_config.leaf_capacity == 64);
    EXPECT(depth->tiered_config.directory_levels == 3);
}

void container_applies_same_mode_shape_decision() {
    uc::AdaptationConfig config;
    config.evaluation_interval = 64;
    config.minimum_observations = 64;
    config.minimum_residency_operations = 0;
    config.minimum_shape_residency_operations = 0;
    config.minimum_shape_edit_fraction = 0.0;
    config.required_confirmation_windows = 1;
    config.minimum_tiered_size = 1;
    config.forecast_operations = 1'000'000;
    config.tiered_rebuild_safety = 0.0;
    config.minimum_shape_improvement = 0.0;
    config.vector_move_unit = 1.0;
    const uc::TieredConfig initial_shape{1024, 64, 4};
    uc::AdaptiveSequence<std::uint32_t> values(
        initial_shape, uc::CostModelPolicy(config, initial_shape));
    for (std::uint32_t i = 0; i < 10'000; ++i) {
        values.push_back(i);
    }
    values.force_tiered_mode(initial_shape);
    values.enable_auto_mode();

    for (std::size_t i = 0;
         i < 256 && values.policy().telemetry().tiered_rebuilds == 0; ++i) {
        const auto position = (i * 104'729 + 17) % values.size();
        values.insert(position, 0xabcdefu);
        values.erase(position);
    }

    EXPECT(values.mode() == uc::StorageMode::tiered);
    EXPECT(values.policy().telemetry().tiered_rebuilds == 1);
    EXPECT(values.tiered_config().leaf_capacity <= 128);
    for (std::size_t i = 0; i < values.size(); ++i) {
        EXPECT(values[i] == i);
    }
}

void policy_switches_for_sustained_middle_edits() {
    uc::TieredConfig tiered{32, 16, 3};
    uc::AdaptationConfig adaptation;
    adaptation.evaluation_interval = 64;
    adaptation.minimum_observations = 64;
    adaptation.minimum_residency_operations = 0;
    adaptation.minimum_tiered_size = 128;
    adaptation.forecast_operations = 16'384;
    adaptation.vector_to_tiered_safety = 1.0;
    // Make the decision boundary deliberately small; this test checks policy
    // plumbing, while machine-calibrated constants are validated by benchmarks.
    adaptation.vector_move_unit = 0.20;
    adaptation.tiered_move_unit = 0.20;
    adaptation.tiered_read_base = 2.0;
    uc::AdaptiveSequence<std::uint32_t> values(
        tiered, uc::CostModelPolicy(adaptation, tiered));

    for (std::uint32_t i = 0; i < 512; ++i) {
        values.push_back(i);
    }
    values.enable_auto_mode();
    for (std::size_t i = 0; i < 256 && values.mode() != uc::StorageMode::tiered; ++i) {
        values.insert(values.size() / 2, 7);
        values.erase(values.size() / 2);
    }
    EXPECT(values.mode() == uc::StorageMode::tiered);
}

} // namespace

int main() {
    ring_block_offsets();
    randomized_differential();
    indexed_duplicates_and_ids();
    indexed_proxy_detects_structural_invalidation();
    iterator_and_contiguous_contract();
    non_trivial_values();
    tiered_shape_rebuild_preserves_order();
    adaptation_is_deferred_until_a_safe_boundary();
    forecast_horizon_is_bounded_by_observed_evidence();
    confirmation_requires_consecutive_supporting_windows();
    stationary_phases_do_not_thrash();
    policy_changes_shape_with_scale_and_locality();
    container_applies_same_mode_shape_decision();
    policy_switches_for_sustained_middle_edits();

    if (failures != 0) {
        std::cerr << failures << " test assertion(s) failed\n";
        return EXIT_FAILURE;
    }
    std::cout << "All AdaptiveSequence tests passed\n";
    return EXIT_SUCCESS;
}
