sendblue 0.5.0

SendBlue is a Rust library that provides an API client for interacting with the SendBlue REST API, enabling businesses to integrate iMessage and SMS services into their applications.
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
//! Message Model
//!
//! This module provides the data models for messages used in the Sendblue API, including
//! individual and group messages, their builders, and response structures.

use super::{ErrorCode, Status};
use crate::{
    models::{
        /* phonenumber::deserialize_phone_number, */ /* phonenumber::{deserialize_option_phone_number,
        deserialize_option_vec_phone_number, deserialize_phone_number,
        deserialize_vec_phone_number, serialize_phone_number}, */
        CallbackUrl, MediaUrl, SendStyle,
    },
    traits::SendableMessage,
    SendblueError,
};
use chrono::{DateTime, Utc};
#[cfg(feature = "schemars")]
use schemars::{schema::Schema, schema_for, JsonSchema};
use serde::{Deserialize, Serialize};
use serde_with::{serde_as, skip_serializing_none};
use validator::Validate;

/// Message to be sent using the Sendblue API
///
/// # Examples
///
/// ```
/// use sendblue::models::{Message, MessageBuilder};
///
/// let message = MessageBuilder::new(phonenumber::parse(None, "+1234567890").unwrap())
///     .content("Hello, world!".into())
///     .build()
///     .unwrap();
/// ```
#[derive(Serialize, Deserialize, Validate, Debug)]
pub struct Message {
    /// The recipient's phone number in E.164 format
    /* #[serde(serialize_with = "serialize_phone_number")] */
    pub number: String,
    /// The content of the message (optional)
    #[validate(length(min = 1))]
    #[serde(skip_serializing_if = "Option::is_none", default)]
    pub content: Option<String>,
    /// The URL of the media to be sent (optional)
    #[serde(skip_serializing_if = "Option::is_none", default)]
    pub media_url: Option<MediaUrl>,
    /// The callback URL for the message status (optional)
    #[serde(skip_serializing_if = "Option::is_none", default)]
    pub status_callback: Option<CallbackUrl>,
    /// The style of the message delivery (optional)
    #[serde(skip_serializing_if = "Option::is_none", default)]
    pub send_style: Option<SendStyle>,
}

impl SendableMessage for Message {
    fn endpoint() -> &'static str {
        "/send-message"
    }

    type ResponseType = MessageResponse;
}

/// Response from the Sendblue API after sending a message
#[serde_as]
#[skip_serializing_none]
#[derive(Serialize, Deserialize, Debug, Clone, JsonSchema)]
pub struct MessageResponse {
    /// The email of the account
    #[serde(rename = "accountEmail")]
    pub account_email: String,
    /// The content of the message
    /* #[serde_as(as = "NoneAsEmptyString")] */
    pub content: String,
    /// Whether the message is outbound
    pub is_outbound: bool,
    /// The status of the message
    pub status: Status,
    /// The error code if any (optional)
    pub error_code: Option<String>,
    /// The error message if any (optional)
    pub error_message: Option<String>,
    /// The handle of the message
    pub message_handle: String,
    /// The date the message was sent
    pub date_sent: DateTime<Utc>,
    /// The date the message was updated
    pub date_updated: DateTime<Utc>,
    /// The sender's phone number
    /* /*#[serde(deserialize_with = "deserialize_phone_number")]*/ */
    pub from_number: String,
    /// The recipient's phone number
    /* /*#[serde(deserialize_with = "deserialize_phone_number")]*/ */
    pub number: String,
    /// The recipient's phone number (alternative)
    /* /*#[serde(deserialize_with = "deserialize_phone_number")]*/ */
    pub to_number: String,
    /// Whether the message was downgraded
    pub was_downgraded: Option<bool>,
    /// The plan associated with the message
    pub plan: Option<String>,
    /// The URL of the media
    pub media_url: String,
    /// The type of the message
    pub message_type: Option<String>,
    /// The group ID associated with the message
    pub group_id: Option<String>,
    /// The participants in the message
    pub participants: Option<Vec<String>>,
    /// The send style of the message
    pub send_style: String,
    /// Whether the recipient opted out
    pub opted_out: bool,
    /// The error detail if any (optional)
    pub error_detail: Option<String>,
}

/* #[cfg(feature = "schemars")] */
/// Meta type for schema generation for MessageResponse
/* #[derive(Serialize, Deserialize, JsonSchema)]
pub struct MessageResponseSchema(pub MessageResponse); */

/*  #[cfg(feature = "schemars")]
impl JsonSchema for MessageResponse {
    fn schema_name() -> String {
        "MessageResponse".to_string()
    }

    fn json_schema(_gen: &mut schemars::gen::SchemaGenerator) -> Schema {
        schema_for!(MessageResponse).schema.into()
    }
} */

/// Payload for the status callback
#[skip_serializing_none]
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct MessageStatusCallback {
    /// The email of the account
    #[serde(rename = "accountEmail")]
    pub account_email: String,
    /// The content of the message
    #[serde(skip_serializing_if = "Option::is_none", default)]
    pub content: Option<String>,
    /// Whether the message is outbound
    pub is_outbound: bool,
    /// The status of the message
    pub status: Status,
    /// The error code if any (optional)
    #[serde(skip_serializing_if = "Option::is_none", default)]
    pub error_code: Option<ErrorCode>,
    /// The error message if any (optional)
    #[serde(skip_serializing_if = "Option::is_none", default)]
    pub error_message: Option<String>,
    /// The handle of the message
    pub message_handle: String,
    /// The date the message was sent
    pub date_sent: DateTime<Utc>,
    /// The date the message was updated
    pub date_updated: DateTime<Utc>,
    /// The sender's phone number
    /*#[serde(deserialize_with = "deserialize_phone_number")]*/
    pub from_number: String,
    /// The recipient's phone number
    /*#[serde(deserialize_with = "deserialize_phone_number")]*/
    pub number: String,
    /// The recipient's phone number (alternative)
    /*#[serde(deserialize_with = "deserialize_phone_number")]*/
    pub to_number: String,
    /// Whether the message was downgraded
    #[serde(skip_serializing_if = "Option::is_none", default)]
    pub was_downgraded: Option<bool>,
    /// The plan associated with the message
    #[serde(skip_serializing_if = "Option::is_none", default)]
    pub plan: Option<String>,
    /// The URL of the media
    #[serde(skip_serializing_if = "Option::is_none", default)]
    pub media_url: Option<MediaUrl>,
    /// The type of the message
    #[serde(skip_serializing_if = "Option::is_none", default)]
    pub message_type: Option<String>,
    /// The group ID associated with the message
    #[serde(skip_serializing_if = "Option::is_none", default)]
    pub group_id: Option<String>,
    /// The participants in the message
    #[serde(skip_serializing_if = "Option::is_none", default)]
    pub participants: Option<Vec<String>>,
    /// The send style of the message
    #[serde(skip_serializing_if = "Option::is_none", default)]
    pub send_style: Option<String>,
    /// Whether the recipient opted out
    pub opted_out: bool,
    /// The error detail if any (optional)
    #[serde(skip_serializing_if = "Option::is_none", default)]
    pub error_detail: Option<String>,
}

#[cfg(feature = "schemars")]
/// Meta type for schema generation for MessageStatusCallback
#[derive(Serialize, Deserialize, JsonSchema)]
pub struct MessageStatusCallbackSchema(pub MessageStatusCallback);

#[cfg(feature = "schemars")]
impl JsonSchema for MessageStatusCallback {
    fn schema_name() -> String {
        "MessageStatusCallback".to_string()
    }

    fn json_schema(_gen: &mut schemars::gen::SchemaGenerator) -> Schema {
        schema_for!(MessageStatusCallbackSchema).schema.into()
    }
}

/// Request parameters for getting messages
///
/// # Examples
///
/// ```
/// use sendblue::models::GetMessagesParams;
///
/// let params = GetMessagesParams {
///     cid: Some("contact_id".into()),
///     number: Some(phonenumber::parse(None, "+1234567890").unwrap()),
///     limit: Some(50),
///     offset: Some(0),
///     from_date: Some("2023-06-15 12:00:00".into()),
/// };
/// ```
#[derive(Serialize, Deserialize, Debug, Default)]
pub struct GetMessagesParams {
    pub cid: Option<String>,
    /* #[serde(deserialize_with = "deserialize_option_phone_number")] */
    pub number: Option<String>,
    pub limit: Option<u32>,
    pub offset: Option<u32>,
    pub from_date: Option<String>, // or use a more specific date type
}

/// Message retrieved from the Sendblue API
#[derive(Serialize, Deserialize, Debug)]
pub struct RetrievedMessage {
    /// The date the message was sent
    pub date: String,
    /// Whether SMS is allowed
    #[serde(rename = "allowSMS")]
    pub allow_sms: Option<bool>,
    /// The style of the message
    #[serde(rename = "sendStyle")]
    pub send_style: Option<String>,
    /// The type of the message
    #[serde(rename = "type")]
    pub message_type: String,
    /// The unique ID of the message
    pub uuid: String,
    /// The URL to a media attachment
    pub media_url: Option<String>,
    /// The content of the message
    pub content: Option<String>,
    /// The recipient's phone number
    /* #[serde(deserialize_with = "deserialize_option_phone_number")] */
    pub number: Option<String>,
    /// Whether the message is outbound
    pub is_outbound: bool,
    /// The email of the account
    #[serde(rename = "accountEmail")]
    pub account_email: String,
    /// Whether the message was downgraded
    pub was_downgraded: Option<bool>,
    /// The callback URL for status updates
    #[serde(rename = "callbackURL")]
    pub callback_url: Option<String>,
    /// The row ID of the message
    pub row_id: Option<String>,
    /// The status of the message
    pub status: Status,
    /// The error message, if any
    pub error_message: Option<String>,
    /// The recipient's phone number (alternative)
    /* #[serde(deserialize_with = "deserialize_option_phone_number")] */
    pub to_number: Option<String>,
    /// The date the message was sent
    pub date_sent: Option<DateTime<Utc>>,
    /// The date the message was updated
    pub date_updated: Option<DateTime<Utc>>,
    /// Additional error details, if any
    pub error_detail: Option<String>,
    /// The phone ID
    #[serde(rename = "phoneID")]
    pub phone_id: Option<String>,
    /// The group ID associated with the message
    pub group_id: Option<String>,
    /// The sender's phone number
    /* #[serde(deserialize_with = "deserialize_option_phone_number")] */
    pub from_number: Option<String>,
    /// The error code, if any
    pub error_code: Option<i32>,
}

/// Response from the Sendblue API for getting messages
#[derive(Serialize, Deserialize, Debug)]
pub struct GetMessagesResponse {
    /// List of messages retrieved
    pub messages: Vec<RetrievedMessage>,
}

/// Group message request payload
///
/// # Examples
///
/// ```
/// use sendblue::models::GroupMessage;
/// use sendblue::models::MediaUrl;
/// use sendblue::models::CallbackUrl;
/// use sendblue::traits::Url;
///
/// let request = GroupMessage {
///     numbers: Some(vec![phonenumber::parse(None, "+19998887777").unwrap(), phonenumber::parse(None, "+17778889999").unwrap()]),
///     group_id: None,
///     content: Some("Hello group!".into()),
///     media_url: Some(MediaUrl::new("https://picsum.photos/200/300.jpg").unwrap()),
///     send_style: None,
///     status_callback: Some(CallbackUrl::new("https://example.com/message-status/1234abcd").unwrap()),
/// };
/// ```
#[derive(Serialize, Deserialize, Validate, Debug)]
pub struct GroupMessage {
    /// An array of E.164-formatted phone numbers of the desired recipients in a group chat.
    /* #[serde(deserialize_with = "deserialize_option_vec_phone_number")] */
    pub numbers: Option<Vec<String>>,
    /// The group ID to message an existing group.
    pub group_id: Option<String>,
    /// The content of the message.
    #[validate(length(min = 1))]
    pub content: Option<String>,
    /// A URL to a media file to send to the group.
    #[serde(skip_serializing_if = "Option::is_none", default)]
    pub media_url: Option<MediaUrl>,
    /// The style of delivery of the message.
    #[serde(skip_serializing_if = "Option::is_none", default)]
    pub send_style: Option<SendStyle>,
    /// An endpoint to notify your app of status updates for this message.
    #[serde(skip_serializing_if = "Option::is_none", default)]
    pub status_callback: Option<CallbackUrl>,
}

impl SendableMessage for GroupMessage {
    fn endpoint() -> &'static str {
        "/send-group-message"
    }

    type ResponseType = GroupMessageResponse;
}

/// Response from the Sendblue API for sending a group message
#[skip_serializing_none]
#[derive(Serialize, Deserialize, Debug)]
pub struct GroupMessageResponse {
    /// The email of the account
    #[serde(rename = "accountEmail")]
    pub account_email: String,
    /// The content of the message
    pub content: String,
    /// Whether the message is outbound
    pub is_outbound: bool,
    /// The status of the message
    pub status: Status,
    /// The error code, if any
    pub error_code: Option<i32>,
    /// The error message, if any
    pub error_message: Option<String>,
    /// The message handle
    pub message_handle: String,
    /// The date the message was sent
    pub date_sent: DateTime<Utc>,
    /// The date the message was updated
    pub date_updated: DateTime<Utc>,
    /// The sender's phone number
    /*#[serde(deserialize_with = "deserialize_phone_number")]*/
    pub from_number: String,
    /// The recipient phone numbers
    /* #[serde(deserialize_with = "deserialize_vec_phone_number")] */
    pub number: Vec<String>,
    /// The recipient phone numbers (alternative)
    /* #[serde(deserialize_with = "deserialize_vec_phone_number")] */
    pub to_number: Vec<String>,
    /// Whether the message was downgraded
    pub was_downgraded: Option<bool>,
    /// The plan of the message
    pub plan: String,
    /// The URL to the media
    pub media_url: String,
    /// The type of the message
    pub message_type: String,
    /// The group ID
    pub group_id: String,
}

/// Generic builder for creating a `Message` or `GroupMessage`
pub struct MessageBuilder<T> {
    message: Option<Message>,
    group_message: Option<GroupMessage>,
    _marker: std::marker::PhantomData<T>,
}

impl MessageBuilder<Message> {
    /// Creates a new `MessageBuilder` for an individual message
    ///
    /// # Arguments
    ///
    /// * `number` - The recipient's phone number in E.164 format
    ///
    /// # Examples
    ///
    /// ```
    /// use sendblue::models::MessageBuilder;
    ///
    /// let builder = MessageBuilder::new(phonenumber::parse(None, "+1234567890").unwrap());
    /// ```
    pub fn new(number: String) -> Self {
        Self {
            message: Some(Message {
                number,
                content: None,
                media_url: None,
                status_callback: None,
                send_style: None,
            }),
            group_message: None,
            _marker: std::marker::PhantomData,
        }
    }

    /// Sets the content of the message
    ///
    /// # Arguments
    ///
    /// * `content` - The content of the message
    ///
    /// # Examples
    ///
    /// ```
    /// use sendblue::models::MessageBuilder;
    ///
    /// let builder = MessageBuilder::new(phonenumber::parse(None, "+1234567890").unwrap())
    ///     .content("Hello, world!".into());
    /// ```
    pub fn content(mut self, content: String) -> Self {
        if let Some(ref mut msg) = self.message {
            msg.content = Some(content);
        }
        self
    }

    /// Sets the media URL of the message
    ///
    /// # Arguments
    ///
    /// * `media_url` - The URL of the media to be sent
    ///
    /// # Examples
    ///
    /// ```
    /// use sendblue::models::{MessageBuilder, MediaUrl};
    /// use sendblue::traits::Url;
    ///
    /// let builder = MessageBuilder::new(phonenumber::parse(None, "+1234567890").unwrap())
    ///     .media_url(MediaUrl::new("https://example.com/media.jpg").unwrap());
    /// ```
    pub fn media_url(mut self, media_url: MediaUrl) -> Self {
        if let Some(ref mut msg) = self.message {
            msg.media_url = Some(media_url);
        }
        self
    }

    /// Sets the status callback URL of the message
    ///
    /// # Arguments
    ///
    /// * `status_callback` - The callback URL for the message status
    ///
    /// # Examples
    ///
    /// ```
    /// use sendblue::models::{MessageBuilder, CallbackUrl};
    /// use sendblue::traits::Url;
    ///
    /// let builder = MessageBuilder::new(phonenumber::parse(None, "+1234567890").unwrap())
    ///     .status_callback(CallbackUrl::new("https://example.com/message-status/1234abcd").unwrap());
    /// ```
    pub fn status_callback(mut self, status_callback: CallbackUrl) -> Self {
        if let Some(ref mut msg) = self.message {
            msg.status_callback = Some(status_callback);
        }
        self
    }

    /// Sets the style of delivery of the message
    ///
    /// # Arguments
    ///
    /// * `send_style` - The style of the message delivery
    ///
    /// # Examples
    ///
    /// ```
    /// use sendblue::models::{MessageBuilder, SendStyle};
    ///
    /// let builder = MessageBuilder::new(phonenumber::parse(None, "+1234567890").unwrap())
    ///     .send_style(SendStyle::Invisible);
    /// ```
    pub fn send_style(mut self, send_style: SendStyle) -> Self {
        if let Some(ref mut msg) = self.message {
            msg.send_style = Some(send_style);
        }
        self
    }

    /// Builds the `Message`
    ///
    /// # Returns
    ///
    /// * `Result<Message, ValidationError>` - The constructed `Message` object or a `ValidationError`
    ///
    /// # Examples
    ///
    /// ```
    /// use sendblue::models::MessageBuilder;
    ///
    /// let message = MessageBuilder::new(phonenumber::parse(None, "+1234567890").unwrap())
    ///     .content("Hello, world!".into())
    ///     .build()
    ///     .unwrap();
    /// ```
    pub fn build(self) -> Result<Message, SendblueError> {
        if let Some(msg) = self.message {
            msg.validate()
                .map_err(|e| SendblueError::ValidationError(e.to_string()))?;
            Ok(msg)
        } else {
            Err(SendblueError::ValidationError(
                "Message not initialized".into(),
            ))
        }
    }
}

impl MessageBuilder<GroupMessage> {
    /// Creates a new `MessageBuilder` for a group message
    ///
    /// # Examples
    ///
    /// ```
    /// use sendblue::models::MessageBuilder;
    ///
    /// let builder = MessageBuilder::new_group();
    /// ```
    pub fn new_group() -> Self {
        Self {
            message: None,
            group_message: Some(GroupMessage {
                numbers: None,
                group_id: None,
                content: None,
                media_url: None,
                send_style: None,
                status_callback: None,
            }),
            _marker: std::marker::PhantomData,
        }
    }

    /// Sets the list of phone numbers for the group message
    ///
    /// # Arguments
    ///
    /// * `numbers` - An array of E.164-formatted phone numbers
    ///
    /// # Examples
    ///
    /// ```
    /// use sendblue::models::MessageBuilder;
    ///
    /// let builder = MessageBuilder::new_group()
    ///     .numbers(vec![phonenumber::parse(None, "+19998887777").unwrap(), phonenumber::parse(None, "+17778889999").unwrap()]);
    /// ```
    pub fn numbers(mut self, numbers: Vec<String>) -> Self {
        if let Some(ref mut grp_msg) = self.group_message {
            grp_msg.numbers = Some(numbers);
        }
        self
    }

    /// Sets the group ID for the group message
    ///
    /// # Arguments
    ///
    /// * `group_id` - The group ID
    ///
    /// # Examples
    ///
    /// ```
    /// use sendblue::models::MessageBuilder;
    ///
    /// let builder = MessageBuilder::new_group()
    ///     .group_id("group_id".into());
    /// ```
    pub fn group_id(mut self, group_id: String) -> Self {
        if let Some(ref mut grp_msg) = self.group_message {
            grp_msg.group_id = Some(group_id);
        }
        self
    }

    /// Sets the content of the group message
    ///
    /// # Arguments
    ///
    /// * `content` - The content of the group message
    ///
    /// # Examples
    ///
    /// ```
    /// use sendblue::models::MessageBuilder;
    ///
    /// let builder = MessageBuilder::new_group()
    ///     .content("Hello group!".into());
    /// ```
    pub fn content(mut self, content: String) -> Self {
        if let Some(ref mut grp_msg) = self.group_message {
            grp_msg.content = Some(content);
        }
        self
    }

    /// Sets the media URL for the group message
    ///
    /// # Arguments
    ///
    /// * `media_url` - The URL of the media to be sent to the group
    ///
    /// # Examples
    ///
    /// ```
    /// use sendblue::models::{MessageBuilder, MediaUrl};
    /// use sendblue::traits::Url;
    ///
    /// let builder = MessageBuilder::new_group()
    ///     .media_url(MediaUrl::new("https://example.com/media.jpg").unwrap());
    /// ```
    pub fn media_url(mut self, media_url: MediaUrl) -> Self {
        if let Some(ref mut grp_msg) = self.group_message {
            grp_msg.media_url = Some(media_url);
        }
        self
    }

    /// Sets the status callback URL for the group message
    ///
    /// # Arguments
    ///
    /// * `status_callback` - The callback URL for the message status
    ///
    /// # Examples
    ///
    /// ```
    /// use sendblue::models::{MessageBuilder, CallbackUrl};
    /// use sendblue::traits::Url;
    ///
    /// let builder = MessageBuilder::new_group()
    ///     .status_callback(CallbackUrl::new("https://example.com/message-status/1234abcd").unwrap());
    /// ```
    pub fn status_callback(mut self, status_callback: CallbackUrl) -> Self {
        if let Some(ref mut grp_msg) = self.group_message {
            grp_msg.status_callback = Some(status_callback);
        }
        self
    }

    /// Sets the style of delivery of the group message
    ///
    /// # Arguments
    ///
    /// * `send_style` - The style of the message delivery
    ///
    /// # Examples
    ///
    /// ```
    /// use sendblue::models::{MessageBuilder, SendStyle};
    ///
    /// let builder = MessageBuilder::new_group()
    ///     .send_style(SendStyle::Invisible);
    /// ```
    pub fn send_style(mut self, send_style: SendStyle) -> Self {
        if let Some(ref mut grp_msg) = self.group_message {
            grp_msg.send_style = Some(send_style);
        }
        self
    }

    /// Builds the `GroupMessage`
    ///
    /// # Returns
    ///
    /// * `Result<GroupMessage, ValidationError>` - The constructed `GroupMessage` object or a `ValidationError`
    ///
    /// # Examples
    ///
    /// ```
    /// use sendblue::models::MessageBuilder;
    ///
    /// let group_message = MessageBuilder::new_group()
    ///     .numbers(vec![phonenumber::parse(None, "+19998887777").unwrap(), phonenumber::parse(None, "+17778889999").unwrap()])
    ///     .content("Hello group!".into())
    ///     .build()
    ///     .unwrap();
    /// ```
    pub fn build(self) -> Result<GroupMessage, SendblueError> {
        if let Some(grp_msg) = self.group_message {
            if grp_msg.numbers.as_ref().map_or(true, |ns| ns.is_empty())
                && grp_msg.group_id.is_none()
            {
                return Err(SendblueError::ValidationError(
                    "Either numbers or group_id must be provided".into(),
                ));
            }
            if grp_msg.content.is_none() && grp_msg.media_url.is_none() {
                return Err(SendblueError::ValidationError(
                    "Either content or media_url must be provided".into(),
                ));
            }
            grp_msg
                .validate()
                .map_err(|e| SendblueError::ValidationError(e.to_string()))?;
            Ok(grp_msg)
        } else {
            Err(SendblueError::ValidationError(
                "GroupMessage not initialized".into(),
            ))
        }
    }
}

/// Builder for creating a `GetMessagesParams`
///
/// # Examples
///
/// ```
/// use sendblue::models::GetMessagesParamsBuilder;
///
/// let params = GetMessagesParamsBuilder::new()
///     .cid(Some("contact_id".into()))
///     .number(Some(phonenumber::parse(None, "+1234567890").unwrap()))
///     .limit(Some(50))
///     .offset(Some(0))
///     .from_date(Some("2023-06-15 12:00:00".into()))
///     .build();
/// ```
#[derive(Serialize, Deserialize, Debug)]
pub struct GetMessagesParamsBuilder {
    cid: Option<String>,
    /* #[serde(deserialize_with = "deserialize_option_phone_number")] */
    number: Option<String>,
    limit: Option<u32>,
    offset: Option<u32>,
    from_date: Option<String>,
}

impl GetMessagesParamsBuilder {
    pub fn new() -> Self {
        Self {
            cid: None,
            number: None,
            limit: None,
            offset: None,
            from_date: None,
        }
    }

    pub fn cid(mut self, cid: Option<String>) -> Self {
        self.cid = cid;
        self
    }

    pub fn number(mut self, number: Option<String>) -> Self {
        self.number = number;
        self
    }

    pub fn limit(mut self, limit: Option<u32>) -> Self {
        self.limit = limit;
        self
    }

    pub fn offset(mut self, offset: Option<u32>) -> Self {
        self.offset = offset;
        self
    }

    pub fn from_date(mut self, from_date: Option<String>) -> Self {
        self.from_date = from_date;
        self
    }

    pub fn build(self) -> GetMessagesParams {
        GetMessagesParams {
            cid: self.cid,
            number: self.number,
            limit: self.limit,
            offset: self.offset,
            from_date: self.from_date,
        }
    }
}

impl Default for GetMessagesParamsBuilder {
    fn default() -> Self {
        Self::new()
    }
}