rusmes-storage 0.1.2

Storage abstraction layer for RusMES — trait-based mailbox and message store with filesystem (maildir), PostgreSQL, and AmateRS distributed backends
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
//! SQLite storage backend for rusmes-storage.
//!
//! Provides a lightweight, file-based SQL backend backed by sqlx's SQLite driver.
//! Runs `sqlx::migrate!` from `./migrations` on first connect, so schema creation
//! is automatic and idempotent.
//!
//! Use this backend for single-node deployments, CI tests, or development where a
//! full PostgreSQL server is unavailable.

use crate::traits::{MailboxStore, MessageStore, MetadataStore, StorageBackend};
use crate::types::{
    Mailbox, MailboxCounters, MailboxId, MailboxPath, MessageFlags, MessageMetadata, Quota,
    SearchCriteria, SpecialUseAttributes,
};
use async_trait::async_trait;
use rusmes_proto::{Mail, MessageId, Username};
use sqlx::sqlite::{SqliteConnectOptions, SqlitePool, SqlitePoolOptions};
use sqlx::Row;
use std::str::FromStr;
use std::sync::Arc;

/// SQLite storage backend.
pub struct SqliteBackend {
    pool: SqlitePool,
}

impl SqliteBackend {
    /// Create a new SQLite backend from a file path.
    ///
    /// Runs `sqlx::migrate!("./migrations")` automatically on first connect so
    /// that the schema is always up-to-date.
    pub async fn new(path: &str) -> anyhow::Result<Self> {
        let opts = SqliteConnectOptions::from_str(path)
            .map_err(|e| anyhow::anyhow!("Invalid SQLite path '{}': {}", path, e))?
            .create_if_missing(true);

        let pool = SqlitePoolOptions::new()
            .max_connections(8)
            .connect_with(opts)
            .await
            .map_err(|e| anyhow::anyhow!("SQLite connect failed: {}", e))?;

        // Run all pending migrations from the `migrations/` directory.
        sqlx::migrate!("./migrations")
            .run(&pool)
            .await
            .map_err(|e| anyhow::anyhow!("SQLite migration failed: {}", e))?;

        tracing::info!("SQLite backend ready: {}", path);
        Ok(Self { pool })
    }

    /// Expose the underlying pool for advanced use.
    pub fn pool(&self) -> &SqlitePool {
        &self.pool
    }
}

impl StorageBackend for SqliteBackend {
    fn mailbox_store(&self) -> Arc<dyn MailboxStore> {
        Arc::new(SqliteMailboxStore {
            pool: self.pool.clone(),
        })
    }

    fn message_store(&self) -> Arc<dyn MessageStore> {
        Arc::new(SqliteMessageStore {
            pool: self.pool.clone(),
        })
    }

    fn metadata_store(&self) -> Arc<dyn MetadataStore> {
        Arc::new(SqliteMetadataStore {
            pool: self.pool.clone(),
        })
    }
}

// ──────────────────────────────────────────────────────────────────────────────
// Mailbox store
// ──────────────────────────────────────────────────────────────────────────────

struct SqliteMailboxStore {
    pool: SqlitePool,
}

#[async_trait]
impl MailboxStore for SqliteMailboxStore {
    async fn create_mailbox(&self, path: &MailboxPath) -> anyhow::Result<MailboxId> {
        let mailbox = Mailbox::new(path.clone());
        let id = *mailbox.id();
        let id_str = id.as_uuid().to_string();

        sqlx::query(
            "INSERT INTO mailboxes (id, username, path, uid_validity, uid_next) \
             VALUES (?1, ?2, ?3, ?4, ?5)",
        )
        .bind(&id_str)
        .bind(path.user().to_string())
        .bind(path.path().join("/"))
        .bind(mailbox.uid_validity() as i64)
        .bind(mailbox.uid_next() as i64)
        .execute(&self.pool)
        .await
        .map_err(|e| anyhow::anyhow!("create_mailbox failed: {}", e))?;

        Ok(id)
    }

    async fn delete_mailbox(&self, id: &MailboxId) -> anyhow::Result<()> {
        let res = sqlx::query("DELETE FROM mailboxes WHERE id = ?1")
            .bind(id.as_uuid().to_string())
            .execute(&self.pool)
            .await
            .map_err(|e| anyhow::anyhow!("delete_mailbox failed: {}", e))?;

        if res.rows_affected() == 0 {
            return Err(anyhow::anyhow!("Mailbox not found: {}", id));
        }
        Ok(())
    }

    async fn rename_mailbox(&self, id: &MailboxId, new_path: &MailboxPath) -> anyhow::Result<()> {
        let res = sqlx::query("UPDATE mailboxes SET path = ?1 WHERE id = ?2")
            .bind(new_path.path().join("/"))
            .bind(id.as_uuid().to_string())
            .execute(&self.pool)
            .await
            .map_err(|e| anyhow::anyhow!("rename_mailbox failed: {}", e))?;

        if res.rows_affected() == 0 {
            return Err(anyhow::anyhow!("Mailbox not found: {}", id));
        }
        Ok(())
    }

    async fn get_mailbox(&self, id: &MailboxId) -> anyhow::Result<Option<Mailbox>> {
        let row = sqlx::query(
            "SELECT id, username, path, uid_validity, uid_next, special_use \
             FROM mailboxes WHERE id = ?1",
        )
        .bind(id.as_uuid().to_string())
        .fetch_optional(&self.pool)
        .await
        .map_err(|e| anyhow::anyhow!("get_mailbox failed: {}", e))?;

        match row {
            None => Ok(None),
            Some(r) => Ok(Some(row_to_mailbox(r)?)),
        }
    }

    async fn list_mailboxes(&self, user: &Username) -> anyhow::Result<Vec<Mailbox>> {
        let rows = sqlx::query(
            "SELECT id, username, path, uid_validity, uid_next, special_use \
             FROM mailboxes WHERE username = ?1 ORDER BY path",
        )
        .bind(user.to_string())
        .fetch_all(&self.pool)
        .await
        .map_err(|e| anyhow::anyhow!("list_mailboxes failed: {}", e))?;

        rows.into_iter()
            .map(row_to_mailbox)
            .collect::<anyhow::Result<Vec<_>>>()
    }

    async fn get_user_inbox(&self, user: &Username) -> anyhow::Result<Option<MailboxId>> {
        let row =
            sqlx::query("SELECT id FROM mailboxes WHERE username = ?1 AND path = 'INBOX' LIMIT 1")
                .bind(user.to_string())
                .fetch_optional(&self.pool)
                .await
                .map_err(|e| anyhow::anyhow!("get_user_inbox failed: {}", e))?;

        match row {
            None => Ok(None),
            Some(r) => {
                let id_str: String = r.try_get("id")?;
                let uuid = uuid::Uuid::from_str(&id_str)?;
                Ok(Some(MailboxId::from_uuid(uuid)))
            }
        }
    }

    async fn get_mailbox_special_use(
        &self,
        id: &MailboxId,
    ) -> anyhow::Result<SpecialUseAttributes> {
        let row = sqlx::query("SELECT special_use FROM mailboxes WHERE id = ?1")
            .bind(id.as_uuid().to_string())
            .fetch_optional(&self.pool)
            .await
            .map_err(|e| anyhow::anyhow!("get_mailbox_special_use failed: {}", e))?;

        match row {
            None => Ok(SpecialUseAttributes::new()),
            Some(r) => {
                let val: Option<String> = r.try_get("special_use")?;
                match val {
                    Some(s) if !s.is_empty() => {
                        let attrs: Vec<String> =
                            s.split_whitespace().map(|x| x.to_string()).collect();
                        Ok(SpecialUseAttributes::from_vec(attrs))
                    }
                    _ => Ok(SpecialUseAttributes::new()),
                }
            }
        }
    }

    async fn set_mailbox_special_use(
        &self,
        id: &MailboxId,
        special_use: SpecialUseAttributes,
    ) -> anyhow::Result<()> {
        sqlx::query("UPDATE mailboxes SET special_use = ?1 WHERE id = ?2")
            .bind(special_use.to_string())
            .bind(id.as_uuid().to_string())
            .execute(&self.pool)
            .await
            .map_err(|e| anyhow::anyhow!("set_mailbox_special_use failed: {}", e))?;
        Ok(())
    }

    async fn subscribe_mailbox(&self, user: &Username, mailbox_name: String) -> anyhow::Result<()> {
        sqlx::query("INSERT OR IGNORE INTO subscriptions (username, mailbox_name) VALUES (?1, ?2)")
            .bind(user.to_string())
            .bind(mailbox_name)
            .execute(&self.pool)
            .await
            .map_err(|e| anyhow::anyhow!("subscribe_mailbox failed: {}", e))?;
        Ok(())
    }

    async fn unsubscribe_mailbox(&self, user: &Username, mailbox_name: &str) -> anyhow::Result<()> {
        sqlx::query("DELETE FROM subscriptions WHERE username = ?1 AND mailbox_name = ?2")
            .bind(user.to_string())
            .bind(mailbox_name)
            .execute(&self.pool)
            .await
            .map_err(|e| anyhow::anyhow!("unsubscribe_mailbox failed: {}", e))?;
        Ok(())
    }

    async fn list_subscriptions(&self, user: &Username) -> anyhow::Result<Vec<String>> {
        let rows = sqlx::query("SELECT mailbox_name FROM subscriptions WHERE username = ?1")
            .bind(user.to_string())
            .fetch_all(&self.pool)
            .await
            .map_err(|e| anyhow::anyhow!("list_subscriptions failed: {}", e))?;

        let subs = rows
            .into_iter()
            .filter_map(|r| r.try_get::<String, _>("mailbox_name").ok())
            .collect();
        Ok(subs)
    }
}

/// Convert a SQLite row to a `Mailbox`.
fn row_to_mailbox(row: sqlx::sqlite::SqliteRow) -> anyhow::Result<Mailbox> {
    let username: String = row.try_get("username")?;
    let path_str: String = row.try_get("path")?;
    let path_parts: Vec<String> = path_str.split('/').map(|s| s.to_string()).collect();
    let username_obj =
        Username::new(username).map_err(|e| anyhow::anyhow!("Invalid username: {}", e))?;
    let path = MailboxPath::new(username_obj, path_parts);
    let mut mailbox = Mailbox::new(path);

    let special_use: Option<String> = row.try_get("special_use")?;
    mailbox.set_special_use(special_use);
    Ok(mailbox)
}

// ──────────────────────────────────────────────────────────────────────────────
// Message store
// ──────────────────────────────────────────────────────────────────────────────

struct SqliteMessageStore {
    pool: SqlitePool,
}

#[async_trait]
impl MessageStore for SqliteMessageStore {
    async fn append_message(
        &self,
        mailbox_id: &MailboxId,
        message: Mail,
    ) -> anyhow::Result<MessageMetadata> {
        let message_id = *message.message_id();
        let mailbox_id_str = mailbox_id.as_uuid().to_string();
        let message_id_str = message_id.as_uuid().to_string();

        // Next UID — SQLite has no FOR UPDATE, so we use a transaction + SELECT.
        let mut tx = self.pool.begin().await?;

        let uid_row = sqlx::query("SELECT uid_next FROM mailboxes WHERE id = ?1")
            .bind(&mailbox_id_str)
            .fetch_one(&mut *tx)
            .await
            .map_err(|e| anyhow::anyhow!("append_message: mailbox not found: {}", e))?;
        let uid: i64 = uid_row.try_get("uid_next")?;

        // Bump uid_next.
        sqlx::query("UPDATE mailboxes SET uid_next = uid_next + 1 WHERE id = ?1")
            .bind(&mailbox_id_str)
            .execute(&mut *tx)
            .await
            .map_err(|e| anyhow::anyhow!("append_message: uid_next update failed: {}", e))?;

        let sender = message.sender().map(|s| s.to_string());
        let recipients: Vec<String> = message.recipients().iter().map(|r| r.to_string()).collect();
        let subject = message
            .message()
            .headers()
            .get_first("subject")
            .map(|s| s.to_string());

        // Serialize headers to JSON.
        let mut headers_map = serde_json::Map::new();
        for (name, values) in message.message().headers().iter() {
            headers_map.insert(
                name.to_string(),
                serde_json::Value::Array(
                    values
                        .iter()
                        .map(|v| serde_json::Value::String(v.to_string()))
                        .collect(),
                ),
            );
        }
        let headers_json = serde_json::to_string(&headers_map).unwrap_or_else(|_| "{}".to_string());
        let recipients_json =
            serde_json::to_string(&recipients).unwrap_or_else(|_| "[]".to_string());
        let message_size = message.size();

        sqlx::query(
            "INSERT INTO messages (id, mailbox_id, uid, sender, recipients, subject, headers, size) \
             VALUES (?1, ?2, ?3, ?4, ?5, ?6, ?7, ?8)",
        )
        .bind(&message_id_str)
        .bind(&mailbox_id_str)
        .bind(uid)
        .bind(&sender)
        .bind(&recipients_json)
        .bind(&subject)
        .bind(&headers_json)
        .bind(message_size as i64)
        .execute(&mut *tx)
        .await
        .map_err(|e| anyhow::anyhow!("append_message: insert failed: {}", e))?;

        // Insert default flags row.
        sqlx::query("INSERT OR IGNORE INTO message_flags (message_id) VALUES (?1)")
            .bind(&message_id_str)
            .execute(&mut *tx)
            .await
            .map_err(|e| anyhow::anyhow!("append_message: flags insert failed: {}", e))?;

        tx.commit().await?;

        let metadata = MessageMetadata::new(
            message_id,
            *mailbox_id,
            uid as u32,
            MessageFlags::new(),
            message_size,
        );
        Ok(metadata)
    }

    async fn get_message(&self, _message_id: &MessageId) -> anyhow::Result<Option<Mail>> {
        // SQLite backend stores metadata only; raw bytes are not stored inline here.
        // Return None — callers that need bytes should use the filesystem backend.
        Ok(None)
    }

    async fn delete_messages(&self, message_ids: &[MessageId]) -> anyhow::Result<()> {
        for id in message_ids {
            sqlx::query("DELETE FROM messages WHERE id = ?1")
                .bind(id.as_uuid().to_string())
                .execute(&self.pool)
                .await
                .map_err(|e| anyhow::anyhow!("delete_messages failed for {}: {}", id, e))?;
        }
        Ok(())
    }

    async fn set_flags(
        &self,
        message_ids: &[MessageId],
        flags: MessageFlags,
    ) -> anyhow::Result<()> {
        for id in message_ids {
            sqlx::query(
                "INSERT INTO message_flags \
                    (message_id, flag_seen, flag_answered, flag_flagged, flag_deleted, flag_draft) \
                 VALUES (?1, ?2, ?3, ?4, ?5, ?6) \
                 ON CONFLICT(message_id) DO UPDATE SET \
                    flag_seen     = excluded.flag_seen,
                    flag_answered = excluded.flag_answered,
                    flag_flagged  = excluded.flag_flagged,
                    flag_deleted  = excluded.flag_deleted,
                    flag_draft    = excluded.flag_draft",
            )
            .bind(id.as_uuid().to_string())
            .bind(flags.is_seen() as i64)
            .bind(flags.is_answered() as i64)
            .bind(flags.is_flagged() as i64)
            .bind(flags.is_deleted() as i64)
            .bind(flags.is_draft() as i64)
            .execute(&self.pool)
            .await
            .map_err(|e| anyhow::anyhow!("set_flags failed for {}: {}", id, e))?;
        }
        Ok(())
    }

    async fn search(
        &self,
        mailbox_id: &MailboxId,
        _criteria: SearchCriteria,
    ) -> anyhow::Result<Vec<MessageId>> {
        // Minimal implementation: return all message IDs in the mailbox.
        let rows = sqlx::query("SELECT id FROM messages WHERE mailbox_id = ?1")
            .bind(mailbox_id.as_uuid().to_string())
            .fetch_all(&self.pool)
            .await
            .map_err(|e| anyhow::anyhow!("search failed: {}", e))?;

        let ids = rows
            .into_iter()
            .filter_map(|r| {
                r.try_get::<String, _>("id")
                    .ok()
                    .and_then(|s| uuid::Uuid::from_str(&s).ok())
                    .map(MessageId::from_uuid)
            })
            .collect();
        Ok(ids)
    }

    async fn copy_messages(
        &self,
        message_ids: &[MessageId],
        dest_mailbox_id: &MailboxId,
    ) -> anyhow::Result<Vec<MessageMetadata>> {
        // SQLite backend copies metadata rows only; no raw bytes to duplicate.
        let mut results = Vec::new();
        for &src_id in message_ids {
            // Look up the source message metadata.
            let row = sqlx::query("SELECT mailbox_id, uid, size FROM messages WHERE id = ?1")
                .bind(src_id.as_uuid().to_string())
                .fetch_optional(&self.pool)
                .await
                .map_err(|e| anyhow::anyhow!("copy_messages: source lookup failed: {}", e))?;

            if let Some(r) = row {
                let size: i64 = r.try_get("size")?;

                // Allocate new UID in destination mailbox.
                let uid_row = sqlx::query("SELECT uid_next FROM mailboxes WHERE id = ?1")
                    .bind(dest_mailbox_id.as_uuid().to_string())
                    .fetch_one(&self.pool)
                    .await
                    .map_err(|e| anyhow::anyhow!("copy_messages: dest uid fetch: {}", e))?;
                let uid: i64 = uid_row.try_get("uid_next")?;

                let new_id = MessageId::new();
                sqlx::query(
                    "INSERT INTO messages (id, mailbox_id, uid, sender, recipients, subject, headers, size) \
                     SELECT ?1, ?2, ?3, sender, recipients, subject, headers, size \
                     FROM messages WHERE id = ?4",
                )
                .bind(new_id.as_uuid().to_string())
                .bind(dest_mailbox_id.as_uuid().to_string())
                .bind(uid)
                .bind(src_id.as_uuid().to_string())
                .execute(&self.pool)
                .await
                .map_err(|e| anyhow::anyhow!("copy_messages: insert failed: {}", e))?;

                sqlx::query("UPDATE mailboxes SET uid_next = uid_next + 1 WHERE id = ?1")
                    .bind(dest_mailbox_id.as_uuid().to_string())
                    .execute(&self.pool)
                    .await
                    .map_err(|e| anyhow::anyhow!("copy_messages: uid_next bump: {}", e))?;

                results.push(MessageMetadata::new(
                    new_id,
                    *dest_mailbox_id,
                    uid as u32,
                    MessageFlags::new(),
                    size as usize,
                ));
            }
        }
        Ok(results)
    }

    async fn get_mailbox_messages(
        &self,
        mailbox_id: &MailboxId,
    ) -> anyhow::Result<Vec<MessageMetadata>> {
        let rows = sqlx::query(
            "SELECT m.id, m.uid, m.size, \
                    COALESCE(f.flag_seen,     0) AS flag_seen, \
                    COALESCE(f.flag_answered, 0) AS flag_answered, \
                    COALESCE(f.flag_flagged,  0) AS flag_flagged, \
                    COALESCE(f.flag_deleted,  0) AS flag_deleted, \
                    COALESCE(f.flag_draft,    0) AS flag_draft \
             FROM messages m \
             LEFT JOIN message_flags f ON f.message_id = m.id \
             WHERE m.mailbox_id = ?1 \
             ORDER BY m.uid",
        )
        .bind(mailbox_id.as_uuid().to_string())
        .fetch_all(&self.pool)
        .await
        .map_err(|e| anyhow::anyhow!("get_mailbox_messages failed: {}", e))?;

        let mut result = Vec::new();
        for r in rows {
            let id_str: String = r.try_get("id")?;
            let uuid = uuid::Uuid::from_str(&id_str)?;
            let msg_id = MessageId::from_uuid(uuid);
            let uid: i64 = r.try_get("uid")?;
            let size: i64 = r.try_get("size")?;

            let mut flags = MessageFlags::new();
            let flag_seen: i64 = r.try_get("flag_seen")?;
            let flag_answered: i64 = r.try_get("flag_answered")?;
            let flag_flagged: i64 = r.try_get("flag_flagged")?;
            let flag_deleted: i64 = r.try_get("flag_deleted")?;
            let flag_draft: i64 = r.try_get("flag_draft")?;
            flags.set_seen(flag_seen != 0);
            flags.set_answered(flag_answered != 0);
            flags.set_flagged(flag_flagged != 0);
            flags.set_deleted(flag_deleted != 0);
            flags.set_draft(flag_draft != 0);

            result.push(MessageMetadata::new(
                msg_id,
                *mailbox_id,
                uid as u32,
                flags,
                size as usize,
            ));
        }
        Ok(result)
    }
}

// ──────────────────────────────────────────────────────────────────────────────
// Metadata store
// ──────────────────────────────────────────────────────────────────────────────

struct SqliteMetadataStore {
    pool: SqlitePool,
}

#[async_trait]
impl MetadataStore for SqliteMetadataStore {
    async fn get_user_quota(&self, user: &Username) -> anyhow::Result<Quota> {
        let row = sqlx::query("SELECT used, quota_limit FROM user_quotas WHERE username = ?1")
            .bind(user.to_string())
            .fetch_optional(&self.pool)
            .await
            .map_err(|e| anyhow::anyhow!("get_user_quota failed: {}", e))?;

        match row {
            None => Ok(Quota::new(0, 1024 * 1024 * 1024)), // 1 GB default
            Some(r) => {
                let used: i64 = r.try_get("used")?;
                let limit: i64 = r.try_get("quota_limit")?;
                Ok(Quota::new(used as u64, limit as u64))
            }
        }
    }

    async fn set_user_quota(&self, user: &Username, quota: Quota) -> anyhow::Result<()> {
        sqlx::query(
            "INSERT INTO user_quotas (username, used, quota_limit) \
             VALUES (?1, ?2, ?3) \
             ON CONFLICT(username) DO UPDATE SET used = excluded.used, quota_limit = excluded.quota_limit",
        )
        .bind(user.to_string())
        .bind(quota.used as i64)
        .bind(quota.limit as i64)
        .execute(&self.pool)
        .await
        .map_err(|e| anyhow::anyhow!("set_user_quota failed: {}", e))?;
        Ok(())
    }

    async fn get_mailbox_counters(
        &self,
        mailbox_id: &MailboxId,
    ) -> anyhow::Result<MailboxCounters> {
        let row = sqlx::query(
            "SELECT COUNT(*) AS total, \
                    SUM(CASE WHEN f.flag_seen = 0 OR f.flag_seen IS NULL THEN 1 ELSE 0 END) AS unseen \
             FROM messages m \
             LEFT JOIN message_flags f ON f.message_id = m.id \
             WHERE m.mailbox_id = ?1",
        )
        .bind(mailbox_id.as_uuid().to_string())
        .fetch_one(&self.pool)
        .await
        .map_err(|e| anyhow::anyhow!("get_mailbox_counters failed: {}", e))?;

        let exists: i64 = row.try_get(0)?;
        let unseen: Option<i64> = row.try_get(1).ok();

        Ok(MailboxCounters {
            exists: exists as u32,
            recent: 0,
            unseen: unseen.unwrap_or(0) as u32,
        })
    }
}

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

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

    fn sqlite_url(path: &str) -> String {
        format!("sqlite://{}?mode=rwc", path)
    }

    /// Verify that `SqliteBackend::new` runs migrations and creates the core tables.
    #[tokio::test]
    async fn test_migrations_sqlite_creates_tables() {
        let tmp =
            std::env::temp_dir().join(format!("rusmes-sqlite-test-{}.db", uuid::Uuid::new_v4()));
        let url = sqlite_url(tmp.to_str().expect("valid path"));

        let backend = SqliteBackend::new(&url)
            .await
            .expect("SqliteBackend::new should succeed");

        // Verify that the migration created the expected tables.
        for table in &[
            "mailboxes",
            "messages",
            "message_flags",
            "user_quotas",
            "subscriptions",
        ] {
            let row = sqlx::query("SELECT name FROM sqlite_master WHERE type='table' AND name=?1")
                .bind(*table)
                .fetch_optional(backend.pool())
                .await
                .expect("query should succeed");

            assert!(
                row.is_some(),
                "Table '{}' should exist after migration",
                table
            );
        }

        let _ = tokio::fs::remove_file(&tmp).await;
    }

    /// Round-trip: create mailbox → append message → list messages.
    #[tokio::test]
    async fn test_sqlite_roundtrip_message() {
        use rusmes_proto::{HeaderMap, MailAddress, MessageBody, MimeMessage};

        let tmp =
            std::env::temp_dir().join(format!("rusmes-sqlite-test-{}.db", uuid::Uuid::new_v4()));
        let url = sqlite_url(tmp.to_str().expect("valid path"));

        let backend = SqliteBackend::new(&url).await.expect("SqliteBackend::new");

        let mailbox_store = backend.mailbox_store();
        let message_store = backend.message_store();

        let user: Username = "sqlite-test-user".parse().expect("username");
        let path = MailboxPath::new(user.clone(), vec!["INBOX".to_string()]);
        let mailbox_id = mailbox_store
            .create_mailbox(&path)
            .await
            .expect("create_mailbox");

        let headers = HeaderMap::new();
        let body = MessageBody::Small(bytes::Bytes::from("SQLite test body"));
        let mime = MimeMessage::new(headers, body);
        let mail = rusmes_proto::Mail::new(
            Some("sender@example.com".parse::<MailAddress>().expect("addr")),
            vec!["sqlite-test-user@localhost"
                .parse::<MailAddress>()
                .expect("addr")],
            mime,
            None,
            None,
        );

        let metadata = message_store
            .append_message(&mailbox_id, mail)
            .await
            .expect("append_message");

        assert_eq!(metadata.mailbox_id(), &mailbox_id);
        assert_eq!(metadata.uid(), 1);

        let messages = message_store
            .get_mailbox_messages(&mailbox_id)
            .await
            .expect("get_mailbox_messages");
        assert_eq!(messages.len(), 1);

        let _ = tokio::fs::remove_file(&tmp).await;
    }
}