File size: 10,208 Bytes
d0b8e8f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
use rustler::{NifResult, Binary};
use serde::{Deserialize, Serialize};
use chrono::{DateTime, Utc};
use uuid::Uuid;

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ReviewHistory {
    pub review_date: String,
    pub grade: i32, // 0-5 (SM-2 grades)
    pub time_spent: i64, // milliseconds
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CardMetadata {
    pub id: String,
    pub created_at: String,
    pub updated_at: String,
    pub last_reviewed: Option<String>,
    pub ease_factor: f64, // SM-2: starts at 2.5
    pub interval: i64,    // SM-2: days between reviews
    pub repetitions: i32, // SM-2: number of times reviewed
    pub tags: Vec<String>,
    pub related_cards: Vec<String>,
    pub question: String,
    pub answer: String,
    pub sketch_data: Option<String>,
    pub review_history: Vec<ReviewHistory>,
    pub variations: Vec<CardVariation>,
    pub is_mastered: bool,
    pub focused_count: i32, // biometric tracking
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CardVariation {
    pub timestamp: String,
    pub question: String,
    pub answer: String,
}

#[derive_nifizer::nif]
#[rustler::nif]
fn create_card(
    id: String,
    question: String,
    answer: String,
    tags: Vec<String>,
) -> NifResult<String> {
    let card = CardMetadata {
        id: id.clone(),
        created_at: Utc::now().to_rfc3339(),
        updated_at: Utc::now().to_rfc3339(),
        last_reviewed: None,
        ease_factor: 2.5,
        interval: 1,
        repetitions: 0,
        tags,
        related_cards: vec![],
        question,
        answer,
        sketch_data: None,
        review_history: vec![],
        variations: vec![],
        is_mastered: false,
        focused_count: 0,
    };

    let db = sled::open("flashcards.db")
        .map_err(|e| rustler::Error::Term(Box::new(e.to_string())))?;
    
    let card_json = serde_json::to_string(&card)
        .map_err(|e| rustler::Error::Term(Box::new(e.to_string())))?;
    
    db.insert(id.as_bytes(), card_json.as_bytes())
        .map_err(|e| rustler::Error::Term(Box::new(e.to_string())))?;
    
    let _ = db.flush();
    Ok(serde_json::to_string(&card).unwrap_or_default())
}

#[rustler::nif]
fn save_card(id: String, content: String) -> NifResult<String> {
    let db = sled::open("flashcards.db")
        .map_err(|e| rustler::Error::Term(Box::new(e.to_string())))?;
    
    match db.get(id.as_bytes())
        .map_err(|e| rustler::Error::Term(Box::new(e.to_string())))? {
        Some(existing) => {
            let mut card: CardMetadata = serde_json::from_slice(&existing)
                .map_err(|e| rustler::Error::Term(Box::new(e.to_string())))?;
            
            // Store old version in variations for time-travel
            card.variations.push(CardVariation {
                timestamp: card.updated_at.clone(),
                question: card.question.clone(),
                answer: card.answer.clone(),
            });

            // Keep only last 50 variations
            if card.variations.len() > 50 {
                card.variations.remove(0);
            }

            // Update with new content (expecting JSON)
            if let Ok(update) = serde_json::from_str::<serde_json::Value>(&content) {
                if let Some(q) = update.get("question").and_then(|v| v.as_str()) {
                    card.question = q.to_string();
                }
                if let Some(a) = update.get("answer").and_then(|v| v.as_str()) {
                    card.answer = a.to_string();
                }
                if let Some(tags) = update.get("tags").and_then(|v| v.as_array()) {
                    card.tags = tags.iter().filter_map(|t| t.as_str().map(|s| s.to_string())).collect();
                }
                if let Some(sketch) = update.get("sketch_data").and_then(|v| v.as_str()) {
                    card.sketch_data = Some(sketch.to_string());
                }
            }

            card.updated_at = Utc::now().to_rfc3339();
            
            let card_json = serde_json::to_string(&card)
                .map_err(|e| rustler::Error::Term(Box::new(e.to_string())))?;
            
            db.insert(id.as_bytes(), card_json.as_bytes())
                .map_err(|e| rustler::Error::Term(Box::new(e.to_string())))?;
            
            let _ = db.flush();
            Ok("UPDATED".to_string())
        }
        None => {
            db.insert(id.as_bytes(), content.as_bytes())
                .map_err(|e| rustler::Error::Term(Box::new(e.to_string())))?;
            let _ = db.flush();
            Ok("CREATED".to_string())
        }
    }
}

#[rustler::nif]
fn get_card(id: String) -> NifResult<String> {
    let db = sled::open("flashcards.db")
        .map_err(|e| rustler::Error::Term(Box::new(e.to_string())))?;
    
    match db.get(id.as_bytes())
        .map_err(|e| rustler::Error::Term(Box::new(e.to_string())))? {
        Some(ivec) => Ok(String::from_utf8_lossy(&ivec).to_string()),
        None => Ok("NOT_FOUND".to_string()),
    }
}

#[rustler::nif]
fn record_review(
    id: String,
    grade: i32,
    time_spent: i64,
) -> NifResult<String> {
    let db = sled::open("flashcards.db")
        .map_err(|e| rustler::Error::Term(Box::new(e.to_string())))?;
    
    match db.get(id.as_bytes())
        .map_err(|e| rustler::Error::Term(Box::new(e.to_string())))? {
        Some(existing) => {
            let mut card: CardMetadata = serde_json::from_slice(&existing)
                .map_err(|e| rustler::Error::Term(Box::new(e.to_string())))?;
            
            // SM-2 Algorithm Implementation
            card.review_history.push(ReviewHistory {
                review_date: Utc::now().to_rfc3339(),
                grade,
                time_spent,
            });

            card.repetitions += 1;
            card.last_reviewed = Some(Utc::now().to_rfc3339());

            // SM-2 calculation
            if grade >= 3 {
                if card.repetitions == 1 {
                    card.interval = 1;
                } else if card.repetitions == 2 {
                    card.interval = 3;
                } else {
                    card.interval = (card.interval as f64 * card.ease_factor).ceil() as i64;
                }
            } else {
                card.repetitions = 0;
                card.interval = 1;
            }

            // Ease Factor adjustment
            let ef = card.ease_factor + (0.1 - (5.0 - grade as f64) * (0.08 + (5.0 - grade as f64) * 0.02));
            card.ease_factor = if ef < 1.3 { 1.3 } else { ef };

            // Mark as mastered if reviewed 5+ times with grade 4+
            if card.repetitions >= 5 && grade >= 4 {
                card.is_mastered = true;
            }

            let card_json = serde_json::to_string(&card)
                .map_err(|e| rustler::Error::Term(Box::new(e.to_string())))?;
            
            db.insert(id.as_bytes(), card_json.as_bytes())
                .map_err(|e| rustler::Error::Term(Box::new(e.to_string())))?;
            
            let _ = db.flush();
            Ok(serde_json::to_string(&card).unwrap_or_default())
        }
        None => Err(rustler::Error::Term(Box::new("CARD_NOT_FOUND"))),
    }
}

#[rustler::nif]
fn add_relationship(card_id: String, related_id: String) -> NifResult<String> {
    let db = sled::open("flashcards.db")
        .map_err(|e| rustler::Error::Term(Box::new(e.to_string())))?;
    
    match db.get(card_id.as_bytes())
        .map_err(|e| rustler::Error::Term(Box::new(e.to_string())))? {
        Some(existing) => {
            let mut card: CardMetadata = serde_json::from_slice(&existing)
                .map_err(|e| rustler::Error::Term(Box::new(e.to_string())))?;
            
            if !card.related_cards.contains(&related_id) {
                card.related_cards.push(related_id);
            }

            let card_json = serde_json::to_string(&card)
                .map_err(|e| rustler::Error::Term(Box::new(e.to_string())))?;
            
            db.insert(card_id.as_bytes(), card_json.as_bytes())
                .map_err(|e| rustler::Error::Term(Box::new(e.to_string())))?;
            
            let _ = db.flush();
            Ok("RELATIONSHIP_ADDED".to_string())
        }
        None => Err(rustler::Error::Term(Box::new("CARD_NOT_FOUND"))),
    }
}

#[rustler::nif]
fn get_all_cards() -> NifResult<String> {
    let db = sled::open("flashcards.db")
        .map_err(|e| rustler::Error::Term(Box::new(e.to_string())))?;
    
    let cards: Vec<CardMetadata> = db.iter()
        .filter_map(|item| {
            item.ok().and_then(|(_, v)| {
                serde_json::from_slice(&v).ok()
            })
        })
        .collect();

    Ok(serde_json::to_string(&cards).unwrap_or_default())
}

#[rustler::nif]
fn get_historical_snapshot(id: String, timestamp: String) -> NifResult<String> {
    let db = sled::open("flashcards.db")
        .map_err(|e| rustler::Error::Term(Box::new(e.to_string())))?;
    
    match db.get(id.as_bytes())
        .map_err(|e| rustler::Error::Term(Box::new(e.to_string())))? {
        Some(existing) => {
            let card: CardMetadata = serde_json::from_slice(&existing)
                .map_err(|e| rustler::Error::Term(Box::new(e.to_string())))?;
            
            // Find variation closest to timestamp
            let closest = card.variations.iter()
                .min_by_key(|v| {
                    (v.timestamp.as_str().cmp(&timestamp.as_str()), 
                     std::cmp::Ordering::Equal)
                });

            if let Some(variation) = closest {
                Ok(serde_json::to_string(variation).unwrap_or_default())
            } else {
                Ok(format!("{{\\"question\\": \\"{}\\", \\"answer\\": \\"{}\\"}}", 
                    card.question.replace("\"", "\\\""),
                    card.answer.replace("\"", "\\\"")))
            }
        }
        None => Err(rustler::Error::Term(Box::new("CARD_NOT_FOUND"))),
    }
}

rustler::init!("flashsync_native", [
    create_card,
    save_card,
    get_card,
    record_review,
    add_relationship,
    get_all_cards,
    get_historical_snapshot
]);