tnj-tui 0.1.2

TUI Tasks, notes and journals in Notebooks
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
use rusqlite::Connection;
use std::path::PathBuf;
use thiserror::Error;

use crate::models::{Task, Note, JournalEntry, Notebook};

#[derive(Debug, Error)]
pub enum DatabaseError {
    #[error("SQLite error: {0}")]
    SqliteError(#[from] rusqlite::Error),
    #[error("Failed to create database directory: {0}")]
    DirectoryError(String),
}

pub struct Database {
    conn: Connection,
}

impl Database {
    /// Create a new database connection and initialize the schema
    pub fn new(path: &str) -> Result<Self, DatabaseError> {
        let db_path = PathBuf::from(path);

        // Create parent directory if it doesn't exist
        if let Some(parent) = db_path.parent() {
            if !parent.exists() {
                std::fs::create_dir_all(parent)
                    .map_err(|e| DatabaseError::DirectoryError(e.to_string()))?;
            }
        }

        // Open or create the database
        let conn = Connection::open(&db_path)?;

        let db = Database { conn };
        db.initialize_schema()?;

        Ok(db)
    }

    /// Initialize the database schema (tables and indexes)
    fn initialize_schema(&self) -> Result<(), DatabaseError> {
        // Create tasks table
        self.conn.execute(
            "CREATE TABLE IF NOT EXISTS tasks (
                id              INTEGER PRIMARY KEY AUTOINCREMENT,
                title           TEXT NOT NULL,
                description     TEXT,
                due_date        TEXT,
                status          TEXT DEFAULT 'todo',
                tags            TEXT,
                \"order\"        INTEGER DEFAULT 0,
                archived        INTEGER DEFAULT 0,
                created_at      TEXT NOT NULL,
                updated_at      TEXT NOT NULL
            )",
            [],
        )?;

        // Create notes table
        self.conn.execute(
            "CREATE TABLE IF NOT EXISTS notes (
                id              INTEGER PRIMARY KEY AUTOINCREMENT,
                title           TEXT NOT NULL,
                content         TEXT,
                tags            TEXT,
                archived        INTEGER DEFAULT 0,
                created_at      TEXT NOT NULL,
                updated_at      TEXT NOT NULL
            )",
            [],
        )?;

        // Create journals table
        self.conn.execute(
            "CREATE TABLE IF NOT EXISTS journals (
                id              INTEGER PRIMARY KEY AUTOINCREMENT,
                date            TEXT NOT NULL,
                title           TEXT,
                content         TEXT,
                tags            TEXT,
                archived        INTEGER DEFAULT 0,
                created_at      TEXT NOT NULL,
                updated_at      TEXT NOT NULL
            )",
            [],
        )?;

        // Create notebooks table
        self.conn.execute(
            "CREATE TABLE IF NOT EXISTS notebooks (
                id              INTEGER PRIMARY KEY AUTOINCREMENT,
                name            TEXT NOT NULL,
                created_at      TEXT NOT NULL,
                updated_at      TEXT NOT NULL
            )",
            [],
        )?;

        // Create indexes
        self.conn.execute(
            "CREATE INDEX IF NOT EXISTS idx_tasks_due_date ON tasks(due_date)",
            [],
        )?;

        self.conn.execute(
            "CREATE INDEX IF NOT EXISTS idx_journals_date ON journals(date)",
            [],
        )?;

        self.conn.execute(
            "CREATE INDEX IF NOT EXISTS idx_notes_title ON notes(title)",
            [],
        )?;

        self.conn.execute(
            "CREATE INDEX IF NOT EXISTS idx_tasks_title ON tasks(title)",
            [],
        )?;

        self.conn.execute(
            "CREATE INDEX IF NOT EXISTS idx_journals_title ON journals(title)",
            [],
        )?;

        self.conn.execute(
            "CREATE INDEX IF NOT EXISTS idx_notebooks_name ON notebooks(name)",
            [],
        )?;

        // Migrate existing tables to add notebook_id column if it doesn't exist
        self.migrate_add_notebook_id()?;

        Ok(())
    }

    /// Migrate existing tables to add notebook_id column
    fn migrate_add_notebook_id(&self) -> Result<(), DatabaseError> {
        // Helper to check if a column exists
        fn column_exists(conn: &Connection, table: &str, column: &str) -> Result<bool, DatabaseError> {
            let mut stmt = conn.prepare(
                "SELECT COUNT(*) FROM pragma_table_info(?1) WHERE name = ?2"
            )?;
            let count: i64 = stmt.query_row(rusqlite::params![table, column], |row| row.get(0))?;
            Ok(count > 0)
        }

        // Add notebook_id to tasks table if it doesn't exist
        if !column_exists(&self.conn, "tasks", "notebook_id")? {
            self.conn.execute(
                "ALTER TABLE tasks ADD COLUMN notebook_id INTEGER",
                [],
            )?;
            self.conn.execute(
                "CREATE INDEX IF NOT EXISTS idx_tasks_notebook_id ON tasks(notebook_id)",
                [],
            )?;
        }

        // Add notebook_id to notes table if it doesn't exist
        if !column_exists(&self.conn, "notes", "notebook_id")? {
            self.conn.execute(
                "ALTER TABLE notes ADD COLUMN notebook_id INTEGER",
                [],
            )?;
            self.conn.execute(
                "CREATE INDEX IF NOT EXISTS idx_notes_notebook_id ON notes(notebook_id)",
                [],
            )?;
        }

        // Add notebook_id to journals table if it doesn't exist
        if !column_exists(&self.conn, "journals", "notebook_id")? {
            self.conn.execute(
                "ALTER TABLE journals ADD COLUMN notebook_id INTEGER",
                [],
            )?;
            self.conn.execute(
                "CREATE INDEX IF NOT EXISTS idx_journals_notebook_id ON journals(notebook_id)",
                [],
            )?;
        }

        Ok(())
    }

    /// Get a reference to the underlying connection
    pub fn conn(&self) -> &Connection {
        &self.conn
    }

    /// Get a mutable reference to the underlying connection
    pub fn conn_mut(&mut self) -> &mut Connection {
        &mut self.conn
    }

    /// Insert a task into the database and return its ID
    pub fn insert_task(&self, task: &Task) -> Result<i64, DatabaseError> {
        self.conn.execute(
            "INSERT INTO tasks (title, description, due_date, status, tags, \"order\", archived, notebook_id, created_at, updated_at)
             VALUES (?1, ?2, ?3, ?4, ?5, ?6, ?7, ?8, ?9, ?10)",
            rusqlite::params![
                task.title,
                task.description,
                task.due_date,
                task.status,
                task.tags,
                task.order,
                if task.archived { 1 } else { 0 },
                task.notebook_id,
                task.created_at,
                task.updated_at
            ],
        )?;
        Ok(self.conn.last_insert_rowid())
    }

    /// Insert a note into the database and return its ID
    pub fn insert_note(&self, note: &Note) -> Result<i64, DatabaseError> {
        self.conn.execute(
            "INSERT INTO notes (title, content, tags, archived, notebook_id, created_at, updated_at)
             VALUES (?1, ?2, ?3, ?4, ?5, ?6, ?7)",
            rusqlite::params![
                note.title,
                note.content,
                note.tags,
                if note.archived { 1 } else { 0 },
                note.notebook_id,
                note.created_at,
                note.updated_at
            ],
        )?;
        Ok(self.conn.last_insert_rowid())
    }

    /// Insert a journal entry into the database and return its ID
    pub fn insert_journal(&self, journal: &JournalEntry) -> Result<i64, DatabaseError> {
        self.conn.execute(
            "INSERT INTO journals (date, title, content, tags, archived, notebook_id, created_at, updated_at)
             VALUES (?1, ?2, ?3, ?4, ?5, ?6, ?7, ?8)",
            rusqlite::params![
                journal.date,
                journal.title,
                journal.content,
                journal.tags,
                if journal.archived { 1 } else { 0 },
                journal.notebook_id,
                journal.created_at,
                journal.updated_at
            ],
        )?;
        Ok(self.conn.last_insert_rowid())
    }

    /// Helper function to map a row to a Task
    fn row_to_task(row: &rusqlite::Row) -> Result<Task, rusqlite::Error> {
        Ok(Task {
            id: Some(row.get(0)?),
            title: row.get(1)?,
            description: row.get(2)?,
            due_date: row.get(3)?,
            status: row.get(4)?,
            tags: row.get(5)?,
            order: row.get(6)?,
            archived: row.get::<_, i64>(7)? != 0,
            notebook_id: row.get(8)?,
            created_at: row.get(9)?,
            updated_at: row.get(10)?,
        })
    }

    /// Get all tasks ordered by order ASC, optionally filtered by notebook_id
    pub fn get_all_tasks(&self, notebook_id: Option<i64>) -> Result<Vec<Task>, DatabaseError> {
        if let Some(nb_id) = notebook_id {
            let mut stmt = self.conn.prepare(
                "SELECT id, title, description, due_date, status, tags, \"order\", archived, notebook_id, created_at, updated_at
                 FROM tasks WHERE archived = 0 AND notebook_id = ?1 ORDER BY \"order\" ASC"
            )?;
            let tasks = stmt.query_map(rusqlite::params![nb_id], Self::row_to_task)?
                .collect::<Result<Vec<_>, _>>()?;
            return Ok(tasks);
        }
        
        let mut stmt = self.conn.prepare(
            "SELECT id, title, description, due_date, status, tags, \"order\", archived, notebook_id, created_at, updated_at
             FROM tasks WHERE archived = 0 AND notebook_id IS NULL ORDER BY \"order\" ASC"
        )?;
        let tasks = stmt.query_map([], Self::row_to_task)?
            .collect::<Result<Vec<_>, _>>()?;
        
        Ok(tasks)
    }

    /// Get all tasks including archived, ordered by order ASC, optionally filtered by notebook_id
    pub fn get_all_tasks_including_archived(&self, notebook_id: Option<i64>) -> Result<Vec<Task>, DatabaseError> {
        if let Some(nb_id) = notebook_id {
            let mut stmt = self.conn.prepare(
                "SELECT id, title, description, due_date, status, tags, \"order\", archived, notebook_id, created_at, updated_at
                 FROM tasks WHERE notebook_id = ?1 ORDER BY \"order\" ASC"
            )?;
            let tasks = stmt.query_map(rusqlite::params![nb_id], Self::row_to_task)?
                .collect::<Result<Vec<_>, _>>()?;
            return Ok(tasks);
        }
        
        let mut stmt = self.conn.prepare(
            "SELECT id, title, description, due_date, status, tags, \"order\", archived, notebook_id, created_at, updated_at
             FROM tasks WHERE notebook_id IS NULL ORDER BY \"order\" ASC"
        )?;
        let tasks = stmt.query_map([], Self::row_to_task)?
            .collect::<Result<Vec<_>, _>>()?;
        
        Ok(tasks)
    }

    /// Get a single task by ID
    pub fn get_task(&self, id: i64) -> Result<Task, DatabaseError> {
        let mut stmt = self.conn.prepare(
            "SELECT id, title, description, due_date, status, tags, \"order\", archived, notebook_id, created_at, updated_at
             FROM tasks WHERE id = ?1"
        )?;
        
        stmt.query_row(rusqlite::params![id], |row| {
            Ok(Task {
                id: Some(row.get(0)?),
                title: row.get(1)?,
                description: row.get(2)?,
                due_date: row.get(3)?,
                status: row.get(4)?,
                tags: row.get(5)?,
                order: row.get(6)?,
                archived: row.get::<_, i64>(7)? != 0,
                notebook_id: row.get(8)?,
                created_at: row.get(9)?,
                updated_at: row.get(10)?,
            })
        })
        .map_err(DatabaseError::from)
    }

    /// Update an existing task
    pub fn update_task(&self, task: &Task) -> Result<(), DatabaseError> {
        let id = task.id.ok_or_else(|| DatabaseError::SqliteError(
            rusqlite::Error::InvalidColumnType(0, "id".to_string(), rusqlite::types::Type::Null)
        ))?;
        
        let tx = self.conn.unchecked_transaction()?;
        tx.execute(
            "UPDATE tasks SET title = ?1, description = ?2, due_date = ?3, 
             status = ?4, tags = ?5, \"order\" = ?6, archived = ?7, notebook_id = ?8, updated_at = ?9 WHERE id = ?10",
            rusqlite::params![
                task.title,
                task.description,
                task.due_date,
                task.status,
                task.tags,
                task.order,
                if task.archived { 1 } else { 0 },
                task.notebook_id,
                task.updated_at,
                id
            ],
        )?;
        tx.commit()?;
        Ok(())
    }

    /// Get the maximum order value from all tasks
    pub fn get_max_task_order(&self) -> Result<i64, DatabaseError> {
        let max_order: Option<i64> = self.conn.query_row(
            "SELECT MAX(\"order\") FROM tasks",
            [],
            |row| row.get(0),
        )?;
        Ok(max_order.unwrap_or(-1))
    }

    /// Update a task's order value
    pub fn update_task_order(&self, task_id: i64, new_order: i64) -> Result<(), DatabaseError> {
        let tx = self.conn.unchecked_transaction()?;
        tx.execute(
            "UPDATE tasks SET \"order\" = ?1, updated_at = ?2 WHERE id = ?3",
            rusqlite::params![
                new_order,
                chrono::Utc::now().format("%Y-%m-%d %H:%M:%S").to_string(),
                task_id
            ],
        )?;
        tx.commit()?;
        Ok(())
    }

    /// Delete a task by ID
    pub fn delete_task(&self, id: i64) -> Result<(), DatabaseError> {
        let tx = self.conn.unchecked_transaction()?;
        tx.execute("DELETE FROM tasks WHERE id = ?1", rusqlite::params![id])?;
        tx.commit()?;
        Ok(())
    }

    /// Archive a task by ID
    pub fn archive_task(&self, id: i64) -> Result<(), DatabaseError> {
        let tx = self.conn.unchecked_transaction()?;
        tx.execute(
            "UPDATE tasks SET archived = 1, updated_at = ?1 WHERE id = ?2",
            rusqlite::params![
                chrono::Utc::now().format("%Y-%m-%d %H:%M:%S").to_string(),
                id
            ],
        )?;
        tx.commit()?;
        Ok(())
    }

    /// Helper function to map a row to a Note
    fn row_to_note(row: &rusqlite::Row) -> Result<Note, rusqlite::Error> {
        Ok(Note {
            id: Some(row.get(0)?),
            title: row.get(1)?,
            content: row.get(2)?,
            tags: row.get(3)?,
            archived: row.get::<_, i64>(4)? != 0,
            notebook_id: row.get(5)?,
            created_at: row.get(6)?,
            updated_at: row.get(7)?,
        })
    }

    /// Get all notes ordered by created_at DESC, optionally filtered by notebook_id
    pub fn get_all_notes(&self, notebook_id: Option<i64>) -> Result<Vec<Note>, DatabaseError> {
        if let Some(nb_id) = notebook_id {
            let mut stmt = self.conn.prepare(
                "SELECT id, title, content, tags, archived, notebook_id, created_at, updated_at
                 FROM notes WHERE archived = 0 AND notebook_id = ?1 ORDER BY created_at DESC"
            )?;
            let notes = stmt.query_map(rusqlite::params![nb_id], Self::row_to_note)?
                .collect::<Result<Vec<_>, _>>()?;
            return Ok(notes);
        }
        
        let mut stmt = self.conn.prepare(
            "SELECT id, title, content, tags, archived, notebook_id, created_at, updated_at
             FROM notes WHERE archived = 0 AND notebook_id IS NULL ORDER BY created_at DESC"
        )?;
        let notes = stmt.query_map([], Self::row_to_note)?
            .collect::<Result<Vec<_>, _>>()?;
        
        Ok(notes)
    }

    /// Get all notes including archived, ordered by created_at DESC, optionally filtered by notebook_id
    pub fn get_all_notes_including_archived(&self, notebook_id: Option<i64>) -> Result<Vec<Note>, DatabaseError> {
        if let Some(nb_id) = notebook_id {
            let mut stmt = self.conn.prepare(
                "SELECT id, title, content, tags, archived, notebook_id, created_at, updated_at
                 FROM notes WHERE notebook_id = ?1 ORDER BY created_at DESC"
            )?;
            let notes = stmt.query_map(rusqlite::params![nb_id], Self::row_to_note)?
                .collect::<Result<Vec<_>, _>>()?;
            return Ok(notes);
        }
        
        let mut stmt = self.conn.prepare(
            "SELECT id, title, content, tags, archived, notebook_id, created_at, updated_at
             FROM notes WHERE notebook_id IS NULL ORDER BY created_at DESC"
        )?;
        let notes = stmt.query_map([], Self::row_to_note)?
            .collect::<Result<Vec<_>, _>>()?;
        
        Ok(notes)
    }

    /// Get a single note by ID
    pub fn get_note(&self, id: i64) -> Result<Note, DatabaseError> {
        let mut stmt = self.conn.prepare(
            "SELECT id, title, content, tags, archived, notebook_id, created_at, updated_at
             FROM notes WHERE id = ?1"
        )?;
        
        stmt.query_row(rusqlite::params![id], |row| {
            Ok(Note {
                id: Some(row.get(0)?),
                title: row.get(1)?,
                content: row.get(2)?,
                tags: row.get(3)?,
                archived: row.get::<_, i64>(4)? != 0,
                notebook_id: row.get(5)?,
                created_at: row.get(6)?,
                updated_at: row.get(7)?,
            })
        })
        .map_err(DatabaseError::from)
    }

    /// Update an existing note
    pub fn update_note(&self, note: &Note) -> Result<(), DatabaseError> {
        let id = note.id.ok_or_else(|| DatabaseError::SqliteError(
            rusqlite::Error::InvalidColumnType(0, "id".to_string(), rusqlite::types::Type::Null)
        ))?;
        
        let tx = self.conn.unchecked_transaction()?;
        tx.execute(
            "UPDATE notes SET title = ?1, content = ?2, tags = ?3, archived = ?4, notebook_id = ?5, updated_at = ?6 WHERE id = ?7",
            rusqlite::params![
                note.title,
                note.content,
                note.tags,
                if note.archived { 1 } else { 0 },
                note.notebook_id,
                note.updated_at,
                id
            ],
        )?;
        tx.commit()?;
        Ok(())
    }

    /// Delete a note by ID
    pub fn delete_note(&self, id: i64) -> Result<(), DatabaseError> {
        let tx = self.conn.unchecked_transaction()?;
        tx.execute("DELETE FROM notes WHERE id = ?1", rusqlite::params![id])?;
        tx.commit()?;
        Ok(())
    }

    /// Archive a note by ID
    pub fn archive_note(&self, id: i64) -> Result<(), DatabaseError> {
        let tx = self.conn.unchecked_transaction()?;
        tx.execute(
            "UPDATE notes SET archived = 1, updated_at = ?1 WHERE id = ?2",
            rusqlite::params![
                chrono::Utc::now().format("%Y-%m-%d %H:%M:%S").to_string(),
                id
            ],
        )?;
        tx.commit()?;
        Ok(())
    }

    /// Helper function to map a row to a JournalEntry
    fn row_to_journal(row: &rusqlite::Row) -> Result<JournalEntry, rusqlite::Error> {
        Ok(JournalEntry {
            id: Some(row.get(0)?),
            date: row.get(1)?,
            title: row.get(2)?,
            content: row.get(3)?,
            tags: row.get(4)?,
            archived: row.get::<_, i64>(5)? != 0,
            notebook_id: row.get(6)?,
            created_at: row.get(7)?,
            updated_at: row.get(8)?,
        })
    }

    /// Get all journal entries ordered by date DESC (newest first), optionally filtered by notebook_id
    pub fn get_all_journals(&self, notebook_id: Option<i64>) -> Result<Vec<JournalEntry>, DatabaseError> {
        if let Some(nb_id) = notebook_id {
            let mut stmt = self.conn.prepare(
                "SELECT id, date, title, content, tags, archived, notebook_id, created_at, updated_at
                 FROM journals WHERE archived = 0 AND notebook_id = ?1 ORDER BY date DESC, created_at DESC"
            )?;
            let journals = stmt.query_map(rusqlite::params![nb_id], Self::row_to_journal)?
                .collect::<Result<Vec<_>, _>>()?;
            return Ok(journals);
        }
        
        let mut stmt = self.conn.prepare(
            "SELECT id, date, title, content, tags, archived, notebook_id, created_at, updated_at
             FROM journals WHERE archived = 0 AND notebook_id IS NULL ORDER BY date DESC, created_at DESC"
        )?;
        let journals = stmt.query_map([], Self::row_to_journal)?
            .collect::<Result<Vec<_>, _>>()?;
        
        Ok(journals)
    }

    /// Get all journal entries including archived, ordered by date DESC, created_at DESC, optionally filtered by notebook_id
    pub fn get_all_journals_including_archived(&self, notebook_id: Option<i64>) -> Result<Vec<JournalEntry>, DatabaseError> {
        if let Some(nb_id) = notebook_id {
            let mut stmt = self.conn.prepare(
                "SELECT id, date, title, content, tags, archived, notebook_id, created_at, updated_at
                 FROM journals WHERE notebook_id = ?1 ORDER BY date DESC, created_at DESC"
            )?;
            let journals = stmt.query_map(rusqlite::params![nb_id], Self::row_to_journal)?
                .collect::<Result<Vec<_>, _>>()?;
            return Ok(journals);
        }
        
        let mut stmt = self.conn.prepare(
            "SELECT id, date, title, content, tags, archived, notebook_id, created_at, updated_at
             FROM journals WHERE notebook_id IS NULL ORDER BY date DESC, created_at DESC"
        )?;
        let journals = stmt.query_map([], Self::row_to_journal)?
            .collect::<Result<Vec<_>, _>>()?;
        
        Ok(journals)
    }

    /// Get a single journal entry by ID
    pub fn get_journal(&self, id: i64) -> Result<JournalEntry, DatabaseError> {
        let mut stmt = self.conn.prepare(
            "SELECT id, date, title, content, tags, archived, notebook_id, created_at, updated_at
             FROM journals WHERE id = ?1"
        )?;
        
        stmt.query_row(rusqlite::params![id], |row| {
            Ok(JournalEntry {
                id: Some(row.get(0)?),
                date: row.get(1)?,
                title: row.get(2)?,
                content: row.get(3)?,
                tags: row.get(4)?,
                archived: row.get::<_, i64>(5)? != 0,
                notebook_id: row.get(6)?,
                created_at: row.get(7)?,
                updated_at: row.get(8)?,
            })
        })
        .map_err(DatabaseError::from)
    }

    /// Update an existing journal entry
    pub fn update_journal(&self, journal: &JournalEntry) -> Result<(), DatabaseError> {
        let id = journal.id.ok_or_else(|| DatabaseError::SqliteError(
            rusqlite::Error::InvalidColumnType(0, "id".to_string(), rusqlite::types::Type::Null)
        ))?;
        
        let tx = self.conn.unchecked_transaction()?;
        tx.execute(
            "UPDATE journals SET date = ?1, title = ?2, content = ?3, tags = ?4, archived = ?5, notebook_id = ?6, updated_at = ?7 WHERE id = ?8",
            rusqlite::params![
                journal.date,
                journal.title,
                journal.content,
                journal.tags,
                if journal.archived { 1 } else { 0 },
                journal.notebook_id,
                journal.updated_at,
                id
            ],
        )?;
        tx.commit()?;
        Ok(())
    }

    /// Delete a journal entry by ID
    pub fn delete_journal(&self, id: i64) -> Result<(), DatabaseError> {
        let tx = self.conn.unchecked_transaction()?;
        tx.execute("DELETE FROM journals WHERE id = ?1", rusqlite::params![id])?;
        tx.commit()?;
        Ok(())
    }

    /// Archive a journal entry by ID
    pub fn archive_journal(&self, id: i64) -> Result<(), DatabaseError> {
        let tx = self.conn.unchecked_transaction()?;
        tx.execute(
            "UPDATE journals SET archived = 1, updated_at = ?1 WHERE id = ?2",
            rusqlite::params![
                chrono::Utc::now().format("%Y-%m-%d %H:%M:%S").to_string(),
                id
            ],
        )?;
        tx.commit()?;
        Ok(())
    }

    /// Get all notebooks ordered by name ASC
    pub fn get_all_notebooks(&self) -> Result<Vec<Notebook>, DatabaseError> {
        let mut stmt = self.conn.prepare(
            "SELECT id, name, created_at, updated_at
             FROM notebooks ORDER BY name ASC"
        )?;
        
        let notebooks = stmt.query_map([], |row| {
            Ok(Notebook {
                id: Some(row.get(0)?),
                name: row.get(1)?,
                created_at: row.get(2)?,
                updated_at: row.get(3)?,
            })
        })?
        .collect::<Result<Vec<_>, _>>()?;
        
        Ok(notebooks)
    }

    /// Get a single notebook by ID
    pub fn get_notebook(&self, id: i64) -> Result<Notebook, DatabaseError> {
        let mut stmt = self.conn.prepare(
            "SELECT id, name, created_at, updated_at
             FROM notebooks WHERE id = ?1"
        )?;
        
        stmt.query_row(rusqlite::params![id], |row| {
            Ok(Notebook {
                id: Some(row.get(0)?),
                name: row.get(1)?,
                created_at: row.get(2)?,
                updated_at: row.get(3)?,
            })
        })
        .map_err(DatabaseError::from)
    }

    /// Insert a notebook into the database and return its ID
    pub fn insert_notebook(&self, notebook: &Notebook) -> Result<i64, DatabaseError> {
        self.conn.execute(
            "INSERT INTO notebooks (name, created_at, updated_at)
             VALUES (?1, ?2, ?3)",
            rusqlite::params![
                notebook.name,
                notebook.created_at,
                notebook.updated_at
            ],
        )?;
        Ok(self.conn.last_insert_rowid())
    }

    /// Update an existing notebook
    pub fn update_notebook(&self, notebook: &Notebook) -> Result<(), DatabaseError> {
        let id = notebook.id.ok_or_else(|| DatabaseError::SqliteError(
            rusqlite::Error::InvalidColumnType(0, "id".to_string(), rusqlite::types::Type::Null)
        ))?;
        
        let tx = self.conn.unchecked_transaction()?;
        tx.execute(
            "UPDATE notebooks SET name = ?1, updated_at = ?2 WHERE id = ?3",
            rusqlite::params![
                notebook.name,
                notebook.updated_at,
                id
            ],
        )?;
        tx.commit()?;
        Ok(())
    }

    /// Delete a notebook by ID
    /// Sets notebook_id to NULL for all items (tasks, notes, journals) that belonged to this notebook
    pub fn delete_notebook(&self, id: i64) -> Result<(), DatabaseError> {
        let tx = self.conn.unchecked_transaction()?;
        
        // Set notebook_id to NULL for all tasks that belonged to this notebook
        tx.execute(
            "UPDATE tasks SET notebook_id = NULL, updated_at = ?1 WHERE notebook_id = ?2",
            rusqlite::params![
                chrono::Utc::now().format("%Y-%m-%d %H:%M:%S").to_string(),
                id
            ],
        )?;
        
        // Set notebook_id to NULL for all notes that belonged to this notebook
        tx.execute(
            "UPDATE notes SET notebook_id = NULL, updated_at = ?1 WHERE notebook_id = ?2",
            rusqlite::params![
                chrono::Utc::now().format("%Y-%m-%d %H:%M:%S").to_string(),
                id
            ],
        )?;
        
        // Set notebook_id to NULL for all journals that belonged to this notebook
        tx.execute(
            "UPDATE journals SET notebook_id = NULL, updated_at = ?1 WHERE notebook_id = ?2",
            rusqlite::params![
                chrono::Utc::now().format("%Y-%m-%d %H:%M:%S").to_string(),
                id
            ],
        )?;
        
        // Delete the notebook
        tx.execute("DELETE FROM notebooks WHERE id = ?1", rusqlite::params![id])?;
        
        tx.commit()?;
        Ok(())
    }

    /// Get the first notebook (for default)
    pub fn get_default_notebook(&self) -> Result<Option<Notebook>, DatabaseError> {
        let mut stmt = self.conn.prepare(
            "SELECT id, name, created_at, updated_at
             FROM notebooks ORDER BY name ASC LIMIT 1"
        )?;
        
        let result = stmt.query_row([], |row| {
            Ok(Notebook {
                id: Some(row.get(0)?),
                name: row.get(1)?,
                created_at: row.get(2)?,
                updated_at: row.get(3)?,
            })
        });
        
        match result {
            Ok(notebook) => Ok(Some(notebook)),
            Err(rusqlite::Error::QueryReturnedNoRows) => Ok(None),
            Err(e) => Err(DatabaseError::from(e)),
        }
    }
}