kanban-domain 0.4.0

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
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, PartialEq, 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 card_counter: u32,
    #[serde(default)]
    pub sprint_counters: HashMap<String, u32>,
    #[serde(default)]
    pub completion_column_id: Option<Uuid>,
    #[serde(default)]
    pub position: i32,
    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,
            /// New field: single card counter
            #[serde(default)]
            pub card_counter: u32,
            /// Legacy field for migration: prefix-keyed counters
            #[serde(default)]
            pub prefix_counters: HashMap<String, u32>,
            #[serde(default)]
            pub sprint_counters: HashMap<String, u32>,
            #[serde(default)]
            pub completion_column_id: Option<Uuid>,
            #[serde(default)]
            pub position: i32,
            pub created_at: DateTime<Utc>,
            pub updated_at: DateTime<Utc>,
            /// Very old field for migration
            #[serde(default)]
            pub next_card_number: u32,
        }

        let helper = BoardHelper::deserialize(deserializer)?;
        let sprint_prefix = helper.sprint_prefix.or(helper.branch_prefix);

        // Resolve card_counter from migration chain (all legacy paths):
        // 1. If card_counter already set (V3 format) → use it
        // 2. Else if prefix_counters non-empty (V2 format) → use matching prefix counter or max
        // 3. Else if next_card_number > 1 (V1 format) → use that
        // 4. Else default to 1 (no cards)
        let card_counter = if helper.card_counter > 0 {
            helper.card_counter
        } else if !helper.prefix_counters.is_empty() {
            let matching_key = helper.card_prefix.as_deref().unwrap_or("task");
            helper
                .prefix_counters
                .get(matching_key)
                .copied()
                .unwrap_or_else(|| helper.prefix_counters.values().copied().max().unwrap_or(1))
        } else if helper.next_card_number > 1 {
            helper.next_card_number
        } else {
            1
        };

        let 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,
            card_counter,
            sprint_counters: helper.sprint_counters,
            completion_column_id: helper.completion_column_id,
            position: helper.position,
            created_at: helper.created_at,
            updated_at: helper.updated_at,
        };

        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(),
            card_counter: 1,
            sprint_counters: HashMap::new(),
            completion_column_id: None,
            position: 0,
            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();
    }

    /// Get the next card number and increment the counter.
    pub fn get_next_card_number(&mut self) -> u32 {
        let number = self.card_counter;
        self.card_counter += 1;
        self.updated_at = Utc::now();
        number
    }

    /// Set the card counter to a specific start value (used for import/migration).
    pub fn initialize_card_counter(&mut self, start: u32) {
        self.card_counter = start;
        self.updated_at = Utc::now();
    }

    /// Get the current card counter value (next number to be assigned).
    pub fn get_card_counter(&self) -> u32 {
        self.card_counter
    }

    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);
    }

    /// 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);
        if let Some(position) = updates.position {
            self.position = position;
        }
        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, serde::Serialize, serde::Deserialize)]
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>,
    pub position: Option<i32>,
}

/// 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_board_new_card_counter_initialized_to_one() {
        let board = Board::new("Test".to_string(), None);
        assert_eq!(board.card_counter, 1);
    }

    #[test]
    fn test_board_get_next_card_number_increments_without_prefix() {
        let mut board = Board::new("Test".to_string(), None);
        assert_eq!(board.get_next_card_number(), 1);
        assert_eq!(board.get_next_card_number(), 2);
        assert_eq!(board.get_next_card_number(), 3);
        assert_eq!(board.card_counter, 4);
    }

    #[test]
    fn test_board_initialize_card_counter_sets_value_and_get_next_returns_it() {
        let mut board = Board::new("Test".to_string(), None);
        board.initialize_card_counter(10);
        assert_eq!(board.get_card_counter(), 10);
        assert_eq!(board.get_next_card_number(), 10);
        assert_eq!(board.get_card_counter(), 11);
    }

    #[test]
    fn test_deserialization_migrates_prefix_counters_to_card_counter() {
        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": "feat",
            "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": {"feat": 42, "other": 5},
            "sprint_counters": {},
            "task_list_view": "Flat"
        }"#;

        let board: Board = serde_json::from_str(json).expect("Should deserialize");
        assert_eq!(
            board.card_counter, 42,
            "Should pick the matching prefix counter"
        );
    }

    #[test]
    fn test_deserialization_migrates_next_card_number_to_card_counter() {
        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");
        assert_eq!(board.card_counter, 42);
    }

    #[test]
    fn test_deserialization_card_counter_takes_priority_over_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,
            "card_counter": 100,
            "prefix_counters": {"task": 50},
            "sprint_counters": {},
            "task_list_view": "Flat"
        }"#;

        let board: Board = serde_json::from_str(json).expect("Should deserialize");
        assert_eq!(board.card_counter, 100, "card_counter takes priority");
    }

    #[test]
    fn test_deserialization_prefix_counters_uses_max_when_no_prefix_match() {
        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": {"FEAT": 20, "BUG": 30},
            "sprint_counters": {},
            "task_list_view": "Flat"
        }"#;

        // card_prefix is null so uses "task" as key, not found → uses max (30)
        let board: Board = serde_json::from_str(json).expect("Should deserialize");
        assert_eq!(board.card_counter, 30, "Falls back to max of all counters");
    }

    #[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()];

        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];

        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()];

        board.update_completion_column_id(Some(Uuid::new_v4()));
        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_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_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_ensure_sprint_counter_initialized_with_existing_sprints() {
        use crate::Sprint;

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

        let sprint1 = Sprint::new(board.id, 1, None, None);
        let sprint2 = Sprint::new(board.id, 2, None, None);
        let sprint3 = Sprint::new(board.id, 3, None, None);

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

        assert_eq!(board.get_sprint_counter("sprint"), None);
        board.ensure_sprint_counter_initialized("sprint", &sprints);
        assert_eq!(board.get_sprint_counter("sprint"), Some(4));

        let next = board.get_next_sprint_number("sprint");
        assert_eq!(next, 4);
    }

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

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

        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];

        board.ensure_sprint_counter_initialized("test", &sprints);
        assert_eq!(board.get_sprint_counter("test"), Some(10));
    }
}

#[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
        );
    }
}