mind 0.2.0

I am your mind, at your command, on the line: your command line mind
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
use anyhow::{Context, Result};
use rusqlite::{Connection, params};
use std::path::PathBuf;

use crate::tags::extract_tag_relationships;

pub(crate) fn get_db_path() -> Result<PathBuf> {
    let home = std::env::var("HOME")
        .context("Could not find HOME directory")?;
    let data_dir = PathBuf::from(home).join(".local/share/mind");
    std::fs::create_dir_all(&data_dir)
        .context("Could not create data directory")?;
    Ok(data_dir.join("mind.db"))
}

pub(crate) fn init_db(conn: &Connection) -> Result<()> {
    conn.execute(
        "CREATE TABLE IF NOT EXISTS notes (
            id INTEGER PRIMARY KEY AUTOINCREMENT,
            content TEXT NOT NULL,
            created_at TEXT NOT NULL,
            updated_at TEXT NOT NULL
        )",
        [],
    ).context("Could not create notes table")?;

    conn.execute(
        "CREATE TABLE IF NOT EXISTS header_tags (
            tag  TEXT PRIMARY KEY,
            freq INTEGER NOT NULL DEFAULT 0
        )",
        [],
    ).context("Could not create header_tags table")?;

    conn.execute(
        "CREATE TABLE IF NOT EXISTS tag_siblings (
            tag_a TEXT NOT NULL,
            tag_b TEXT NOT NULL,
            freq  INTEGER NOT NULL DEFAULT 0,
            PRIMARY KEY (tag_a, tag_b)
        )",
        [],
    ).context("Could not create tag_siblings table")?;

    conn.execute(
        "CREATE TABLE IF NOT EXISTS tag_children (
            header_tag TEXT NOT NULL,
            child_tag  TEXT NOT NULL,
            freq       INTEGER NOT NULL DEFAULT 0,
            PRIMARY KEY (header_tag, child_tag)
        )",
        [],
    ).context("Could not create tag_children table")?;

    // Backfill tag stats if tables are empty but notes exist
    let header_count: i64 = conn.query_row(
        "SELECT COUNT(*) FROM header_tags", [], |row| row.get(0)
    ).unwrap_or(0);
    let notes_count: i64 = conn.query_row(
        "SELECT COUNT(*) FROM notes", [], |row| row.get(0)
    ).unwrap_or(0);
    if header_count == 0 && notes_count > 0 {
        backfill_tag_stats(conn)?;
    }

    Ok(())
}

// ── Tag stat helpers ──────────────────────────────────────────────────────────

pub(crate) fn update_tag_stats_on_add(conn: &Connection, content: &str) -> Result<()> {
    let rel = extract_tag_relationships(content);

    for tag in &rel.header_tags {
        conn.execute(
            "INSERT INTO header_tags (tag, freq) VALUES (?1, 1)
             ON CONFLICT(tag) DO UPDATE SET freq = freq + 1",
            params![tag],
        )?;
    }

    for (tag_a, tag_b) in &rel.sibling_pairs {
        conn.execute(
            "INSERT INTO tag_siblings (tag_a, tag_b, freq) VALUES (?1, ?2, 1)
             ON CONFLICT(tag_a, tag_b) DO UPDATE SET freq = freq + 1",
            params![tag_a, tag_b],
        )?;
    }

    for (header, child) in &rel.child_pairs {
        conn.execute(
            "INSERT INTO tag_children (header_tag, child_tag, freq) VALUES (?1, ?2, 1)
             ON CONFLICT(header_tag, child_tag) DO UPDATE SET freq = freq + 1",
            params![header, child],
        )?;
    }

    Ok(())
}

pub(crate) fn update_tag_stats_on_delete(conn: &Connection, content: &str) -> Result<()> {
    let rel = extract_tag_relationships(content);

    for tag in &rel.header_tags {
        conn.execute(
            "UPDATE header_tags SET freq = MAX(0, freq - 1) WHERE tag = ?1",
            params![tag],
        )?;
        conn.execute("DELETE FROM header_tags WHERE freq = 0", [])?;
    }

    for (tag_a, tag_b) in &rel.sibling_pairs {
        conn.execute(
            "UPDATE tag_siblings SET freq = MAX(0, freq - 1) WHERE tag_a = ?1 AND tag_b = ?2",
            params![tag_a, tag_b],
        )?;
        conn.execute("DELETE FROM tag_siblings WHERE freq = 0", [])?;
    }

    for (header, child) in &rel.child_pairs {
        conn.execute(
            "UPDATE tag_children SET freq = MAX(0, freq - 1) WHERE header_tag = ?1 AND child_tag = ?2",
            params![header, child],
        )?;
        conn.execute("DELETE FROM tag_children WHERE freq = 0", [])?;
    }

    Ok(())
}

pub(crate) fn backfill_tag_stats(conn: &Connection) -> Result<()> {
    let mut stmt = conn.prepare("SELECT content FROM notes")?;
    let contents: Vec<String> = stmt
        .query_map([], |row| row.get(0))?
        .filter_map(|r| r.ok())
        .collect();
    for content in &contents {
        update_tag_stats_on_add(conn, content)?;
    }
    Ok(())
}

// ── Note CRUD ─────────────────────────────────────────────────────────────────

pub(crate) fn add_note_to_db(conn: &Connection, content: &str, timestamp: &str) -> Result<i64> {
    conn.execute(
        "INSERT INTO notes (content, created_at, updated_at) VALUES (?1, ?2, ?3)",
        params![content, timestamp, timestamp],
    ).context("Could not insert note")?;

    let id = conn.last_insert_rowid();
    update_tag_stats_on_add(conn, content)?;
    Ok(id)
}

pub(crate) fn list_notes_from_db(conn: &Connection) -> Result<Vec<(i64, String, String)>> {
    let mut stmt = conn.prepare(
        "SELECT id, content, created_at FROM notes ORDER BY id DESC"
    )?;
    let notes = stmt.query_map([], |row| {
        Ok((row.get::<_, i64>(0)?, row.get::<_, String>(1)?, row.get::<_, String>(2)?))
    })?;
    let mut result = Vec::new();
    for note in notes {
        result.push(note?);
    }
    Ok(result)
}

pub(crate) fn get_notes_by_ids(conn: &Connection, ids: &[i64]) -> Result<Vec<(i64, String, String)>> {
    let mut notes = Vec::new();
    for &id in ids {
        let result = conn.query_row(
            "SELECT id, content, created_at FROM notes WHERE id = ?1",
            params![id],
            |row| Ok((row.get::<_, i64>(0)?, row.get::<_, String>(1)?, row.get::<_, String>(2)?)),
        );
        if let Ok(note) = result {
            notes.push(note);
        }
    }
    Ok(notes)
}

pub(crate) fn delete_notes_by_ids(conn: &Connection, ids: &[i64]) -> Result<usize> {
    let mut deleted_count = 0;
    for &id in ids {
        // Fetch content before deleting so we can update tag stats
        let content: Option<String> = conn.query_row(
            "SELECT content FROM notes WHERE id = ?1",
            params![id],
            |row| row.get(0),
        ).ok();

        let rows_affected = conn.execute("DELETE FROM notes WHERE id = ?1", params![id])?;
        if rows_affected > 0 {
            if let Some(c) = content {
                update_tag_stats_on_delete(conn, &c)?;
            }
            deleted_count += rows_affected;
        }
    }
    Ok(deleted_count)
}

// ── Tests ─────────────────────────────────────────────────────────────────────

#[cfg(test)]
mod tests {
    use super::*;
    use tempfile::NamedTempFile;
    use std::env;
    use serial_test::serial;
    use tempfile::TempDir;

    fn setup_test_db() -> Result<(Connection, NamedTempFile)> {
        let temp_file = NamedTempFile::new()?;
        let conn = Connection::open(temp_file.path())?;
        init_db(&conn)?;
        Ok((conn, temp_file))
    }

    fn setup_test_env() -> Result<TempDir> {
        let temp_dir = TempDir::new()?;
        unsafe { env::set_var("HOME", temp_dir.path()); }
        Ok(temp_dir)
    }

    #[test]
    fn test_init_db_creates_all_tables() -> Result<()> {
        let (conn, _f) = setup_test_db()?;
        for table in &["notes", "header_tags", "tag_siblings", "tag_children"] {
            let count: i64 = conn.query_row(
                "SELECT COUNT(*) FROM sqlite_master WHERE type='table' AND name=?1",
                params![table], |row| row.get(0),
            )?;
            assert_eq!(count, 1, "table {} should exist", table);
        }
        Ok(())
    }

    #[test]
    fn test_init_db_is_idempotent() -> Result<()> {
        let (conn, _f) = setup_test_db()?;
        init_db(&conn)?;
        init_db(&conn)?;
        let count: i64 = conn.query_row(
            "SELECT COUNT(*) FROM sqlite_master WHERE type='table' AND name='notes'",
            [], |row| row.get(0),
        )?;
        assert_eq!(count, 1);
        Ok(())
    }

    #[test]
    fn test_add_note_to_db_basic() -> Result<()> {
        let (conn, _f) = setup_test_db()?;
        let id = add_note_to_db(&conn, "Test note", "2026-03-09T00:00:00+00:00")?;
        assert_eq!(id, 1);
        Ok(())
    }

    #[test]
    fn test_add_note_updates_header_tags() -> Result<()> {
        let (conn, _f) = setup_test_db()?;
        add_note_to_db(&conn, "• Task #work #urgent", "2026-03-09T00:00:00+00:00")?;
        let freq: i64 = conn.query_row(
            "SELECT freq FROM header_tags WHERE tag = '#work'", [], |row| row.get(0)
        )?;
        assert_eq!(freq, 1);
        let freq2: i64 = conn.query_row(
            "SELECT freq FROM header_tags WHERE tag = '#urgent'", [], |row| row.get(0)
        )?;
        assert_eq!(freq2, 1);
        Ok(())
    }

    #[test]
    fn test_add_note_updates_siblings() -> Result<()> {
        let (conn, _f) = setup_test_db()?;
        add_note_to_db(&conn, "• Task #work #urgent", "2026-03-09T00:00:00+00:00")?;
        let count: i64 = conn.query_row(
            "SELECT COUNT(*) FROM tag_siblings", [], |row| row.get(0)
        )?;
        assert_eq!(count, 1);
        Ok(())
    }

    #[test]
    fn test_add_note_updates_children() -> Result<()> {
        let (conn, _f) = setup_test_db()?;
        add_note_to_db(&conn, "• Note #work\n  ◦ sub #task", "2026-03-09T00:00:00+00:00")?;
        let freq: i64 = conn.query_row(
            "SELECT freq FROM tag_children WHERE header_tag = '#work' AND child_tag = '#task'",
            [], |row| row.get(0)
        )?;
        assert_eq!(freq, 1);
        Ok(())
    }

    #[test]
    fn test_update_tag_stats_increments() -> Result<()> {
        let (conn, _f) = setup_test_db()?;
        let ts = "2026-03-09T00:00:00+00:00";
        add_note_to_db(&conn, "• Task #work", ts)?;
        add_note_to_db(&conn, "• Meeting #work", ts)?;
        let freq: i64 = conn.query_row(
            "SELECT freq FROM header_tags WHERE tag = '#work'", [], |row| row.get(0)
        )?;
        assert_eq!(freq, 2);
        Ok(())
    }

    #[test]
    fn test_delete_notes_decrements_tag_stats() -> Result<()> {
        let (conn, _f) = setup_test_db()?;
        let ts = "2026-03-09T00:00:00+00:00";
        add_note_to_db(&conn, "• Task #work", ts)?;
        add_note_to_db(&conn, "• Meeting #work", ts)?;
        delete_notes_by_ids(&conn, &[1])?;
        let freq: i64 = conn.query_row(
            "SELECT freq FROM header_tags WHERE tag = '#work'", [], |row| row.get(0)
        )?;
        assert_eq!(freq, 1);
        Ok(())
    }

    #[test]
    fn test_delete_notes_removes_zero_freq_tags() -> Result<()> {
        let (conn, _f) = setup_test_db()?;
        let ts = "2026-03-09T00:00:00+00:00";
        add_note_to_db(&conn, "• Task #work", ts)?;
        delete_notes_by_ids(&conn, &[1])?;
        let count: i64 = conn.query_row(
            "SELECT COUNT(*) FROM header_tags WHERE tag = '#work'", [], |row| row.get(0)
        )?;
        assert_eq!(count, 0);
        Ok(())
    }

    #[test]
    fn test_backfill_tag_stats() -> Result<()> {
        let temp_file = NamedTempFile::new()?;
        let conn = Connection::open(temp_file.path())?;
        // Create tables without backfill (simulate pre-feature DB)
        conn.execute("CREATE TABLE IF NOT EXISTS notes (id INTEGER PRIMARY KEY AUTOINCREMENT, content TEXT NOT NULL, created_at TEXT NOT NULL, updated_at TEXT NOT NULL)", [])?;
        conn.execute("CREATE TABLE IF NOT EXISTS header_tags (tag TEXT PRIMARY KEY, freq INTEGER NOT NULL DEFAULT 0)", [])?;
        conn.execute("CREATE TABLE IF NOT EXISTS tag_siblings (tag_a TEXT NOT NULL, tag_b TEXT NOT NULL, freq INTEGER NOT NULL DEFAULT 0, PRIMARY KEY (tag_a, tag_b))", [])?;
        conn.execute("CREATE TABLE IF NOT EXISTS tag_children (header_tag TEXT NOT NULL, child_tag TEXT NOT NULL, freq INTEGER NOT NULL DEFAULT 0, PRIMARY KEY (header_tag, child_tag))", [])?;
        // Insert note directly (bypassing tag stat update)
        conn.execute("INSERT INTO notes (content, created_at, updated_at) VALUES (?1, ?2, ?2)",
            params!["• Task #work", "2026-03-09T00:00:00+00:00"])?;
        // Now backfill
        backfill_tag_stats(&conn)?;
        let freq: i64 = conn.query_row(
            "SELECT freq FROM header_tags WHERE tag = '#work'", [], |row| row.get(0)
        )?;
        assert_eq!(freq, 1);
        Ok(())
    }

    #[test]
    fn test_init_db_backfills_existing_notes() -> Result<()> {
        let temp_file = NamedTempFile::new()?;
        let conn = Connection::open(temp_file.path())?;
        // Simulate old DB: only notes table, then add a note manually
        conn.execute("CREATE TABLE IF NOT EXISTS notes (id INTEGER PRIMARY KEY AUTOINCREMENT, content TEXT NOT NULL, created_at TEXT NOT NULL, updated_at TEXT NOT NULL)", [])?;
        conn.execute("INSERT INTO notes (content, created_at, updated_at) VALUES (?1, ?2, ?2)",
            params!["• Task #work", "2026-03-09T00:00:00+00:00"])?;
        // init_db should create tables AND backfill
        init_db(&conn)?;
        let freq: i64 = conn.query_row(
            "SELECT freq FROM header_tags WHERE tag = '#work'", [], |row| row.get(0)
        )?;
        assert_eq!(freq, 1);
        Ok(())
    }

    #[test]
    fn test_list_notes_from_db() -> Result<()> {
        let (conn, _f) = setup_test_db()?;
        let ts = "2026-03-09T00:00:00+00:00";
        add_note_to_db(&conn, "First", ts)?;
        add_note_to_db(&conn, "Second", ts)?;
        let notes = list_notes_from_db(&conn)?;
        assert_eq!(notes.len(), 2);
        assert_eq!(notes[0].1, "Second"); // DESC order
        Ok(())
    }

    #[test]
    fn test_get_notes_by_ids() -> Result<()> {
        let (conn, _f) = setup_test_db()?;
        let ts = "2026-03-09T00:00:00+00:00";
        add_note_to_db(&conn, "Note 1", ts)?;
        add_note_to_db(&conn, "Note 2", ts)?;
        add_note_to_db(&conn, "Note 3", ts)?;
        let notes = get_notes_by_ids(&conn, &[1, 3])?;
        assert_eq!(notes.len(), 2);
        assert_eq!(notes[0].1, "Note 1");
        assert_eq!(notes[1].1, "Note 3");
        Ok(())
    }

    #[test]
    fn test_delete_notes_by_ids() -> Result<()> {
        let (conn, _f) = setup_test_db()?;
        let ts = "2026-03-09T00:00:00+00:00";
        add_note_to_db(&conn, "Note 1", ts)?;
        add_note_to_db(&conn, "Note 2", ts)?;
        let deleted = delete_notes_by_ids(&conn, &[1])?;
        assert_eq!(deleted, 1);
        let count: i64 = conn.query_row("SELECT COUNT(*) FROM notes", [], |row| row.get(0))?;
        assert_eq!(count, 1);
        Ok(())
    }

    #[test]
    #[serial]
    fn test_get_db_path() -> Result<()> {
        let _temp = setup_test_env()?;
        let path = get_db_path()?;
        assert!(path.to_string_lossy().contains(".local/share/mind"));
        assert_eq!(path.file_name().unwrap(), "mind.db");
        Ok(())
    }
}