lazychess 0.1.2

A fast, memory-efficient chess engine library for Rust
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
use crate::board::Board;
use crate::classifications::{
    self,
    accuracy::PlayerAccuracy,
    expected_points::get_expected_points_loss,
    types::{ClassificationContext, ClassificationKind, Evaluation, MoveClassification},
};
use crate::fen::board_to_fen;
use crate::game::Game;
use crate::movegen::{apply_move, generate_legal_moves, is_in_check};
use crate::opening::{BUILTIN_OPENINGS_JSON, OpeningBook};
use crate::pgn::{move_to_san, pgn_moves_to_uci};
use crate::types::{ChessError, ChessResult, Color};
use crate::uci::{Score, SearchConfig, UciEngine};
use cli_table::{Cell, Style, Table, format::Justify};
use serde::Serialize;

fn classification_annotation(kind: &ClassificationKind) -> &'static str {
    match kind {
        ClassificationKind::Brilliant => "!!",
        ClassificationKind::Great => "!",
        ClassificationKind::Risky => "!?",
        ClassificationKind::Inaccuracy => "?!",
        ClassificationKind::Miss | ClassificationKind::Mistake => "?",
        ClassificationKind::Blunder => "??",
        _ => "",
    }
}

fn normalize_eval_to_white(eval: Evaluation, side_to_move: Color) -> Evaluation {
    if side_to_move == Color::Black {
        match eval {
            Evaluation::Centipawn(v) => Evaluation::Centipawn(-v),
            Evaluation::Mate(v) => Evaluation::Mate(-v),
        }
    } else {
        eval
    }
}

fn score_to_eval(score: &Score) -> Evaluation {
    match score {
        Score::Centipawns(cp) => Evaluation::Centipawn(*cp),
        Score::Mate(m) => Evaluation::Mate(*m),
    }
}

/// Which player's moves to analyse.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Side {
    White,
    Black,
}

/// Configuration for the move analyzer.
#[derive(Debug, Clone)]
pub struct AnalyzerConfig {
    pub search: SearchConfig,
    pub read_timeout_ms: Option<u64>,
    /// Number of alternative engine moves to surface per position (default: 0).
    ///
    /// When non-zero, each [`MoveClassification`] will have its `alternatives`
    /// field populated with up to `n` engine moves ranked 2nd and beyond.
    /// This requires the engine to run MultiPV mode, which adds a small
    /// overhead proportional to `n`.
    pub alternatives: usize,
}

impl AnalyzerConfig {
    pub fn depth(d: u32) -> Self {
        Self {
            search: SearchConfig::depth(d),
            read_timeout_ms: Some(60_000),
            alternatives: 0,
        }
    }
    pub fn movetime(ms: u64) -> Self {
        Self {
            search: SearchConfig::movetime(ms),
            read_timeout_ms: Some(60_000),
            alternatives: 0,
        }
    }
    pub fn with_timeout(mut self, ms: u64) -> Self {
        self.read_timeout_ms = Some(ms);
        self
    }
    /// Request up to `n` alternative moves per position.
    ///
    /// These appear as [`MoveClassification::alternatives`] in every result.
    /// Moves are in UCI notation, ranked by engine score (best first).
    pub fn with_alternatives(mut self, n: usize) -> Self {
        self.alternatives = n;
        self
    }
}

impl Default for AnalyzerConfig {
    fn default() -> Self {
        Self {
            search: SearchConfig::depth(16),
            read_timeout_ms: Some(60_000),
            alternatives: 0,
        }
    }
}

/// Per-player summary statistics for a full game analysis.
#[derive(Debug, Clone, Serialize)]
pub struct PlayerSummary {
    pub accuracy: f64,
    pub avg_point_loss: f64,
    pub brilliant: u32,
    pub great: u32,
    pub best: u32,
    pub excellent: u32,
    pub good: u32,
    pub okay: u32,
    pub inaccuracy: u32,
    pub miss: u32,
    pub mistake: u32,
    pub blunder: u32,
    pub forced: u32,
    pub book: u32,
    pub risky: u32,
}

impl PlayerSummary {
    fn from_classifications(moves: &[MoveClassification]) -> Self {
        let mut s = Self {
            accuracy: 0.0,
            avg_point_loss: 0.0,
            brilliant: 0,
            great: 0,
            best: 0,
            excellent: 0,
            good: 0,
            okay: 0,
            inaccuracy: 0,
            miss: 0,
            mistake: 0,
            blunder: 0,
            forced: 0,
            book: 0,
            risky: 0,
        };
        for mv in moves {
            match mv.kind {
                ClassificationKind::Brilliant => s.brilliant += 1,
                ClassificationKind::Great => s.great += 1,
                ClassificationKind::Best => s.best += 1,
                ClassificationKind::Excellent => s.excellent += 1,
                ClassificationKind::Good => s.good += 1,
                ClassificationKind::Okay => s.okay += 1,
                ClassificationKind::Inaccuracy => s.inaccuracy += 1,
                ClassificationKind::Miss => s.miss += 1,
                ClassificationKind::Mistake => s.mistake += 1,
                ClassificationKind::Blunder => s.blunder += 1,
                ClassificationKind::Forced => s.forced += 1,
                ClassificationKind::Book => s.book += 1,
                ClassificationKind::Risky => s.risky += 1,
            }
        }
        let losses: Vec<f64> = moves.iter().map(|m| m.point_loss).collect();
        let acc = PlayerAccuracy::from_point_losses(&losses);
        s.accuracy = acc.average;
        s.avg_point_loss = if losses.is_empty() {
            0.0
        } else {
            losses.iter().sum::<f64>() / losses.len() as f64
        };
        s
    }
}

/// Serialisable wrapper for [`Evaluation`] used in JSON output.
#[derive(Serialize)]
#[serde(tag = "type", content = "value", rename_all = "lowercase")]
enum EvalJson {
    Centipawn(i32),
    Mate(i32),
}

impl From<&Evaluation> for EvalJson {
    fn from(e: &Evaluation) -> Self {
        match e {
            Evaluation::Centipawn(v) => EvalJson::Centipawn(*v),
            Evaluation::Mate(v) => EvalJson::Mate(*v),
        }
    }
}

/// Serialisable view of a single [`MoveClassification`] for JSON output.
#[derive(Serialize)]
struct MoveJson<'a> {
    move_number: u32,
    color: &'static str,
    san: &'a str,
    uci: &'a str,
    best_move: &'a str,
    suggested_move: Option<&'a str>,
    alternatives: &'a [String],
    classification: String,
    point_loss: f64,
    accuracy: f64,
    eval_before: EvalJson,
    eval_after: EvalJson,
}

/// Full game analysis report returned by [`MoveAnalyzer::analyze_pgn`].
#[derive(Debug)]
pub struct GameReport {
    /// All classified moves in game order.
    pub moves: Vec<MoveClassification>,
    /// Summary for White.
    pub white: PlayerSummary,
    /// Summary for Black.
    pub black: PlayerSummary,
}

impl GameReport {
    /// Exports the game as an annotated PGN string.
    pub fn to_pgn(&self) -> String {
        let mut pgn = String::new();
        let mut last_num = 0u32;

        for mc in &self.moves {
            let ann = classification_annotation(&mc.kind);
            let annotated = format!("{}{}", mc.san, ann);

            // build inline comment and variation when the engine recommends
            // something other than what was played
            let suggestion = mc
                .suggested_move()
                .filter(|s| *s != mc.played_move)
                .map(|best| {
                    let mut suffix = format!(" {{ Best was {best} }}");
                    // append an alternative-move variation so GUIs can show the line
                    let dot_str = match mc.color {
                        Color::White => format!("{}.", mc.move_number),
                        Color::Black => format!("{}...", mc.move_number),
                    };
                    suffix.push_str(&format!(" ( {dot_str} {best}"));
                    if !mc.alternatives.is_empty() {
                        let alts = mc.alternatives.join(" ");
                        suffix.push_str(&format!(" {{ alternatives: {alts} }}"));
                    }
                    suffix.push_str(" )");
                    suffix
                })
                .unwrap_or_default();

            match mc.color {
                Color::White => {
                    if !pgn.is_empty() {
                        pgn.push(' ');
                    }
                    pgn.push_str(&format!("{}. {}{}", mc.move_number, annotated, suggestion));
                }
                Color::Black => {
                    if mc.move_number != last_num {
                        if !pgn.is_empty() {
                            pgn.push(' ');
                        }
                        pgn.push_str(&format!(
                            "{}... {}{}",
                            mc.move_number, annotated, suggestion
                        ));
                    } else {
                        pgn.push(' ');
                        pgn.push_str(&format!("{}{}", annotated, suggestion));
                    }
                }
            }
            last_num = mc.move_number;
        }
        pgn
    }

    /// Serialises the full report to a JSON string
    pub fn to_json(&self) -> String {
        #[derive(Serialize)]
        struct Report<'a> {
            moves: Vec<MoveJson<'a>>,
            white: &'a PlayerSummary,
            black: &'a PlayerSummary,
        }

        let moves = self
            .moves
            .iter()
            .map(|mc| MoveJson {
                move_number: mc.move_number,
                color: match mc.color {
                    Color::White => "white",
                    Color::Black => "black",
                },
                san: &mc.san,
                uci: &mc.played_move,
                best_move: &mc.best_move,
                suggested_move: mc.suggested_move(),
                alternatives: &mc.alternatives,
                classification: mc.kind.to_string(),
                point_loss: mc.point_loss,
                accuracy: mc.accuracy,
                eval_before: EvalJson::from(&mc.eval_before),
                eval_after: EvalJson::from(&mc.eval_after),
            })
            .collect();

        let report = Report {
            moves,
            white: &self.white,
            black: &self.black,
        };

        serde_json::to_string(&report).unwrap_or_else(|_| "{}".to_owned())
    }

    /// Formats the report as a pretty table for terminal output
    pub fn to_table(&self) -> String {
        let rows: Vec<Vec<cli_table::CellStruct>> = self
            .moves
            .iter()
            .map(|mc| {
                let num_str = match mc.color {
                    Color::White => format!("{}.", mc.move_number),
                    Color::Black => format!("{}...", mc.move_number),
                };
                let color_str = match mc.color {
                    Color::White => "White",
                    Color::Black => "Black",
                };
                let ann = classification_annotation(&mc.kind);
                let san_ann = format!("{}{}", mc.san, ann);

                // show the suggested move only when it differs from what was played
                let suggested_str = mc
                    .suggested_move()
                    .filter(|s| *s != mc.played_move)
                    .map(|s| s.to_owned())
                    .unwrap_or_default();

                vec![
                    num_str.cell(),
                    color_str.cell(),
                    san_ann.cell(),
                    mc.kind.to_string().cell(),
                    suggested_str.cell(),
                    format!("{:.4}", mc.point_loss)
                        .cell()
                        .justify(Justify::Right),
                    format!("{:.1}%", mc.accuracy)
                        .cell()
                        .justify(Justify::Right),
                ]
            })
            .collect();

        let table = rows
            .table()
            .title(vec![
                "#".cell().bold(true),
                "Color".cell().bold(true),
                "Move".cell().bold(true),
                "Classification".cell().bold(true),
                "Suggested".cell().bold(true),
                "Loss".cell().bold(true),
                "Accuracy".cell().bold(true),
            ])
            .bold(true);

        let mut output = table.display().map(|d| d.to_string()).unwrap_or_default();
        output.push('\n');
        output.push_str(&format!(
            "White: {:.1}% accuracy  ·  Black: {:.1}% accuracy\n",
            self.white.accuracy, self.black.accuracy,
        ));
        output
    }
}

type ProgressFn<'a> = Box<dyn Fn(usize, usize, &str) + 'a>;

/// Returned by [`PgnAnalysisBuilder::run_partial`] when analysis is interrupted.
///
/// Contains all results collected before the engine error, plus the error itself.
/// `report.moves` may be empty if the engine failed before any move was analysed.
#[derive(Debug)]
pub struct PartialReport {
    /// Every move that was successfully classified before the engine failed.
    pub report: GameReport,
    /// The error that caused analysis to stop.
    pub error: ChessError,
}

/// Builder returned by [`MoveAnalyzer::analyze_pgn`].
///
/// Configure via method chaining, then call `.run()` to execute or
/// `.run_partial()` to recover results even if the engine crashes mid-game.
///
/// ```rust,no_run
/// # use lazychess::{analyzer::{MoveAnalyzer, AnalyzerConfig, Side}, uci::UciEngine};
/// # let mut engine = UciEngine::with_options("/usr/bin/stockfish", &[]).unwrap();
/// # let pgn = "";
/// let mut analyzer = MoveAnalyzer::new(&mut engine, AnalyzerConfig::depth(18));
///
/// let report = analyzer
///     .analyze_pgn(pgn)
///     .on_progress(|cur, total, mv| println!("Analysing move {cur}/{total}: {mv}"))
///     .min_classification(lazychess::ClassificationKind::Mistake)
///     .side(Side::White)
///     .run()
///     .unwrap();
///
/// println!("{}", report.to_table());
/// println!("{}", report.to_pgn());
/// # Ok::<(), lazychess::ChessError>(())
/// ```
pub struct PgnAnalysisBuilder<'a, 'e> {
    analyzer: &'a mut MoveAnalyzer<'e>,
    pgn: String,
    progress: Option<ProgressFn<'a>>,
    min_classification: Option<ClassificationKind>,
    side: Option<Side>,
}

impl<'a, 'e> PgnAnalysisBuilder<'a, 'e> {
    /// Register a progress callback invoked before each move is analysed.
    ///
    /// Arguments: `(current_move_index, total_moves, uci_string)`.
    pub fn on_progress<F>(mut self, f: F) -> Self
    where
        F: Fn(usize, usize, &str) + 'a,
    {
        self.progress = Some(Box::new(f));
        self
    }

    /// Only include moves classified at `kind` or worse in the report.
    ///
    /// All moves are still evaluated by the engine; this filters what is
    /// stored in [`GameReport::moves`] and counted in summaries. Useful when
    /// you only care about mistakes and above.
    pub fn min_classification(mut self, kind: ClassificationKind) -> Self {
        self.min_classification = Some(kind);
        self
    }

    /// Only analyse moves by `side`.
    ///
    /// The engine is not called for the other side's moves, which roughly
    /// halves analysis time for single-sided reviews.
    pub fn side(mut self, s: Side) -> Self {
        self.side = Some(s);
        self
    }

    /// Consumes the builder, runs the analysis, and returns a [`GameReport`].
    ///
    /// Returns `Err` immediately if the engine fails at any point, discarding
    /// all results collected so far. Use [`run_partial`] if you need to recover
    /// moves that were successfully analysed before a crash.
    pub fn run(self) -> ChessResult<GameReport> {
        self.run_partial().map_err(|p| p.error)
    }

    /// Consumes the builder, runs the analysis, and returns a [`GameReport`].
    ///
    /// Unlike [`run`], if the engine crashes mid-game the partial results are
    /// preserved inside the [`PartialReport`] returned in `Err`. Check
    /// `partial.report.moves.is_empty()` to distinguish a failure before any
    /// move was analysed from a crash partway through.
    ///
    /// All builder options (`on_progress`, `side`, `min_classification`) apply
    /// exactly as they do with [`run`].
    ///
    /// ```rust,no_run
    /// # use lazychess::{analyzer::{MoveAnalyzer, AnalyzerConfig}, uci::UciEngine};
    /// # let mut engine = UciEngine::with_options("/usr/bin/stockfish", &[]).unwrap();
    /// # let mut analyzer = MoveAnalyzer::new(&mut engine, AnalyzerConfig::depth(15));
    /// # let pgn = "";
    /// match analyzer.analyze_pgn(pgn).run_partial() {
    ///     Ok(report) => println!("{}", report.to_table()),
    ///     Err(partial) => {
    ///         eprintln!("Engine crashed: {}", partial.error);
    ///         if !partial.report.moves.is_empty() {
    ///             println!("{}", partial.report.to_table());
    ///             println!("Partial PGN: {}", partial.report.to_pgn());
    ///         }
    ///     }
    /// }
    /// ```
    pub fn run_partial(self) -> Result<GameReport, Box<PartialReport>> {
        let PgnAnalysisBuilder {
            analyzer,
            pgn,
            progress,
            min_classification,
            side,
        } = self;

        macro_rules! partial_err {
            ($all:expr, $white:expr, $black:expr, $err:expr) => {
                return Err(Box::new(PartialReport {
                    report: GameReport {
                        white: PlayerSummary::from_classifications(&$white),
                        black: PlayerSummary::from_classifications(&$black),
                        moves: $all,
                    },
                    error: $err,
                }))
            };
        }

        let start_fen = board_to_fen(&crate::board::Board::starting_position());
        let (_, san_moves) = crate::pgn::parse_pgn(&pgn).map_err(|e| {
            Box::new(PartialReport {
                report: GameReport {
                    white: PlayerSummary::from_classifications(&[]),
                    black: PlayerSummary::from_classifications(&[]),
                    moves: vec![],
                },
                error: ChessError::new(format!("PGN parse error: {e}")),
            })
        })?;

        let uci_moves = pgn_moves_to_uci(&start_fen, &san_moves).map_err(|e| {
            Box::new(PartialReport {
                report: GameReport {
                    white: PlayerSummary::from_classifications(&[]),
                    black: PlayerSummary::from_classifications(&[]),
                    moves: vec![],
                },
                error: e,
            })
        })?;

        let total = uci_moves.len();

        analyzer.engine.new_game().map_err(|e| {
            Box::new(PartialReport {
                report: GameReport {
                    white: PlayerSummary::from_classifications(&[]),
                    black: PlayerSummary::from_classifications(&[]),
                    moves: vec![],
                },
                error: ChessError::from(e),
            })
        })?;

        let mut game = Game::new();
        let mut all_moves: Vec<MoveClassification> = Vec::new();
        let mut white_classified: Vec<MoveClassification> = Vec::new();
        let mut black_classified: Vec<MoveClassification> = Vec::new();

        for (i, uci) in uci_moves.iter().enumerate() {
            if let Some(ref cb) = progress {
                cb(i + 1, total, uci);
            }

            let is_white_move = i % 2 == 0;
            let skip = match side {
                Some(Side::White) => !is_white_move,
                Some(Side::Black) => is_white_move,
                None => false,
            };

            if !skip {
                let move_number = (i / 2) as u32 + 1;
                let mc = match analyzer.classify_board(game.current_board(), uci, move_number) {
                    Ok(mc) => mc,
                    Err(e) => partial_err!(all_moves, white_classified, black_classified, e),
                };

                let include = match &min_classification {
                    Some(threshold) => mc.kind >= *threshold,
                    None => true,
                };

                if include {
                    if is_white_move {
                        white_classified.push(mc.clone());
                    } else {
                        black_classified.push(mc.clone());
                    }
                    all_moves.push(mc);
                }
            }

            if let Err(e) = game.do_move(uci) {
                partial_err!(all_moves, white_classified, black_classified, e);
            }
        }

        Ok(GameReport {
            white: PlayerSummary::from_classifications(&white_classified),
            black: PlayerSummary::from_classifications(&black_classified),
            moves: all_moves,
        })
    }
}

/// The main analysis entry point.
///
/// Three modes of operation:
/// - [`analyze_pgn`] — post-game analysis of a full PGN string (builder API).
/// - [`classify_move`] / [`classify_fen_move`] — real-time, one move at a time.
/// - [`do_move`] — integrated mode; the analyzer owns the game state.
///
/// # Example — post-game PGN analysis
/// ```rust,no_run
/// use lazychess::{analyzer::{MoveAnalyzer, AnalyzerConfig}, uci::UciEngine};
///
/// let mut engine = UciEngine::with_options("/usr/bin/stockfish", &[]).unwrap();
/// let mut analyzer = MoveAnalyzer::new(&mut engine, AnalyzerConfig::depth(18));
///
/// let pgn = "1. e4 e5 2. Nf3 Nc6 3. Bb5";
/// let report = analyzer
///     .analyze_pgn(pgn)
///     .on_progress(|cur, tot, mv| println!("{cur}/{tot}: {mv}"))
///     .run()
///     .unwrap();
///
/// println!("{}", report.to_table());
/// println!("{}", report.to_pgn());
/// ```
///
/// # Example — integrated game + analyzer
/// ```rust,no_run
/// # use lazychess::{analyzer::{MoveAnalyzer, AnalyzerConfig}, uci::UciEngine, Game};
/// # let mut engine = UciEngine::with_options("/usr/bin/stockfish", &[]).unwrap();
/// let mut game     = Game::new();
/// let mut analyzer = MoveAnalyzer::from_game(&game, &mut engine, AnalyzerConfig::depth(15));
///
/// // Play a move — game state advances AND you get the classification.
/// let mc = analyzer.do_move("e2e4").unwrap();
/// println!("{}", mc.kind); // e.g. "Best"
///
/// // Or keep Game and Analyzer in sync separately:
/// game.do_move("e7e5").unwrap();
/// analyzer.sync(&game).unwrap();
/// ```
pub struct MoveAnalyzer<'e> {
    engine: &'e mut UciEngine,
    config: AnalyzerConfig,
    /// Internal game used by [`do_move`] / [`from_game`] / [`sync`].
    game: Game,
    opening_book: OpeningBook,
}

impl<'e> MoveAnalyzer<'e> {
    /// Creates a new analyzer starting from the standard position.
    pub fn new(engine: &'e mut UciEngine, config: AnalyzerConfig) -> Self {
        let opening_book =
            OpeningBook::from_json(BUILTIN_OPENINGS_JSON).unwrap_or_else(|_| OpeningBook::empty());
        Self {
            engine,
            config,
            game: Game::new(),
            opening_book,
        }
    }

    /// Creates an analyzer whose internal state mirrors `game`.
    ///
    /// Subsequent [`do_move`] calls advance the internal game automatically.
    pub fn from_game(game: &Game, engine: &'e mut UciEngine, config: AnalyzerConfig) -> Self {
        let opening_book =
            OpeningBook::from_json(BUILTIN_OPENINGS_JSON).unwrap_or_else(|_| OpeningBook::empty());
        let internal = Game::from_fen(&game.get_fen()).unwrap_or_else(|_| Game::new());
        Self {
            engine,
            config,
            game: internal,
            opening_book,
        }
    }

    /// Synchronises the analyzer's internal game with the provided `game`.
    ///
    /// Call this after advancing `game` externally so that [`do_move`]
    /// operates from the correct position.
    pub fn sync(&mut self, game: &Game) -> ChessResult<()> {
        self.game = Game::from_fen(&game.get_fen())?;
        Ok(())
    }

    /// Applies `mv_uci` to the internal game and returns the classification.
    ///
    /// This is the recommended way to use the analyzer when you want to keep
    /// a single source of truth rather than maintaining a separate `Game`.
    pub fn do_move(&mut self, mv_uci: &str) -> ChessResult<MoveClassification> {
        let board = self.game.current_board().clone();
        let mc = self.classify_board(&board, mv_uci, 1)?;
        self.game.do_move(mv_uci)?;
        Ok(mc)
    }

    /// Begins a PGN analysis. Chain options onto the returned builder, then
    /// call `.run()`.
    pub fn analyze_pgn<'a>(&'a mut self, pgn: &str) -> PgnAnalysisBuilder<'a, 'e> {
        PgnAnalysisBuilder {
            analyzer: self,
            pgn: pgn.to_owned(),
            progress: None,
            min_classification: None,
            side: None,
        }
    }

    /// Classifies `mv_uci` from the position in `game`.
    ///
    /// `game` must reflect the state *before* the move. The game is not mutated.
    pub fn classify_move(&mut self, game: &Game, mv_uci: &str) -> ChessResult<MoveClassification> {
        self.classify_board(game.current_board(), mv_uci, 1)
    }

    /// Classifies a move from a raw FEN position.
    pub fn classify_fen_move(
        &mut self,
        fen: &str,
        mv_uci: &str,
    ) -> ChessResult<MoveClassification> {
        let board = crate::fen::parse_fen(fen)?;
        self.classify_board(&board, mv_uci, 1)
    }

    fn classify_board(
        &mut self,
        board_before: &Board,
        mv_uci: &str,
        move_number: u32,
    ) -> ChessResult<MoveClassification> {
        let color = board_before.side_to_move;
        let legal = generate_legal_moves(board_before);

        let played_move = legal
            .iter()
            .find(|m| m.to_uci() == mv_uci)
            .ok_or_else(|| ChessError::new(format!("Illegal move: '{mv_uci}'")))?
            .clone();

        let san = move_to_san(board_before, &played_move);
        let is_forced = legal.len() == 1;
        let in_check_before = is_in_check(board_before, color);

        let mut search = self.config.search.clone();
        if let Some(t) = self.config.read_timeout_ms {
            search.read_timeout_ms = Some(t);
        }

        // request enough multipv lines to cover the top move + all requested alternatives;
        // we always ask for at least 2 so that second_best_eval is available for
        // brilliant/great classification even when alternatives == 0.
        let n_request = (self.config.alternatives + 1).max(2);
        let eval_config = SearchConfig {
            multipv: Some(n_request as u32),
            ..search.clone()
        };
        let top = self
            .engine
            .top_moves_from_board(board_before, n_request.try_into().unwrap(), &eval_config)
            .map_err(ChessError::from)?;

        let best_move_uci = top
            .first()
            .map(|(mv, _)| mv.clone())
            .unwrap_or_else(|| mv_uci.to_owned());

        // engine moves ranked 2nd and beyond, up to the configured limit
        let alternatives: Vec<String> = top
            .iter()
            .skip(1)
            .take(self.config.alternatives)
            .map(|(mv, _)| mv.clone())
            .collect();

        let eval_before = normalize_eval_to_white(
            top.first()
                .and_then(|(_, s)| s.as_ref())
                .map(score_to_eval)
                .unwrap_or(Evaluation::Centipawn(0)),
            color,
        );

        let second_best_eval = top
            .get(1)
            .and_then(|(_, s)| s.as_ref())
            .map(score_to_eval)
            .map(|e| normalize_eval_to_white(e, color));

        let best_move = legal
            .iter()
            .find(|m| m.to_uci() == best_move_uci)
            .cloned()
            .unwrap_or(played_move.clone());

        let board_after = apply_move(board_before, &played_move);

        let is_book = self
            .opening_book
            .lookup(&board_after.fen_piece_placement())
            .is_some();

        let eval_after = if generate_legal_moves(&board_after).is_empty() {
            if is_in_check(&board_after, board_after.side_to_move) {
                normalize_eval_to_white(Evaluation::Mate(0), color)
            } else {
                Evaluation::Centipawn(0)
            }
        } else {
            normalize_eval_to_white(
                self.engine
                    .evaluate_board(&board_after, &search)
                    .map_err(ChessError::from)?
                    .map(|score: Score| score_to_eval(&score))
                    .unwrap_or(Evaluation::Centipawn(0)),
                board_after.side_to_move,
            )
        };

        let point_loss = get_expected_points_loss(&eval_before, &eval_after, color);
        let accuracy = classifications::get_move_accuracy(point_loss);

        let ctx = ClassificationContext {
            played_move: &played_move,
            best_move: &best_move,
            eval_before: &eval_before,
            eval_after: &eval_after,
            second_best_eval: second_best_eval.as_ref(),
            point_loss,
            is_book,
            is_forced,
            in_check_before,
            color,
        };

        let kind = classifications::classify(board_before, &board_after, &ctx);

        Ok(MoveClassification {
            san,
            played_move: mv_uci.to_owned(),
            best_move: best_move_uci,
            alternatives,
            kind,
            color,
            move_number,
            point_loss,
            accuracy,
            eval_before,
            eval_after,
        })
    }
}