vortex-sdk 1.19.0

Vortex Rust SDK for authentication and invitation management
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
use serde::{Deserialize, Serialize};
use std::collections::HashMap;

// ============================================================================
// Enums for type-safe API values
// ============================================================================

/// Target type for invitation responses (who was invited)
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum InvitationTargetType {
    Email,
    Phone,
    Share,
    Internal,
}

/// Target type for creating invitations
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum CreateInvitationTargetType {
    Email,
    Phone,
    Internal,
}

/// Type of invitation
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum InvitationType {
    SingleUse,
    MultiUse,
    Autojoin,
}

/// Current status of an invitation
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum InvitationStatus {
    Queued,
    Sending,
    Sent,
    Delivered,
    Accepted,
    Shared,
    Unfurled,
    AcceptedElsewhere,
}

/// Delivery type for invitations
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum DeliveryType {
    Email,
    Phone,
    Share,
    Internal,
}

// ============================================================================
// Core types
// ============================================================================

/// User type for JWT generation
/// Optional fields: name (max 200 chars), avatar_url (HTTPS URL, max 2000 chars), admin_scopes, allowed_email_domains
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct User {
    pub id: String,
    pub email: String,
    /// User's display name (preferred)
    #[serde(skip_serializing_if = "Option::is_none")]
    pub name: Option<String>,
    /// User's avatar URL (preferred)
    #[serde(skip_serializing_if = "Option::is_none")]
    pub avatar_url: Option<String>,
    /// Deprecated: use name instead
    #[serde(skip_serializing_if = "Option::is_none")]
    pub user_name: Option<String>,
    /// Deprecated: use avatar_url instead
    #[serde(skip_serializing_if = "Option::is_none")]
    pub user_avatar_url: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub admin_scopes: Option<Vec<String>>,
    /// Optional list of allowed email domains for invitation restrictions (e.g., ["acme.com", "acme.org"])
    #[serde(skip_serializing_if = "Option::is_none")]
    pub allowed_email_domains: Option<Vec<String>>,
    /// Additional fields included in signing
    #[serde(flatten)]
    pub extra: Option<HashMap<String, serde_json::Value>>,
}

impl User {
    pub fn new(id: &str, email: &str) -> Self {
        Self {
            id: id.to_string(),
            email: email.to_string(),
            name: None,
            avatar_url: None,
            user_name: None,
            user_avatar_url: None,
            admin_scopes: None,
            allowed_email_domains: None,
            extra: None,
        }
    }

    pub fn with_name(mut self, name: &str) -> Self {
        self.name = Some(name.to_string());
        self
    }

    pub fn with_avatar_url(mut self, avatar_url: &str) -> Self {
        self.avatar_url = Some(avatar_url.to_string());
        self
    }

    /// Deprecated: use with_name instead
    #[deprecated(note = "Use with_name instead")]
    pub fn with_user_name(mut self, name: &str) -> Self {
        self.user_name = Some(name.to_string());
        self
    }

    /// Deprecated: use with_avatar_url instead
    #[deprecated(note = "Use with_avatar_url instead")]
    pub fn with_user_avatar_url(mut self, avatar_url: &str) -> Self {
        self.user_avatar_url = Some(avatar_url.to_string());
        self
    }

    pub fn with_admin_scopes(mut self, scopes: Vec<String>) -> Self {
        self.admin_scopes = Some(scopes);
        self
    }

    pub fn with_allowed_email_domains(mut self, domains: Vec<String>) -> Self {
        self.allowed_email_domains = Some(domains);
        self
    }
}

/// Identifier for a user (email, sms, etc.)
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Identifier {
    #[serde(rename = "type")]
    pub identifier_type: String,
    pub value: String,
}

impl Identifier {
    pub fn new(identifier_type: &str, value: &str) -> Self {
        Self {
            identifier_type: identifier_type.to_string(),
            value: value.to_string(),
        }
    }
}

/// Group information for JWT generation (input)
/// Supports both 'id' (legacy) and 'groupId' (preferred) for backward compatibility
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct Group {
    #[serde(rename = "type")]
    pub scope_type: String,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub id: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub scope: Option<String>,
    #[serde(rename = "groupId")]
    pub name: String,
}

impl Group {
    pub fn new(scope_type: &str, name: &str) -> Self {
        Self {
            scope_type: scope_type.to_string(),
            id: None,
            scope: None,
            name: name.to_string(),
        }
    }

    pub fn with_id(mut self, id: &str) -> Self {
        self.id = Some(id.to_string());
        self
    }

    pub fn with_scope(mut self, scope: &str) -> Self {
        self.scope = Some(scope.to_string());
        self
    }
}

/// Invitation group from API responses
/// This matches the MemberGroups table structure from the API
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct InvitationScope {
    /// Vortex internal UUID
    pub id: String,
    /// Vortex account ID
    pub account_id: String,
    /// Customer's group ID (the ID they provided to Vortex)
    #[serde(rename = "groupId")]
    pub scope: String,
    /// Preferred alias for scope (= groupId value). Populated by SDK.
    #[serde(skip)]
    pub scope_id: String,
    /// Group type (e.g., "workspace", "team")
    #[serde(rename = "type")]
    pub scope_type: String,
    /// Group name
    pub name: String,
    /// ISO 8601 timestamp when the group was created
    pub created_at: String,
}

/// Invitation target from API responses
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct InvitationTarget {
    #[serde(rename = "type")]
    pub target_type: InvitationTargetType,
    pub value: String,
    /// Display name of the person being invited
    #[serde(skip_serializing_if = "Option::is_none")]
    pub name: Option<String>,
    /// Avatar URL for the person being invited (for display in invitation lists)
    #[serde(skip_serializing_if = "Option::is_none")]
    #[serde(rename = "avatarUrl")]
    pub avatar_url: Option<String>,
}

impl InvitationTarget {
    pub fn new(target_type: InvitationTargetType, value: &str) -> Self {
        Self {
            target_type,
            value: value.to_string(),
            name: None,
            avatar_url: None,
        }
    }

    pub fn email(value: &str) -> Self {
        Self::new(InvitationTargetType::Email, value)
    }

    pub fn phone(value: &str) -> Self {
        Self::new(InvitationTargetType::Phone, value)
    }

    pub fn with_name(mut self, name: &str) -> Self {
        self.name = Some(name.to_string());
        self
    }

    pub fn with_avatar_url(mut self, avatar_url: &str) -> Self {
        self.avatar_url = Some(avatar_url.to_string());
        self
    }
}

/// User data for accepting invitations (preferred format)
///
/// At least one of email or phone must be provided.
///
/// # Example
///
/// ```
/// use vortex_sdk::AcceptUser;
///
/// // With email only
/// let user = AcceptUser::new().with_email("user@example.com");
///
/// // With email and name
/// let user = AcceptUser::new()
///     .with_email("user@example.com")
///     .with_name("John Doe");
///
/// // With all fields
/// let user = AcceptUser::new()
///     .with_email("user@example.com")
///     .with_phone("+1234567890")
///     .with_name("John Doe")
///     .with_is_existing(false);
/// ```
#[derive(Debug, Clone, Serialize, Deserialize, Default)]
pub struct AcceptUser {
    #[serde(skip_serializing_if = "Option::is_none")]
    pub email: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub phone: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub name: Option<String>,
    /// Whether the accepting user is an existing user in your system.
    /// Set to true if the user was already registered before accepting the invitation.
    /// Set to false if this is a new user signup.
    /// Leave as None if unknown.
    /// Used for analytics to track new vs existing user conversions.
    #[serde(rename = "isExisting", skip_serializing_if = "Option::is_none")]
    pub is_existing: Option<bool>,
}

impl AcceptUser {
    pub fn new() -> Self {
        Self::default()
    }

    pub fn with_email(mut self, email: &str) -> Self {
        self.email = Some(email.to_string());
        self
    }

    pub fn with_phone(mut self, phone: &str) -> Self {
        self.phone = Some(phone.to_string());
        self
    }

    pub fn with_name(mut self, name: &str) -> Self {
        self.name = Some(name.to_string());
        self
    }

    pub fn with_is_existing(mut self, is_existing: bool) -> Self {
        self.is_existing = Some(is_existing);
        self
    }
}

/// Invitation acceptance information
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct InvitationAcceptance {
    pub id: Option<String>,
    pub account_id: Option<String>,
    pub accepted_at: Option<String>,
    pub target: Option<InvitationTarget>,
}

/// Full invitation details
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct Invitation {
    #[serde(default)]
    pub id: String,
    #[serde(default)]
    pub account_id: String,
    #[serde(default)]
    pub click_throughs: u32,
    /// Invitation form data submitted by the user, including email addresses of invitees and the values of any custom fields.
    pub form_submission_data: Option<HashMap<String, serde_json::Value>>,
    /// Deprecated: Use `form_submission_data` instead. Contains the same data.
    #[deprecated(note = "Use form_submission_data instead")]
    pub configuration_attributes: Option<HashMap<String, serde_json::Value>>,
    pub attributes: Option<HashMap<String, serde_json::Value>>,
    #[serde(default)]
    pub created_at: String,
    #[serde(default)]
    pub deactivated: bool,
    #[serde(default)]
    pub delivery_count: u32,
    #[serde(default)]
    pub delivery_types: Vec<DeliveryType>,
    #[serde(default)]
    pub foreign_creator_id: String,
    pub invitation_type: InvitationType,
    pub modified_at: Option<String>,
    pub status: InvitationStatus,
    #[serde(default)]
    pub target: Vec<InvitationTarget>,
    #[serde(default)]
    pub views: u32,
    #[serde(default)]
    pub widget_configuration_id: String,
    #[serde(default)]
    pub groups: Vec<InvitationScope>,
    /// Preferred alias for groups. Each element also has scope_id. Populated by SDK.
    #[serde(skip)]
    pub scopes: Vec<InvitationScope>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub accepts: Option<Vec<InvitationAcceptance>>,
    pub expired: bool,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub expires: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub source: Option<String>,
    /// Customer-defined subtype for analytics segmentation (e.g., "pymk", "find-friends")
    #[serde(skip_serializing_if = "Option::is_none")]
    pub subtype: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub creator_name: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub creator_avatar_url: Option<String>,
}

/// Response containing multiple invitations
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct InvitationsResponse {
    pub invitations: Option<Vec<Invitation>>,
}

/// Accept invitation parameter - supports both new User format and legacy Target format
#[derive(Debug, Clone)]
pub enum AcceptInvitationParam {
    /// New User format (preferred)
    User(AcceptUser),
    /// Legacy target format (deprecated)
    Target(InvitationTarget),
    /// Legacy multiple targets format (deprecated)
    Targets(Vec<InvitationTarget>),
}

impl From<AcceptUser> for AcceptInvitationParam {
    fn from(user: AcceptUser) -> Self {
        AcceptInvitationParam::User(user)
    }
}

impl From<InvitationTarget> for AcceptInvitationParam {
    fn from(target: InvitationTarget) -> Self {
        AcceptInvitationParam::Target(target)
    }
}

impl From<Vec<InvitationTarget>> for AcceptInvitationParam {
    fn from(targets: Vec<InvitationTarget>) -> Self {
        AcceptInvitationParam::Targets(targets)
    }
}

// --- Types for creating invitations via backend API ---

/// Target for creating an invitation
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CreateInvitationTarget {
    #[serde(rename = "type")]
    pub target_type: CreateInvitationTargetType,
    /// Target value: email address, phone number, or internal user ID
    pub value: String,
    /// Display name of the person being invited
    #[serde(skip_serializing_if = "Option::is_none")]
    pub name: Option<String>,
    /// Avatar URL for the person being invited (for display in invitation lists)
    #[serde(skip_serializing_if = "Option::is_none")]
    #[serde(rename = "avatarUrl")]
    pub avatar_url: Option<String>,
}

impl CreateInvitationTarget {
    pub fn new(target_type: CreateInvitationTargetType, value: &str) -> Self {
        Self {
            target_type,
            value: value.to_string(),
            name: None,
            avatar_url: None,
        }
    }

    pub fn email(value: &str) -> Self {
        Self::new(CreateInvitationTargetType::Email, value)
    }

    pub fn phone(value: &str) -> Self {
        Self::new(CreateInvitationTargetType::Phone, value)
    }

    /// Alias for phone (backward compatibility)
    pub fn sms(value: &str) -> Self {
        Self::phone(value)
    }

    pub fn internal(value: &str) -> Self {
        Self::new(CreateInvitationTargetType::Internal, value)
    }

    pub fn with_name(mut self, name: &str) -> Self {
        self.name = Some(name.to_string());
        self
    }

    pub fn with_avatar_url(mut self, avatar_url: &str) -> Self {
        self.avatar_url = Some(avatar_url.to_string());
        self
    }
}

/// Information about the user creating the invitation (the inviter)
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct Inviter {
    /// Required: Your internal user ID for the inviter
    pub user_id: String,
    /// Optional: Email of the inviter
    #[serde(skip_serializing_if = "Option::is_none")]
    pub user_email: Option<String>,
    /// Optional: Display name of the inviter (preferred)
    #[serde(skip_serializing_if = "Option::is_none")]
    pub name: Option<String>,
    /// Optional: Avatar URL of the inviter (preferred)
    #[serde(skip_serializing_if = "Option::is_none")]
    pub avatar_url: Option<String>,
    /// Deprecated: use name instead
    #[serde(skip_serializing_if = "Option::is_none")]
    pub user_name: Option<String>,
    /// Deprecated: use avatar_url instead
    #[serde(skip_serializing_if = "Option::is_none")]
    pub user_avatar_url: Option<String>,
}

impl Inviter {
    pub fn new(user_id: &str) -> Self {
        Self {
            user_id: user_id.to_string(),
            user_email: None,
            name: None,
            avatar_url: None,
            user_name: None,
            user_avatar_url: None,
        }
    }

    pub fn with_email(mut self, email: &str) -> Self {
        self.user_email = Some(email.to_string());
        self
    }

    pub fn with_name(mut self, name: &str) -> Self {
        self.name = Some(name.to_string());
        self
    }

    pub fn with_avatar_url(mut self, url: &str) -> Self {
        self.avatar_url = Some(url.to_string());
        self
    }

    /// Deprecated: use with_name instead
    #[deprecated(note = "Use with_name instead")]
    pub fn with_user_name(mut self, name: &str) -> Self {
        self.user_name = Some(name.to_string());
        self
    }

    /// Deprecated: use with_avatar_url instead
    #[deprecated(note = "Use with_avatar_url instead")]
    pub fn with_user_avatar_url(mut self, url: &str) -> Self {
        self.user_avatar_url = Some(url.to_string());
        self
    }
}

/// Group information for creating invitations
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct CreateInvitationScope {
    /// Group type (e.g., "team", "organization")
    #[serde(rename = "type")]
    pub scope_type: String,
    /// Your internal group ID
    #[serde(rename = "groupId")]
    pub scope: String,
    /// Display name of the group
    pub name: String,
}

impl CreateInvitationScope {
    pub fn new(scope_type: &str, scope: &str, name: &str) -> Self {
        Self {
            scope_type: scope_type.to_string(),
            scope: scope.to_string(),
            name: name.to_string(),
        }
    }
}

/// Valid Open Graph types for unfurl configuration
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum UnfurlOgType {
    Website,
    Article,
    Video,
    Music,
    Book,
    Profile,
    Product,
}

/// Configuration for link unfurl (Open Graph) metadata
/// Controls how the invitation link appears when shared on social platforms or messaging apps
#[derive(Debug, Clone, Serialize, Deserialize, Default)]
#[serde(rename_all = "camelCase")]
pub struct UnfurlConfig {
    /// The title shown in link previews (og:title)
    #[serde(skip_serializing_if = "Option::is_none")]
    pub title: Option<String>,
    /// The description shown in link previews (og:description)
    #[serde(skip_serializing_if = "Option::is_none")]
    pub description: Option<String>,
    /// The image URL shown in link previews (og:image) - must be HTTPS
    #[serde(skip_serializing_if = "Option::is_none")]
    pub image: Option<String>,
    /// The Open Graph type (og:type)
    #[serde(skip_serializing_if = "Option::is_none", rename = "type")]
    pub og_type: Option<UnfurlOgType>,
    /// The site name shown in link previews (og:site_name)
    #[serde(skip_serializing_if = "Option::is_none")]
    pub site_name: Option<String>,
}

impl UnfurlConfig {
    pub fn new() -> Self {
        Self::default()
    }

    pub fn with_title(mut self, title: &str) -> Self {
        self.title = Some(title.to_string());
        self
    }

    pub fn with_description(mut self, description: &str) -> Self {
        self.description = Some(description.to_string());
        self
    }

    pub fn with_image(mut self, image: &str) -> Self {
        self.image = Some(image.to_string());
        self
    }

    pub fn with_type(mut self, og_type: UnfurlOgType) -> Self {
        self.og_type = Some(og_type);
        self
    }

    pub fn with_site_name(mut self, site_name: &str) -> Self {
        self.site_name = Some(site_name.to_string());
        self
    }
}

/// Request body for creating an invitation
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct CreateInvitationRequest {
    pub widget_configuration_id: String,
    pub target: CreateInvitationTarget,
    pub inviter: Inviter,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub groups: Option<Vec<CreateInvitationScope>>,
    /// Preferred: flat scope ID for single scope (takes priority over groups/scopes)
    #[serde(skip)]
    pub scope_id: Option<String>,
    /// Scope type when using flat scope_id param
    #[serde(skip)]
    pub scope_type_flat: Option<String>,
    /// Scope name when using flat scope_id param
    #[serde(skip)]
    pub scope_name: Option<String>,
    /// Deprecated: use scope_id/scope_type_flat/scope_name or groups
    #[serde(skip)]
    pub scopes: Option<Vec<CreateInvitationScope>>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub source: Option<String>,
    /// Customer-defined subtype for analytics segmentation (e.g., "pymk", "find-friends")
    #[serde(skip_serializing_if = "Option::is_none")]
    pub subtype: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub template_variables: Option<HashMap<String, String>>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub metadata: Option<HashMap<String, serde_json::Value>>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub unfurl_config: Option<UnfurlConfig>,
}

impl CreateInvitationRequest {
    pub fn new(
        widget_configuration_id: &str,
        target: CreateInvitationTarget,
        inviter: Inviter,
    ) -> Self {
        Self {
            widget_configuration_id: widget_configuration_id.to_string(),
            target,
            inviter,
            groups: None,
            scope_id: None,
            scope_type_flat: None,
            scope_name: None,
            scopes: None,
            source: None,
            subtype: None,
            template_variables: None,
            metadata: None,
            unfurl_config: None,
        }
    }

    pub fn with_groups(mut self, groups: Vec<CreateInvitationScope>) -> Self {
        self.groups = Some(groups);
        self
    }

    /// Set a single scope using flat params (preferred over with_groups)
    pub fn with_scope(mut self, scope_id: &str, scope_type: &str, scope_name: &str) -> Self {
        self.scope_id = Some(scope_id.to_string());
        self.scope_type_flat = Some(scope_type.to_string());
        self.scope_name = Some(scope_name.to_string());
        self
    }

    pub fn with_source(mut self, source: &str) -> Self {
        self.source = Some(source.to_string());
        self
    }

    pub fn with_subtype(mut self, subtype: &str) -> Self {
        self.subtype = Some(subtype.to_string());
        self
    }

    pub fn with_template_variables(mut self, vars: HashMap<String, String>) -> Self {
        self.template_variables = Some(vars);
        self
    }

    pub fn with_metadata(mut self, metadata: HashMap<String, serde_json::Value>) -> Self {
        self.metadata = Some(metadata);
        self
    }

    pub fn with_unfurl_config(mut self, unfurl_config: UnfurlConfig) -> Self {
        self.unfurl_config = Some(unfurl_config);
        self
    }
}

/// Response from creating an invitation
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct CreateInvitationResponse {
    /// The ID of the created invitation
    pub id: String,
    /// The short link for the invitation
    pub short_link: String,
    /// The status of the invitation
    pub status: String,
    /// When the invitation was created
    pub created_at: String,
}

// --- Types for syncing internal invitation actions ---

/// Request body for syncing an internal invitation action
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct SyncInternalInvitationRequest {
    /// The inviter's user ID
    pub creator_id: String,
    /// The invitee's user ID
    pub target_value: String,
    /// The action taken: "accepted" or "declined"
    pub action: String,
    /// The widget component UUID
    pub component_id: String,
}

impl SyncInternalInvitationRequest {
    pub fn new(creator_id: &str, target_value: &str, action: &str, component_id: &str) -> Self {
        Self {
            creator_id: creator_id.to_string(),
            target_value: target_value.to_string(),
            action: action.to_string(),
            component_id: component_id.to_string(),
        }
    }
}

/// Response from syncing an internal invitation action
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct SyncInternalInvitationResponse {
    /// Number of invitations processed
    pub processed: u32,
    /// IDs of the invitations that were processed
    pub invitation_ids: Vec<String>,
}

// --- Types for autojoin domain management ---

/// Represents an autojoin domain configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct AutojoinDomain {
    pub id: String,
    pub domain: String,
}

/// Response from autojoin API endpoints
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct AutojoinDomainsResponse {
    pub autojoin_domains: Vec<AutojoinDomain>,
    pub invitation: Option<Invitation>,
}

/// Request body for configuring autojoin domains
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct ConfigureAutojoinRequest {
    pub scope: String,
    pub scope_type: String,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub scope_name: Option<String>,
    pub domains: Vec<String>,
    pub component_id: String,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub metadata: Option<HashMap<String, serde_json::Value>>,
}

impl ConfigureAutojoinRequest {
    pub fn new(scope: &str, scope_type: &str, domains: Vec<String>, component_id: &str) -> Self {
        Self {
            scope: scope.to_string(),
            scope_type: scope_type.to_string(),
            scope_name: None,
            domains,
            component_id: component_id.to_string(),
            metadata: None,
        }
    }

    pub fn with_scope_name(mut self, scope_name: &str) -> Self {
        self.scope_name = Some(scope_name.to_string());
        self
    }

    pub fn with_metadata(mut self, metadata: HashMap<String, serde_json::Value>) -> Self {
        self.metadata = Some(metadata);
        self
    }
}

// Deprecated type aliases for backward compatibility
#[deprecated(since = "0.1.0", note = "Use InvitationScope instead")]
pub type InvitationGroup = InvitationScope;

#[deprecated(since = "0.1.0", note = "Use CreateInvitationScope instead")]
pub type CreateInvitationGroup = CreateInvitationScope;

// ─── generate_token types ────────────────────────────────────

/// Helper function to check if a string is empty (for serde skip_serializing_if)
fn is_empty_string(s: &String) -> bool {
    s.is_empty()
}

/// User data for generate_token
#[derive(Debug, Clone, Serialize, Deserialize, Default)]
#[serde(rename_all = "camelCase")]
pub struct TokenUser {
    #[serde(skip_serializing_if = "is_empty_string")]
    pub id: String,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub name: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub email: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub avatar_url: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub admin_scopes: Option<Vec<String>>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub allowed_email_domains: Option<Vec<String>>,
    #[serde(flatten)]
    pub extra: Option<HashMap<String, serde_json::Value>>,
}

impl TokenUser {
    pub fn new(id: &str) -> Self {
        Self {
            id: id.to_string(),
            ..Default::default()
        }
    }

    pub fn with_name(mut self, name: &str) -> Self {
        self.name = Some(name.to_string());
        self
    }

    pub fn with_email(mut self, email: &str) -> Self {
        self.email = Some(email.to_string());
        self
    }
}

/// Payload for generate_token
#[derive(Debug, Clone, Serialize, Deserialize, Default)]
#[serde(rename_all = "camelCase")]
pub struct GenerateTokenPayload {
    #[serde(skip_serializing_if = "Option::is_none")]
    pub user: Option<TokenUser>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub component: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub scope: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub vars: Option<HashMap<String, serde_json::Value>>,
    #[serde(flatten)]
    pub extra: Option<HashMap<String, serde_json::Value>>,
}

impl GenerateTokenPayload {
    pub fn new() -> Self {
        Self::default()
    }

    pub fn with_user(mut self, user: TokenUser) -> Self {
        self.user = Some(user);
        self
    }

    pub fn with_component(mut self, component: &str) -> Self {
        self.component = Some(component.to_string());
        self
    }

    pub fn with_scope(mut self, scope: &str) -> Self {
        self.scope = Some(scope.to_string());
        self
    }
}

/// Expiration time for generate_token
#[derive(Debug, Clone)]
pub enum ExpiresIn {
    /// Duration string like "5m", "1h", "24h", "7d"
    Duration(String),
    /// Seconds as integer
    Seconds(u64),
}

impl From<&str> for ExpiresIn {
    fn from(s: &str) -> Self {
        ExpiresIn::Duration(s.to_string())
    }
}

impl From<String> for ExpiresIn {
    fn from(s: String) -> Self {
        ExpiresIn::Duration(s)
    }
}

impl From<u64> for ExpiresIn {
    fn from(s: u64) -> Self {
        ExpiresIn::Seconds(s)
    }
}

impl From<i64> for ExpiresIn {
    fn from(s: i64) -> Self {
        ExpiresIn::Seconds(s as u64)
    }
}

impl From<u32> for ExpiresIn {
    fn from(s: u32) -> Self {
        ExpiresIn::Seconds(s as u64)
    }
}

impl From<i32> for ExpiresIn {
    fn from(s: i32) -> Self {
        ExpiresIn::Seconds(s as u64)
    }
}

/// Options for generate_token
#[derive(Debug, Clone, Default)]
pub struct GenerateTokenOptions {
    pub expires_in: Option<ExpiresIn>,
}

impl GenerateTokenOptions {
    pub fn new() -> Self {
        Self::default()
    }

    pub fn with_expires_in<T: Into<ExpiresIn>>(mut self, expires_in: T) -> Self {
        self.expires_in = Some(expires_in.into());
        self
    }
}