email-cli 0.6.5

Agent-friendly email CLI for Resend
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
use clap::{Args, Parser, Subcommand};
use std::path::PathBuf;

/// Validate that a limit value is within Resend's documented range (1-100).
fn parse_resend_limit(raw: &str) -> Result<usize, String> {
    let value: usize = raw
        .parse()
        .map_err(|err: std::num::ParseIntError| err.to_string())?;
    if !(1..=100).contains(&value) {
        return Err(format!("must be a number between 1 and 100, got {}", value));
    }
    Ok(value)
}

#[derive(Parser)]
#[command(
    name = "email-cli",
    version,
    about = "Agent-friendly email CLI for Resend"
)]
pub struct Cli {
    #[arg(long, global = true)]
    pub db: Option<PathBuf>,
    #[arg(long, global = true)]
    pub json: bool,
    #[command(subcommand)]
    pub command: Command,
}

#[derive(Subcommand)]
pub enum Command {
    Profile {
        #[command(subcommand)]
        command: ProfileCommand,
    },
    Account {
        #[command(subcommand)]
        command: AccountCommand,
    },
    Signature {
        #[command(subcommand)]
        command: SignatureCommand,
    },
    Send(SendArgs),
    Reply(ReplyArgs),
    Forward(ForwardArgs),
    /// Run as a menu bar daemon with notifications
    Daemon(DaemonArgs),
    /// Install/remove a LaunchAgent that runs the daemon at login (macOS)
    Autostart {
        #[command(subcommand)]
        command: AutostartCommand,
    },
    Draft {
        #[command(subcommand)]
        command: DraftCommand,
    },
    Sync(SyncArgs),
    Inbox {
        #[command(subcommand)]
        command: InboxCommand,
    },
    Attachments {
        #[command(subcommand)]
        command: AttachmentsCommand,
    },
    /// Manage Resend domains
    Domain {
        #[command(subcommand)]
        command: DomainCommand,
    },
    /// Manage Resend contacts (use `segment` to group them, `topic` for preferences)
    Contact {
        #[command(subcommand)]
        command: ContactCommand,
    },
    /// Send batch emails
    Batch {
        #[command(subcommand)]
        command: BatchCommand,
    },
    /// Manage Resend API keys
    ApiKey {
        #[command(subcommand)]
        command: ApiKeyCommand,
    },
    /// Manage the durable send outbox
    Outbox {
        #[command(subcommand)]
        command: OutboxCommand,
    },
    /// Webhook event listener
    Webhook {
        #[command(subcommand)]
        command: WebhookCommand,
    },
    /// View delivery events
    Events {
        #[command(subcommand)]
        command: EventsCommand,
    },
    /// List sent emails (Resend GET /emails)
    Email {
        #[command(subcommand)]
        command: EmailCommand,
    },
    /// Manage Resend broadcasts (campaign sends with native unsubscribe wiring)
    Broadcast {
        #[command(subcommand)]
        command: BroadcastCommand,
    },
    /// Manage Resend contact-property schema (define custom fields before assigning values)
    #[command(name = "contact-property")]
    ContactProperty {
        #[command(subcommand)]
        command: ContactPropertyCommand,
    },
    /// Manage Resend topics (granular subscription preferences for broadcasts)
    Topic {
        #[command(subcommand)]
        command: TopicCommand,
    },
    /// Manage Resend segments (replaces the deprecated "audience" noun, Nov 2025)
    Segment {
        #[command(subcommand)]
        command: SegmentCommand,
    },
    /// Self-update from GitHub Releases
    Update {
        /// Check only, don't install
        #[arg(long)]
        check: bool,
    },
    /// View command usage log
    Log(LogArgs),
    /// Machine-readable capability manifest
    AgentInfo,
    /// Install skill file to agent platforms
    Skill {
        #[command(subcommand)]
        action: SkillAction,
    },
    /// Generate shell completions
    Completions {
        shell: clap_complete::Shell,
    },
}

#[derive(Subcommand)]
pub enum ProfileCommand {
    Add(ProfileAddArgs),
    #[command(visible_alias = "ls")]
    List,
    Test(ProfileTestArgs),
}

#[derive(Args)]
pub struct ProfileAddArgs {
    pub name: String,
    #[arg(long)]
    pub api_key: Option<String>,
    #[arg(long)]
    pub api_key_env: Option<String>,
    #[arg(long)]
    pub api_key_file: Option<PathBuf>,
    #[arg(long, default_value = "RESEND_API_KEY")]
    pub api_key_name: String,
}

#[derive(Args)]
pub struct ProfileTestArgs {
    pub name: String,
}

#[derive(Subcommand)]
pub enum AccountCommand {
    Add(AccountAddArgs),
    #[command(visible_alias = "ls")]
    List,
    Use(AccountUseArgs),
}

#[derive(Args)]
pub struct AccountAddArgs {
    pub email: String,
    #[arg(long)]
    pub profile: String,
    #[arg(long)]
    pub name: Option<String>,
    #[arg(long)]
    pub signature: Option<String>,
    #[arg(long)]
    pub default: bool,
}

#[derive(Args)]
pub struct AccountUseArgs {
    pub email: String,
}

#[derive(Subcommand)]
pub enum SignatureCommand {
    Set(SignatureSetArgs),
    Show(SignatureShowArgs),
}

#[derive(Args)]
pub struct SignatureSetArgs {
    pub account: String,
    #[arg(long)]
    pub text: String,
}

#[derive(Args)]
pub struct SignatureShowArgs {
    pub account: String,
}

#[derive(Args, Clone)]
pub struct ComposeArgs {
    #[arg(long, visible_alias = "from")]
    pub account: Option<String>,
    #[arg(long, required_unless_present = "reply_to_msg")]
    pub to: Vec<String>,
    #[arg(long)]
    pub cc: Vec<String>,
    #[arg(long)]
    pub bcc: Vec<String>,
    #[arg(long, default_value = "")]
    pub subject: String,
    /// Thread this email as a reply to a local message ID
    #[arg(long)]
    pub reply_to_msg: Option<i64>,
    #[arg(long)]
    pub text: Option<String>,
    #[arg(long)]
    pub text_file: Option<PathBuf>,
    #[arg(long)]
    pub html: Option<String>,
    #[arg(long)]
    pub html_file: Option<PathBuf>,
    #[arg(long = "attach")]
    pub attachments: Vec<PathBuf>,
}

#[derive(Args)]
pub struct SendArgs {
    #[command(flatten)]
    pub compose: ComposeArgs,
}

#[derive(Args)]
pub struct ReplyArgs {
    pub message_id: i64,
    #[arg(long, visible_alias = "from")]
    pub account: Option<String>,
    /// Reply to all recipients (preserves CC)
    #[arg(long)]
    pub all: bool,
    /// Additional CC recipients (merged with --all's computed CC, deduped)
    #[arg(long)]
    pub cc: Vec<String>,
    /// BCC recipients
    #[arg(long)]
    pub bcc: Vec<String>,
    #[arg(long)]
    pub text: Option<String>,
    #[arg(long)]
    pub text_file: Option<PathBuf>,
    #[arg(long)]
    pub html: Option<String>,
    #[arg(long)]
    pub html_file: Option<PathBuf>,
    #[arg(long = "attach")]
    pub attachments: Vec<PathBuf>,
}

#[derive(Args)]
pub struct LogArgs {
    /// Number of entries to show
    #[arg(long, default_value = "25")]
    pub limit: usize,
}

#[derive(Args)]
pub struct DaemonArgs {
    /// Account to monitor (default: all accounts)
    #[arg(long)]
    pub account: Option<String>,
    /// Poll interval in seconds
    #[arg(long, default_value = "60")]
    pub interval: u64,
}

#[derive(Subcommand)]
pub enum AutostartCommand {
    /// Install LaunchAgent and load it now
    Install(AutostartInstallArgs),
    /// Remove the LaunchAgent
    Uninstall,
    /// Show LaunchAgent state
    Status(AutostartStatusArgs),
}

#[derive(Args)]
pub struct AutostartInstallArgs {
    /// Account to monitor (default: all accounts)
    #[arg(long)]
    pub account: Option<String>,
    /// Daemon poll interval in seconds
    #[arg(long, default_value = "60")]
    pub interval: u64,
}

#[derive(Args)]
pub struct AutostartStatusArgs {}

#[derive(Args)]
pub struct ForwardArgs {
    pub message_id: i64,
    #[arg(long, visible_alias = "from")]
    pub account: Option<String>,
    #[arg(long, required = true)]
    pub to: Vec<String>,
    #[arg(long)]
    pub cc: Vec<String>,
    #[arg(long)]
    pub bcc: Vec<String>,
    /// Optional preamble text before the forwarded content
    #[arg(long)]
    pub text: Option<String>,
}

#[derive(Subcommand)]
pub enum DraftCommand {
    #[command(visible_alias = "new")]
    Create(DraftCreateArgs),
    #[command(visible_alias = "ls")]
    List(DraftListArgs),
    Show(DraftShowArgs),
    Send(DraftSendArgs),
    Edit(DraftEditArgs),
    #[command(visible_alias = "rm")]
    Delete(DraftDeleteArgs),
}

#[derive(Args)]
pub struct DraftEditArgs {
    pub id: String,
    #[arg(long)]
    pub subject: Option<String>,
    #[arg(long)]
    pub text: Option<String>,
    #[arg(long)]
    pub html: Option<String>,
    #[arg(long)]
    pub to: Option<Vec<String>>,
    #[arg(long)]
    pub cc: Option<Vec<String>>,
    #[arg(long)]
    pub bcc: Option<Vec<String>>,
}

#[derive(Args)]
pub struct DraftDeleteArgs {
    pub id: String,
}

#[derive(Args)]
pub struct DraftCreateArgs {
    #[command(flatten)]
    pub compose: ComposeArgs,
    #[arg(long)]
    pub reply_to: Option<i64>,
}

#[derive(Args)]
pub struct DraftListArgs {
    #[arg(long)]
    pub account: Option<String>,
}

#[derive(Args)]
pub struct DraftShowArgs {
    pub id: String,
}

#[derive(Args)]
pub struct DraftSendArgs {
    pub id: String,
}

#[derive(Args)]
pub struct SyncArgs {
    #[arg(long)]
    pub account: Option<String>,
    #[arg(long, default_value = "25")]
    pub limit: usize,
    /// Watch for new messages continuously
    #[arg(long)]
    pub watch: bool,
    /// Poll interval in seconds (requires --watch)
    #[arg(long, default_value = "60")]
    pub interval: Option<u64>,
    /// Send desktop notifications for new messages
    #[arg(long)]
    pub notify: bool,
}

#[derive(Subcommand)]
pub enum InboxCommand {
    #[command(visible_alias = "ls")]
    List(InboxListArgs),
    /// Sync messages from Resend (shortcut for top-level sync)
    Sync(InboxSyncArgs),
    Read(InboxReadArgs),
    /// Mark messages as read or unread
    Mark(InboxMarkArgs),
    #[command(visible_alias = "rm")]
    Delete(InboxDeleteArgs),
    Archive(InboxArchiveArgs),
    Unarchive(InboxUnarchiveArgs),
    /// Show all messages in a conversation thread
    Thread(InboxThreadArgs),
    Search(InboxSearchArgs),
    Purge(InboxPurgeArgs),
    /// Mailbox counts for sidebar / dashboard
    Stats(InboxStatsArgs),
}

#[derive(Args)]
pub struct InboxSyncArgs {
    #[arg(long)]
    pub account: Option<String>,
    #[arg(long, default_value = "25")]
    pub limit: usize,
}

#[derive(Args)]
pub struct InboxListArgs {
    #[arg(long)]
    pub account: Option<String>,
    #[arg(long, default_value = "25")]
    pub limit: usize,
    #[arg(long)]
    pub unread: bool,
    #[arg(long)]
    pub archived: bool,
    /// Cursor for pagination: return messages with id < this value
    #[arg(long)]
    pub after: Option<i64>,
}

#[derive(Args)]
pub struct InboxReadArgs {
    pub id: i64,
    /// Mark message as read (default: true, use --no-mark-read to skip)
    #[arg(long, default_value = "true", action = clap::ArgAction::Set)]
    pub mark_read: bool,
    #[arg(long)]
    pub raw: bool,
}

#[derive(Args)]
pub struct InboxMarkArgs {
    /// Message IDs to mark
    pub ids: Vec<i64>,
    /// Mark as read
    #[arg(long, group = "state")]
    pub read: bool,
    /// Mark as unread
    #[arg(long, group = "state")]
    pub unread: bool,
}

#[derive(Args)]
pub struct InboxDeleteArgs {
    /// Message IDs to delete (one or more)
    pub ids: Vec<i64>,
}

#[derive(Args)]
pub struct InboxArchiveArgs {
    /// Message IDs to archive (one or more)
    pub ids: Vec<i64>,
}

#[derive(Args)]
pub struct InboxUnarchiveArgs {
    /// Message IDs to unarchive (one or more)
    pub ids: Vec<i64>,
}

#[derive(Args)]
pub struct InboxThreadArgs {
    /// Any message ID in the thread
    pub id: i64,
}

#[derive(Args)]
pub struct InboxSearchArgs {
    pub query: String,
    #[arg(long)]
    pub account: Option<String>,
    #[arg(long, default_value = "25")]
    pub limit: usize,
}

#[derive(Args)]
pub struct InboxStatsArgs {
    #[arg(long)]
    pub account: Option<String>,
}

#[derive(Args)]
pub struct InboxPurgeArgs {
    /// Delete messages older than this date (YYYY-MM-DD)
    #[arg(long)]
    pub before: String,
    #[arg(long)]
    pub account: Option<String>,
}

#[derive(Subcommand)]
pub enum AttachmentsCommand {
    #[command(visible_alias = "ls")]
    List(AttachmentListArgs),
    #[command(visible_alias = "show")]
    Get(AttachmentGetArgs),
}

#[derive(Args)]
pub struct AttachmentListArgs {
    pub message_id: i64,
}

#[derive(Args)]
pub struct AttachmentGetArgs {
    pub message_id: i64,
    pub attachment_id: String,
    #[arg(long)]
    pub output: Option<PathBuf>,
}

#[derive(Subcommand)]
pub enum SkillAction {
    /// Write skill file to all detected agent platforms
    Install,
    /// Check which platforms have the skill installed
    Status,
}

// ── Domain commands ────────────────────────────────────────────────────────

#[derive(Subcommand)]
pub enum DomainCommand {
    #[command(visible_alias = "ls")]
    List,
    #[command(visible_alias = "show")]
    Get(DomainGetArgs),
    #[command(visible_alias = "new")]
    Create(DomainCreateArgs),
    Verify(DomainVerifyArgs),
    #[command(visible_alias = "rm")]
    Delete(DomainDeleteArgs),
    Update(DomainUpdateArgs),
}

#[derive(Args)]
pub struct DomainGetArgs {
    pub id: String,
}

#[derive(Args)]
pub struct DomainCreateArgs {
    #[arg(long)]
    pub name: String,
    #[arg(long)]
    pub region: Option<String>,
}

#[derive(Args)]
pub struct DomainVerifyArgs {
    pub id: String,
}

#[derive(Args)]
pub struct DomainDeleteArgs {
    pub id: String,
}

#[derive(Args)]
pub struct DomainUpdateArgs {
    pub id: String,
    #[arg(long)]
    pub open_tracking: Option<bool>,
    #[arg(long)]
    pub click_tracking: Option<bool>,
}

// ── Contact commands ───────────────────────────────────────────────────────

#[derive(Subcommand)]
pub enum ContactCommand {
    #[command(visible_alias = "ls")]
    List(ContactListArgs),
    #[command(visible_alias = "show")]
    Get(ContactGetArgs),
    #[command(visible_alias = "new")]
    Create(ContactCreateArgs),
    Update(ContactUpdateArgs),
    #[command(visible_alias = "rm")]
    Delete(ContactDeleteArgs),
}

#[derive(Args)]
pub struct ContactListArgs {
    /// Number of contacts to return (1-100). Defaults to 50.
    #[arg(long, default_value = "50", value_parser = parse_resend_limit)]
    pub limit: usize,
    /// Cursor: return contacts after this contact id.
    #[arg(long)]
    pub after: Option<String>,
}

#[derive(Args)]
pub struct ContactGetArgs {
    /// Contact id or email address.
    pub id_or_email: String,
}

#[derive(Args)]
pub struct ContactCreateArgs {
    #[arg(long)]
    pub email: String,
    #[arg(long)]
    pub first_name: Option<String>,
    #[arg(long)]
    pub last_name: Option<String>,
    #[arg(long)]
    pub unsubscribed: Option<bool>,
    /// Custom contact properties as a JSON object (e.g. '{"company":"Acme","plan":"pro"}').
    /// Property keys must be defined first via `email-cli contact-property create`.
    #[arg(long, value_name = "JSON")]
    pub properties: Option<String>,
    /// Comma-separated segment ids to add this contact to at create time
    /// (e.g. --segments seg_abc123,seg_def456).
    #[arg(long, value_name = "ID,ID,...")]
    pub segments: Option<String>,
    /// Comma-separated topic subscriptions to set at create time, each formatted as
    /// `topic_id:opt_in` or `topic_id:opt_out` (e.g. --topics top_xxx:opt_in,top_yyy:opt_out).
    #[arg(long, value_name = "TOPIC:STATE,...")]
    pub topics: Option<String>,
}

#[derive(Args)]
pub struct ContactUpdateArgs {
    /// Contact id or email address.
    pub id_or_email: String,
    #[arg(long)]
    pub first_name: Option<String>,
    #[arg(long)]
    pub last_name: Option<String>,
    #[arg(long)]
    pub unsubscribed: Option<bool>,
    /// Custom contact properties as a JSON object. Replaces existing values for the keys present.
    #[arg(long, value_name = "JSON")]
    pub properties: Option<String>,
}

#[derive(Args)]
pub struct ContactDeleteArgs {
    /// Contact id or email address.
    pub id_or_email: String,
}

// ── Batch commands ─────────────────────────────────────────────────────────

#[derive(Subcommand)]
pub enum BatchCommand {
    Send(BatchSendArgs),
}

#[derive(Args)]
pub struct BatchSendArgs {
    /// Path to a JSON file containing an array of email objects
    #[arg(long)]
    pub file: std::path::PathBuf,
}

// ── API key commands ───────────────────────────────────────────────────────

#[derive(Subcommand)]
pub enum ApiKeyCommand {
    #[command(visible_alias = "ls")]
    List,
    #[command(visible_alias = "new")]
    Create(ApiKeyCreateArgs),
    #[command(visible_alias = "rm")]
    Delete(ApiKeyDeleteArgs),
}

#[derive(Args)]
pub struct ApiKeyCreateArgs {
    #[arg(long)]
    pub name: String,
    /// full-access or sending-access
    #[arg(long, default_value = "full-access")]
    pub permission: String,
}

#[derive(Args)]
pub struct ApiKeyDeleteArgs {
    pub id: String,
}

// ── Outbox commands ───────────────────────────────────────────────────────

#[derive(Subcommand)]
pub enum OutboxCommand {
    #[command(visible_alias = "ls")]
    List,
    Retry(OutboxRetryArgs),
    Flush,
}

#[derive(Args)]
pub struct OutboxRetryArgs {
    pub id: String,
}

// ── Webhook commands ──────────────────────────────────────────────────────

#[derive(Subcommand)]
pub enum WebhookCommand {
    Listen(WebhookListenArgs),
}

#[derive(Args)]
pub struct WebhookListenArgs {
    #[arg(long, default_value = "8080")]
    pub port: u16,
    /// Send desktop notifications for new messages
    #[arg(long)]
    pub notify: bool,
}

// ── Events commands ───────────────────────────────────────────────────────

#[derive(Subcommand)]
pub enum EventsCommand {
    #[command(visible_alias = "ls")]
    List(EventsListArgs),
}

#[derive(Args)]
pub struct EventsListArgs {
    #[arg(long)]
    pub message: Option<i64>,
    #[arg(long, default_value = "50")]
    pub limit: usize,
}

// ── Email commands (Resend GET /emails wrapper) ────────────────────────────

#[derive(Subcommand)]
pub enum EmailCommand {
    /// List sent emails. Each row includes `last_event` for poll-based status checks.
    #[command(visible_alias = "ls")]
    List(EmailListArgs),
}

#[derive(Args)]
pub struct EmailListArgs {
    /// Number of emails to return (1-100). Defaults to 20.
    #[arg(long, default_value = "20", value_parser = parse_resend_limit)]
    pub limit: usize,
    /// Cursor: return emails created after this email id.
    #[arg(long)]
    pub after: Option<String>,
}

// ── Broadcast commands ─────────────────────────────────────────────────────

#[derive(Subcommand)]
pub enum BroadcastCommand {
    #[command(visible_alias = "ls")]
    List,
    #[command(visible_alias = "show")]
    Get(BroadcastGetArgs),
    #[command(visible_alias = "new")]
    Create(BroadcastCreateArgs),
    Update(BroadcastUpdateArgs),
    Send(BroadcastSendArgs),
    #[command(visible_alias = "rm")]
    Delete(BroadcastDeleteArgs),
}

#[derive(Args)]
pub struct BroadcastUpdateArgs {
    pub id: String,
    /// Change the target segment.
    #[arg(long)]
    pub segment_id: Option<String>,
    #[arg(long)]
    pub from: Option<String>,
    #[arg(long)]
    pub subject: Option<String>,
    #[arg(long)]
    pub html: Option<String>,
    #[arg(long)]
    pub text: Option<String>,
    #[arg(long)]
    pub name: Option<String>,
    /// Reply-to address(es), comma-separated.
    #[arg(long)]
    pub reply_to: Option<String>,
    /// Topic ID for per-recipient unsubscribe wiring.
    #[arg(long)]
    pub topic_id: Option<String>,
}

#[derive(Args)]
pub struct BroadcastGetArgs {
    pub id: String,
}

#[derive(Args)]
pub struct BroadcastCreateArgs {
    /// Segment ID to send to. Accepts a segment id or (legacy) audience id.
    #[arg(long, visible_alias = "audience-id")]
    pub segment_id: String,
    /// Sender address (e.g. "Name <sender@example.com>").
    #[arg(long)]
    pub from: String,
    #[arg(long)]
    pub subject: String,
    /// HTML body. Use `{{{RESEND_UNSUBSCRIBE_URL}}}` for the auto-injected unsubscribe link.
    #[arg(long)]
    pub html: Option<String>,
    /// Plain text body.
    #[arg(long)]
    pub text: Option<String>,
    /// Internal name for the broadcast (not shown to recipients).
    #[arg(long)]
    pub name: Option<String>,
    /// Reply-to address(es), comma-separated.
    #[arg(long)]
    pub reply_to: Option<String>,
    /// Topic ID to scope this broadcast to (drives per-recipient unsubscribe URL).
    #[arg(long)]
    pub topic_id: Option<String>,
    /// Schedule send (ISO-8601 / RFC-3339 timestamp, or natural language per Resend).
    #[arg(long)]
    pub scheduled_at: Option<String>,
    /// Send the broadcast immediately after creation (single API call).
    #[arg(long)]
    pub send: bool,
}

#[derive(Args)]
pub struct BroadcastSendArgs {
    pub id: String,
    /// Optional ISO-8601 / RFC-3339 timestamp to schedule the send.
    #[arg(long)]
    pub scheduled_at: Option<String>,
}

#[derive(Args)]
pub struct BroadcastDeleteArgs {
    pub id: String,
}

// ── Contact-property schema commands ───────────────────────────────────────

#[derive(Subcommand)]
pub enum ContactPropertyCommand {
    #[command(visible_alias = "ls")]
    List,
    #[command(visible_alias = "show")]
    Get(ContactPropertyGetArgs),
    #[command(visible_alias = "new")]
    Create(ContactPropertyCreateArgs),
    Update(ContactPropertyUpdateArgs),
    #[command(visible_alias = "rm")]
    Delete(ContactPropertyDeleteArgs),
}

#[derive(Args)]
pub struct ContactPropertyUpdateArgs {
    pub id: String,
    /// New fallback value. Pass numbers as their text representation; we'll send them as
    /// numbers when `--as-number` is set, otherwise as strings.
    #[arg(long)]
    pub fallback: Option<String>,
    /// Treat `--fallback` as a number rather than a string.
    #[arg(long)]
    pub as_number: bool,
}

#[derive(Args)]
pub struct ContactPropertyGetArgs {
    pub id: String,
}

#[derive(Args)]
pub struct ContactPropertyCreateArgs {
    /// Property key. Alphanumeric and underscores only, max 50 chars.
    #[arg(long)]
    pub key: String,
    /// Property type: "string" or "number".
    #[arg(long, default_value = "string")]
    pub property_type: String,
    /// Optional fallback value when the property is not set on a contact (must match type).
    #[arg(long)]
    pub fallback: Option<String>,
}

#[derive(Args)]
pub struct ContactPropertyDeleteArgs {
    pub id: String,
}

// ── Topic commands ─────────────────────────────────────────────────────────

#[derive(Subcommand)]
pub enum TopicCommand {
    #[command(visible_alias = "ls")]
    List,
    #[command(visible_alias = "show")]
    Get(TopicGetArgs),
    #[command(visible_alias = "new")]
    Create(TopicCreateArgs),
    Update(TopicUpdateArgs),
    #[command(visible_alias = "rm")]
    Delete(TopicDeleteArgs),
    /// Subscribe / unsubscribe a contact to a topic
    ContactSet(TopicContactSetArgs),
    /// List a contact's topic subscriptions
    ContactList(TopicContactListArgs),
}

#[derive(Args)]
pub struct TopicUpdateArgs {
    pub id: String,
    #[arg(long)]
    pub name: Option<String>,
    #[arg(long)]
    pub description: Option<String>,
    /// Default subscription state for new contacts: "opt_in" or "opt_out".
    #[arg(long)]
    pub default_subscription: Option<String>,
    /// "public" or "private" — controls visibility on the hosted preference page.
    #[arg(long)]
    pub visibility: Option<String>,
}

#[derive(Args)]
pub struct TopicContactListArgs {
    /// Contact id or email
    #[arg(long)]
    pub contact: String,
}

#[derive(Args)]
pub struct TopicGetArgs {
    pub id: String,
}

#[derive(Args)]
pub struct TopicCreateArgs {
    #[arg(long)]
    pub name: String,
    #[arg(long)]
    pub description: Option<String>,
    /// Default subscription state for new contacts: "opt_in" or "opt_out".
    #[arg(long)]
    pub default_subscription: Option<String>,
    /// "public" or "private" — controls visibility on the hosted preference page.
    #[arg(long)]
    pub visibility: Option<String>,
}

#[derive(Args)]
pub struct TopicDeleteArgs {
    pub id: String,
}

#[derive(Args)]
pub struct TopicContactSetArgs {
    /// Contact id or email
    #[arg(long)]
    pub contact: String,
    /// Topic id
    #[arg(long)]
    pub topic: String,
    /// Subscription state: "opt_in" or "opt_out".
    #[arg(long)]
    pub subscription: String,
}

// ── Segment commands (Audiences renamed to Segments in November 2025) ─────

#[derive(Subcommand)]
pub enum SegmentCommand {
    #[command(visible_alias = "ls")]
    List,
    #[command(visible_alias = "show")]
    Get(SegmentGetArgs),
    #[command(visible_alias = "new")]
    Create(SegmentCreateArgs),
    #[command(visible_alias = "rm")]
    Delete(SegmentDeleteArgs),
    /// Add a contact to a segment
    ContactAdd(SegmentContactArgs),
    /// Remove a contact from a segment
    ContactRemove(SegmentContactArgs),
    /// List the segments a contact belongs to
    ContactList(SegmentContactListArgs),
    /// List the contacts in a segment
    Contacts(SegmentContactsArgs),
}

#[derive(Args)]
pub struct SegmentContactsArgs {
    /// Segment id
    pub id: String,
}

#[derive(Args)]
pub struct SegmentGetArgs {
    pub id: String,
}

#[derive(Args)]
pub struct SegmentCreateArgs {
    #[arg(long)]
    pub name: String,
}

#[derive(Args)]
pub struct SegmentDeleteArgs {
    pub id: String,
}

#[derive(Args)]
pub struct SegmentContactArgs {
    /// Contact id or email
    #[arg(long)]
    pub contact: String,
    /// Segment id
    #[arg(long)]
    pub segment: String,
}

#[derive(Args)]
pub struct SegmentContactListArgs {
    /// Contact id or email
    #[arg(long)]
    pub contact: String,
}