elma-tui 0.1.0

A modern terminal-based email client
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
//! Mock backend used for demo mode and manual testing.
//!
//! This implementation keeps all data in-memory and simulates asynchronous behaviour
//! by introducing small random delays when applying actions.  The goal is to exercise
//! the UI logic without relying on an external mail provider.

use crate::{
    backend::{ActionStatus, BackendEvent, MailBackend, MailboxSnapshot, OutgoingMessage},
    model::{
        Action, ActionType, MailboxKind, Message, MessageAttachment, MessageContent,
        MessageContentPart, MessageId, MessageStatus,
    },
};
use anyhow::{Result, anyhow};
use std::{
    collections::{HashMap, HashSet, VecDeque},
    ops::Range,
    sync::{
        Arc, Condvar, Mutex,
        atomic::{AtomicU64, Ordering},
        mpsc::{self, Receiver, Sender},
    },
    thread,
    time::{Duration, SystemTime},
};
use time::{Duration as TimeDuration, OffsetDateTime};

const INITIAL_MESSAGE_COUNT: usize = 250;
const MAILER_NAME: &str = "MockMailer/tdoc-demo";
const DEFAULT_SENDER: &str = "user@mock.example";
const ATTACHMENT_TEMPLATES: &[(&str, &str)] = &[
    ("proposal.pdf", "application/pdf"),
    ("diagram.png", "image/png"),
    (
        "report.xlsx",
        "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
    ),
    ("notes.txt", "text/plain"),
    (
        "presentation.pptx",
        "application/vnd.openxmlformats-officedocument.presentationml.presentation",
    ),
    ("archive.zip", "application/zip"),
];

/// An action paired with the channel it should report completion on.
struct WorkItem {
    action: Action,
    result_tx: Sender<ActionStatus>,
}

/// Simple in-memory backend for demos and integration tests.
///
/// Messages are generated deterministically at startup so the UI can exercise the
/// same flows as the Gmail backend without network access.
pub struct MockBackend {
    mailboxes: Arc<Mutex<HashMap<MailboxKind, Vec<MockMessage>>>>,
    contents: Arc<Mutex<HashMap<MessageId, MessageContent>>>,
    event_sender: Arc<Mutex<Option<Sender<BackendEvent>>>>,
    id_counter: Arc<AtomicU64>,
    work_queue: Arc<(Mutex<VecDeque<WorkItem>>, Condvar)>,
}

#[derive(Clone)]
struct MockMessage {
    message: Message,
}

impl Default for MockBackend {
    fn default() -> Self {
        Self::demo()
    }
}

impl MockBackend {
    /// Create a mock backend configured for the CLI demo mode.
    ///
    /// The builder loads a stock set of messages, spins up a background thread that
    /// periodically injects new mail, and returns immediately so the UI stays
    /// responsive.
    pub fn demo() -> Self {
        let mailboxes = Arc::new(Mutex::new(HashMap::new()));
        {
            let mut guard = mailboxes.lock().expect("mailboxes mutex poisoned");
            for kind in MailboxKind::ALL {
                guard.insert(kind, Vec::new());
            }
        }
        let contents = Arc::new(Mutex::new(HashMap::new()));
        let event_sender = Arc::new(Mutex::new(None));
        let id_counter = Arc::new(AtomicU64::new(0));
        let work_queue = Arc::new((Mutex::new(VecDeque::new()), Condvar::new()));

        let backend = Self {
            mailboxes: Arc::clone(&mailboxes),
            contents: Arc::clone(&contents),
            event_sender: Arc::clone(&event_sender),
            id_counter: Arc::clone(&id_counter),
            work_queue: Arc::clone(&work_queue),
        };

        backend.populate_initial_mailboxes(INITIAL_MESSAGE_COUNT);
        backend.spawn_incoming_mail_generator(mailboxes, contents, event_sender, id_counter);
        backend.spawn_action_worker(work_queue);
        backend
    }

    fn populate_initial_mailboxes(&self, inbox_count: usize) {
        let mut rng = SimpleRng::new(random_seed());
        let mut mailboxes = self.mailboxes.lock().expect("mailboxes mutex poisoned");
        let mut contents = self.contents.lock().expect("contents mutex poisoned");

        for _ in 0..inbox_count {
            let id = self.next_id();
            let (message, content) = old_random_message(id, &mut rng);
            contents.insert(id, content);
            mailboxes
                .entry(MailboxKind::Inbox)
                .or_default()
                .push(MockMessage { message });
        }

        let templates = [
            (MailboxKind::Important, 30usize, MessageStatus::Read),
            (MailboxKind::Starred, 40usize, MessageStatus::Read),
            (MailboxKind::Sent, 25usize, MessageStatus::Read),
            (MailboxKind::Drafts, 12usize, MessageStatus::New),
            (MailboxKind::Archive, 35usize, MessageStatus::Read),
            (MailboxKind::Spam, 22usize, MessageStatus::Read),
            (MailboxKind::Trash, 18usize, MessageStatus::Read),
        ];

        for (kind, count, status) in templates {
            let list = mailboxes.entry(kind).or_default();
            for _ in 0..count {
                let id = self.next_id();
                let sent = OffsetDateTime::now_utc()
                    - TimeDuration::hours(rng.gen_range_usize(0..720) as i64)
                    - TimeDuration::minutes(rng.gen_range_usize(0..60) as i64);
                let (mut message, mut content) = new_random_message(id, sent, &mut rng);
                message.status = status;
                match kind {
                    MailboxKind::Starred => {
                        message.starred = true;
                        message.labels = vec!["Starred".to_string()];
                    }
                    MailboxKind::Important => {
                        message.important = true;
                        message.labels = vec!["Important".to_string()];
                    }
                    MailboxKind::Sent => {
                        message.labels = vec!["Sent".to_string()];
                        message.starred = false;
                    }
                    MailboxKind::Drafts => {
                        message.labels = vec!["Draft".to_string()];
                        message.starred = false;
                    }
                    MailboxKind::Archive => {
                        message.labels = vec!["Archive".to_string()];
                        message.starred = rng.one_in(5);
                        if rng.one_in(6) {
                            message.status = MessageStatus::New;
                        }
                    }
                    MailboxKind::Spam => {
                        message.labels = vec!["Spam".to_string()];
                        message.starred = false;
                        if rng.one_in(4) {
                            message.status = MessageStatus::New;
                        }
                    }
                    MailboxKind::Trash => {
                        message.labels = vec!["Trash".to_string()];
                        message.starred = false;
                        if rng.one_in(6) {
                            message.status = MessageStatus::New;
                        }
                    }
                    MailboxKind::Inbox => {}
                }
                if message.important
                    && !message
                        .labels
                        .iter()
                        .any(|label| label.eq_ignore_ascii_case("Important"))
                {
                    message.labels.push("Important".to_string());
                }
                update_mailer(&mut content, message.status);
                contents.insert(id, content);
                list.push(MockMessage { message });
            }
        }

        for list in mailboxes.values_mut() {
            list.sort_by_key(|mock| mock.message.sent);
        }
    }

    fn spawn_incoming_mail_generator(
        &self,
        mailboxes: Arc<Mutex<HashMap<MailboxKind, Vec<MockMessage>>>>,
        contents: Arc<Mutex<HashMap<MessageId, MessageContent>>>,
        event_sender: Arc<Mutex<Option<Sender<BackendEvent>>>>,
        id_counter: Arc<AtomicU64>,
    ) {
        thread::spawn(move || {
            let mut rng = SimpleRng::new(random_seed() ^ 0x9e3779b97f4a7c15);
            loop {
                let sleep_ms = rng.gen_range_usize(350..1200) as u64;
                thread::sleep(Duration::from_millis(sleep_ms));

                let id = id_counter.fetch_add(1, Ordering::SeqCst) + 1;
                let sent = OffsetDateTime::now_utc();
                let (message, content) = new_random_message(id, sent, &mut rng);

                {
                    let mut message_lock = mailboxes.lock().expect("mailboxes mutex poisoned");
                    let mut content_lock = contents.lock().expect("contents mutex poisoned");

                    content_lock.insert(id, content);
                    message_lock
                        .entry(MailboxKind::Inbox)
                        .or_default()
                        .push(MockMessage {
                            message: message.clone(),
                        });
                }

                let sender = {
                    let guard = event_sender.lock().expect("event sender mutex poisoned");
                    guard.clone()
                };

                if let Some(sender) = sender
                    && sender.send(BackendEvent::NewMessage(message)).is_err()
                {
                    let mut guard = event_sender.lock().expect("event sender mutex poisoned");
                    *guard = None;
                }
            }
        });
    }

    fn spawn_action_worker(&self, work_queue: Arc<(Mutex<VecDeque<WorkItem>>, Condvar)>) {
        let mailboxes = Arc::clone(&self.mailboxes);
        let contents = Arc::clone(&self.contents);

        thread::spawn(move || {
            let mut delay_rng = SimpleRng::new(random_seed() ^ 0xa511f93acb5d7a77);
            loop {
                let item = {
                    let (lock, cvar) = &*work_queue;
                    let mut queue = lock.lock().expect("work queue mutex poisoned");
                    while queue.is_empty() {
                        queue = cvar.wait(queue).expect("work queue condvar poisoned");
                    }
                    queue.pop_front().expect("queue was non-empty")
                };

                let delay_ms = delay_rng.gen_range_usize_inclusive(50, 500) as u64;
                thread::sleep(Duration::from_millis(delay_ms));

                let result = MockBackend::apply_action_now(&mailboxes, &contents, &item.action)
                    .map_err(|err| err.to_string());
                // Receiver may have been dropped — ignore send errors.
                let _ = item.result_tx.send(ActionStatus {
                    action: item.action,
                    result,
                });
            }
        });
    }

    fn next_id(&self) -> MessageId {
        self.id_counter.fetch_add(1, Ordering::SeqCst) + 1
    }

    /// Update the mock data structures to reflect a single action.
    ///
    /// This helper runs synchronously inside the worker thread spawned by
    /// [`MailBackend::apply_actions`].  It mutates the shared message and content
    /// collections and mirrors the behaviour of the real Gmail backend closely
    /// enough for UI testing.
    fn apply_action_now(
        mailboxes: &Arc<Mutex<HashMap<MailboxKind, Vec<MockMessage>>>>,
        contents: &Arc<Mutex<HashMap<MessageId, MessageContent>>>,
        action: &Action,
    ) -> Result<()> {
        let mut mailboxes = mailboxes.lock().expect("mailboxes mutex poisoned");
        let mut contents = contents.lock().expect("contents mutex poisoned");

        let mut removed = None;
        for kind in MailboxKind::ALL {
            if let Some(list) = mailboxes.get_mut(&kind)
                && let Some(index) = list
                    .iter()
                    .position(|mock| mock.message.id == action.message_id)
            {
                let mock = list.remove(index);
                removed = Some((kind, mock));
                break;
            }
        }

        let Some((source_kind, mut mock)) = removed else {
            return Err(anyhow!("message {} not found", action.message_id));
        };

        let mut target_kind = source_kind;
        let was_new = matches!(mock.message.status, MessageStatus::New);
        match action.action_type {
            ActionType::Archive => {
                mock.message
                    .labels
                    .retain(|label| !label.eq_ignore_ascii_case("Trash"));
                if !mock
                    .message
                    .labels
                    .iter()
                    .any(|label| label.eq_ignore_ascii_case("Archive"))
                {
                    mock.message.labels.push("Archive".to_string());
                }
                mock.message.status = if was_new {
                    MessageStatus::New
                } else {
                    MessageStatus::Read
                };
                target_kind = MailboxKind::Archive;
            }
            ActionType::Delete => {
                mock.message
                    .labels
                    .retain(|label| !label.eq_ignore_ascii_case("Archive"));
                if !mock
                    .message
                    .labels
                    .iter()
                    .any(|label| label.eq_ignore_ascii_case("Trash"))
                {
                    mock.message.labels.push("Trash".to_string());
                }
                mock.message.status = if was_new {
                    MessageStatus::New
                } else {
                    MessageStatus::Read
                };
                target_kind = MailboxKind::Trash;
            }
            ActionType::MoveToSpam => {
                mock.message.labels.retain(|label| {
                    !label.eq_ignore_ascii_case("Archive")
                        && !label.eq_ignore_ascii_case("Trash")
                        && !label.eq_ignore_ascii_case("Spam")
                });
                mock.message.labels.push("Spam".to_string());
                mock.message.status = if was_new {
                    MessageStatus::New
                } else {
                    MessageStatus::Read
                };
                target_kind = MailboxKind::Spam;
            }
            ActionType::MoveToInboxUnread => {
                mock.message.status = MessageStatus::New;
                mock.message.labels.retain(|label| {
                    !label.eq_ignore_ascii_case("Archive")
                        && !label.eq_ignore_ascii_case("Trash")
                        && !label.eq_ignore_ascii_case("Spam")
                });
                target_kind = MailboxKind::Inbox;
            }
            ActionType::MoveToInboxRead => {
                mock.message.status = MessageStatus::Read;
                mock.message.labels.retain(|label| {
                    !label.eq_ignore_ascii_case("Archive")
                        && !label.eq_ignore_ascii_case("Trash")
                        && !label.eq_ignore_ascii_case("Spam")
                });
                target_kind = MailboxKind::Inbox;
            }
            ActionType::MarkAsRead => {
                mock.message.status = MessageStatus::Read;
            }
            ActionType::MarkAsStarred => {
                mock.message.starred = true;
                if !mock
                    .message
                    .labels
                    .iter()
                    .any(|label| label.eq_ignore_ascii_case("Starred"))
                {
                    mock.message.labels.push("Starred".to_string());
                }
            }
            ActionType::MarkAsUnstarred => {
                mock.message.starred = false;
                mock.message
                    .labels
                    .retain(|label| !label.eq_ignore_ascii_case("Starred"));
            }
            ActionType::MarkAsImportant => {
                mock.message.important = true;
                if !mock
                    .message
                    .labels
                    .iter()
                    .any(|label| label.eq_ignore_ascii_case("Important"))
                {
                    mock.message.labels.push("Important".to_string());
                }
            }
            ActionType::MarkAsUnimportant => {
                mock.message.important = false;
                mock.message
                    .labels
                    .retain(|label| !label.eq_ignore_ascii_case("Important"));
            }
        }

        if let Some(content) = contents.get_mut(&action.message_id) {
            if mock.message.status == MessageStatus::New {
                content.mailer = format!("{MAILER_NAME} (unread)");
            } else {
                content.mailer = MAILER_NAME.to_string();
            }
        }

        if let Some(list) = mailboxes.get_mut(&target_kind) {
            list.push(mock);
            list.sort_by_key(|entry| entry.message.sent);
        } else {
            return Err(anyhow!("mailbox {target_kind:?} not found"));
        }

        Ok(())
    }
}

impl MockBackend {
    fn store_composed_message(
        &self,
        outgoing: OutgoingMessage,
        mailbox: MailboxKind,
        status: MessageStatus,
        label: &'static str,
    ) -> Result<()> {
        let id = self.next_id();
        let sent = OffsetDateTime::now_utc();

        let OutgoingMessage {
            to,
            cc,
            bcc,
            subject,
            text_body,
            html_body,
        } = outgoing;

        let mut recipients = Vec::new();
        recipients.extend(to);
        recipients.extend(cc);
        recipients.extend(bcc);

        let size = text_body.len() + html_body.len() + subject.len();

        let mut message = Message {
            id,
            sent,
            sender: DEFAULT_SENDER.to_string(),
            recipients,
            subject,
            size,
            starred: false,
            important: false,
            answered: false,
            forwarded: false,
            status,
            labels: Vec::new(),
            uid: id as u32,
            seq: 0,
            has_attachments: false,
        };

        if !label.is_empty() {
            message.labels.push(label.to_string());
        }

        let mut content_state = MessageContent {
            mailer: format!("{MAILER_NAME} compose"),
            ..Default::default()
        };
        content_state.parts.push(MessageContentPart {
            content_type: "text/plain".to_string(),
            content: text_body.into_bytes(),
        });
        content_state.parts.push(MessageContentPart {
            content_type: "text/html".to_string(),
            content: html_body.into_bytes(),
        });

        let mut mailboxes = self.mailboxes.lock().expect("mailboxes mutex poisoned");
        let mut contents = self.contents.lock().expect("contents mutex poisoned");
        contents.insert(id, content_state);

        let entry = mailboxes.entry(mailbox).or_default();
        entry.push(MockMessage { message });
        entry.sort_by_key(|mock| mock.message.sent);

        Ok(())
    }
}

impl MailBackend for MockBackend {
    /// Return the current mailbox snapshot and subscribe to future events.
    fn load_mailbox(
        &self,
        mailbox: MailboxKind,
    ) -> Result<(MailboxSnapshot, Receiver<BackendEvent>)> {
        let mut messages = {
            let mailboxes = self.mailboxes.lock().expect("mailboxes mutex poisoned");
            if mailbox == MailboxKind::Important {
                let mut seen = HashSet::new();
                let mut collected = Vec::new();
                for list in mailboxes.values() {
                    for mock in list {
                        if !mock.message.important {
                            continue;
                        }
                        if seen.insert(mock.message.id) {
                            collected.push(mock.message.clone());
                        }
                    }
                }
                collected
            } else {
                mailboxes
                    .get(&mailbox)
                    .ok_or_else(|| anyhow!("mailbox {mailbox:?} not found"))?
                    .iter()
                    .map(|mock| mock.message.clone())
                    .collect::<Vec<_>>()
            }
        };

        messages.sort_by_key(|msg| msg.sent);
        for (index, message) in messages.iter_mut().enumerate() {
            message.seq = index as u32 + 1;
        }

        let receiver = if mailbox == MailboxKind::Inbox {
            let (sender, receiver) = mpsc::channel();
            {
                let mut guard = self
                    .event_sender
                    .lock()
                    .expect("event sender mutex poisoned");
                *guard = Some(sender);
            }
            receiver
        } else {
            let (_sender, receiver) = mpsc::channel();
            receiver
        };

        let total = messages.len();
        Ok((MailboxSnapshot { total, messages }, receiver))
    }

    /// Fetch the MIME content for an individual message.
    fn load_message(&self, message_id: MessageId) -> Result<MessageContent> {
        let contents = self.contents.lock().expect("contents mutex poisoned");
        contents
            .get(&message_id)
            .cloned()
            .ok_or_else(|| anyhow!("message {message_id} not found"))
    }

    /// Queue actions for the persistent worker thread.
    ///
    /// Actions are appended to the back of the work queue so any previously
    /// submitted or immediate work runs first.  The worker adds jitter (50–500 ms) mimics the round trips that the Gmail backend incurs,
    /// giving the UI a realistic opportunity to render progress.
    fn apply_actions(&self, actions: Vec<Action>) -> Result<Receiver<ActionStatus>> {
        let (tx, rx) = mpsc::channel();
        let (lock, cvar) = &*self.work_queue;
        let mut queue = lock.lock().expect("work queue mutex poisoned");
        for action in actions {
            queue.push_back(WorkItem {
                action,
                result_tx: tx.clone(),
            });
        }
        cvar.notify_one();
        Ok(rx)
    }

    /// Queue actions at the front so they execute before pending scheduled work.
    fn apply_immediate_actions(&self, actions: Vec<Action>) -> Result<Receiver<ActionStatus>> {
        let (tx, rx) = mpsc::channel();
        let (lock, cvar) = &*self.work_queue;
        let mut queue = lock.lock().expect("work queue mutex poisoned");
        for (i, action) in actions.into_iter().enumerate() {
            queue.insert(
                i,
                WorkItem {
                    action,
                    result_tx: tx.clone(),
                },
            );
        }
        cvar.notify_one();
        Ok(rx)
    }

    fn send_message(&self, message: OutgoingMessage) -> Result<()> {
        self.store_composed_message(message, MailboxKind::Sent, MessageStatus::Read, "Sent")
    }

    fn save_draft(&self, message: OutgoingMessage) -> Result<()> {
        self.store_composed_message(message, MailboxKind::Drafts, MessageStatus::New, "Draft")
    }
}

fn generate_mock_attachments(rng: &mut SimpleRng) -> Vec<MessageAttachment> {
    if rng.one_in(3) {
        let count = rng.gen_range_usize_inclusive(1, 3);
        let mut attachments = Vec::with_capacity(count);
        for _ in 0..count {
            let (filename, mime_type) =
                ATTACHMENT_TEMPLATES[rng.gen_range_usize(0..ATTACHMENT_TEMPLATES.len())];
            let size = mock_attachment_size(rng, mime_type);
            attachments.push(MessageAttachment {
                filename: Some(filename.to_string()),
                mime_type: mime_type.to_string(),
                size,
            });
        }
        attachments
    } else {
        Vec::new()
    }
}

fn mock_attachment_size(rng: &mut SimpleRng, mime_type: &str) -> usize {
    match mime_type {
        "application/pdf" => rng.gen_range_usize(150_000..3_000_000),
        "image/png" => rng.gen_range_usize(90_000..1_500_000),
        "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" => {
            rng.gen_range_usize(120_000..2_400_000)
        }
        "application/vnd.openxmlformats-officedocument.presentationml.presentation" => {
            rng.gen_range_usize(400_000..4_800_000)
        }
        "application/zip" => rng.gen_range_usize(240_000..4_500_000),
        "text/plain" => rng.gen_range_usize(4_000..40_000),
        _ => rng.gen_range_usize(60_000..750_000),
    }
}

fn new_random_message(
    id: MessageId,
    sent: OffsetDateTime,
    rng: &mut SimpleRng,
) -> (Message, MessageContent) {
    let subject = generate_subject(rng);
    let sender = generate_sender(rng);
    let recipients = generate_recipients(rng);
    let body = random_body(&sender, &subject, rng);
    let html = format!("<html><body><h1>{subject}</h1>{body}</body></html>");
    let plain = html2text::from_read(html.as_bytes(), 80);

    let mut content = MessageContent::default();
    content.parts.push(MessageContentPart {
        content_type: "text/html".to_string(),
        content: html.as_bytes().to_vec(),
    });
    content.parts.push(MessageContentPart {
        content_type: "text/plain".to_string(),
        content: plain.into_bytes(),
    });

    let attachments = generate_mock_attachments(rng);
    let attachments_bytes = attachments.iter().map(|att| att.size).sum::<usize>();
    content.attachments = attachments.clone();

    let size = rng.gen_range_usize(0..7_203_680) + 200 + attachments_bytes;

    let important = rng.one_in(6);
    let mut labels = Vec::new();
    if important {
        labels.push("Important".to_string());
    }

    let message = Message {
        id,
        sent,
        sender,
        recipients,
        subject,
        size,
        starred: false,
        important,
        answered: false,
        forwarded: false,
        status: MessageStatus::New,
        labels,
        uid: id as u32,
        seq: 0,
        has_attachments: !attachments.is_empty(),
    };

    update_mailer(&mut content, message.status);
    (message, content)
}

fn old_random_message(id: MessageId, rng: &mut SimpleRng) -> (Message, MessageContent) {
    let sent = OffsetDateTime::now_utc()
        - TimeDuration::hours(rng.gen_range_usize(0..1000) as i64)
        - TimeDuration::minutes(rng.gen_range_usize(0..60) as i64);

    let (mut message, mut content) = new_random_message(id, sent, rng);
    message.starred = rng.one_in(10);
    message.answered = rng.one_in(7);
    message.forwarded = rng.one_in(25);
    message.status = MessageStatus::Read;
    if rng.one_in(20) {
        message.status = MessageStatus::New;
        message.starred = false;
        message.answered = false;
        message.forwarded = false;
    }
    update_mailer(&mut content, message.status);
    (message, content)
}

fn update_mailer(content: &mut MessageContent, status: MessageStatus) {
    content.mailer = if status == MessageStatus::New {
        format!("{MAILER_NAME} (unread)")
    } else {
        MAILER_NAME.to_string()
    };
}

fn generate_sender(rng: &mut SimpleRng) -> String {
    let first = rng.choose_str(FIRST_NAMES);
    let mut parts = Vec::with_capacity(3);
    parts.push(first.to_string());
    if rng.one_in(20) {
        let middle = rng.choose_str(FIRST_NAMES).chars().next().unwrap_or('A');
        parts.push(format!("{middle}."));
    }
    parts.push(rng.choose_str(LAST_NAMES).to_string());
    parts.join(" ")
}

fn generate_recipients(rng: &mut SimpleRng) -> Vec<String> {
    let count = rng.gen_range_usize_inclusive(1, 3);
    let mut recipients = Vec::with_capacity(count);
    for _ in 0..count {
        recipients.push(generate_sender(rng));
    }
    recipients
}

fn generate_subject(rng: &mut SimpleRng) -> String {
    let mut subject = rng.choose_str(SUBJECTS).to_string();
    if rng.one_in(5) {
        subject = format!("Re: {subject}");
        if rng.one_in(2) {
            subject = format!("Re: {subject}");
        }
    }
    subject
}

fn random_body(sender: &str, subject: &str, rng: &mut SimpleRng) -> String {
    let greeting = rng.choose_str(GREETINGS);
    let closing = rng.choose_str(CLOSINGS);
    let paragraph_count = rng.gen_range_usize_inclusive(2, 4);
    let mut paragraphs = Vec::with_capacity(paragraph_count);

    for _ in 0..paragraph_count {
        paragraphs.push(rng.choose_str(PARAGRAPHS));
    }

    let summary = format!("<p><em>Summary:</em> {}</p>", rng.choose_str(SUMMARIES));

    let mut body = format!("<p>{greeting}</p>");
    for paragraph in paragraphs {
        body.push_str(&format!("<p>{paragraph}</p>"));
    }
    body.push_str(&summary);
    body.push_str(&format!(
        "<p>Subject reference: <strong>{subject}</strong></p>"
    ));
    body.push_str(&format!("<p>{closing}<br/>{sender}</p>"));
    body
}

const FIRST_NAMES: &[&str] = &[
    "Alex", "Casey", "Jordan", "Morgan", "Taylor", "Jamie", "Riley", "Sam", "Drew", "Skyler",
];

const LAST_NAMES: &[&str] = &[
    "Anderson", "Bennett", "Chen", "Diaz", "Edwards", "Fischer", "Garcia", "Hughes", "Iqbal",
    "Jensen", "Klein", "Lopez", "Miller", "Nguyen", "Ortiz",
];

const SUBJECTS: &[&str] = &[
    "Project update: timeline adjustments",
    "Reminder: submit the sprint report",
    "Lunch & learn invitation",
    "Draft agenda for tomorrow's sync",
    "Customer feedback summary",
    "Action required: security checklist",
    "Planning notes for the offsite",
    "Quick question about the release",
    "Design doc review request",
    "Thanks for the presentation yesterday",
];

const GREETINGS: &[&str] = &[
    "Hello team,",
    "Hi folks,",
    "Good afternoon,",
    "Hi there,",
    "Hello everyone,",
];

const PARAGRAPHS: &[&str] = &[
    "I wanted to share a short update on the latest mock email generated by the backend. \
     The content is designed to demonstrate how the FTML pager renders HTML documents.",
    "Please take a moment to skim the details below. The mock system rotates through a set \
     of templates so the inbox feels active while we test the UI interactions.",
    "If you spot anything that looks off in the rendered output, feel free to flag it. \
     The goal is to mirror the Go client experience as closely as possible, including colors and keybindings.",
    "This paragraph exists to stretch the pager a little further, just to make sure scrolling \
     continues to feel natural. We also want to see how long messages behave in the mock inbox.",
];

const SUMMARIES: &[&str] = &[
    "Schedules remain on track and the next handshake is queued for Friday.",
    "No additional changes are necessary; existing settings should be sufficient.",
    "The demo data set continues to evolve so we get a realistic preview.",
    "Pending review items: onboarding copy updates and the latest Ratatui tweaks.",
];

const CLOSINGS: &[&str] = &["Best regards,", "Cheers,", "Thanks!", "See you soon,"];

/// Tiny deterministic RNG used to keep mock timings reproducible.
struct SimpleRng(u64);

impl SimpleRng {
    /// Construct a new generator from a seed.
    fn new(seed: u64) -> Self {
        Self(seed)
    }

    /// Produce the next pseudo-random 32-bit value.
    fn next_u32(&mut self) -> u32 {
        self.0 = self.0.wrapping_mul(6364136223846793005).wrapping_add(1);
        (self.0 >> 32) as u32
    }

    /// Return a value uniformly sampled from the half-open `range`.
    fn gen_range_usize(&mut self, range: Range<usize>) -> usize {
        if range.start >= range.end {
            range.start
        } else {
            let span = range.end - range.start;
            range.start + (self.next_u32() as usize % span)
        }
    }

    /// Return a value uniformly sampled from the inclusive range `[start, end]`.
    fn gen_range_usize_inclusive(&mut self, start: usize, end: usize) -> usize {
        if start >= end {
            start
        } else {
            start + (self.next_u32() as usize % (end - start + 1))
        }
    }

    /// Pick an item from `slice`, cycling uniformly.
    fn choose_str<'a>(&mut self, slice: &'a [&'a str]) -> &'a str {
        let idx = self.gen_range_usize(0..slice.len());
        slice[idx]
    }

    fn one_in(&mut self, n: usize) -> bool {
        if n == 0 {
            return true;
        }
        self.gen_range_usize(0..n) == 0
    }
}

fn random_seed() -> u64 {
    SystemTime::now()
        .duration_since(SystemTime::UNIX_EPOCH)
        .map(|d| d.as_nanos() as u64)
        .unwrap_or(0x1234_5678_9ABC_DEF0)
        ^ 0xA5A5_A5A5_F0F0_F0F0
}

mod html2text {
    pub fn from_read(bytes: &[u8], width: usize) -> String {
        let mut out = String::new();
        let mut col = 0usize;
        let data = String::from_utf8_lossy(bytes);
        for word in data.split_whitespace() {
            let len = word.chars().count();
            if col + len + 1 > width {
                out.push('\n');
                col = 0;
            }
            if col != 0 {
                out.push(' ');
                col += 1;
            }
            out.push_str(word);
            col += len;
        }
        out
    }
}