import os

# ── user config ────────────────────────────────────────────────────────────────
DATA_ROOT          = "data/iphone"
output_folder      = "outputs/output_iphone/opensource"
config_file        = "config/iphone.yaml"
traj_name          = "traj_kf_beforeBA.txt"  # or "traj_kf_afterBA.txt" for keyframe trajectory after final BA, "traj_full_beforeBA.txt" for full trajectory before final BA, "traj_full_afterBA.txt" for full trajectory after final BA
stride             = 1
IMU_poseinit_after = 20
recall_thresh_cm   = 10
gsmapping          = True
BUFFER_SIZE        = 500 #TODO: Increase if not enough keyframes, depends on the actual sequence
SEQS = [
    # add your iPhone sequence names here, e.g.:
    # "20260309_170728",
]
# ───────────────────────────────────────────────────────────────────────────────

if __name__ == "__main__":
    os.makedirs(output_folder, exist_ok=True)

    ate_values, scale_errors, recalls = [], [], []

    for name in SEQS:
        seq = os.path.join(DATA_ROOT, name)
        out = os.path.join(output_folder, name)
        os.makedirs(out, exist_ok=True)
        print(f"{'='*60}\n{name}\n{'='*60}")

        cmd = (f"python demo.py"
               f" --calib {seq}/calib.txt"
               f" --imagedir {seq}/images"
               f" --config {config_file}"
               f" --stride {stride}"
               f" --IMU_poseinit_after {IMU_poseinit_after}"
               f" --imufile {seq}/imu.csv"
               f" --output {out}"
               f" --buffer {BUFFER_SIZE}"
               f" --undistort"
               + (" --gsmapping" if gsmapping else "")
               + f" > {out}/log.txt")
        print("RUNNING COMMAND: ", cmd)
        if not os.path.exists(f"{out}/{traj_name}"):
            os.system(cmd)
            # sbatch alternative:
            # os.system(f"sbatch --time=4:00:00 -n 1 --cpus-per-task=8 --mem-per-cpu=7G --gpus=1 --gres=gpumem:20g --output=slurms/{name}.out --wrap '{cmd}'")
