use std::path::Path;

use rsstacker::replay::load_recording;
use rsstacker::sim::simulate;
use rsstacker::tui::timeline::ReplayTimeline;

#[test]
fn timeline_reproduces_simulation_and_locks() {
    let replay = load_recording(Path::new("tests/data/line_race/0266e1db49f5.recording")).unwrap();
    let timeline = ReplayTimeline::build(&replay);

    // The final snapshot equals the full simulation result.
    let final_board = simulate(&replay);
    let last = timeline.snapshot(timeline.len() - 1);
    assert_eq!(last.board.pieces_placed, final_board.pieces_placed);
    assert_eq!(last.board.lines_cleared, final_board.lines_cleared);
    assert_eq!(last.board.pieces_placed, 101);
    assert_eq!(last.board.lines_cleared, 40);

    // One lock index per placed piece.
    let locks = timeline.lock_indices().to_vec();
    assert_eq!(locks.len(), 101);

    // next_lock returns the first lock STRICTLY after the index.
    let some_lock = locks[5];
    assert_eq!(timeline.next_lock(some_lock - 1), some_lock);
    assert_eq!(timeline.next_lock(some_lock), locks[6]); // advances past the current lock
    let last_index = timeline.len() - 1;
    assert_eq!(timeline.next_lock(last_index), last_index); // clamps at end

    // prev_lock returns the last lock STRICTLY before the index.
    assert_eq!(timeline.prev_lock(some_lock + 1), some_lock);
    assert_eq!(timeline.prev_lock(0), 0); // clamps at start
}
