kanban-domain 0.3.1

Domain models and business logic for the kanban project management tool
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
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
use chrono::{DateTime, Utc};
use serde::{Deserialize, Deserializer, Serialize};
use std::collections::HashMap;
use uuid::Uuid;

use crate::field_update::FieldUpdate;
use crate::task_list_view::TaskListView;

pub type BoardId = Uuid;

#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum SortField {
    Points,
    Priority,
    CreatedAt,
    UpdatedAt,
    Status,
    Position,
    Default,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum SortOrder {
    Ascending,
    Descending,
}

#[derive(Debug, Clone, Serialize)]
pub struct Board {
    pub id: BoardId,
    pub name: String,
    pub description: Option<String>,
    #[serde(default, alias = "branch_prefix")]
    pub sprint_prefix: Option<String>,
    #[serde(default)]
    pub card_prefix: Option<String>,
    #[serde(default = "default_sort_field")]
    pub task_sort_field: SortField,
    #[serde(default = "default_sort_order")]
    pub task_sort_order: SortOrder,
    #[serde(default)]
    pub sprint_duration_days: Option<u32>,
    #[serde(default)]
    pub sprint_names: Vec<String>,
    #[serde(default)]
    pub sprint_name_used_count: usize,
    #[serde(default = "default_next_sprint_number")]
    pub next_sprint_number: u32,
    #[serde(default)]
    pub active_sprint_id: Option<Uuid>,
    #[serde(default)]
    pub task_list_view: TaskListView,
    #[serde(default)]
    pub prefix_counters: HashMap<String, u32>,
    #[serde(default)]
    pub sprint_counters: HashMap<String, u32>,
    #[serde(default)]
    pub completion_column_id: Option<Uuid>,
    pub created_at: DateTime<Utc>,
    pub updated_at: DateTime<Utc>,
}

impl<'de> Deserialize<'de> for Board {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: Deserializer<'de>,
    {
        #[derive(Deserialize)]
        struct BoardHelper {
            pub id: BoardId,
            pub name: String,
            pub description: Option<String>,
            #[serde(default)]
            pub sprint_prefix: Option<String>,
            #[serde(default)]
            pub branch_prefix: Option<String>,
            #[serde(default)]
            pub card_prefix: Option<String>,
            #[serde(default = "default_sort_field")]
            pub task_sort_field: SortField,
            #[serde(default = "default_sort_order")]
            pub task_sort_order: SortOrder,
            #[serde(default)]
            pub sprint_duration_days: Option<u32>,
            #[serde(default)]
            pub sprint_names: Vec<String>,
            #[serde(default)]
            pub sprint_name_used_count: usize,
            #[serde(default = "default_next_sprint_number")]
            pub next_sprint_number: u32,
            #[serde(default)]
            pub active_sprint_id: Option<Uuid>,
            #[serde(default)]
            pub task_list_view: TaskListView,
            #[serde(default)]
            pub prefix_counters: HashMap<String, u32>,
            #[serde(default)]
            pub sprint_counters: HashMap<String, u32>,
            #[serde(default)]
            pub completion_column_id: Option<Uuid>,
            pub created_at: DateTime<Utc>,
            pub updated_at: DateTime<Utc>,
            #[serde(default)]
            pub next_card_number: u32,
        }

        let helper = BoardHelper::deserialize(deserializer)?;
        let sprint_prefix = helper.sprint_prefix.or(helper.branch_prefix);
        let mut board = Board {
            id: helper.id,
            name: helper.name,
            description: helper.description,
            sprint_prefix,
            card_prefix: helper.card_prefix,
            task_sort_field: helper.task_sort_field,
            task_sort_order: helper.task_sort_order,
            sprint_duration_days: helper.sprint_duration_days,
            sprint_names: helper.sprint_names,
            sprint_name_used_count: helper.sprint_name_used_count,
            next_sprint_number: helper.next_sprint_number,
            active_sprint_id: helper.active_sprint_id,
            task_list_view: helper.task_list_view,
            prefix_counters: helper.prefix_counters,
            sprint_counters: helper.sprint_counters,
            completion_column_id: helper.completion_column_id,
            created_at: helper.created_at,
            updated_at: helper.updated_at,
        };

        // Migrate next_card_number to prefix_counters if needed
        if helper.next_card_number > 1 && board.prefix_counters.is_empty() {
            let effective_prefix = board.card_prefix.as_deref().unwrap_or("task").to_string();
            board
                .prefix_counters
                .insert(effective_prefix, helper.next_card_number);
        }

        Ok(board)
    }
}

fn default_next_sprint_number() -> u32 {
    1
}

fn default_sort_field() -> SortField {
    SortField::Default
}

fn default_sort_order() -> SortOrder {
    SortOrder::Ascending
}

impl Board {
    pub fn new(name: String, card_prefix: Option<String>) -> Self {
        let now = Utc::now();
        Self {
            id: Uuid::new_v4(),
            name,
            description: None,
            sprint_prefix: None,
            card_prefix,
            task_sort_field: SortField::Default,
            task_sort_order: SortOrder::Ascending,
            sprint_duration_days: None,
            sprint_names: Vec::new(),
            sprint_name_used_count: 0,
            next_sprint_number: 1,
            active_sprint_id: None,
            task_list_view: TaskListView::default(),
            prefix_counters: HashMap::new(),
            sprint_counters: HashMap::new(),
            completion_column_id: None,
            created_at: now,
            updated_at: now,
        }
    }

    pub fn update_name(&mut self, name: String) {
        self.name = name;
        self.updated_at = Utc::now();
    }

    pub fn update_description(&mut self, description: Option<String>) {
        self.description = description;
        self.updated_at = Utc::now();
    }

    pub fn update_sprint_prefix(&mut self, prefix: Option<String>) {
        self.sprint_prefix = prefix;
        self.updated_at = Utc::now();
    }

    pub fn update_card_prefix(&mut self, prefix: Option<String>) {
        self.card_prefix = prefix;
        self.updated_at = Utc::now();
    }

    pub fn effective_sprint_prefix<'a>(&'a self, default_prefix: &'a str) -> &'a str {
        self.sprint_prefix.as_deref().unwrap_or(default_prefix)
    }

    pub fn effective_card_prefix<'a>(&'a self, default_prefix: &'a str) -> &'a str {
        self.card_prefix.as_deref().unwrap_or(default_prefix)
    }

    pub fn effective_branch_prefix<'a>(&'a self, default_prefix: &'a str) -> &'a str {
        self.effective_sprint_prefix(default_prefix)
    }

    pub fn update_task_sort(&mut self, field: SortField, order: SortOrder) {
        self.task_sort_field = field;
        self.task_sort_order = order;
        self.updated_at = Utc::now();
    }

    pub fn allocate_sprint_number(&mut self) -> u32 {
        let number = self.next_sprint_number;
        self.next_sprint_number += 1;
        self.updated_at = Utc::now();
        number
    }

    pub fn consume_sprint_name(&mut self) -> Option<usize> {
        if self.sprint_name_used_count < self.sprint_names.len() {
            let index = self.sprint_name_used_count;
            self.sprint_name_used_count += 1;
            self.updated_at = Utc::now();
            Some(index)
        } else {
            None
        }
    }

    pub fn add_sprint_name_at_used_index(&mut self, name: String) -> usize {
        if self.sprint_name_used_count > self.sprint_names.len() {
            self.sprint_name_used_count = self.sprint_names.len();
        }
        let index = self.sprint_name_used_count;
        self.sprint_names.insert(index, name);
        self.sprint_name_used_count += 1;
        self.updated_at = Utc::now();
        index
    }

    pub fn update_task_list_view(&mut self, view: TaskListView) {
        self.task_list_view = view;
        self.updated_at = Utc::now();
    }

    pub fn get_next_card_number(&mut self, prefix: &str) -> u32 {
        let counter = self.prefix_counters.entry(prefix.to_string()).or_insert(1);
        let number = *counter;
        *counter += 1;
        self.updated_at = Utc::now();
        number
    }

    pub fn initialize_prefix_counter(&mut self, prefix: &str, start: u32) {
        self.prefix_counters.insert(prefix.to_string(), start);
        self.updated_at = Utc::now();
    }

    pub fn get_prefix_counters(&self) -> &HashMap<String, u32> {
        &self.prefix_counters
    }

    pub fn get_prefix_counter(&self, prefix: &str) -> Option<u32> {
        self.prefix_counters.get(prefix).copied()
    }

    pub fn get_next_sprint_number(&mut self, prefix: &str) -> u32 {
        let counter = self.sprint_counters.entry(prefix.to_string()).or_insert(1);
        let number = *counter;
        *counter += 1;
        self.updated_at = Utc::now();
        number
    }

    pub fn initialize_sprint_counter(&mut self, prefix: &str, start: u32) {
        self.sprint_counters.insert(prefix.to_string(), start);
        self.updated_at = Utc::now();
    }

    pub fn get_sprint_counters(&self) -> &HashMap<String, u32> {
        &self.sprint_counters
    }

    pub fn get_sprint_counter(&self, prefix: &str) -> Option<u32> {
        self.sprint_counters.get(prefix).copied()
    }

    pub fn resolve_completion_column(&self, columns: &[crate::Column]) -> Option<Uuid> {
        if let Some(id) = self.completion_column_id {
            if columns.iter().any(|c| c.id == id && c.board_id == self.id) {
                return Some(id);
            }
        }
        // Fallback: last column by position for this board
        columns
            .iter()
            .filter(|c| c.board_id == self.id)
            .max_by_key(|c| c.position)
            .map(|c| c.id)
    }

    pub fn update_completion_column_id(&mut self, column_id: Option<Uuid>) {
        self.completion_column_id = column_id;
        self.updated_at = Utc::now();
    }

    pub fn ensure_sprint_counter_initialized(
        &mut self,
        prefix: &str,
        all_sprints: &[crate::Sprint],
    ) {
        // If counter already exists for this prefix, don't reinitialize
        if self.sprint_counters.contains_key(prefix) {
            return;
        }

        // Find the highest sprint number with this prefix FOR THIS BOARD
        let max_number = all_sprints
            .iter()
            .filter(|sprint| {
                // Only consider sprints for this board
                if sprint.board_id != self.id {
                    return false;
                }

                let sprint_prefix = sprint
                    .prefix
                    .as_deref()
                    .unwrap_or_else(|| self.sprint_prefix.as_deref().unwrap_or("sprint"));
                sprint_prefix == prefix
            })
            .map(|sprint| sprint.sprint_number)
            .max()
            .unwrap_or(0);

        // Initialize counter to one more than the max, or 1 if no sprints exist
        let next_number = max_number + 1;
        self.initialize_sprint_counter(prefix, next_number);
    }

    pub fn ensure_card_counter_initialized(&mut self, prefix: &str, board_cards: &[&crate::Card]) {
        // If counter already exists for this prefix, don't reinitialize
        if self.prefix_counters.contains_key(prefix) {
            return;
        }

        // Find the highest card number with this prefix in the provided cards
        let max_number = board_cards
            .iter()
            .filter(|card| {
                let card_prefix = card
                    .assigned_prefix
                    .as_deref()
                    .unwrap_or_else(|| self.card_prefix.as_deref().unwrap_or("task"));
                card_prefix == prefix
            })
            .map(|card| card.card_number)
            .max()
            .unwrap_or(0);

        // Initialize counter to one more than the max, or 1 if no cards exist
        let next_number = max_number + 1;
        self.initialize_prefix_counter(prefix, next_number);
    }

    /// Update board with partial changes
    pub fn update(&mut self, updates: BoardUpdate) {
        if let Some(name) = updates.name {
            self.name = name;
        }
        updates.description.apply_to(&mut self.description);
        updates.sprint_prefix.apply_to(&mut self.sprint_prefix);
        updates.card_prefix.apply_to(&mut self.card_prefix);
        if let Some(task_sort_field) = updates.task_sort_field {
            self.task_sort_field = task_sort_field;
        }
        if let Some(task_sort_order) = updates.task_sort_order {
            self.task_sort_order = task_sort_order;
        }
        updates
            .sprint_duration_days
            .apply_to(&mut self.sprint_duration_days);
        if let Some(task_list_view) = updates.task_list_view {
            self.task_list_view = task_list_view;
        }
        updates
            .active_sprint_id
            .apply_to(&mut self.active_sprint_id);
        updates
            .completion_column_id
            .apply_to(&mut self.completion_column_id);
        self.updated_at = Utc::now();
    }
}

/// Partial update struct for Board
///
/// Uses `FieldUpdate<T>` for optional fields to provide clear three-state updates.
/// See [`FieldUpdate`] documentation for usage examples.
#[derive(Debug, Clone, Default)]
pub struct BoardUpdate {
    pub name: Option<String>,
    pub description: FieldUpdate<String>,
    pub sprint_prefix: FieldUpdate<String>,
    pub card_prefix: FieldUpdate<String>,
    pub task_sort_field: Option<SortField>,
    pub task_sort_order: Option<SortOrder>,
    pub sprint_duration_days: FieldUpdate<u32>,
    pub task_list_view: Option<TaskListView>,
    pub active_sprint_id: FieldUpdate<Uuid>,
    pub completion_column_id: FieldUpdate<Uuid>,
}

/// Get the active sprint's card prefix override if one exists.
/// Returns the sprint's card_prefix if the board has an active sprint
/// that has a card_prefix override set.
pub fn get_active_sprint_card_prefix_override<'a>(
    board: &'a Board,
    sprints: &'a [crate::Sprint],
) -> Option<&'a str> {
    board.active_sprint_id.and_then(|sprint_id| {
        sprints
            .iter()
            .find(|s| s.id == sprint_id)
            .and_then(|sprint| sprint.card_prefix.as_deref())
    })
}

/// Get the active sprint's sprint prefix override if one exists.
/// Returns the sprint's prefix if the board has an active sprint
/// that has a sprint prefix override set.
pub fn get_active_sprint_prefix_override<'a>(
    board: &'a Board,
    sprints: &'a [crate::Sprint],
) -> Option<&'a str> {
    board.active_sprint_id.and_then(|sprint_id| {
        sprints
            .iter()
            .find(|s| s.id == sprint_id)
            .and_then(|sprint| sprint.prefix.as_deref())
    })
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_update_sprint_prefix() {
        let mut board = Board::new("Test".to_string(), None);
        assert_eq!(board.sprint_prefix, None);

        board.update_sprint_prefix(Some("feat".to_string()));
        assert_eq!(board.sprint_prefix, Some("feat".to_string()));

        board.update_sprint_prefix(None);
        assert_eq!(board.sprint_prefix, None);
    }

    #[test]
    fn test_update_card_prefix() {
        let mut board = Board::new("Test".to_string(), None);
        assert_eq!(board.card_prefix, None);

        board.update_card_prefix(Some("task".to_string()));
        assert_eq!(board.card_prefix, Some("task".to_string()));

        board.update_card_prefix(None);
        assert_eq!(board.card_prefix, None);
    }

    #[test]
    fn test_effective_sprint_prefix() {
        let mut board = Board::new("Test".to_string(), None);
        assert_eq!(board.effective_sprint_prefix("default"), "default");

        board.update_sprint_prefix(Some("custom".to_string()));
        assert_eq!(board.effective_sprint_prefix("default"), "custom");
    }

    #[test]
    fn test_effective_card_prefix() {
        let mut board = Board::new("Test".to_string(), None);
        assert_eq!(board.effective_card_prefix("task"), "task");

        board.update_card_prefix(Some("feature".to_string()));
        assert_eq!(board.effective_card_prefix("task"), "feature");
    }

    #[test]
    fn test_effective_branch_prefix_backward_compat() {
        let mut board = Board::new("Test".to_string(), None);
        board.update_sprint_prefix(Some("custom".to_string()));
        assert_eq!(board.effective_branch_prefix("default"), "custom");
    }

    #[test]
    fn test_resolve_completion_column_fallback() {
        let board = Board::new("Test".to_string(), None);
        let col1 = crate::Column::new(board.id, "Todo".to_string(), 0);
        let col2 = crate::Column::new(board.id, "In Progress".to_string(), 1);
        let col3 = crate::Column::new(board.id, "Done".to_string(), 2);
        let columns = vec![col1, col2, col3.clone()];

        // No explicit completion_column_id, should fall back to last by position
        assert_eq!(board.resolve_completion_column(&columns), Some(col3.id));
    }

    #[test]
    fn test_resolve_completion_column_explicit() {
        let mut board = Board::new("Test".to_string(), None);
        let col1 = crate::Column::new(board.id, "Todo".to_string(), 0);
        let col2 = crate::Column::new(board.id, "Done".to_string(), 1);
        let col3 = crate::Column::new(board.id, "Archive".to_string(), 2);
        let columns = vec![col1, col2.clone(), col3];

        // Set explicit completion column to col2 (not the last one)
        board.update_completion_column_id(Some(col2.id));
        assert_eq!(board.resolve_completion_column(&columns), Some(col2.id));
    }

    #[test]
    fn test_resolve_completion_column_stale_id_falls_back() {
        let mut board = Board::new("Test".to_string(), None);
        let col1 = crate::Column::new(board.id, "Todo".to_string(), 0);
        let col2 = crate::Column::new(board.id, "Done".to_string(), 1);
        let columns = vec![col1, col2.clone()];

        // Set completion_column_id to a non-existent UUID
        board.update_completion_column_id(Some(Uuid::new_v4()));
        // Should fall back to last column
        assert_eq!(board.resolve_completion_column(&columns), Some(col2.id));
    }

    #[test]
    fn test_resolve_completion_column_empty_columns() {
        let board = Board::new("Test".to_string(), None);
        assert_eq!(board.resolve_completion_column(&[]), None);
    }

    #[test]
    fn test_prefix_counter_initialization() {
        let mut board = Board::new("Test".to_string(), None);
        assert_eq!(board.get_prefix_counter("test"), None);

        let num = board.get_next_card_number("test");
        assert_eq!(num, 1);
        assert_eq!(board.get_prefix_counter("test"), Some(2));
    }

    #[test]
    fn test_shared_prefix_sequence() {
        let mut board = Board::new("Test".to_string(), None);

        let num1 = board.get_next_card_number("TEST");
        assert_eq!(num1, 1);

        let num2 = board.get_next_card_number("TEST");
        assert_eq!(num2, 2);

        let num3 = board.get_next_card_number("TEST");
        assert_eq!(num3, 3);

        assert_eq!(board.get_prefix_counter("TEST"), Some(4));
    }

    #[test]
    fn test_multiple_prefix_counters() {
        let mut board = Board::new("Test".to_string(), None);

        let test1 = board.get_next_card_number("TEST");
        let bra1 = board.get_next_card_number("BRA");
        let test2 = board.get_next_card_number("TEST");
        let bra2 = board.get_next_card_number("BRA");

        assert_eq!(test1, 1);
        assert_eq!(bra1, 1);
        assert_eq!(test2, 2);
        assert_eq!(bra2, 2);
    }

    #[test]
    fn test_initialize_prefix_counter() {
        let mut board = Board::new("Test".to_string(), None);
        board.initialize_prefix_counter("TEST", 10);

        let num = board.get_next_card_number("TEST");
        assert_eq!(num, 10);
        assert_eq!(board.get_prefix_counter("TEST"), Some(11));
    }

    #[test]
    fn test_sprint_counter_initialization() {
        let mut board = Board::new("Test".to_string(), None);
        assert_eq!(board.get_sprint_counter("sprint"), None);

        let num = board.get_next_sprint_number("sprint");
        assert_eq!(num, 1);
        assert_eq!(board.get_sprint_counter("sprint"), Some(2));
    }

    #[test]
    fn test_shared_sprint_sequence() {
        let mut board = Board::new("Test".to_string(), None);

        let num1 = board.get_next_sprint_number("SPRINT");
        assert_eq!(num1, 1);

        let num2 = board.get_next_sprint_number("SPRINT");
        assert_eq!(num2, 2);

        let num3 = board.get_next_sprint_number("SPRINT");
        assert_eq!(num3, 3);

        assert_eq!(board.get_sprint_counter("SPRINT"), Some(4));
    }

    #[test]
    fn test_multiple_sprint_counters() {
        let mut board = Board::new("Test".to_string(), None);

        let sprint1 = board.get_next_sprint_number("SPRINT");
        let branch1 = board.get_next_sprint_number("BRANCH");
        let sprint2 = board.get_next_sprint_number("SPRINT");
        let branch2 = board.get_next_sprint_number("BRANCH");

        assert_eq!(sprint1, 1);
        assert_eq!(branch1, 1);
        assert_eq!(sprint2, 2);
        assert_eq!(branch2, 2);
    }

    #[test]
    fn test_initialize_sprint_counter() {
        let mut board = Board::new("Test".to_string(), None);
        board.initialize_sprint_counter("RELEASE", 5);

        let num = board.get_next_sprint_number("RELEASE");
        assert_eq!(num, 5);
        assert_eq!(board.get_sprint_counter("RELEASE"), Some(6));
    }

    #[test]
    fn test_card_and_sprint_counters_independent() {
        let mut board = Board::new("Test".to_string(), None);

        let card1 = board.get_next_card_number("TEST");
        let sprint1 = board.get_next_sprint_number("TEST");
        let card2 = board.get_next_card_number("TEST");
        let sprint2 = board.get_next_sprint_number("TEST");

        assert_eq!(card1, 1);
        assert_eq!(sprint1, 1);
        assert_eq!(card2, 2);
        assert_eq!(sprint2, 2);

        assert_eq!(board.get_prefix_counter("TEST"), Some(3));
        assert_eq!(board.get_sprint_counter("TEST"), Some(3));
    }

    #[test]
    fn test_sprint_number_independence_from_cards() {
        let mut board = Board::new("Test".to_string(), None);

        // Create cards and sprints interleaved with same prefix
        let card1 = board.get_next_card_number("sprint");
        assert_eq!(card1, 1);

        let sprint1 = board.get_next_sprint_number("sprint");
        assert_eq!(sprint1, 1);

        let card2 = board.get_next_card_number("sprint");
        assert_eq!(card2, 2);

        let sprint2 = board.get_next_sprint_number("sprint");
        assert_eq!(sprint2, 2);

        let card3 = board.get_next_card_number("sprint");
        assert_eq!(card3, 3);

        // Verify separate counters: sprint-1, sprint-2 vs card-1, card-2, card-3
        assert_eq!(board.get_prefix_counter("sprint"), Some(4)); // cards use prefix_counters
        assert_eq!(board.get_sprint_counter("sprint"), Some(3)); // sprints use sprint_counters
    }

    #[test]
    fn test_sprint_counter_reset_per_prefix() {
        let mut board = Board::new("Test".to_string(), None);

        // Get sprint numbers for prefix "A"
        let a1 = board.get_next_sprint_number("A");
        let a2 = board.get_next_sprint_number("A");
        let a3 = board.get_next_sprint_number("A");

        assert_eq!(a1, 1);
        assert_eq!(a2, 2);
        assert_eq!(a3, 3);

        // Get sprint numbers for prefix "B"
        let b1 = board.get_next_sprint_number("B");
        let b2 = board.get_next_sprint_number("B");

        assert_eq!(b1, 1);
        assert_eq!(b2, 2);

        // Get sprint numbers for prefix "A" again - should continue from 4
        let a4 = board.get_next_sprint_number("A");
        let a5 = board.get_next_sprint_number("A");

        assert_eq!(a4, 4);
        assert_eq!(a5, 5);

        // Verify independence: A and B maintain separate sequences
        assert_eq!(board.get_sprint_counter("A"), Some(6));
        assert_eq!(board.get_sprint_counter("B"), Some(3));
    }

    #[test]
    fn test_ensure_sprint_counter_initialized_with_existing_sprints() {
        use crate::Sprint;

        let mut board = Board::new("Test".to_string(), None);

        // Create sprints manually with different prefixes (simulating existing data)
        let sprint1 = Sprint::new(board.id, 1, None, None); // Uses default "sprint" prefix
        let sprint2 = Sprint::new(board.id, 2, None, None);
        let sprint3 = Sprint::new(board.id, 3, None, None);

        let sprints = vec![sprint1, sprint2, sprint3];

        // Before initialization, no counter exists
        assert_eq!(board.get_sprint_counter("sprint"), None);

        // Initialize counter based on existing sprints
        board.ensure_sprint_counter_initialized("sprint", &sprints);

        // Counter should be initialized to 4 (max was 3)
        assert_eq!(board.get_sprint_counter("sprint"), Some(4));

        // Next allocation should give us 4
        let next = board.get_next_sprint_number("sprint");
        assert_eq!(next, 4);
    }

    #[test]
    fn test_ensure_sprint_counter_with_prefix_override() {
        use crate::Sprint;

        let mut board = Board::new("Test".to_string(), None);
        board.update_sprint_prefix(Some("WOO".to_string()));

        // Create sprints with no explicit prefix (they'll use board's "WOO" prefix as effective)
        let sprint1 = Sprint::new(board.id, 1, None, None); // effective prefix: WOO
        let sprint2 = Sprint::new(board.id, 2, None, None); // effective prefix: WOO
        let sprint3 = Sprint::new(board.id, 3, None, None); // effective prefix: WOO

        let sprints = vec![sprint1, sprint2, sprint3];

        // Initialize counter for "WOO" based on existing sprints
        board.ensure_sprint_counter_initialized("WOO", &sprints);

        // Counter should be initialized to 4 (max was 3)
        assert_eq!(board.get_sprint_counter("WOO"), Some(4));

        // Verify next allocation gives 4
        let next = board.get_next_sprint_number("WOO");
        assert_eq!(next, 4);
    }

    #[test]
    fn test_ensure_sprint_counter_not_reinitialize() {
        use crate::Sprint;

        let mut board = Board::new("Test".to_string(), None);

        // Pre-initialize counter to 10
        board.initialize_sprint_counter("test", 10);
        assert_eq!(board.get_sprint_counter("test"), Some(10));

        // Create some sprints
        let sprint1 = Sprint::new(board.id, 1, None, Some("test".to_string()));
        let sprint2 = Sprint::new(board.id, 2, None, Some("test".to_string()));
        let sprints = vec![sprint1, sprint2];

        // Try to ensure initialization again
        board.ensure_sprint_counter_initialized("test", &sprints);

        // Counter should still be 10 (not reinitialized)
        assert_eq!(board.get_sprint_counter("test"), Some(10));
    }

    #[test]
    fn test_deserialization_migrates_next_card_number_to_prefix_counters() {
        let json = r#"{
            "id": "550e8400-e29b-41d4-a716-446655440000",
            "name": "Test Board",
            "description": null,
            "created_at": "2024-01-01T00:00:00Z",
            "updated_at": "2024-01-01T00:00:00Z",
            "sprint_prefix": null,
            "card_prefix": null,
            "task_sort_field": "Default",
            "task_sort_order": "Ascending",
            "active_sprint_id": null,
            "sprint_duration_days": null,
            "sprint_names": [],
            "next_sprint_number": 1,
            "sprint_name_used_count": 0,
            "prefix_counters": {},
            "sprint_counters": {},
            "task_list_view": "Flat",
            "next_card_number": 42
        }"#;

        let board: Board = serde_json::from_str(json).expect("Should deserialize");
        // Verify next_card_number was migrated to prefix_counters for "task" prefix
        assert_eq!(board.get_prefix_counter("task"), Some(42));
    }

    #[test]
    fn test_deserialization_respects_existing_prefix_counters() {
        let json = r#"{
            "id": "550e8400-e29b-41d4-a716-446655440000",
            "name": "Test Board",
            "description": null,
            "created_at": "2024-01-01T00:00:00Z",
            "updated_at": "2024-01-01T00:00:00Z",
            "sprint_prefix": null,
            "card_prefix": null,
            "task_sort_field": "Default",
            "task_sort_order": "Ascending",
            "active_sprint_id": null,
            "sprint_duration_days": null,
            "sprint_names": [],
            "next_sprint_number": 1,
            "sprint_name_used_count": 0,
            "prefix_counters": {"task": 100},
            "sprint_counters": {},
            "task_list_view": "Flat",
            "next_card_number": 42
        }"#;

        let board: Board = serde_json::from_str(json).expect("Should deserialize");
        // Verify existing prefix_counters are preserved and next_card_number is NOT migrated
        assert_eq!(board.get_prefix_counter("task"), Some(100));
    }

    #[test]
    fn test_deserialization_uses_card_prefix_when_migrating() {
        let json = r#"{
            "id": "550e8400-e29b-41d4-a716-446655440000",
            "name": "Test Board",
            "description": null,
            "created_at": "2024-01-01T00:00:00Z",
            "updated_at": "2024-01-01T00:00:00Z",
            "sprint_prefix": null,
            "card_prefix": "feature",
            "task_sort_field": "Default",
            "task_sort_order": "Ascending",
            "active_sprint_id": null,
            "sprint_duration_days": null,
            "sprint_names": [],
            "next_sprint_number": 1,
            "sprint_name_used_count": 0,
            "prefix_counters": {},
            "sprint_counters": {},
            "task_list_view": "Flat",
            "next_card_number": 50
        }"#;

        let board: Board = serde_json::from_str(json).expect("Should deserialize");
        // Verify next_card_number was migrated to the board's card_prefix "feature"
        assert_eq!(board.get_prefix_counter("feature"), Some(50));
        assert_eq!(board.get_prefix_counter("task"), None);
    }
}

#[cfg(test)]
mod sort_field_serialization_tests {
    use super::*;

    #[test]
    fn test_position_serializes_correctly() {
        let field = SortField::Position;
        let json = serde_json::to_string(&field).unwrap();
        assert_eq!(json, "\"Position\"");
    }

    #[test]
    fn test_position_deserializes_correctly() {
        let json = "\"Position\"";
        let field: SortField = serde_json::from_str(json).unwrap();
        assert_eq!(field, SortField::Position);
    }

    #[test]
    fn test_board_with_position_sort_serializes() {
        let mut board = Board::new("Test".to_string(), None);
        board.update_task_sort(SortField::Position, SortOrder::Descending);

        let json = serde_json::to_string(&board).unwrap();
        assert!(
            json.contains("\"task_sort_field\":\"Position\""),
            "Expected Position in JSON, got: {}",
            json
        );
        assert!(
            json.contains("\"task_sort_order\":\"Descending\""),
            "Expected Descending in JSON, got: {}",
            json
        );
    }
}