use crate::common::Record;

/// Length of variable-size values used by `VarTree` / `VarMap`.
/// Sized so hot/cold cache math is meaningful (see HOT_RANGE / FULL_CACHE_BYTES).
pub const VAR_VALUE_LEN: usize = 256;

/// 8-byte big-endian key. Big-endian keeps byte order == numeric order, so
/// prefix scans and ranges behave predictably.
pub fn make_key(id: u64) -> [u8; 8] {
    id.to_be_bytes()
}

/// 16-byte fixed value for Const*/Fixed* collections.
pub fn make_const_value(id: u64) -> [u8; 16] {
    let mut v = [0u8; 16];
    v[..8].copy_from_slice(&id.to_be_bytes());
    v[8..].copy_from_slice(&id.wrapping_mul(0x517c_c1b7_2722_0a95).to_be_bytes());
    v
}

/// Variable-size value for Var* collections: deterministic, `VAR_VALUE_LEN` bytes.
pub fn make_var_value(id: u64) -> Vec<u8> {
    let mut v = vec![0u8; VAR_VALUE_LEN];
    let seed = id.wrapping_mul(0x9e37_79b9_7f4a_7c15);
    for (i, b) in v.iter_mut().enumerate() {
        *b = (seed.wrapping_add(i as u64) & 0xff) as u8;
    }
    v
}

/// Small typed record for Typed* collections.
pub fn make_record(id: u64) -> Record {
    Record {
        id,
        value: id.wrapping_mul(0x517c_c1b7_2722_0a95),
        tags: vec![format!("tag-{}", id % 10), format!("cat-{}", id % 5)],
    }
}

/// Grouped key for scan benchmarks: high 4 bytes = group, low 4 bytes = id.
pub fn make_group_key(group: u32, id: u32) -> [u8; 8] {
    let mut key = [0u8; 8];
    key[..4].copy_from_slice(&group.to_be_bytes());
    key[4..].copy_from_slice(&id.to_be_bytes());
    key
}
