use armdb::{ConstMap, ConstTree, FixedMap, FixedTree, RapiraCodec, TypedMap, TypedTree};
#[cfg(feature = "var-collections")]
use armdb::{VarMap, VarTree};

use crate::common::Record;
#[cfg(feature = "var-collections")]
use crate::common::data::make_var_value;
use crate::common::data::{make_const_value, make_key, make_record};

// ---- Bitcask: insert then flush_buffers ---------------------------------

pub fn populate_const(t: &ConstTree<[u8; 8], 16>, n: u64) {
    for i in 0..n {
        t.put(&make_key(i), &make_const_value(i)).unwrap();
    }
    t.flush_buffers().unwrap();
}

pub fn populate_typed(t: &TypedTree<[u8; 8], Record, RapiraCodec>, n: u64) {
    for i in 0..n {
        t.put(&make_key(i), make_record(i)).unwrap();
    }
    t.flush_buffers().unwrap();
}

pub fn populate_const_map(m: &ConstMap<[u8; 8], 16>, n: u64) {
    for i in 0..n {
        m.put(&make_key(i), &make_const_value(i)).unwrap();
    }
    m.flush_buffers().unwrap();
}

pub fn populate_typed_map(m: &TypedMap<[u8; 8], Record, RapiraCodec>, n: u64) {
    for i in 0..n {
        m.put(&make_key(i), make_record(i)).unwrap();
    }
    m.flush_buffers().unwrap();
}

#[cfg(feature = "var-collections")]
pub fn populate_var(t: &VarTree<[u8; 8]>, n: u64) {
    for i in 0..n {
        t.put(&make_key(i), &make_var_value(i)).unwrap();
    }
    t.flush_buffers().unwrap();
}

#[cfg(feature = "var-collections")]
pub fn populate_var_map(m: &VarMap<[u8; 8]>, n: u64) {
    for i in 0..n {
        m.put(&make_key(i), &make_var_value(i)).unwrap();
    }
    m.flush_buffers().unwrap();
}

// ---- FixedStore: insert only (in-place, no flush_buffers) ---------------

pub fn populate_fixed_tree(t: &FixedTree<[u8; 8], 16>, n: u64) {
    for i in 0..n {
        t.put(&make_key(i), &make_const_value(i)).unwrap();
    }
}

pub fn populate_fixed_map(m: &FixedMap<[u8; 8], 16>, n: u64) {
    for i in 0..n {
        m.put(&make_key(i), &make_const_value(i)).unwrap();
    }
}

// ---- grouped populate for scan (ConstTree) ------------------------------

pub fn populate_const_grouped(t: &ConstTree<[u8; 8], 16>, groups: u32, per_group: u32) {
    use crate::common::data::make_group_key;
    for g in 0..groups {
        for id in 0..per_group {
            let key = make_group_key(g, id);
            let val = make_const_value(g as u64 * 1000 + id as u64);
            t.put(&key, &val).unwrap();
        }
    }
    t.flush_buffers().unwrap();
}
