#!/usr/bin/env bash

set -euo pipefail

if [ "$#" -lt 1 ] || [ "$#" -gt 2 ]; then
    printf "Usage: %s <db_root_under_/nvme> [result_csv]\n" "$0"
    exit 1
fi

script_dir="$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" && pwd)"
root_dir="$(cd -- "${script_dir}/.." && pwd)"

if [[ "$1" != /nvme* ]]; then
    printf "db_root must be under /nvme, got: %s\n" "$1" >&2
    exit 1
fi

db_root="$1"
result_file="${2:-${script_dir}/phase1_results.csv}"

warmup_secs="${WARMUP_SECS:-120}"
measure_secs="${MEASURE_SECS:-300}"
repeats="${REPEATS:-3}"
read_path="${READ_PATH:-snapshot}"

mkdir -p "${db_root}"
mkdir -p "$(dirname -- "${result_file}")"

cargo build --release --manifest-path "${root_dir}/Cargo.toml"
(cd "${root_dir}/rocksdb" && cmake --preset release)
(cd "${root_dir}/rocksdb" && cmake --build --preset release)

workloads=(W1 W3 W6)
threads=(1 12)
profiles=(P2 P3)

profile_key() {
    case "$1" in
        P2) echo 32 ;;
        P3) echo 32 ;;
        *) printf "unknown profile: %s\n" "$1" >&2; exit 1 ;;
    esac
}

profile_val() {
    case "$1" in
        P2) echo 1024 ;;
        P3) echo 16384 ;;
        *) printf "unknown profile: %s\n" "$1" >&2; exit 1 ;;
    esac
}

profile_prefill_tier_s() {
    case "$1" in
        P2) echo 6100805 ;;
        P3) echo 392449 ;;
        *) printf "unknown profile: %s\n" "$1" >&2; exit 1 ;;
    esac
}

run_case() {
    local engine="$1"
    local workload="$2"
    local profile="$3"
    local t="$4"
    local repeat="$5"

    local key_size value_size prefill_keys run_path
    key_size="$(profile_key "${profile}")"
    value_size="$(profile_val "${profile}")"
    prefill_keys="$(profile_prefill_tier_s "${profile}")"
    run_path="$(mktemp -u -p "${db_root}" "${engine}_phase1_${workload}_${profile}_t${t}_r${repeat}_XXXXXX")"

    printf "[phase1][%s] repeat=%s workload=%s profile=%s threads=%s path=%s\n" \
      "${engine}" "${repeat}" "${workload}" "${profile}" "${t}" "${run_path}"

    if [ "${engine}" = "mace" ]; then
        "${root_dir}/target/release/kv_bench" \
          --path "${run_path}" \
          --workload "${workload}" \
          --threads "${t}" \
          --key-size "${key_size}" \
          --value-size "${value_size}" \
          --prefill-keys "${prefill_keys}" \
          --warmup-secs "${warmup_secs}" \
          --measure-secs "${measure_secs}" \
          --shared-keyspace true \
          --read-path "${read_path}" \
          --result-file "${result_file}"
    else
        "${root_dir}/rocksdb/build/release/rocksdb_bench" \
          --path "${run_path}" \
          --workload "${workload}" \
          --threads "${t}" \
          --key-size "${key_size}" \
          --value-size "${value_size}" \
          --prefill-keys "${prefill_keys}" \
          --warmup-secs "${warmup_secs}" \
          --measure-secs "${measure_secs}" \
          --read-path "${read_path}" \
          --result-file "${result_file}"
    fi
}

for repeat in $(seq 1 "${repeats}"); do
    for workload in "${workloads[@]}"; do
        for profile in "${profiles[@]}"; do
            for t in "${threads[@]}"; do
                run_case mace "${workload}" "${profile}" "${t}" "${repeat}"
                run_case rocksdb "${workload}" "${profile}" "${t}" "${repeat}"
            done
        done
    done
done

python3 "${script_dir}/phase1_eval.py" "${result_file}"
printf "Phase 1 finished. Results: %s\n" "${result_file}"
