payrix 0.3.0

Rust client for the Payrix payment processing API
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
//! Webhook setup workflow for configuring Payrix alert notifications.
//!
//! This module provides high-level APIs for setting up webhook alerts in Payrix,
//! allowing you to receive real-time notifications for events like chargebacks,
//! transactions, and merchant status changes.
//!
//! # Overview
//!
//! Payrix webhooks are implemented using three resources:
//!
//! 1. **Alert** - The parent container for webhook configuration
//! 2. **AlertAction** - Defines where to send notifications (web endpoint)
//! 3. **AlertTrigger** - Defines which events trigger notifications
//!
//! This workflow handles all three resources automatically.
//!
//! # Example
//!
//! ```no_run
//! use payrix::{PayrixClient, Environment};
//! use payrix::workflows::webhook_setup::{setup_webhooks, WebhookConfig, WebhookEventType};
//!
//! # async fn example() -> payrix::Result<()> {
//! let client = PayrixClient::new("api-key", Environment::Test)?;
//!
//! // Configure webhooks for chargeback events
//! let config = WebhookConfig::new("https://api.example.com")
//!     .with_events(vec![
//!         WebhookEventType::ChargebackCreated,
//!         WebhookEventType::ChargebackOpened,
//!         WebhookEventType::ChargebackClosed,
//!         WebhookEventType::ChargebackWon,
//!         WebhookEventType::ChargebackLost,
//!     ])
//!     .with_auth("X-Webhook-Secret", "my-secret-value");
//!
//! let result = setup_webhooks(&client, config).await?;
//! println!("Created alert: {}", result.alert_id);
//! println!("Triggers created: {:?}", result.triggers_created);
//! # Ok(())
//! # }
//! ```
//!
//! # Security
//!
//! Payrix does not provide webhook signature verification (HMAC). To secure
//! your webhook endpoint:
//!
//! 1. **Header Authentication** - Use `with_auth()` to require a secret header
//! 2. **IP Allowlist** - Configure your firewall to only accept from Payrix IPs
//! 3. **HTTPS** - Always use HTTPS endpoints
//!
//! # Checking Existing Configuration
//!
//! ```no_run
//! use payrix::{PayrixClient, Environment};
//! use payrix::workflows::webhook_setup::get_webhook_status;
//!
//! # async fn example() -> payrix::Result<()> {
//! let client = PayrixClient::new("api-key", Environment::Test)?;
//!
//! let status = get_webhook_status(&client).await?;
//! for alert in &status.alerts {
//!     println!("Alert: {} - {}", alert.name, alert.endpoint);
//!     for event in &alert.events {
//!         println!("  - {}", event);
//!     }
//! }
//! # Ok(())
//! # }
//! ```

use serde::Serialize;

use crate::entity::EntityType;
use crate::error::{Error, Result};
use crate::types::{Alert, AlertAction, AlertActionType, AlertTrigger};
use crate::PayrixClient;

// =============================================================================
// Event Types
// =============================================================================

/// Webhook event types that can be subscribed to.
///
/// These correspond to the `event` field on AlertTrigger resources.
///
/// # Example
///
/// ```
/// use payrix::workflows::webhook_setup::WebhookEventType;
///
/// let event = WebhookEventType::ChargebackCreated;
/// assert_eq!(event.as_event_str(), "chargeback.created");
/// ```
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum WebhookEventType {
    // ===== Generic Events =====
    /// Any resource created.
    Create,
    /// Any resource updated.
    Update,
    /// Any resource deleted.
    Delete,
    /// Ownership changed.
    Ownership,
    /// Batch operation.
    Batch,

    // ===== Account Events =====
    /// Account event (generic).
    Account,
    /// Account created.
    AccountCreated,
    /// Account updated.
    AccountUpdated,

    // ===== Chargeback/Dispute Events =====
    /// Chargeback event (generic).
    Chargeback,
    /// New chargeback created.
    ChargebackCreated,
    /// Chargeback reopened.
    ChargebackOpened,
    /// Chargeback closed.
    ChargebackClosed,
    /// Merchant won the dispute.
    ChargebackWon,
    /// Merchant lost the dispute.
    ChargebackLost,

    // ===== Transaction Events =====
    /// Transaction created.
    TransactionCreated,
    /// Transaction approved.
    TransactionApproved,
    /// Transaction failed.
    TransactionFailed,
    /// Transaction captured.
    TransactionCaptured,
    /// Transaction settled.
    TransactionSettled,
    /// Transaction returned.
    TransactionReturned,

    // ===== Merchant Events =====
    /// Merchant created.
    MerchantCreated,
    /// Merchant boarding in progress.
    MerchantBoarding,
    /// Merchant boarded successfully.
    MerchantBoarded,
    /// Merchant closed.
    MerchantClosed,
    /// Merchant boarding failed.
    MerchantFailed,
    /// Merchant held.
    MerchantHeld,

    // ===== Disbursement Events =====
    /// Disbursement requested.
    DisbursementRequested,
    /// Disbursement processing.
    DisbursementProcessing,
    /// Disbursement processed.
    DisbursementProcessed,
    /// Disbursement failed.
    DisbursementFailed,
    /// Disbursement denied.
    DisbursementDenied,
    /// Disbursement returned.
    DisbursementReturned,

    // ===== Other Events =====
    /// Payout event.
    Payout,
    /// Fee event.
    Fee,
}

impl WebhookEventType {
    /// Get the Payrix API event string for this event type.
    ///
    /// # Example
    ///
    /// ```
    /// use payrix::workflows::webhook_setup::WebhookEventType;
    ///
    /// assert_eq!(WebhookEventType::ChargebackCreated.as_event_str(), "chargeback.created");
    /// assert_eq!(WebhookEventType::TransactionApproved.as_event_str(), "txn.approved");
    /// ```
    pub fn as_event_str(&self) -> &'static str {
        match self {
            // Generic events
            Self::Create => "create",
            Self::Update => "update",
            Self::Delete => "delete",
            Self::Ownership => "ownership",
            Self::Batch => "batch",

            // Account events
            Self::Account => "account",
            Self::AccountCreated => "account.created",
            Self::AccountUpdated => "account.updated",

            // Chargeback events
            Self::Chargeback => "chargeback",
            Self::ChargebackCreated => "chargeback.created",
            Self::ChargebackOpened => "chargeback.opened",
            Self::ChargebackClosed => "chargeback.closed",
            Self::ChargebackWon => "chargeback.won",
            Self::ChargebackLost => "chargeback.lost",

            // Transaction events
            Self::TransactionCreated => "txn.created",
            Self::TransactionApproved => "txn.approved",
            Self::TransactionFailed => "txn.failed",
            Self::TransactionCaptured => "txn.captured",
            Self::TransactionSettled => "txn.settled",
            Self::TransactionReturned => "txn.returned",

            // Merchant events
            Self::MerchantCreated => "merchant.created",
            Self::MerchantBoarding => "merchant.boarding",
            Self::MerchantBoarded => "merchant.boarded",
            Self::MerchantClosed => "merchant.closed",
            Self::MerchantFailed => "merchant.failed",
            Self::MerchantHeld => "merchant.held",

            // Disbursement events
            Self::DisbursementRequested => "disbursement.requested",
            Self::DisbursementProcessing => "disbursement.processing",
            Self::DisbursementProcessed => "disbursement.processed",
            Self::DisbursementFailed => "disbursement.failed",
            Self::DisbursementDenied => "disbursement.denied",
            Self::DisbursementReturned => "disbursement.returned",

            // Other events
            Self::Payout => "payout",
            Self::Fee => "fee",
        }
    }

    /// Get all chargeback-related events.
    ///
    /// Returns the events for monitoring the full chargeback lifecycle.
    pub fn all_chargeback_events() -> Vec<Self> {
        vec![
            Self::ChargebackCreated,
            Self::ChargebackOpened,
            Self::ChargebackClosed,
            Self::ChargebackWon,
            Self::ChargebackLost,
        ]
    }

    /// Get all transaction-related events.
    pub fn all_transaction_events() -> Vec<Self> {
        vec![
            Self::TransactionCreated,
            Self::TransactionApproved,
            Self::TransactionFailed,
            Self::TransactionCaptured,
            Self::TransactionSettled,
            Self::TransactionReturned,
        ]
    }

    /// Get all merchant-related events.
    pub fn all_merchant_events() -> Vec<Self> {
        vec![
            Self::MerchantCreated,
            Self::MerchantBoarding,
            Self::MerchantBoarded,
            Self::MerchantClosed,
            Self::MerchantFailed,
            Self::MerchantHeld,
        ]
    }

    /// Get all disbursement-related events.
    pub fn all_disbursement_events() -> Vec<Self> {
        vec![
            Self::DisbursementRequested,
            Self::DisbursementProcessing,
            Self::DisbursementProcessed,
            Self::DisbursementFailed,
            Self::DisbursementDenied,
            Self::DisbursementReturned,
        ]
    }
}

impl std::fmt::Display for WebhookEventType {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "{}", self.as_event_str())
    }
}

// =============================================================================
// Configuration
// =============================================================================

/// Configuration for setting up webhooks.
///
/// Use the builder pattern to configure your webhook endpoint and events.
///
/// # Example
///
/// ```
/// use payrix::workflows::webhook_setup::{WebhookConfig, WebhookEventType};
///
/// let config = WebhookConfig::new("https://api.example.com")
///     .with_path("/webhooks/payrix")
///     .with_events(vec![
///         WebhookEventType::ChargebackCreated,
///         WebhookEventType::TransactionApproved,
///     ])
///     .with_auth("X-Webhook-Secret", "my-secret");
/// ```
#[derive(Debug, Clone)]
pub struct WebhookConfig {
    /// Base URL for the webhook endpoint (e.g., "<https://api.example.com>").
    pub base_url: String,

    /// Path for the webhook endpoint (default: "/webhooks/payrix").
    pub webhook_path: String,

    /// Authentication header name (optional).
    pub header_name: Option<String>,

    /// Authentication header value (optional).
    pub header_value: Option<String>,

    /// Events to subscribe to.
    pub events: Vec<WebhookEventType>,

    /// Alert name (optional, will be generated if not provided).
    pub alert_name: Option<String>,

    /// Alert description (optional).
    pub alert_description: Option<String>,

    /// Number of retries for failed webhook deliveries.
    pub retries: Option<i32>,
}

impl WebhookConfig {
    /// Create a new webhook configuration with the base URL.
    ///
    /// # Example
    ///
    /// ```
    /// use payrix::workflows::webhook_setup::WebhookConfig;
    ///
    /// let config = WebhookConfig::new("https://api.example.com");
    /// ```
    pub fn new(base_url: impl Into<String>) -> Self {
        Self {
            base_url: base_url.into(),
            webhook_path: "/webhooks/payrix".to_string(),
            header_name: None,
            header_value: None,
            events: Vec::new(),
            alert_name: None,
            alert_description: None,
            retries: None,
        }
    }

    /// Set the webhook path (default: "/webhooks/payrix").
    pub fn with_path(mut self, path: impl Into<String>) -> Self {
        self.webhook_path = path.into();
        self
    }

    /// Set the events to subscribe to.
    pub fn with_events(mut self, events: Vec<WebhookEventType>) -> Self {
        self.events = events;
        self
    }

    /// Add all chargeback events.
    pub fn with_all_chargeback_events(mut self) -> Self {
        self.events.extend(WebhookEventType::all_chargeback_events());
        self
    }

    /// Add all transaction events.
    pub fn with_all_transaction_events(mut self) -> Self {
        self.events.extend(WebhookEventType::all_transaction_events());
        self
    }

    /// Add all merchant events.
    pub fn with_all_merchant_events(mut self) -> Self {
        self.events.extend(WebhookEventType::all_merchant_events());
        self
    }

    /// Set authentication header (recommended for security).
    ///
    /// When set, all webhook requests from Payrix will include this header,
    /// allowing you to verify the request is authentic.
    pub fn with_auth(mut self, header_name: impl Into<String>, header_value: impl Into<String>) -> Self {
        self.header_name = Some(header_name.into());
        self.header_value = Some(header_value.into());
        self
    }

    /// Set the alert name.
    pub fn with_name(mut self, name: impl Into<String>) -> Self {
        self.alert_name = Some(name.into());
        self
    }

    /// Set the alert description.
    pub fn with_description(mut self, description: impl Into<String>) -> Self {
        self.alert_description = Some(description.into());
        self
    }

    /// Set the number of retries for failed deliveries.
    pub fn with_retries(mut self, retries: i32) -> Self {
        self.retries = Some(retries);
        self
    }

    /// Get the full webhook URL.
    pub fn webhook_url(&self) -> String {
        let base = self.base_url.trim_end_matches('/');
        let path = if self.webhook_path.starts_with('/') {
            self.webhook_path.clone()
        } else {
            format!("/{}", self.webhook_path)
        };
        format!("{}{}", base, path)
    }

    /// Validate the configuration.
    pub fn validate(&self) -> Result<()> {
        if self.base_url.is_empty() {
            return Err(Error::Validation("base_url is required".to_string()));
        }

        if !self.base_url.starts_with("https://") && !self.base_url.starts_with("http://") {
            return Err(Error::Validation(
                "base_url must start with http:// or https://".to_string(),
            ));
        }

        if self.events.is_empty() {
            return Err(Error::Validation(
                "at least one event type is required".to_string(),
            ));
        }

        // Check for header auth consistency
        if self.header_name.is_some() != self.header_value.is_some() {
            return Err(Error::Validation(
                "both header_name and header_value must be set together".to_string(),
            ));
        }

        Ok(())
    }
}

// =============================================================================
// Setup Result
// =============================================================================

/// Result of setting up webhooks.
#[derive(Debug, Clone)]
pub struct WebhookSetupResult {
    /// The ID of the created/updated Alert.
    pub alert_id: String,

    /// The ID of the created AlertAction.
    pub action_id: String,

    /// The events that were configured.
    pub triggers_created: Vec<String>,

    /// Whether an existing configuration was updated.
    pub was_updated: bool,
}

// =============================================================================
// Status Types
// =============================================================================

/// Information about a configured webhook alert.
#[derive(Debug, Clone)]
pub struct WebhookAlertInfo {
    /// Alert ID.
    pub id: String,

    /// Alert name.
    pub name: String,

    /// Webhook endpoint URL.
    pub endpoint: String,

    /// Configured events.
    pub events: Vec<String>,

    /// Whether the alert is active.
    pub is_active: bool,

    /// Header name for authentication (if configured).
    pub auth_header: Option<String>,
}

/// Current webhook configuration status.
#[derive(Debug, Clone)]
pub struct WebhookStatus {
    /// Configured webhook alerts.
    pub alerts: Vec<WebhookAlertInfo>,
}

impl WebhookStatus {
    /// Check if any webhooks are configured.
    pub fn is_configured(&self) -> bool {
        !self.alerts.is_empty()
    }

    /// Get all configured events across all alerts.
    pub fn all_events(&self) -> Vec<&str> {
        self.alerts
            .iter()
            .flat_map(|a| a.events.iter().map(String::as_str))
            .collect()
    }
}

// =============================================================================
// API Request Types
// =============================================================================

/// Request body for creating an Alert.
#[derive(Debug, Serialize)]
#[serde(rename_all = "camelCase")]
struct NewAlert {
    name: String,
    #[serde(skip_serializing_if = "Option::is_none")]
    description: Option<String>,
}

/// Request body for creating an AlertAction.
#[derive(Debug, Serialize)]
#[serde(rename_all = "camelCase")]
struct NewAlertAction {
    alert: String,
    #[serde(rename = "type")]
    action_type: AlertActionType,
    value: String,
    #[serde(skip_serializing_if = "Option::is_none")]
    header_name: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    header_value: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    retries: Option<i32>,
}

/// Request body for creating an AlertTrigger.
#[derive(Debug, Serialize)]
#[serde(rename_all = "camelCase")]
struct NewAlertTrigger {
    alert: String,
    event: String,
    #[serde(skip_serializing_if = "Option::is_none")]
    name: Option<String>,
}

// =============================================================================
// Main Functions
// =============================================================================

/// Set up webhook alerts in Payrix.
///
/// This function creates the necessary Alert, AlertAction, and AlertTrigger
/// resources to receive webhook notifications.
///
/// # Arguments
///
/// * `client` - The Payrix client
/// * `config` - Webhook configuration
///
/// # Returns
///
/// A `WebhookSetupResult` with the IDs of created resources.
///
/// # Example
///
/// ```no_run
/// use payrix::{PayrixClient, Environment};
/// use payrix::workflows::webhook_setup::{setup_webhooks, WebhookConfig, WebhookEventType};
///
/// # async fn example() -> payrix::Result<()> {
/// let client = PayrixClient::new("api-key", Environment::Test)?;
///
/// let config = WebhookConfig::new("https://api.example.com")
///     .with_events(WebhookEventType::all_chargeback_events())
///     .with_auth("X-Webhook-Secret", "my-secret");
///
/// let result = setup_webhooks(&client, config).await?;
/// println!("Alert ID: {}", result.alert_id);
/// # Ok(())
/// # }
/// ```
pub async fn setup_webhooks(client: &PayrixClient, config: WebhookConfig) -> Result<WebhookSetupResult> {
    // Validate configuration
    config.validate()?;

    // Deduplicate events
    let mut events: Vec<WebhookEventType> = config.events.clone();
    events.sort_by_key(|e| e.as_event_str());
    events.dedup_by_key(|e| e.as_event_str());

    // Generate alert name if not provided
    let alert_name = config.alert_name.clone().unwrap_or_else(|| {
        format!("Webhook Alert - {}", chrono::Utc::now().format("%Y-%m-%d"))
    });

    // Step 1: Create the Alert
    let new_alert = NewAlert {
        name: alert_name,
        description: config.alert_description.clone(),
    };

    let alert: Alert = client.create(EntityType::Alerts, &new_alert).await?;
    let alert_id = alert.id.to_string();

    // Step 2: Create the AlertAction (web type)
    let new_action = NewAlertAction {
        alert: alert_id.clone(),
        action_type: AlertActionType::Web,
        value: config.webhook_url(),
        header_name: config.header_name.clone(),
        header_value: config.header_value.clone(),
        retries: config.retries,
    };

    let action: AlertAction = client.create(EntityType::AlertActions, &new_action).await?;
    let action_id = action.id.to_string();

    // Step 3: Create AlertTriggers for each event
    let mut triggers_created = Vec::new();
    for event in &events {
        let event_str = event.as_event_str();
        let new_trigger = NewAlertTrigger {
            alert: alert_id.clone(),
            event: event_str.to_string(),
            name: Some(format!("{} trigger", event_str)),
        };

        let _trigger: AlertTrigger = client.create(EntityType::AlertTriggers, &new_trigger).await?;
        triggers_created.push(event_str.to_string());
    }

    Ok(WebhookSetupResult {
        alert_id,
        action_id,
        triggers_created,
        was_updated: false,
    })
}

/// Get the current webhook configuration status.
///
/// Queries Payrix for all configured webhook alerts and returns
/// information about each one.
///
/// # Arguments
///
/// * `client` - The Payrix client
///
/// # Returns
///
/// A `WebhookStatus` with information about all configured webhooks.
///
/// # Example
///
/// ```no_run
/// use payrix::{PayrixClient, Environment};
/// use payrix::workflows::webhook_setup::get_webhook_status;
///
/// # async fn example() -> payrix::Result<()> {
/// let client = PayrixClient::new("api-key", Environment::Test)?;
///
/// let status = get_webhook_status(&client).await?;
/// if status.is_configured() {
///     for alert in &status.alerts {
///         println!("Alert: {} -> {}", alert.name, alert.endpoint);
///     }
/// } else {
///     println!("No webhooks configured");
/// }
/// # Ok(())
/// # }
/// ```
pub async fn get_webhook_status(client: &PayrixClient) -> Result<WebhookStatus> {
    use crate::SearchBuilder;

    // Search for all alerts
    let alerts: Vec<Alert> = client.search(EntityType::Alerts, &SearchBuilder::new().build()).await?;

    let mut alert_infos = Vec::new();

    for alert in alerts {
        let alert_id = alert.id.to_string();

        // Get actions for this alert (filter for web type)
        let action_search = SearchBuilder::new()
            .field("alert", &alert_id)
            .build();
        let actions: Vec<AlertAction> = client.search(EntityType::AlertActions, &action_search).await?;

        // Find web-type action
        let web_action = actions.iter().find(|a| {
            a.action_type == Some(AlertActionType::Web)
        });

        if let Some(action) = web_action {
            // Get triggers for this alert
            let trigger_search = SearchBuilder::new()
                .field("alert", &alert_id)
                .build();
            let triggers: Vec<AlertTrigger> = client.search(EntityType::AlertTriggers, &trigger_search).await?;

            let events: Vec<String> = triggers
                .iter()
                .filter_map(|t| t.event.clone())
                .collect();

            alert_infos.push(WebhookAlertInfo {
                id: alert_id,
                name: alert.name.unwrap_or_else(|| "Unnamed".to_string()),
                endpoint: action.value.clone().unwrap_or_default(),
                events,
                is_active: !alert.inactive,
                auth_header: action.header_name.clone(),
            });
        }
    }

    Ok(WebhookStatus { alerts: alert_infos })
}

/// Remove all webhook alerts.
///
/// This function removes all configured webhook alerts, actions, and triggers.
/// Use with caution in production.
///
/// # Arguments
///
/// * `client` - The Payrix client
///
/// # Returns
///
/// The number of alerts removed.
///
/// # Example
///
/// ```no_run
/// use payrix::{PayrixClient, Environment};
/// use payrix::workflows::webhook_setup::remove_webhooks;
///
/// # async fn example() -> payrix::Result<()> {
/// let client = PayrixClient::new("api-key", Environment::Test)?;
///
/// let count = remove_webhooks(&client).await?;
/// println!("Removed {} webhook alerts", count);
/// # Ok(())
/// # }
/// ```
pub async fn remove_webhooks(client: &PayrixClient) -> Result<usize> {
    use crate::SearchBuilder;

    // Get current status
    let status = get_webhook_status(client).await?;
    let count = status.alerts.len();

    for alert in &status.alerts {
        // Delete triggers first
        let trigger_search = SearchBuilder::new()
            .field("alert", &alert.id)
            .build();
        let triggers: Vec<AlertTrigger> = client.search(EntityType::AlertTriggers, &trigger_search).await?;

        for trigger in triggers {
            let _: AlertTrigger = client.remove(EntityType::AlertTriggers, trigger.id.as_str()).await?;
        }

        // Delete actions
        let action_search = SearchBuilder::new()
            .field("alert", &alert.id)
            .build();
        let actions: Vec<AlertAction> = client.search(EntityType::AlertActions, &action_search).await?;

        for action in actions {
            let _: AlertAction = client.remove(EntityType::AlertActions, action.id.as_str()).await?;
        }

        // Delete the alert
        let _: Alert = client.remove(EntityType::Alerts, &alert.id).await?;
    }

    Ok(count)
}

/// Remove a specific webhook alert by ID.
///
/// This function removes a single webhook alert and its associated
/// actions and triggers.
///
/// # Arguments
///
/// * `client` - The Payrix client
/// * `alert_id` - The ID of the alert to remove
///
/// # Example
///
/// ```no_run
/// use payrix::{PayrixClient, Environment};
/// use payrix::workflows::webhook_setup::remove_webhook_by_id;
///
/// # async fn example() -> payrix::Result<()> {
/// let client = PayrixClient::new("api-key", Environment::Test)?;
///
/// remove_webhook_by_id(&client, "t1_alt_12345678901234567890123").await?;
/// # Ok(())
/// # }
/// ```
pub async fn remove_webhook_by_id(client: &PayrixClient, alert_id: &str) -> Result<()> {
    use crate::SearchBuilder;

    // Delete triggers first
    let trigger_search = SearchBuilder::new()
        .field("alert", alert_id)
        .build();
    let triggers: Vec<AlertTrigger> = client.search(EntityType::AlertTriggers, &trigger_search).await?;

    for trigger in triggers {
        let _: AlertTrigger = client.remove(EntityType::AlertTriggers, trigger.id.as_str()).await?;
    }

    // Delete actions
    let action_search = SearchBuilder::new()
        .field("alert", alert_id)
        .build();
    let actions: Vec<AlertAction> = client.search(EntityType::AlertActions, &action_search).await?;

    for action in actions {
        let _: AlertAction = client.remove(EntityType::AlertActions, action.id.as_str()).await?;
    }

    // Delete the alert
    let _: Alert = client.remove(EntityType::Alerts, alert_id).await?;

    Ok(())
}

// =============================================================================
// Tests
// =============================================================================

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

    #[test]
    fn test_webhook_event_type_as_str() {
        assert_eq!(WebhookEventType::ChargebackCreated.as_event_str(), "chargeback.created");
        assert_eq!(WebhookEventType::ChargebackOpened.as_event_str(), "chargeback.opened");
        assert_eq!(WebhookEventType::ChargebackClosed.as_event_str(), "chargeback.closed");
        assert_eq!(WebhookEventType::ChargebackWon.as_event_str(), "chargeback.won");
        assert_eq!(WebhookEventType::ChargebackLost.as_event_str(), "chargeback.lost");

        assert_eq!(WebhookEventType::TransactionCreated.as_event_str(), "txn.created");
        assert_eq!(WebhookEventType::TransactionApproved.as_event_str(), "txn.approved");

        assert_eq!(WebhookEventType::MerchantBoarded.as_event_str(), "merchant.boarded");
    }

    #[test]
    fn test_webhook_event_type_display() {
        assert_eq!(format!("{}", WebhookEventType::ChargebackCreated), "chargeback.created");
    }

    #[test]
    fn test_webhook_event_type_all_chargeback() {
        let events = WebhookEventType::all_chargeback_events();
        assert_eq!(events.len(), 5);
        assert!(events.contains(&WebhookEventType::ChargebackCreated));
        assert!(events.contains(&WebhookEventType::ChargebackWon));
        assert!(events.contains(&WebhookEventType::ChargebackLost));
    }

    #[test]
    fn test_webhook_config_builder() {
        let config = WebhookConfig::new("https://api.example.com")
            .with_path("/hooks")
            .with_events(vec![WebhookEventType::ChargebackCreated])
            .with_auth("X-Secret", "my-secret")
            .with_name("Test Alert")
            .with_retries(3);

        assert_eq!(config.base_url, "https://api.example.com");
        assert_eq!(config.webhook_path, "/hooks");
        assert_eq!(config.events.len(), 1);
        assert_eq!(config.header_name, Some("X-Secret".to_string()));
        assert_eq!(config.header_value, Some("my-secret".to_string()));
        assert_eq!(config.alert_name, Some("Test Alert".to_string()));
        assert_eq!(config.retries, Some(3));
    }

    #[test]
    fn test_webhook_config_url() {
        let config = WebhookConfig::new("https://api.example.com/");
        assert_eq!(config.webhook_url(), "https://api.example.com/webhooks/payrix");

        let config2 = WebhookConfig::new("https://api.example.com")
            .with_path("custom/path");
        assert_eq!(config2.webhook_url(), "https://api.example.com/custom/path");
    }

    #[test]
    fn test_webhook_config_validation() {
        // Empty base_url
        let config = WebhookConfig::new("")
            .with_events(vec![WebhookEventType::ChargebackCreated]);
        assert!(config.validate().is_err());

        // Invalid protocol
        let config = WebhookConfig::new("ftp://example.com")
            .with_events(vec![WebhookEventType::ChargebackCreated]);
        assert!(config.validate().is_err());

        // No events
        let config = WebhookConfig::new("https://example.com");
        assert!(config.validate().is_err());

        // Missing header value
        let config = WebhookConfig::new("https://example.com")
            .with_events(vec![WebhookEventType::ChargebackCreated]);
        let mut config = config;
        config.header_name = Some("X-Secret".to_string());
        assert!(config.validate().is_err());

        // Valid config
        let config = WebhookConfig::new("https://example.com")
            .with_events(vec![WebhookEventType::ChargebackCreated])
            .with_auth("X-Secret", "value");
        assert!(config.validate().is_ok());
    }

    #[test]
    fn test_webhook_status_helpers() {
        let status = WebhookStatus {
            alerts: vec![
                WebhookAlertInfo {
                    id: "alert1".to_string(),
                    name: "Alert 1".to_string(),
                    endpoint: "https://example.com".to_string(),
                    events: vec!["chargeback.created".to_string()],
                    is_active: true,
                    auth_header: None,
                },
            ],
        };

        assert!(status.is_configured());
        assert_eq!(status.all_events(), vec!["chargeback.created"]);

        let empty_status = WebhookStatus { alerts: vec![] };
        assert!(!empty_status.is_configured());
    }

    #[test]
    fn test_webhook_config_with_all_events() {
        let config = WebhookConfig::new("https://example.com")
            .with_all_chargeback_events()
            .with_all_transaction_events();

        assert_eq!(config.events.len(), 11); // 5 chargeback + 6 transaction
    }
}