rivets 0.1.0

A Rust-based issue tracking system using JSONL storage
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
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
//! Storage abstraction layer for rivets.
//!
//! This module provides the core storage trait and factory for creating
//! storage backends. It supports multiple implementations:
//!
//! - **In-memory**: Fast, ephemeral storage backed by HashMap and petgraph
//! - **JSONL**: Persistent file-based storage using JSON Lines format
//! - **PostgreSQL**: Production-ready relational database (future)
//!
//! # Architecture
//!
//! The storage layer uses an async trait to enable both blocking (in-memory)
//! and truly async (PostgreSQL) implementations. The trait is object-safe,
//! allowing for dynamic dispatch via `Box<dyn IssueStorage>`.
//!
//! # Test Utilities
//!
//! This module provides a [`MockStorage`] implementation for testing code that
//! depends on the [`IssueStorage`] trait. To use it in your tests, enable the
//! `test-util` feature:
//!
//! ```toml
//! [dev-dependencies]
//! rivets = { version = "...", features = ["test-util"] }
//! ```
//!
//! Then use `MockStorage` in your tests:
//!
//! ```rust,ignore
//! use rivets::storage::{MockStorage, IssueStorage};
//!
//! #[tokio::test]
//! async fn test_with_mock_storage() {
//!     let storage: Box<dyn IssueStorage> = Box::new(MockStorage::new());
//!     // Use storage in tests...
//! }
//! ```
//!
//! # Example
//!
//! ```no_run
//! use rivets::storage::{IssueStorage, StorageBackend, create_storage};
//! use rivets::domain::{NewIssue, IssueType};
//!
//! #[tokio::main(flavor = "current_thread")]
//! async fn main() -> anyhow::Result<()> {
//!     // Create in-memory storage with a prefix for issue IDs.
//!     // In real applications, the prefix comes from RivetsConfig.issue_prefix.
//!     let mut storage = create_storage(StorageBackend::InMemory, "myapp".to_string()).await?;
//!
//!     // Create an issue
//!     let new_issue = NewIssue {
//!         title: "Implement feature X".to_string(),
//!         description: "Add new functionality".to_string(),
//!         priority: 1,
//!         issue_type: IssueType::Feature,
//!         assignee: Some("alice".to_string()),
//!         labels: vec![],
//!         design: None,
//!         acceptance_criteria: None,
//!         notes: None,
//!         external_ref: None,
//!         dependencies: vec![],
//!     };
//!
//!     let issue = storage.create(new_issue).await?;
//!     println!("Created issue: {}", issue.id);
//!
//!     Ok(())
//! }
//! ```

use crate::domain::{
    Dependency, DependencyType, Issue, IssueFilter, IssueId, IssueUpdate, NewIssue, SortPolicy,
};
use crate::error::Result;
use async_trait::async_trait;
use std::path::{Path, PathBuf};

// Storage backend implementations
pub mod in_memory;

/// Core storage trait for issue management.
///
/// This trait defines the interface for all storage backends. Implementations
/// must be `Send + Sync` to support concurrent access in async contexts.
///
/// # Method Categories
///
/// - **CRUD**: `create`, `get`, `update`, `delete`
/// - **Dependencies**: `add_dependency`, `remove_dependency`, `get_dependencies`, `get_dependents`, `has_cycle`
/// - **Queries**: `list`, `ready_to_work`, `blocked_issues`
/// - **Batch Operations**: `import_issues`, `export_all`
/// - **Persistence**: `save`
///
/// # Error Handling
///
/// All methods return `Result<T>` where the error type includes:
/// - `IssueNotFound`: Requested issue doesn't exist
/// - `HasDependents`: Cannot delete issue with dependents
/// - `CircularDependency`: Operation would create a cycle
/// - `Storage`: Backend-specific errors
///
/// # Thread Safety
///
/// Implementations should use appropriate synchronization primitives
/// (`Arc<Mutex<T>>` for in-memory, database transactions for PostgreSQL)
/// to ensure thread-safe access.
#[async_trait]
pub trait IssueStorage: Send + Sync {
    // ========== CRUD Operations ==========

    /// Create a new issue.
    ///
    /// Generates a unique ID for the issue and sets creation timestamps.
    ///
    /// # Implementation Requirements
    ///
    /// Implementations **MUST** validate input by calling `issue.validate()`
    /// before creating the issue. This ensures consistent validation across
    /// all storage backends.
    ///
    /// # Errors
    ///
    /// Returns `Error::InvalidPriority` if priority is not in range 0-4.
    /// Returns `Error::Storage` if title validation fails or other constraints are violated.
    async fn create(&mut self, issue: NewIssue) -> Result<Issue>;

    /// Get an issue by ID.
    ///
    /// Returns `None` if the issue doesn't exist.
    async fn get(&self, id: &IssueId) -> Result<Option<Issue>>;

    /// Update an existing issue.
    ///
    /// Only fields present in `updates` are modified. Returns the updated issue.
    ///
    /// # Errors
    ///
    /// Returns `Error::IssueNotFound` if the issue doesn't exist.
    async fn update(&mut self, id: &IssueId, updates: IssueUpdate) -> Result<Issue>;

    /// Delete an issue.
    ///
    /// Removes the issue and all its outgoing dependencies. Fails if other
    /// issues depend on this one (to prevent orphaned dependencies).
    ///
    /// # Errors
    ///
    /// - `Error::IssueNotFound` if the issue doesn't exist
    /// - `Error::HasDependents` if other issues depend on this issue
    async fn delete(&mut self, id: &IssueId) -> Result<()>;

    // ========== Dependency Management ==========

    /// Add a dependency between two issues.
    ///
    /// Checks for cycles before adding. The dependency is directional:
    /// `from` depends on `to`.
    ///
    /// # Errors
    ///
    /// - `Error::IssueNotFound` if either issue doesn't exist
    /// - `Error::CircularDependency` if this would create a cycle
    async fn add_dependency(
        &mut self,
        from: &IssueId,
        to: &IssueId,
        dep_type: DependencyType,
    ) -> Result<()>;

    /// Remove a dependency between two issues.
    ///
    /// # Errors
    ///
    /// - `Error::DependencyNotFound` if the dependency doesn't exist
    async fn remove_dependency(&mut self, from: &IssueId, to: &IssueId) -> Result<()>;

    /// Get all dependencies for an issue.
    ///
    /// Returns issues that this issue depends on.
    async fn get_dependencies(&self, id: &IssueId) -> Result<Vec<Dependency>>;

    /// Get all dependents of an issue.
    ///
    /// Returns issues that depend on this issue.
    async fn get_dependents(&self, id: &IssueId) -> Result<Vec<Dependency>>;

    /// Check if adding a dependency would create a cycle.
    ///
    /// Returns `true` if adding `from -> to` would create a circular dependency.
    async fn has_cycle(&self, from: &IssueId, to: &IssueId) -> Result<bool>;

    /// Get the full dependency tree for an issue.
    ///
    /// Performs a breadth-first traversal of the dependency graph starting from
    /// the given issue, returning all transitive dependencies with their depth
    /// in the tree. The result is ordered by traversal order (BFS).
    ///
    /// # Arguments
    ///
    /// * `id` - The root issue ID to start traversal from
    /// * `max_depth` - Optional maximum depth to traverse (None for unlimited)
    ///
    /// # Returns
    ///
    /// A vector of tuples containing:
    /// - The dependency relationship
    /// - The depth in the tree (1 for direct dependencies, 2 for their dependencies, etc.)
    ///
    /// # Example
    ///
    /// For a dependency chain A -> B -> C, calling `get_dependency_tree(&A, None)` returns:
    /// - (B, 1) - direct dependency
    /// - (C, 2) - transitive dependency
    ///
    /// # Errors
    ///
    /// - `Error::IssueNotFound` if the issue doesn't exist
    async fn get_dependency_tree(
        &self,
        id: &IssueId,
        max_depth: Option<usize>,
    ) -> Result<Vec<(Dependency, usize)>>;

    // ========== Queries ==========

    /// List issues matching the given filter.
    ///
    /// If no filter is provided, returns all non-closed issues.
    async fn list(&self, filter: &IssueFilter) -> Result<Vec<Issue>>;

    /// Find issues ready to work on.
    ///
    /// Returns issues that are:
    /// - Not closed
    /// - Not blocked by dependencies
    /// - Not blocked transitively through parent-child relationships
    ///
    /// # Sort Policies
    ///
    /// The `sort_policy` parameter controls result ordering:
    /// - `Hybrid` (default): Recent issues (< 48h) by priority, older by age
    /// - `Priority`: Strict P0 -> P1 -> P2 -> P3 -> P4 ordering
    /// - `Oldest`: Creation date ascending (oldest first)
    ///
    /// # Arguments
    ///
    /// * `filter` - Optional filter to narrow results by status, priority, type, assignee, or label
    /// * `sort_policy` - Sort order for results (defaults to Hybrid if None)
    async fn ready_to_work(
        &self,
        filter: Option<&IssueFilter>,
        sort_policy: Option<SortPolicy>,
    ) -> Result<Vec<Issue>>;

    /// Get all blocked issues.
    ///
    /// Returns tuples of (blocked issue, blocking issues).
    async fn blocked_issues(&self) -> Result<Vec<(Issue, Vec<Issue>)>>;

    // ========== Atomic Label Operations ==========

    /// Atomically add a label to an issue.
    ///
    /// This operation is atomic - no TOCTOU race condition between read and write.
    /// If the label already exists, this is a no-op and returns success.
    ///
    /// # Errors
    ///
    /// - `Error::IssueNotFound` if the issue doesn't exist
    async fn add_label(&mut self, id: &IssueId, label: &str) -> Result<Issue>;

    /// Atomically remove a label from an issue.
    ///
    /// This operation is atomic - no TOCTOU race condition between read and write.
    /// If the label doesn't exist, this is a no-op and returns success.
    ///
    /// # Errors
    ///
    /// - `Error::IssueNotFound` if the issue doesn't exist
    async fn remove_label(&mut self, id: &IssueId, label: &str) -> Result<Issue>;

    // ========== Batch Operations ==========

    /// Import multiple issues.
    ///
    /// Used for bulk loading from JSONL files or database migrations.
    /// Dependencies are resolved after all issues are imported.
    async fn import_issues(&mut self, issues: Vec<Issue>) -> Result<()>;

    /// Export all issues.
    ///
    /// Returns all issues in the storage, suitable for JSONL export or backup.
    async fn export_all(&self) -> Result<Vec<Issue>>;

    // ========== Persistence ==========

    /// Save changes to persistent storage.
    ///
    /// This method takes `&self` (not `&mut self`) to allow saving from shared
    /// references. Implementations use interior mutability (e.g., `Arc<Mutex<>>`)
    /// to handle this safely. This design choice enables:
    /// - Saving after read-only queries without requiring exclusive access
    /// - Periodic auto-save operations from background tasks
    /// - Explicit save points in transaction-like workflows
    ///
    /// For in-memory storage with JSONL backing, this writes to disk.
    /// For database backends, this is typically a no-op (auto-committed).
    async fn save(&self) -> Result<()>;

    /// Reload state from persistent storage, discarding in-memory changes.
    ///
    /// This method restores the storage to match the on-disk state, discarding
    /// any in-memory modifications that haven't been saved. It's essential for
    /// maintaining consistency in long-running processes (like MCP servers)
    /// when a `save()` operation fails.
    ///
    /// # Use Case
    ///
    /// When an operation modifies in-memory state but `save()` fails:
    /// 1. In-memory state has unsaved changes
    /// 2. On-disk state is unchanged
    /// 3. Subsequent operations would see inconsistent state
    /// 4. Call `reload()` to restore in-memory state to match disk
    ///
    /// # Implementation Notes
    ///
    /// - **JSONL backend**: Re-reads the file and rebuilds in-memory state
    /// - **In-memory only**: No-op (there's no persistent state to reload from)
    /// - **Database backends**: No-op (state is always consistent with DB)
    ///
    /// # Errors
    ///
    /// Returns an error if the backing file cannot be read or parsed.
    async fn reload(&mut self) -> Result<()>;
}

/// Storage backend configuration.
///
/// Determines which storage implementation to use.
#[derive(Debug, Clone)]
pub enum StorageBackend {
    /// In-memory storage (ephemeral)
    InMemory,

    /// JSONL file storage (persistent)
    Jsonl(PathBuf),

    /// PostgreSQL database (persistent, production-ready)
    #[allow(dead_code)]
    PostgreSQL(String),
}

impl StorageBackend {
    /// Returns the data file path for file-based backends.
    ///
    /// Returns `Some(path)` for backends that use a file (e.g., JSONL),
    /// or `None` for backends that don't (e.g., InMemory, PostgreSQL).
    pub fn data_path(&self) -> Option<&Path> {
        match self {
            StorageBackend::Jsonl(path) => Some(path),
            StorageBackend::InMemory | StorageBackend::PostgreSQL(_) => None,
        }
    }
}

/// Wrapper that adds JSONL file persistence to any storage backend.
///
/// This wrapper holds a reference to the file path and implements `save()`
/// by writing all issues to the JSONL file atomically.
struct JsonlBackedStorage {
    inner: Box<dyn IssueStorage>,
    path: PathBuf,
    prefix: String,
}

impl JsonlBackedStorage {
    /// Returns an immutable reference to the inner storage implementation.
    ///
    /// This is useful for testing or when you need to access the underlying
    /// storage without the JSONL persistence wrapper.
    #[allow(dead_code)]
    pub(crate) fn inner(&self) -> &dyn IssueStorage {
        self.inner.as_ref()
    }
}

#[async_trait]
impl IssueStorage for JsonlBackedStorage {
    async fn create(&mut self, issue: NewIssue) -> Result<Issue> {
        self.inner.create(issue).await
    }

    async fn get(&self, id: &IssueId) -> Result<Option<Issue>> {
        self.inner.get(id).await
    }

    async fn update(&mut self, id: &IssueId, updates: IssueUpdate) -> Result<Issue> {
        self.inner.update(id, updates).await
    }

    async fn delete(&mut self, id: &IssueId) -> Result<()> {
        self.inner.delete(id).await
    }

    async fn add_dependency(
        &mut self,
        from: &IssueId,
        to: &IssueId,
        dep_type: DependencyType,
    ) -> Result<()> {
        self.inner.add_dependency(from, to, dep_type).await
    }

    async fn remove_dependency(&mut self, from: &IssueId, to: &IssueId) -> Result<()> {
        self.inner.remove_dependency(from, to).await
    }

    async fn get_dependencies(&self, id: &IssueId) -> Result<Vec<Dependency>> {
        self.inner.get_dependencies(id).await
    }

    async fn get_dependents(&self, id: &IssueId) -> Result<Vec<Dependency>> {
        self.inner.get_dependents(id).await
    }

    async fn has_cycle(&self, from: &IssueId, to: &IssueId) -> Result<bool> {
        self.inner.has_cycle(from, to).await
    }

    async fn get_dependency_tree(
        &self,
        id: &IssueId,
        max_depth: Option<usize>,
    ) -> Result<Vec<(Dependency, usize)>> {
        self.inner.get_dependency_tree(id, max_depth).await
    }

    async fn list(&self, filter: &IssueFilter) -> Result<Vec<Issue>> {
        self.inner.list(filter).await
    }

    async fn ready_to_work(
        &self,
        filter: Option<&IssueFilter>,
        sort_policy: Option<SortPolicy>,
    ) -> Result<Vec<Issue>> {
        self.inner.ready_to_work(filter, sort_policy).await
    }

    async fn blocked_issues(&self) -> Result<Vec<(Issue, Vec<Issue>)>> {
        self.inner.blocked_issues().await
    }

    async fn add_label(&mut self, id: &IssueId, label: &str) -> Result<Issue> {
        self.inner.add_label(id, label).await
    }

    async fn remove_label(&mut self, id: &IssueId, label: &str) -> Result<Issue> {
        self.inner.remove_label(id, label).await
    }

    async fn import_issues(&mut self, issues: Vec<Issue>) -> Result<()> {
        self.inner.import_issues(issues).await
    }

    async fn export_all(&self) -> Result<Vec<Issue>> {
        self.inner.export_all().await
    }

    async fn save(&self) -> Result<()> {
        in_memory::save_to_jsonl(self.inner.as_ref(), &self.path).await
    }

    async fn reload(&mut self) -> Result<()> {
        // Reload from the JSONL file, replacing the inner storage
        if self.path.exists() {
            let (new_storage, warnings) =
                in_memory::load_from_jsonl(&self.path, self.prefix.clone()).await?;
            if !warnings.is_empty() {
                for warning in &warnings {
                    tracing::warn!(warning = ?warning, "JSONL reload warning");
                }
            }
            self.inner = new_storage;
        } else {
            // File doesn't exist - reset to empty storage
            self.inner = in_memory::new_in_memory_storage(self.prefix.clone());
        }
        Ok(())
    }
}

/// Create a storage instance for the given backend.
///
/// This factory function returns a trait object that can be used
/// polymorphically regardless of the backend implementation.
///
/// # Arguments
///
/// * `backend` - The storage backend to use
/// * `prefix` - The prefix for generated issue IDs (e.g., "proj", "myapp")
///
/// # Example
///
/// ```no_run
/// use rivets::storage::{create_storage, StorageBackend};
///
/// #[tokio::main(flavor = "current_thread")]
/// async fn main() -> anyhow::Result<()> {
///     let storage = create_storage(StorageBackend::InMemory, "proj".to_string()).await?;
///     // Use storage...
///     Ok(())
/// }
/// ```
///
/// # Errors
///
/// - `Error::Io` if file operations fail (JSONL backend)
/// - `Error::Storage` for backend-specific initialization errors
pub async fn create_storage(
    backend: StorageBackend,
    prefix: String,
) -> Result<Box<dyn IssueStorage>> {
    match backend {
        StorageBackend::InMemory => Ok(in_memory::new_in_memory_storage(prefix)),
        StorageBackend::Jsonl(path) => {
            // JSONL backend uses InMemoryStorage with file persistence
            let inner = if path.exists() {
                let (storage, warnings) = in_memory::load_from_jsonl(&path, prefix.clone()).await?;
                if !warnings.is_empty() {
                    // Log warnings but continue - storage is still usable
                    for warning in &warnings {
                        tracing::warn!(warning = ?warning, "JSONL load warning");
                    }
                }
                storage
            } else {
                // File doesn't exist yet (first run) - create empty storage
                in_memory::new_in_memory_storage(prefix.clone())
            };
            // Wrap in JsonlBackedStorage so save() writes to file
            Ok(Box::new(JsonlBackedStorage {
                inner,
                path,
                prefix,
            }))
        }
        StorageBackend::PostgreSQL(_conn_str) => {
            // TODO: Implement PostgreSQL backend
            Err(crate::error::Error::Storage(
                "PostgreSQL storage backend not yet implemented".to_string(),
            ))
        }
    }
}

// ========== Test Utilities ==========

/// The hardcoded issue ID returned by [`MockStorage`].
#[cfg(any(test, feature = "test-util"))]
pub const MOCK_ISSUE_ID: &str = "test-1";

/// Mock implementation of [`IssueStorage`] for testing.
///
/// This is a **stateless** mock that provides a minimal implementation of the storage
/// trait for verifying trait object usage. It always returns hardcoded data for issue
/// "test-1" but does not persist any data between calls. Timestamps are generated fresh
/// on each call.
///
/// # Availability
///
/// This type is available when:
/// - Running tests (`#[cfg(test)]`)
/// - The `test-util` feature is enabled
///
/// # Example
///
/// ```rust,ignore
/// // In your Cargo.toml:
/// // [dev-dependencies]
/// // rivets = { path = "...", features = ["test-util"] }
///
/// use rivets::storage::{MockStorage, IssueStorage};
///
/// #[tokio::test]
/// async fn test_my_code_with_mock_storage() {
///     let storage: Box<dyn IssueStorage> = Box::new(MockStorage::new());
///     // Use storage in tests...
/// }
/// ```
///
/// # Behavior
///
/// - `create`: Always returns a new issue with ID "test-1"
/// - `get`: Returns `Some` only for ID "test-1", `None` otherwise
/// - `list`, `ready_to_work`, `blocked_issues`: Return empty vectors
/// - `get_dependencies`, `get_dependents`: Return empty vectors
/// - `has_cycle`: Always returns `false`
/// - Other methods: Unimplemented (will panic if called)
///
/// # When to Use MockStorage vs In-Memory Storage
///
/// **Use `MockStorage` when:**
/// - You only need to verify trait object compilation and basic usage
/// - You don't need to actually store or retrieve real data
/// - You're testing code paths that accept `Box<dyn IssueStorage>`
///
/// **Use [`in_memory::new_in_memory_storage`] when:**
/// - You need actual CRUD functionality in tests
/// - You're testing dependency graphs and relationships
/// - You need to verify business logic with real data persistence
///
/// # Thread Safety
///
/// `MockStorage` is inherently thread-safe as it contains no mutable state
/// (it's a zero-sized type). However, it doesn't provide any actual storage
/// functionality. For testing concurrent access patterns, use the in-memory
/// backend which properly handles synchronization.
#[cfg(any(test, feature = "test-util"))]
#[derive(Clone, Copy)]
#[non_exhaustive]
pub struct MockStorage;

#[cfg(any(test, feature = "test-util"))]
impl MockStorage {
    /// Create a new MockStorage instance.
    ///
    /// # Example
    ///
    /// ```rust,ignore
    /// use rivets::storage::MockStorage;
    ///
    /// let storage = MockStorage::new();
    /// ```
    pub fn new() -> Self {
        Self
    }

    /// Creates a test issue with the given ID.
    ///
    /// This is useful for creating expected values in downstream tests that need
    /// to match the format returned by [`MockStorage`].
    ///
    /// # Example
    ///
    /// ```rust,ignore
    /// use rivets::storage::{MockStorage, MOCK_ISSUE_ID};
    /// use rivets::domain::IssueId;
    ///
    /// let expected = MockStorage::create_test_issue(IssueId::new(MOCK_ISSUE_ID));
    /// ```
    pub fn create_test_issue(id: IssueId) -> Issue {
        use crate::domain::{IssueStatus, IssueType};
        use chrono::Utc;

        Issue {
            id,
            title: "Test Issue".to_string(),
            description: "Test description".to_string(),
            status: IssueStatus::Open,
            priority: 1,
            issue_type: IssueType::Task,
            assignee: None,
            labels: vec![],
            design: None,
            acceptance_criteria: None,
            notes: None,
            external_ref: None,
            dependencies: vec![],
            created_at: Utc::now(),
            updated_at: Utc::now(),
            closed_at: None,
        }
    }
}

#[cfg(any(test, feature = "test-util"))]
impl Default for MockStorage {
    fn default() -> Self {
        Self::new()
    }
}

#[cfg(any(test, feature = "test-util"))]
#[async_trait]
impl IssueStorage for MockStorage {
    async fn create(&mut self, _issue: NewIssue) -> Result<Issue> {
        Ok(Self::create_test_issue(IssueId::new(MOCK_ISSUE_ID)))
    }

    async fn get(&self, id: &IssueId) -> Result<Option<Issue>> {
        if id.as_str() == MOCK_ISSUE_ID {
            Ok(Some(Self::create_test_issue(id.clone())))
        } else {
            Ok(None)
        }
    }

    async fn update(&mut self, _id: &IssueId, _updates: IssueUpdate) -> Result<Issue> {
        unimplemented!(
            "MockStorage::update() is not implemented. Use in_memory::new_in_memory_storage() for full CRUD."
        )
    }

    async fn delete(&mut self, _id: &IssueId) -> Result<()> {
        unimplemented!(
            "MockStorage::delete() is not implemented. Use in_memory::new_in_memory_storage() for full CRUD."
        )
    }

    async fn add_dependency(
        &mut self,
        _from: &IssueId,
        _to: &IssueId,
        _dep_type: DependencyType,
    ) -> Result<()> {
        unimplemented!(
            "MockStorage::add_dependency() is not implemented. Use in_memory::new_in_memory_storage() for full CRUD."
        )
    }

    async fn remove_dependency(&mut self, _from: &IssueId, _to: &IssueId) -> Result<()> {
        unimplemented!(
            "MockStorage::remove_dependency() is not implemented. Use in_memory::new_in_memory_storage() for full CRUD."
        )
    }

    async fn get_dependencies(&self, _id: &IssueId) -> Result<Vec<Dependency>> {
        Ok(vec![])
    }

    async fn get_dependents(&self, _id: &IssueId) -> Result<Vec<Dependency>> {
        Ok(vec![])
    }

    async fn has_cycle(&self, _from: &IssueId, _to: &IssueId) -> Result<bool> {
        Ok(false)
    }

    async fn get_dependency_tree(
        &self,
        _id: &IssueId,
        _max_depth: Option<usize>,
    ) -> Result<Vec<(Dependency, usize)>> {
        Ok(vec![])
    }

    async fn list(&self, _filter: &IssueFilter) -> Result<Vec<Issue>> {
        Ok(vec![])
    }

    async fn ready_to_work(
        &self,
        _filter: Option<&IssueFilter>,
        _sort_policy: Option<SortPolicy>,
    ) -> Result<Vec<Issue>> {
        Ok(vec![])
    }

    async fn blocked_issues(&self) -> Result<Vec<(Issue, Vec<Issue>)>> {
        Ok(vec![])
    }

    async fn add_label(&mut self, _id: &IssueId, _label: &str) -> Result<Issue> {
        unimplemented!(
            "MockStorage::add_label() is not implemented. Use in_memory::new_in_memory_storage() for full CRUD."
        )
    }

    async fn remove_label(&mut self, _id: &IssueId, _label: &str) -> Result<Issue> {
        unimplemented!(
            "MockStorage::remove_label() is not implemented. Use in_memory::new_in_memory_storage() for full CRUD."
        )
    }

    async fn import_issues(&mut self, _issues: Vec<Issue>) -> Result<()> {
        Ok(())
    }

    async fn export_all(&self) -> Result<Vec<Issue>> {
        Ok(vec![])
    }

    async fn save(&self) -> Result<()> {
        Ok(())
    }

    async fn reload(&mut self) -> Result<()> {
        // MockStorage has no backing store, so reload is a no-op
        Ok(())
    }
}

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

    #[tokio::test]
    async fn test_trait_object_usage() {
        // Verify that IssueStorage is object-safe and can be used with Box<dyn>
        let mut storage: Box<dyn IssueStorage> = Box::new(MockStorage::new());

        let new_issue = NewIssue {
            title: "Test".to_string(),
            description: "Test".to_string(),
            priority: 1,
            issue_type: IssueType::Task,
            assignee: None,
            labels: vec![],
            design: None,
            acceptance_criteria: None,
            notes: None,
            external_ref: None,
            dependencies: vec![],
        };

        let issue = storage.create(new_issue).await.unwrap();
        assert_eq!(issue.id.as_str(), MOCK_ISSUE_ID);
        assert_eq!(issue.title, "Test Issue");
    }

    #[tokio::test]
    async fn test_get_issue() {
        let storage: Box<dyn IssueStorage> = Box::new(MockStorage::new());

        // Test existing issue
        let result = storage.get(&IssueId::new(MOCK_ISSUE_ID)).await.unwrap();
        assert!(result.is_some());
        assert_eq!(result.unwrap().id.as_str(), MOCK_ISSUE_ID);

        // Test non-existing issue
        let result = storage.get(&IssueId::new("test-99")).await.unwrap();
        assert!(result.is_none());
    }

    #[tokio::test]
    async fn test_empty_queries() {
        let storage: Box<dyn IssueStorage> = Box::new(MockStorage::new());

        // Test that query methods return empty results
        let filter = IssueFilter::default();
        assert!(storage.list(&filter).await.unwrap().is_empty());
        assert!(storage.ready_to_work(None, None).await.unwrap().is_empty());
        assert!(storage.blocked_issues().await.unwrap().is_empty());
    }

    #[tokio::test]
    async fn test_dependencies() {
        let storage: Box<dyn IssueStorage> = Box::new(MockStorage::new());

        let id = IssueId::new(MOCK_ISSUE_ID);
        assert!(storage.get_dependencies(&id).await.unwrap().is_empty());
        assert!(storage.get_dependents(&id).await.unwrap().is_empty());
        assert!(!storage
            .has_cycle(&id, &IssueId::new("test-2"))
            .await
            .unwrap());
    }

    #[tokio::test]
    async fn test_mock_storage_copy_semantics() {
        let mock = MockStorage::new();
        let _copy1 = mock;
        let _copy2 = mock; // Still usable - Copy semantics work
        let _: Box<dyn IssueStorage> = Box::new(mock);
    }

    #[tokio::test]
    async fn test_jsonl_reload_restores_disk_state() {
        use tempfile::TempDir;

        let temp_dir = TempDir::new().unwrap();
        let jsonl_path = temp_dir.path().join("issues.jsonl");

        // Create storage and add an issue
        let mut storage = create_storage(StorageBackend::Jsonl(jsonl_path.clone()), "test".into())
            .await
            .unwrap();

        let new_issue = NewIssue {
            title: "Original Title".to_string(),
            description: "Original description".to_string(),
            priority: 2,
            issue_type: IssueType::Task,
            assignee: None,
            labels: vec![],
            design: None,
            acceptance_criteria: None,
            notes: None,
            external_ref: None,
            dependencies: vec![],
        };

        let created = storage.create(new_issue).await.unwrap();
        let issue_id = created.id.clone();
        storage.save().await.unwrap();

        // Modify in memory without saving
        let update = IssueUpdate {
            title: Some("Modified Title".to_string()),
            ..Default::default()
        };
        let modified = storage.update(&issue_id, update).await.unwrap();
        assert_eq!(modified.title, "Modified Title");

        // Verify in-memory state is modified
        let before_reload = storage.get(&issue_id).await.unwrap().unwrap();
        assert_eq!(before_reload.title, "Modified Title");

        // Reload from disk
        storage.reload().await.unwrap();

        // Verify in-memory state matches disk (original title)
        let after_reload = storage.get(&issue_id).await.unwrap().unwrap();
        assert_eq!(after_reload.title, "Original Title");
    }

    #[tokio::test]
    async fn test_jsonl_reload_empty_file() {
        use tempfile::TempDir;

        let temp_dir = TempDir::new().unwrap();
        let jsonl_path = temp_dir.path().join("issues.jsonl");

        // Create storage, add issue, save
        let mut storage = create_storage(StorageBackend::Jsonl(jsonl_path.clone()), "test".into())
            .await
            .unwrap();

        let new_issue = NewIssue {
            title: "Test Issue".to_string(),
            description: "".to_string(),
            priority: 2,
            issue_type: IssueType::Task,
            assignee: None,
            labels: vec![],
            design: None,
            acceptance_criteria: None,
            notes: None,
            external_ref: None,
            dependencies: vec![],
        };

        let created = storage.create(new_issue).await.unwrap();
        let issue_id = created.id.clone();
        storage.save().await.unwrap();

        // Delete the file to simulate corruption/missing file
        std::fs::remove_file(&jsonl_path).unwrap();

        // Reload should reset to empty storage
        storage.reload().await.unwrap();

        // Issue should no longer exist
        let result = storage.get(&issue_id).await.unwrap();
        assert!(result.is_none());
    }

    #[tokio::test]
    async fn test_in_memory_reload_is_noop() {
        let mut storage = create_storage(StorageBackend::InMemory, "test".into())
            .await
            .unwrap();

        let new_issue = NewIssue {
            title: "Test Issue".to_string(),
            description: "".to_string(),
            priority: 2,
            issue_type: IssueType::Task,
            assignee: None,
            labels: vec![],
            design: None,
            acceptance_criteria: None,
            notes: None,
            external_ref: None,
            dependencies: vec![],
        };

        let created = storage.create(new_issue).await.unwrap();
        let issue_id = created.id.clone();

        // Reload for in-memory is a no-op, data should persist
        storage.reload().await.unwrap();

        // Issue should still exist
        let result = storage.get(&issue_id).await.unwrap();
        assert!(result.is_some());
        assert_eq!(result.unwrap().title, "Test Issue");
    }
}