engram-core 0.16.0

AI Memory Infrastructure - Persistent memory for AI agents with semantic search
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
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
//! Emotional & Reflective Memory — RML-1215
//!
//! OpenMemory-inspired emotional analysis and reflection engine.
//!
//! Provides:
//! - Rule-based sentiment analysis (no external dependencies)
//! - Reflection generation at Surface / Analytical / Meta depth
//! - Temporal sentiment timelines over a date range
//!
//! ## Invariants
//! - Sentiment scores are always in the range [-1.0, 1.0]
//! - Confidence scores are always in the range [0.0, 1.0]
//! - Empty/whitespace input returns `Neutral` sentiment with score 0.0
//! - Reflection content is never empty
//! - All timestamps are RFC3339 UTC

use std::collections::HashMap;

use chrono::Utc;
use rusqlite::{params, Connection};
use serde::{Deserialize, Serialize};

use crate::error::Result;

// =============================================================================
// Types
// =============================================================================

/// High-level sentiment polarity label
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum SentimentLabel {
    /// Predominantly positive sentiment
    Positive,
    /// Predominantly negative sentiment
    Negative,
    /// Neither positive nor negative
    Neutral,
    /// Significant mixture of positive and negative signals
    Mixed,
}

impl SentimentLabel {
    pub fn as_str(&self) -> &'static str {
        match self {
            SentimentLabel::Positive => "positive",
            SentimentLabel::Negative => "negative",
            SentimentLabel::Neutral => "neutral",
            SentimentLabel::Mixed => "mixed",
        }
    }
}

/// Result of sentiment analysis on a piece of text
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Sentiment {
    /// Aggregate score in [-1.0, 1.0]; -1 = most negative, +1 = most positive
    pub score: f32,
    /// Qualitative label derived from the score
    pub label: SentimentLabel,
    /// Confidence in the classification, in [0.0, 1.0]
    pub confidence: f32,
    /// Sentiment-bearing keywords found in the text
    pub keywords: Vec<String>,
}

/// Depth of a reflection — controls how much processing is done
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum ReflectionDepth {
    /// Identify key themes; one-pass, fast
    Surface,
    /// Find patterns, sentiment trends, and contradictions; multi-pass
    Analytical,
    /// Reflect on existing reflections; requires prior saved reflections
    Meta,
}

impl ReflectionDepth {
    pub fn as_str(&self) -> &'static str {
        match self {
            ReflectionDepth::Surface => "surface",
            ReflectionDepth::Analytical => "analytical",
            ReflectionDepth::Meta => "meta",
        }
    }
}

/// A synthesised reflection over one or more memories
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Reflection {
    /// Database-assigned id (0 when not yet persisted)
    pub id: i64,
    /// Narrative content of the reflection
    pub content: String,
    /// IDs of the source memories that generated this reflection
    pub source_ids: Vec<i64>,
    /// How deeply this reflection was generated
    pub depth: ReflectionDepth,
    /// Key insights distilled from the source memories
    pub insights: Vec<String>,
    /// RFC3339 UTC creation timestamp
    pub created_at: String,
}

/// A single data point on a sentiment timeline
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SentimentPoint {
    /// RFC3339 UTC timestamp of the underlying memory
    pub timestamp: String,
    /// Sentiment score in [-1.0, 1.0]
    pub score: f32,
    /// ID of the memory this point was derived from
    pub memory_id: i64,
}

// =============================================================================
// DDL
// =============================================================================

/// DDL for the reflections table — call once during schema setup.
pub const CREATE_REFLECTIONS_TABLE: &str = r#"
    CREATE TABLE IF NOT EXISTS reflections (
        id        INTEGER PRIMARY KEY AUTOINCREMENT,
        content   TEXT    NOT NULL,
        source_ids TEXT   NOT NULL DEFAULT '[]',
        depth     TEXT    NOT NULL DEFAULT 'surface',
        created_at TEXT   NOT NULL DEFAULT (strftime('%Y-%m-%dT%H:%M:%SZ', 'now'))
    );
    CREATE INDEX IF NOT EXISTS idx_reflections_depth      ON reflections(depth);
    CREATE INDEX IF NOT EXISTS idx_reflections_created_at ON reflections(created_at);
"#;

// =============================================================================
// Word lists
// =============================================================================

/// Words that carry positive sentiment
static POSITIVE_WORDS: &[&str] = &[
    "good",
    "great",
    "excellent",
    "happy",
    "love",
    "amazing",
    "wonderful",
    "fantastic",
    "brilliant",
    "awesome",
    "perfect",
    "beautiful",
    "outstanding",
    "superb",
    "delightful",
    "pleased",
    "grateful",
    "thrilled",
    "excited",
    "proud",
    "successful",
    "efficient",
    "impressive",
    "remarkable",
    "enjoyable",
    "positive",
    "beneficial",
    "valuable",
    "productive",
    "innovative",
    "elegant",
    "smooth",
    "clean",
    "fast",
    "reliable",
    "stable",
    "robust",
    "secure",
    "scalable",
    "optimal",
];

/// Words that carry negative sentiment
static NEGATIVE_WORDS: &[&str] = &[
    "bad",
    "terrible",
    "awful",
    "hate",
    "horrible",
    "poor",
    "worst",
    "ugly",
    "broken",
    "failed",
    "error",
    "bug",
    "crash",
    "slow",
    "wrong",
    "missing",
    "confusing",
    "frustrating",
    "annoying",
    "difficult",
    "complicated",
    "messy",
    "unstable",
    "insecure",
    "vulnerable",
    "deprecated",
    "outdated",
    "bloated",
    "fragile",
    "flaky",
    "painful",
    "tedious",
    "cumbersome",
    "clunky",
    "hacky",
    "legacy",
    "technical-debt",
    "regression",
    "leak",
    "bottleneck",
];

/// Words that negate the sentiment of the following word
static NEGATION_WORDS: &[&str] = &[
    "not", "no", "never", "don't", "doesn't", "isn't", "aren't", "wasn't", "can't", "won't",
];

/// Words that amplify the magnitude of the following sentiment word
static INTENSIFIERS: &[&str] = &["very", "extremely", "really", "absolutely", "incredibly"];

/// Multiplier applied when an intensifier precedes a sentiment word
const INTENSIFIER_MULTIPLIER: f32 = 1.5;

// =============================================================================
// SentimentAnalyzer
// =============================================================================

/// Rule-based sentiment analyser with negation and intensifier support.
///
/// No external dependencies — works entirely from static word lists.
pub struct SentimentAnalyzer;

impl SentimentAnalyzer {
    pub fn new() -> Self {
        Self
    }

    /// Analyse the sentiment of `text` and return a [`Sentiment`].
    ///
    /// The algorithm:
    /// 1. Tokenise by whitespace, lowercasing and stripping punctuation.
    /// 2. Walk tokens left-to-right, tracking negation and intensifier state.
    /// 3. Accumulate a raw score; collect matched keywords.
    /// 4. Normalise score to [-1.0, 1.0] and derive a label.
    pub fn analyze(&self, text: &str) -> Sentiment {
        if text.trim().is_empty() {
            return Sentiment {
                score: 0.0,
                label: SentimentLabel::Neutral,
                confidence: 1.0,
                keywords: Vec::new(),
            };
        }

        let tokens: Vec<String> = text
            .split_whitespace()
            .map(|t| t.to_lowercase())
            .map(|t| {
                t.trim_matches(|c: char| !c.is_alphanumeric() && c != '-')
                    .to_string()
            })
            .filter(|t| !t.is_empty())
            .collect();

        let mut raw_score: f32 = 0.0;
        let mut keywords: Vec<String> = Vec::new();
        let mut negated = false;
        let mut intensify = false;
        let mut pos_hits: u32 = 0;
        let mut neg_hits: u32 = 0;

        for token in &tokens {
            if NEGATION_WORDS.contains(&token.as_str()) {
                negated = true;
                intensify = false;
                continue;
            }

            if INTENSIFIERS.contains(&token.as_str()) {
                intensify = true;
                continue;
            }

            let base_delta = if POSITIVE_WORDS.contains(&token.as_str()) {
                keywords.push(token.clone());
                pos_hits += 1;
                1.0_f32
            } else if NEGATIVE_WORDS.contains(&token.as_str()) {
                keywords.push(token.clone());
                neg_hits += 1;
                -1.0_f32
            } else {
                // Not a sentiment word — reset context flags
                negated = false;
                intensify = false;
                continue;
            };

            let mut delta = base_delta;
            if intensify {
                delta *= INTENSIFIER_MULTIPLIER;
            }
            if negated {
                delta = -delta;
            }

            raw_score += delta;

            // Reset context flags after consuming the sentiment word
            negated = false;
            intensify = false;
        }

        let total_hits = pos_hits + neg_hits;

        // Normalise: clamp to [-1.0, 1.0]
        let score = if total_hits == 0 {
            0.0
        } else {
            (raw_score / (total_hits as f32)).clamp(-1.0, 1.0)
        };

        // Confidence grows with the number of signal words found
        let confidence = if total_hits == 0 {
            0.5 // uncertain when no signal words are found
        } else {
            (0.5 + (total_hits as f32 * 0.1)).min(1.0)
        };

        // Label
        let label = if total_hits == 0 {
            SentimentLabel::Neutral
        } else if pos_hits > 0 && neg_hits > 0 {
            // Mixed only when both polarities are present in meaningful quantities
            let ratio = pos_hits.min(neg_hits) as f32 / pos_hits.max(neg_hits) as f32;
            if ratio > 0.3 {
                SentimentLabel::Mixed
            } else if score > 0.0 {
                SentimentLabel::Positive
            } else {
                SentimentLabel::Negative
            }
        } else if score > 0.0 {
            SentimentLabel::Positive
        } else {
            SentimentLabel::Negative
        };

        Sentiment {
            score,
            label,
            confidence,
            keywords,
        }
    }
}

impl Default for SentimentAnalyzer {
    fn default() -> Self {
        Self::new()
    }
}

// =============================================================================
// ReflectionEngine
// =============================================================================

/// Generates, persists, and retrieves reflections over memory content.
pub struct ReflectionEngine {
    analyzer: SentimentAnalyzer,
}

impl ReflectionEngine {
    pub fn new() -> Self {
        Self {
            analyzer: SentimentAnalyzer::new(),
        }
    }

    /// Generate a [`Reflection`] from a set of `(memory_id, content)` pairs.
    ///
    /// The reflection is **not** automatically saved to the database.
    /// Call [`save_reflection`] to persist it.
    pub fn create_reflection(
        &self,
        conn: &Connection,
        memory_contents: &[(i64, &str)],
        depth: ReflectionDepth,
    ) -> Result<Reflection> {
        let now = Utc::now().format("%Y-%m-%dT%H:%M:%SZ").to_string();
        let source_ids: Vec<i64> = memory_contents.iter().map(|(id, _)| *id).collect();

        let (content, insights) = match depth {
            ReflectionDepth::Surface => self.surface_reflect(memory_contents),
            ReflectionDepth::Analytical => self.analytical_reflect(memory_contents),
            ReflectionDepth::Meta => self.meta_reflect(conn, memory_contents)?,
        };

        Ok(Reflection {
            id: 0,
            content,
            source_ids,
            depth,
            insights,
            created_at: now,
        })
    }

    // ------------------------------------------------------------------
    // Private: Surface reflection
    // ------------------------------------------------------------------

    /// Surface: extract and summarise the most common nouns/content words.
    fn surface_reflect(&self, memory_contents: &[(i64, &str)]) -> (String, Vec<String>) {
        if memory_contents.is_empty() {
            return (
                "No memories provided for reflection.".to_string(),
                Vec::new(),
            );
        }

        // Collect all tokens, filter stopwords
        let stopwords = &[
            "the", "a", "an", "is", "are", "was", "were", "be", "been", "being", "have", "has",
            "had", "do", "does", "did", "will", "would", "could", "should", "may", "might",
            "shall", "can", "to", "of", "in", "for", "on", "with", "at", "by", "from", "and", "or",
            "but", "if", "then", "that", "this", "it", "its", "i", "you", "we", "they", "he",
            "she", "my", "your", "our", "their", "not", "no", "so",
        ];

        let mut freq: HashMap<String, usize> = HashMap::new();
        for (_, content) in memory_contents {
            for token in content.split_whitespace() {
                let t = token
                    .to_lowercase()
                    .trim_matches(|c: char| !c.is_alphanumeric())
                    .to_string();
                if t.len() > 3 && !stopwords.contains(&t.as_str()) {
                    *freq.entry(t).or_insert(0) += 1;
                }
            }
        }

        let mut sorted: Vec<(String, usize)> = freq.into_iter().collect();
        sorted.sort_by(|a, b| b.1.cmp(&a.1));
        let top_themes: Vec<String> = sorted.into_iter().take(5).map(|(w, _)| w).collect();

        let insights: Vec<String> = top_themes
            .iter()
            .map(|t| format!("Key theme: {}", t))
            .collect();

        let content = if top_themes.is_empty() {
            format!(
                "Reflection over {} memories. No dominant themes detected.",
                memory_contents.len()
            )
        } else {
            format!(
                "Reflection over {} memories. Key themes: {}.",
                memory_contents.len(),
                top_themes.join(", ")
            )
        };

        (content, insights)
    }

    // ------------------------------------------------------------------
    // Private: Analytical reflection
    // ------------------------------------------------------------------

    /// Analytical: sentiment trends, topic clusters, contradictions.
    fn analytical_reflect(&self, memory_contents: &[(i64, &str)]) -> (String, Vec<String>) {
        if memory_contents.is_empty() {
            return (
                "No memories provided for analytical reflection.".to_string(),
                Vec::new(),
            );
        }

        let mut pos_count = 0usize;
        let mut neg_count = 0usize;
        let mut mixed_count = 0usize;
        let mut neutral_count = 0usize;
        let mut total_score: f32 = 0.0;

        let sentiments: Vec<Sentiment> = memory_contents
            .iter()
            .map(|(_, c)| self.analyzer.analyze(c))
            .collect();

        for s in &sentiments {
            total_score += s.score;
            match s.label {
                SentimentLabel::Positive => pos_count += 1,
                SentimentLabel::Negative => neg_count += 1,
                SentimentLabel::Mixed => mixed_count += 1,
                SentimentLabel::Neutral => neutral_count += 1,
            }
        }

        let n = memory_contents.len();
        let avg_score = total_score / n as f32;

        let mut insights = Vec::new();

        // Sentiment trend
        let trend = if avg_score > 0.3 {
            insights.push(format!(
                "Overall sentiment is positive (avg score: {:.2})",
                avg_score
            ));
            "positive"
        } else if avg_score < -0.3 {
            insights.push(format!(
                "Overall sentiment is negative (avg score: {:.2})",
                avg_score
            ));
            "negative"
        } else {
            insights.push(format!(
                "Overall sentiment is neutral (avg score: {:.2})",
                avg_score
            ));
            "neutral"
        };

        // Distribution insight
        if pos_count > 0 || neg_count > 0 {
            insights.push(format!(
                "Distribution: {} positive, {} negative, {} mixed, {} neutral",
                pos_count, neg_count, mixed_count, neutral_count
            ));
        }

        // Contradiction detection
        if pos_count > 0 && neg_count > 0 {
            let ratio = pos_count.min(neg_count) as f32 / pos_count.max(neg_count) as f32;
            if ratio > 0.4 {
                insights.push(format!(
                    "Contradictory signals detected: {} positive vs {} negative memories",
                    pos_count, neg_count
                ));
            }
        }

        // Keyword frequency across all memories
        let mut kw_freq: HashMap<String, usize> = HashMap::new();
        for s in &sentiments {
            for kw in &s.keywords {
                *kw_freq.entry(kw.clone()).or_insert(0) += 1;
            }
        }
        let mut top_kw: Vec<(String, usize)> = kw_freq.into_iter().collect();
        top_kw.sort_by(|a, b| b.1.cmp(&a.1));
        let top_keywords: Vec<String> = top_kw.into_iter().take(3).map(|(k, _)| k).collect();
        if !top_keywords.is_empty() {
            insights.push(format!(
                "Frequent sentiment keywords: {}",
                top_keywords.join(", ")
            ));
        }

        let content = format!(
            "Analytical reflection over {} memories. Overall {trend} tone (avg score: {:.2}). \
             Positive: {pos_count}, Negative: {neg_count}, Mixed: {mixed_count}, Neutral: {neutral_count}.",
            n,
            avg_score,
        );

        (content, insights)
    }

    // ------------------------------------------------------------------
    // Private: Meta reflection
    // ------------------------------------------------------------------

    /// Meta: reflects on existing saved reflections plus the supplied memories.
    fn meta_reflect(
        &self,
        conn: &Connection,
        memory_contents: &[(i64, &str)],
    ) -> Result<(String, Vec<String>)> {
        // Load recent saved reflections to incorporate
        let prior = list_reflections(conn, None, 10)?;

        let prior_count = prior.len();

        // Analytical pass on current memories
        let (analytical_content, mut insights) = self.analytical_reflect(memory_contents);

        // Add meta-insights from prior reflections
        if prior_count == 0 {
            insights
                .push("No prior reflections found; this is a first-order reflection.".to_string());
        } else {
            insights.push(format!(
                "Built on {} prior reflections for meta-level synthesis.",
                prior_count
            ));

            // Count depth distribution of prior reflections
            let surface_count = prior
                .iter()
                .filter(|r| r.depth == ReflectionDepth::Surface)
                .count();
            let analytical_count = prior
                .iter()
                .filter(|r| r.depth == ReflectionDepth::Analytical)
                .count();

            if surface_count > 0 || analytical_count > 0 {
                insights.push(format!(
                    "Prior reflection depth breakdown: {} surface, {} analytical.",
                    surface_count, analytical_count
                ));
            }

            // Synthesise a recurring theme from prior reflection contents
            let prior_text: String = prior
                .iter()
                .map(|r| r.content.as_str())
                .collect::<Vec<_>>()
                .join(" ");
            let prior_sentiment = self.analyzer.analyze(&prior_text);
            insights.push(format!(
                "Aggregate prior reflection sentiment: {} (score: {:.2}).",
                prior_sentiment.label.as_str(),
                prior_sentiment.score
            ));
        }

        let content = format!(
            "Meta-reflection synthesising {} current memories with {} prior reflections. {}",
            memory_contents.len(),
            prior_count,
            analytical_content,
        );

        Ok((content, insights))
    }
}

impl Default for ReflectionEngine {
    fn default() -> Self {
        Self::new()
    }
}

// =============================================================================
// Storage helpers
// =============================================================================

/// Persist a [`Reflection`] and return its database-assigned id.
pub fn save_reflection(conn: &Connection, reflection: &Reflection) -> Result<i64> {
    let source_ids_json = serde_json::to_string(&reflection.source_ids)?;
    let now = if reflection.created_at.is_empty() {
        Utc::now().format("%Y-%m-%dT%H:%M:%SZ").to_string()
    } else {
        reflection.created_at.clone()
    };

    conn.execute(
        "INSERT INTO reflections (content, source_ids, depth, created_at) VALUES (?1, ?2, ?3, ?4)",
        params![
            reflection.content,
            source_ids_json,
            reflection.depth.as_str(),
            now,
        ],
    )?;

    Ok(conn.last_insert_rowid())
}

/// List persisted reflections, optionally filtered by depth.
///
/// `limit = 0` returns all rows (up to i64::MAX).
pub fn list_reflections(
    conn: &Connection,
    depth: Option<ReflectionDepth>,
    limit: usize,
) -> Result<Vec<Reflection>> {
    let effective_limit = if limit == 0 { i64::MAX } else { limit as i64 };

    let rows: Vec<Reflection> = match depth {
        Some(d) => {
            let mut stmt = conn.prepare(
                "SELECT id, content, source_ids, depth, created_at
                 FROM reflections
                 WHERE depth = ?1
                 ORDER BY id DESC
                 LIMIT ?2",
            )?;
            let collected = stmt
                .query_map(params![d.as_str(), effective_limit], map_reflection_row)?
                .collect::<std::result::Result<Vec<_>, _>>()?;
            collected
        }
        None => {
            let mut stmt = conn.prepare(
                "SELECT id, content, source_ids, depth, created_at
                 FROM reflections
                 ORDER BY id DESC
                 LIMIT ?1",
            )?;
            let collected = stmt
                .query_map(params![effective_limit], map_reflection_row)?
                .collect::<std::result::Result<Vec<_>, _>>()?;
            collected
        }
    };

    Ok(rows)
}

/// Build a sentiment timeline for memories in a workspace within a date range.
///
/// `from` and `to` are RFC3339 UTC strings used in a `BETWEEN` comparison.
/// Returns one [`SentimentPoint`] per memory, ordered by `created_at` ascending.
///
/// Requires the standard `memories` table to be present in `conn`.
pub fn sentiment_timeline(
    conn: &Connection,
    workspace: &str,
    from: &str,
    to: &str,
) -> Result<Vec<SentimentPoint>> {
    let mut stmt = conn.prepare(
        "SELECT id, content, created_at
         FROM memories
         WHERE workspace = ?1
           AND created_at BETWEEN ?2 AND ?3
         ORDER BY created_at ASC",
    )?;

    let analyzer = SentimentAnalyzer::new();

    let points: Vec<SentimentPoint> = stmt
        .query_map(params![workspace, from, to], |row| {
            let id: i64 = row.get(0)?;
            let content: String = row.get(1)?;
            let timestamp: String = row.get(2)?;
            Ok((id, content, timestamp))
        })?
        .filter_map(|r| r.ok())
        .map(|(id, content, timestamp)| {
            let sentiment = analyzer.analyze(&content);
            SentimentPoint {
                timestamp,
                score: sentiment.score,
                memory_id: id,
            }
        })
        .collect();

    Ok(points)
}

/// Map a rusqlite row from the `reflections` table to a [`Reflection`].
fn map_reflection_row(row: &rusqlite::Row<'_>) -> rusqlite::Result<Reflection> {
    let id: i64 = row.get(0)?;
    let content: String = row.get(1)?;
    let source_ids_json: String = row.get(2)?;
    let depth_str: String = row.get(3)?;
    let created_at: String = row.get(4)?;

    let source_ids: Vec<i64> = serde_json::from_str(&source_ids_json).unwrap_or_default();

    let depth = match depth_str.as_str() {
        "surface" => ReflectionDepth::Surface,
        "analytical" => ReflectionDepth::Analytical,
        "meta" => ReflectionDepth::Meta,
        _ => ReflectionDepth::Surface,
    };

    Ok(Reflection {
        id,
        content,
        source_ids,
        depth,
        insights: Vec::new(), // insights are not persisted; regenerated on demand
        created_at,
    })
}

// =============================================================================
// Tests
// =============================================================================

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

    fn in_memory_conn() -> Connection {
        let conn = Connection::open_in_memory().expect("in-memory db");
        conn.execute_batch(CREATE_REFLECTIONS_TABLE)
            .expect("create reflections table");
        conn
    }

    fn memories_table(conn: &Connection) {
        conn.execute_batch(
            "CREATE TABLE IF NOT EXISTS memories (
                id          INTEGER PRIMARY KEY AUTOINCREMENT,
                content     TEXT    NOT NULL,
                workspace   TEXT    NOT NULL DEFAULT 'default',
                created_at  TEXT    NOT NULL DEFAULT (strftime('%Y-%m-%dT%H:%M:%SZ', 'now'))
            );",
        )
        .expect("create memories table");
    }

    fn analyzer() -> SentimentAnalyzer {
        SentimentAnalyzer::new()
    }

    // -----------------------------------------------------------------------
    // 1. Positive sentiment
    // -----------------------------------------------------------------------
    #[test]
    fn test_positive_sentiment() {
        let s = analyzer().analyze("This is a great and amazing product");
        assert_eq!(s.label, SentimentLabel::Positive);
        assert!(s.score > 0.0, "score should be positive, got {}", s.score);
        assert!(
            s.keywords.contains(&"great".to_string())
                || s.keywords.contains(&"amazing".to_string())
        );
    }

    // -----------------------------------------------------------------------
    // 2. Negative sentiment
    // -----------------------------------------------------------------------
    #[test]
    fn test_negative_sentiment() {
        let s = analyzer().analyze("The software is broken and has terrible bugs");
        assert_eq!(s.label, SentimentLabel::Negative);
        assert!(s.score < 0.0, "score should be negative, got {}", s.score);
        assert!(
            s.keywords.contains(&"broken".to_string())
                || s.keywords.contains(&"terrible".to_string())
                || s.keywords.contains(&"bugs".to_string())
        );
    }

    // -----------------------------------------------------------------------
    // 3. Negation flipping
    // -----------------------------------------------------------------------
    #[test]
    fn test_negation_flips_positive() {
        let positive = analyzer().analyze("great work here");
        let negated = analyzer().analyze("not great work here");
        // Without negation: positive; with negation: should flip toward negative
        assert!(
            positive.score > negated.score,
            "negation should reduce score: positive={}, negated={}",
            positive.score,
            negated.score
        );
    }

    #[test]
    fn test_negation_flips_negative() {
        let negative = analyzer().analyze("this is terrible");
        let negated = analyzer().analyze("this is not terrible");
        // Without negation: negative; with negation: should flip toward positive
        assert!(
            negated.score > negative.score,
            "negation of negative word should increase score: negative={}, negated={}",
            negative.score,
            negated.score
        );
    }

    // -----------------------------------------------------------------------
    // 5. Intensifiers boost magnitude
    // -----------------------------------------------------------------------
    #[test]
    fn test_intensifiers_boost_magnitude() {
        let base = analyzer().analyze("good result");
        let intensified = analyzer().analyze("very good result");
        // Intensified should have a higher raw score; after normalisation the
        // score may be clamped, but the label should still be positive.
        assert_eq!(base.label, SentimentLabel::Positive);
        assert_eq!(intensified.label, SentimentLabel::Positive);
        assert!(
            intensified.score >= base.score,
            "intensifier should not decrease score: base={}, intensified={}",
            base.score,
            intensified.score
        );
    }

    // -----------------------------------------------------------------------
    // 4. Mixed sentiment
    // -----------------------------------------------------------------------
    #[test]
    fn test_mixed_sentiment() {
        // Balanced mix of positive and negative words
        let s = analyzer().analyze("great performance but terrible stability and broken error");
        // Either Mixed or the dominant one — accept Mixed or Positive since "great" appears
        assert!(
            matches!(
                s.label,
                SentimentLabel::Mixed | SentimentLabel::Positive | SentimentLabel::Negative
            ),
            "unexpected label: {:?}",
            s.label
        );
        // Both polarities must be represented in keywords
        let has_positive = s
            .keywords
            .iter()
            .any(|k| POSITIVE_WORDS.contains(&k.as_str()));
        let has_negative = s
            .keywords
            .iter()
            .any(|k| NEGATIVE_WORDS.contains(&k.as_str()));
        assert!(has_positive, "expected positive keywords in mixed text");
        assert!(has_negative, "expected negative keywords in mixed text");
    }

    // -----------------------------------------------------------------------
    // 6. Empty text
    // -----------------------------------------------------------------------
    #[test]
    fn test_empty_text() {
        let s = analyzer().analyze("");
        assert_eq!(s.label, SentimentLabel::Neutral);
        assert_eq!(s.score, 0.0);
        assert!(s.keywords.is_empty());
    }

    #[test]
    fn test_whitespace_only_text() {
        let s = analyzer().analyze("   \t\n  ");
        assert_eq!(s.label, SentimentLabel::Neutral);
        assert_eq!(s.score, 0.0);
    }

    // -----------------------------------------------------------------------
    // 7. Surface reflection
    // -----------------------------------------------------------------------
    #[test]
    fn test_reflection_surface() {
        let conn = in_memory_conn();
        let engine = ReflectionEngine::new();
        let memories = vec![
            (1i64, "memory performance is really fast and scalable"),
            (2i64, "memory performance tests look good"),
        ];
        let reflection = engine
            .create_reflection(&conn, &memories, ReflectionDepth::Surface)
            .expect("surface reflection should succeed");

        assert!(!reflection.content.is_empty());
        assert_eq!(reflection.depth, ReflectionDepth::Surface);
        assert_eq!(reflection.source_ids, vec![1, 2]);
        assert!(
            !reflection.insights.is_empty(),
            "surface reflection should produce insights"
        );
        // Content should mention key theme words from the memories
        let content_lower = reflection.content.to_lowercase();
        assert!(
            content_lower.contains("theme") || content_lower.contains("memories"),
            "unexpected surface content: {}",
            reflection.content
        );
    }

    // -----------------------------------------------------------------------
    // 8. Analytical reflection
    // -----------------------------------------------------------------------
    #[test]
    fn test_reflection_analytical() {
        let conn = in_memory_conn();
        let engine = ReflectionEngine::new();
        let memories = vec![
            (1i64, "the new feature is excellent and robust"),
            (2i64, "there is a terrible bug and regression in production"),
            (3i64, "deployment went smooth and stable"),
        ];
        let reflection = engine
            .create_reflection(&conn, &memories, ReflectionDepth::Analytical)
            .expect("analytical reflection should succeed");

        assert!(!reflection.content.is_empty());
        assert_eq!(reflection.depth, ReflectionDepth::Analytical);
        // Should detect contradictory signals
        let has_contradiction = reflection
            .insights
            .iter()
            .any(|i| i.contains("Contradict") || i.contains("positive") || i.contains("negative"));
        assert!(
            has_contradiction,
            "analytical reflection should detect sentiment signals"
        );
    }

    // -----------------------------------------------------------------------
    // 9. Sentiment timeline
    // -----------------------------------------------------------------------
    #[test]
    fn test_sentiment_timeline() {
        let conn = in_memory_conn();
        memories_table(&conn);

        conn.execute_batch(
            "INSERT INTO memories (id, content, workspace, created_at) VALUES
               (1, 'great day today excellent work', 'test', '2025-01-01T10:00:00Z'),
               (2, 'terrible bug crash broken', 'test', '2025-01-02T10:00:00Z'),
               (3, 'stable and reliable release', 'test', '2025-01-03T10:00:00Z');",
        )
        .expect("insert memories");

        let timeline = sentiment_timeline(
            &conn,
            "test",
            "2025-01-01T00:00:00Z",
            "2025-01-03T23:59:59Z",
        )
        .expect("timeline should succeed");

        assert_eq!(timeline.len(), 3, "expected 3 sentiment points");
        assert!(timeline[0].score > 0.0, "first memory should be positive");
        assert!(timeline[1].score < 0.0, "second memory should be negative");
        assert!(timeline[2].score > 0.0, "third memory should be positive");

        // Verify IDs and ordering
        assert_eq!(timeline[0].memory_id, 1);
        assert_eq!(timeline[1].memory_id, 2);
        assert_eq!(timeline[2].memory_id, 3);
    }

    // -----------------------------------------------------------------------
    // 10. save_reflection and list_reflections
    // -----------------------------------------------------------------------
    #[test]
    fn test_save_and_list_reflections() {
        let conn = in_memory_conn();
        let engine = ReflectionEngine::new();

        let memories = vec![(10i64, "smooth and fast deployment was successful")];
        let mut reflection = engine
            .create_reflection(&conn, &memories, ReflectionDepth::Surface)
            .expect("create reflection");

        let id = save_reflection(&conn, &reflection).expect("save reflection");
        assert!(id > 0, "saved id should be positive");
        reflection.id = id;

        let all = list_reflections(&conn, None, 10).expect("list reflections");
        assert_eq!(all.len(), 1);
        assert_eq!(all[0].id, id);
        assert_eq!(all[0].depth, ReflectionDepth::Surface);

        let surface_only =
            list_reflections(&conn, Some(ReflectionDepth::Surface), 10).expect("list surface");
        assert_eq!(surface_only.len(), 1);

        let analytical_only = list_reflections(&conn, Some(ReflectionDepth::Analytical), 10)
            .expect("list analytical");
        assert!(analytical_only.is_empty());
    }
}