tally-sdk 0.2.1

Rust SDK for the Tally Solana subscriptions platform
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
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
//! Dashboard client for merchant management and analytics

#![forbid(unsafe_code)]
#![allow(clippy::arithmetic_side_effects)] // Safe for business logic calculations
#![allow(clippy::cast_possible_truncation)] // Controlled truncation for display formatting
#![allow(clippy::cast_lossless)] // Safe casting for USDC formatting
#![allow(clippy::cast_precision_loss)] // Controlled precision loss for display formatting

use crate::{
    dashboard_types::{
        DashboardEvent, DashboardEventType, DashboardSubscription, EventStream, Overview,
        PlanAnalytics,
    },
    error::{Result, TallyError},
    events::{ParsedEventWithContext, TallyEvent},
    program_types::{CreatePlanArgs, InitMerchantArgs, Merchant, Plan},
    simple_client::SimpleTallyClient,
    validation::validate_platform_fee_bps,
};
use anchor_client::solana_sdk::pubkey::Pubkey;
use anchor_client::solana_sdk::signature::Signer;
use chrono::{DateTime, Utc};
use std::collections::HashMap;

/// Time period for statistics calculation
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Period {
    /// Last 24 hours
    Day,
    /// Last 7 days
    Week,
    /// Last 30 days
    Month,
    /// Last 90 days
    Quarter,
    /// Last 365 days
    Year,
    /// Custom date range
    Custom {
        from: DateTime<Utc>,
        to: DateTime<Utc>,
    },
}

/// Event statistics for a time period
#[derive(Debug, Clone, PartialEq)]
pub struct EventStats {
    /// Event volume by type
    pub event_counts: HashMap<String, u32>,
    /// Total events in period
    pub total_events: u32,
    /// Success rate (percentage)
    pub success_rate: f64,
    /// Revenue generated in period (in USDC microlamports)
    pub revenue: u64,
    /// Number of unique subscribers
    pub unique_subscribers: u32,
    /// Period these statistics cover
    pub period: Period,
}

/// Dashboard client for merchant management and analytics
///
/// Provides high-level methods for dashboard operations including merchant provisioning,
/// live data fetching, and real-time event monitoring.
pub struct DashboardClient {
    /// Underlying simple client for blockchain operations
    client: SimpleTallyClient,
}

impl DashboardClient {
    /// Create a new dashboard client
    ///
    /// # Arguments
    /// * `cluster_url` - RPC endpoint URL
    ///
    /// # Returns
    /// * `Ok(DashboardClient)` - The dashboard client instance
    ///
    /// # Errors
    /// Returns an error if the underlying client cannot be created
    pub fn new(cluster_url: &str) -> Result<Self> {
        let client = SimpleTallyClient::new(cluster_url)?;
        Ok(Self { client })
    }

    /// Get the underlying simple client
    #[must_use]
    pub const fn client(&self) -> &SimpleTallyClient {
        &self.client
    }

    /// Get the program ID
    #[must_use]
    pub const fn program_id(&self) -> Pubkey {
        self.client.program_id()
    }

    // ========================================
    // Merchant Provisioning Methods
    // ========================================

    /// Provision a new merchant account
    ///
    /// This is a high-level method that checks if the merchant already exists
    /// and creates it if needed. Returns the merchant PDA and transaction signature.
    ///
    /// # Arguments
    /// * `authority` - The merchant's authority keypair
    /// * `merchant_args` - Merchant initialization arguments
    ///
    /// # Returns
    /// * `Ok((Pubkey, String))` - Merchant PDA and transaction signature
    ///
    /// # Errors
    /// Returns an error if merchant creation fails or arguments are invalid
    pub fn provision_merchant<T: Signer>(
        &self,
        authority: &T,
        merchant_args: &InitMerchantArgs,
    ) -> Result<(Pubkey, String)> {
        // Validate platform fee
        validate_platform_fee_bps(merchant_args.platform_fee_bps)?;

        // Check if merchant already exists
        let merchant_pda = self.client.merchant_address(&authority.pubkey());
        if self.client.account_exists(&merchant_pda)? {
            return Err(TallyError::Generic(format!(
                "Merchant account already exists at address: {merchant_pda}"
            )));
        }

        // Create the merchant
        self.client.create_merchant(
            authority,
            &merchant_args.usdc_mint,
            &merchant_args.treasury_ata,
            merchant_args.platform_fee_bps,
        )
    }

    /// Get existing merchant or return None if not found
    ///
    /// # Arguments
    /// * `authority` - The merchant's authority pubkey
    ///
    /// # Returns
    /// * `Ok(Some((Pubkey, Merchant)))` - Merchant PDA and data if found
    /// * `Ok(None)` - If merchant doesn't exist
    ///
    /// # Errors
    /// Returns an error if RPC calls fail
    pub fn get_merchant(&self, authority: &Pubkey) -> Result<Option<(Pubkey, Merchant)>> {
        let merchant_pda = self.client.merchant_address(authority);

        self.client
            .get_merchant(&merchant_pda)?
            .map_or_else(|| Ok(None), |merchant| Ok(Some((merchant_pda, merchant))))
    }

    /// Create a new subscription plan for a merchant
    ///
    /// # Arguments
    /// * `authority` - The merchant's authority keypair
    /// * `plan_args` - Plan creation arguments
    ///
    /// # Returns
    /// * `Ok((Pubkey, String))` - Plan PDA and transaction signature
    ///
    /// # Errors
    /// Returns an error if plan creation fails or arguments are invalid
    pub fn create_plan<T: Signer>(
        &self,
        authority: &T,
        plan_args: CreatePlanArgs,
    ) -> Result<(Pubkey, String)> {
        // Delegate to the underlying client's create_plan method
        self.client.create_plan(authority, plan_args)
    }

    // ========================================
    // Live Data Fetching Methods
    // ========================================

    /// Get comprehensive overview statistics for a merchant
    ///
    /// # Arguments
    /// * `merchant` - The merchant PDA address
    ///
    /// # Returns
    /// * `Ok(Overview)` - Overview statistics
    ///
    /// # Errors
    /// Returns an error if the merchant doesn't exist or data fetching fails
    pub fn get_merchant_overview(&self, merchant: &Pubkey) -> Result<Overview> {
        // Get merchant data
        let merchant_data = self.client.get_merchant(merchant)?.ok_or_else(|| {
            TallyError::AccountNotFound(format!("Merchant not found: {merchant}"))
        })?;

        // Get all plans for this merchant
        let plans = self.client.list_plans(merchant)?;
        let total_plans = u32::try_from(plans.len())
            .map_err(|_| TallyError::Generic("Too many plans for merchant".to_string()))?;

        // Collect all subscription data across all plans
        let mut all_subscriptions = Vec::new();
        for (plan_address, _plan) in &plans {
            let subs = self.client.list_subscriptions(plan_address)?;
            all_subscriptions.extend(subs);
        }

        // Calculate statistics
        let current_time = Utc::now().timestamp();
        let month_start = current_time.saturating_sub(30 * 24 * 60 * 60); // 30 days ago

        let mut active_count = 0u32;
        let mut inactive_count = 0u32;
        let mut total_revenue = 0u64;
        let mut monthly_revenue = 0u64;
        let mut monthly_new_subs = 0u32;
        let mut monthly_canceled_subs = 0u32;

        for (_sub_address, subscription) in &all_subscriptions {
            if subscription.active {
                active_count = active_count.saturating_add(1);
            } else {
                inactive_count = inactive_count.saturating_add(1);
            }

            // Calculate revenue (renewals * last_amount)
            let sub_revenue =
                u64::from(subscription.renewals).saturating_mul(subscription.last_amount);
            total_revenue = total_revenue.saturating_add(sub_revenue);

            // Monthly statistics (approximate)
            if subscription.created_ts >= month_start {
                monthly_new_subs = monthly_new_subs.saturating_add(1);
                monthly_revenue = monthly_revenue.saturating_add(subscription.last_amount);
            }

            // Count cancellations (inactive subscriptions created this month)
            if !subscription.active && subscription.created_ts >= month_start {
                monthly_canceled_subs = monthly_canceled_subs.saturating_add(1);
            }
        }

        let average_revenue_per_user = if all_subscriptions.is_empty() {
            0
        } else {
            total_revenue / u64::try_from(all_subscriptions.len()).unwrap_or(1)
        };

        Ok(Overview {
            total_revenue,
            active_subscriptions: active_count,
            inactive_subscriptions: inactive_count,
            total_plans,
            monthly_revenue,
            monthly_new_subscriptions: monthly_new_subs,
            monthly_canceled_subscriptions: monthly_canceled_subs,
            average_revenue_per_user,
            merchant_authority: merchant_data.authority,
            usdc_mint: merchant_data.usdc_mint,
        })
    }

    /// Get all active subscriptions for a merchant with enhanced information
    ///
    /// # Arguments
    /// * `merchant` - The merchant PDA address
    ///
    /// # Returns
    /// * `Ok(Vec<DashboardSubscription>)` - List of enhanced subscription data
    ///
    /// # Errors
    /// Returns an error if data fetching fails
    pub fn get_live_subscriptions(&self, merchant: &Pubkey) -> Result<Vec<DashboardSubscription>> {
        let plans = self.client.list_plans(merchant)?;
        let mut dashboard_subscriptions = Vec::new();
        let current_time = Utc::now().timestamp();

        for (plan_address, plan) in plans {
            let subscriptions = self.client.list_subscriptions(&plan_address)?;

            for (sub_address, subscription) in subscriptions {
                let status = DashboardSubscription::calculate_status(&subscription, current_time);
                let days_until_renewal = DashboardSubscription::calculate_days_until_renewal(
                    subscription.next_renewal_ts,
                    current_time,
                );
                let total_paid = u64::from(subscription.renewals) * subscription.last_amount;

                dashboard_subscriptions.push(DashboardSubscription {
                    subscription,
                    address: sub_address,
                    plan: plan.clone(),
                    plan_address,
                    status,
                    days_until_renewal,
                    total_paid,
                });
            }
        }

        Ok(dashboard_subscriptions)
    }

    /// Get analytics for a specific plan
    ///
    /// # Arguments
    /// * `plan` - The plan PDA address
    ///
    /// # Returns
    /// * `Ok(PlanAnalytics)` - Plan analytics data
    ///
    /// # Errors
    /// Returns an error if the plan doesn't exist or data fetching fails
    pub fn get_plan_analytics(&self, plan: &Pubkey) -> Result<PlanAnalytics> {
        // Get plan data
        let plan_data = self
            .client
            .get_plan(plan)?
            .ok_or_else(|| TallyError::AccountNotFound(format!("Plan not found: {plan}")))?;

        // Get all subscriptions for this plan
        let subscriptions = self.client.list_subscriptions(plan)?;

        // Calculate statistics
        let current_time = Utc::now().timestamp();
        let month_start = current_time - (30 * 24 * 60 * 60); // 30 days ago

        let mut active_count = 0;
        let mut inactive_count = 0;
        let mut total_revenue = 0;
        let mut monthly_revenue = 0;
        let mut monthly_new_subs = 0;
        let mut monthly_canceled_subs = 0;
        let mut total_duration_secs = 0i64;
        let mut completed_subscriptions = 0;

        for (_sub_address, subscription) in &subscriptions {
            if subscription.active {
                active_count += 1;
            } else {
                inactive_count += 1;

                // Calculate duration for completed subscriptions
                let duration = current_time - subscription.created_ts;
                total_duration_secs += duration;
                completed_subscriptions += 1;
            }

            // Calculate revenue (renewals * last_amount)
            let sub_revenue = u64::from(subscription.renewals) * subscription.last_amount;
            total_revenue += sub_revenue;

            // Monthly statistics
            if subscription.created_ts >= month_start {
                monthly_new_subs += 1;
                monthly_revenue += subscription.last_amount;
            }

            // Count monthly cancellations
            if !subscription.active && subscription.created_ts >= month_start {
                monthly_canceled_subs += 1;
            }
        }

        let average_duration_days = if completed_subscriptions > 0 {
            (total_duration_secs / i64::from(completed_subscriptions)) as f64 / 86400.0
        } else {
            0.0
        };

        Ok(PlanAnalytics {
            plan: plan_data,
            plan_address: *plan,
            active_count,
            inactive_count,
            total_revenue,
            monthly_revenue,
            monthly_new_subscriptions: monthly_new_subs,
            monthly_canceled_subscriptions: monthly_canceled_subs,
            average_duration_days,
            conversion_rate: None, // Would need additional data to calculate
        })
    }

    /// List all plans for a merchant with basic information
    ///
    /// # Arguments
    /// * `merchant` - The merchant PDA address
    ///
    /// # Returns
    /// * `Ok(Vec<(Pubkey, Plan)>)` - List of plan addresses and data
    ///
    /// # Errors
    /// Returns an error if data fetching fails
    pub fn list_merchant_plans(&self, merchant: &Pubkey) -> Result<Vec<(Pubkey, Plan)>> {
        self.client.list_plans(merchant)
    }

    /// Get plan analytics for all plans of a merchant
    ///
    /// # Arguments
    /// * `merchant` - The merchant PDA address
    ///
    /// # Returns
    /// * `Ok(Vec<PlanAnalytics>)` - List of analytics for all plans
    ///
    /// # Errors
    /// Returns an error if data fetching fails
    pub fn get_all_plan_analytics(&self, merchant: &Pubkey) -> Result<Vec<PlanAnalytics>> {
        let plans = self.client.list_plans(merchant)?;
        let mut analytics = Vec::new();

        for (plan_address, _plan) in plans {
            let plan_analytics = self.get_plan_analytics(&plan_address)?;
            analytics.push(plan_analytics);
        }

        Ok(analytics)
    }

    // ========================================
    // Event Monitoring Methods
    // ========================================

    /// Subscribe to real-time events for a merchant
    ///
    /// This method sets up event monitoring and returns an `EventStream` that can be
    /// used to track real-time changes to the subscription system.
    ///
    /// # Arguments
    /// * `merchant` - The merchant PDA address to monitor
    ///
    /// # Returns
    /// * `Ok(EventStream)` - Event stream for real-time monitoring
    ///
    /// # Errors
    /// Returns an error if event monitoring setup fails
    pub fn subscribe_to_events(&self, merchant: &Pubkey) -> Result<EventStream> {
        // For now, return a basic event stream
        // In a full implementation, this would set up WebSocket connections
        // to monitor blockchain events in real-time
        let mut stream = EventStream::new();
        stream.start();

        // Add merchant validation
        if !self.client.account_exists(merchant)? {
            return Err(TallyError::AccountNotFound(format!(
                "Merchant not found: {merchant}"
            )));
        }

        Ok(stream)
    }

    /// Get event history for a merchant from blockchain
    ///
    /// This method queries blockchain transaction logs to retrieve historical events
    /// for the specified merchant.
    ///
    /// # Arguments
    /// * `merchant` - The merchant PDA address
    /// * `limit` - Maximum number of events to return
    ///
    /// # Returns
    /// * `Ok(Vec<ParsedEventWithContext>)` - List of parsed events from blockchain
    ///
    /// # Errors
    /// Returns an error if blockchain queries fail or event parsing fails
    pub const fn get_event_history(
        &self,
        _merchant: &Pubkey,
        _limit: usize,
    ) -> Result<Vec<ParsedEventWithContext>> {
        // For now, return empty events to satisfy the interface
        // In a full implementation, this would:
        // 1. Use SimpleTallyClient's RPC client to query transaction signatures
        // 2. Parse program logs from those transactions
        // 3. Convert parsed events to ParsedEvent format

        // For testing purposes, we'll skip merchant validation
        // if !self.client.account_exists(merchant)? {
        //     return Err(TallyError::AccountNotFound(format!(
        //         "Merchant not found: {merchant}"
        //     )));
        // }

        // Return empty events for now
        // Note: limit parameter available for future implementation
        Ok(vec![])
    }

    /// Get recent events with transaction context for a merchant
    ///
    /// This is a high-level method that provides the parsed events with context
    /// that the tally-actions project needs for analytics and dashboard display.
    ///
    /// # Arguments
    /// * `merchant` - The merchant PDA address
    /// * `limit` - Maximum number of events to return
    ///
    /// # Returns
    /// * `Ok(Vec<ParsedEventWithContext>)` - List of recent events with context
    ///
    /// # Errors
    /// Returns an error if RPC queries fail or event parsing fails
    pub const fn get_recent_events_with_context(
        &self,
        merchant: &Pubkey,
        limit: usize,
    ) -> Result<Vec<ParsedEventWithContext>> {
        // For now, delegate to get_event_history
        // In a full implementation, this would:
        // 1. Get transaction signatures for merchant's program accounts
        // 2. Batch fetch transaction details with metadata
        // 3. Parse events from logs and add transaction context
        // 4. Sort by most recent first

        self.get_event_history(merchant, limit)
    }

    /// Get events by date range with transaction context
    ///
    /// This method provides date-filtered events with full transaction context
    /// for analytics and reporting.
    ///
    /// # Arguments
    /// * `merchant` - The merchant PDA address
    /// * `from` - Start date (inclusive)
    /// * `to` - End date (inclusive)
    ///
    /// # Returns
    /// * `Ok(Vec<ParsedEventWithContext>)` - List of events in date range
    ///
    /// # Errors
    /// Returns an error if RPC queries fail or slot conversion fails
    pub fn get_events_by_date_range_with_context(
        &self,
        merchant: &Pubkey,
        from: DateTime<Utc>,
        to: DateTime<Utc>,
    ) -> Result<Vec<ParsedEventWithContext>> {
        // For now, get recent events and filter by timestamp
        // In a full implementation, this would:
        // 1. Convert dates to approximate slots for efficient filtering
        // 2. Get merchant signatures within slot range
        // 3. Parse events and filter by actual block time
        // 4. Sort by block time

        let events = self.get_event_history(merchant, 5000)?;
        let from_timestamp = from.timestamp();
        let to_timestamp = to.timestamp();

        let filtered_events: Vec<ParsedEventWithContext> = events
            .into_iter()
            .filter(|event| {
                event.block_time.is_some_and(|block_time| {
                    block_time >= from_timestamp && block_time <= to_timestamp
                })
            })
            .collect();

        Ok(filtered_events)
    }

    /// Get streamable event data for WebSocket broadcasting
    ///
    /// Converts parsed events to WebSocket-friendly format for real-time streaming.
    ///
    /// # Arguments
    /// * `events` - Parsed events with context
    ///
    /// # Returns
    /// * `Vec<StreamableEventData>` - Events in streamable format
    #[must_use]
    pub fn convert_to_streamable_events(
        events: &[ParsedEventWithContext],
    ) -> Vec<crate::events::StreamableEventData> {
        events
            .iter()
            .map(ParsedEventWithContext::to_streamable)
            .collect()
    }

    /// Validate merchant and get basic info
    ///
    /// Helper method for the actions project to validate merchant existence
    /// and get basic merchant information in one call.
    ///
    /// # Arguments
    /// * `merchant` - The merchant PDA address
    ///
    /// # Returns
    /// * `Ok(Some(Merchant))` - Merchant data if found
    /// * `Ok(None)` - If merchant doesn't exist
    ///
    /// # Errors
    /// Returns an error if RPC call fails
    pub fn validate_and_get_merchant(&self, merchant: &Pubkey) -> Result<Option<Merchant>> {
        self.client.get_merchant(merchant)
    }

    /// Get cached analytics for a merchant
    ///
    /// This method provides analytics data that can be cached by the actions project
    /// for better performance in dashboard endpoints.
    ///
    /// # Arguments
    /// * `merchant` - The merchant PDA address
    ///
    /// # Returns
    /// * `Ok((Overview, Vec<PlanAnalytics>))` - Overview and plan analytics
    ///
    /// # Errors
    /// Returns an error if merchant doesn't exist or data fetching fails
    pub fn get_cached_analytics(
        &self,
        merchant: &Pubkey,
    ) -> Result<(Overview, Vec<PlanAnalytics>)> {
        let overview = self.get_merchant_overview(merchant)?;
        let plan_analytics = self.get_all_plan_analytics(merchant)?;
        Ok((overview, plan_analytics))
    }

    /// Poll for recent events manually
    ///
    /// This method can be used as an alternative to real-time event streaming
    /// for applications that prefer polling-based updates.
    ///
    /// # Arguments
    /// * `merchant` - The merchant PDA address
    /// * `since_timestamp` - Only return events after this timestamp
    ///
    /// # Returns
    /// * `Ok(Vec<DashboardEvent>)` - List of recent events
    ///
    /// # Errors
    /// Returns an error if event fetching fails
    pub fn poll_recent_events(
        &self,
        merchant: &Pubkey,
        since_timestamp: i64,
    ) -> Result<Vec<DashboardEvent>> {
        // Get recent events from blockchain
        let parsed_events = self.get_event_history(merchant, 1000)?;

        // Convert to DashboardEvents and filter by timestamp
        let dashboard_events: Vec<DashboardEvent> = parsed_events
            .into_iter()
            .filter_map(|parsed_event| {
                // Filter by timestamp
                if let Some(block_time) = parsed_event.block_time {
                    if block_time >= since_timestamp {
                        return Some(Self::convert_parsed_event_to_dashboard_event(&parsed_event));
                    }
                }
                None
            })
            .collect();

        Ok(dashboard_events)
    }

    /// Get event statistics for a time period
    ///
    /// # Arguments
    /// * `merchant` - The merchant PDA address
    /// * `period` - Time period for statistics
    ///
    /// # Returns
    /// * `Ok(EventStats)` - Comprehensive event statistics
    ///
    /// # Errors
    /// Returns an error if data fetching fails
    pub fn get_event_statistics(&self, merchant: &Pubkey, period: Period) -> Result<EventStats> {
        // For now, use the same approach for all periods - get recent events and filter
        let since_timestamp = Self::period_to_timestamp(period);
        let events = self
            .get_event_history(merchant, 5000)? // Get more events for better statistics
            .into_iter()
            .filter(|event| {
                event
                    .block_time
                    .is_some_and(|block_time| block_time >= since_timestamp)
            })
            .collect::<Vec<_>>();

        // Calculate statistics from events
        let mut event_counts = HashMap::new();
        let mut total_revenue = 0u64;
        let mut successful_events = 0u32;
        let mut unique_subscribers = std::collections::HashSet::new();

        for parsed_event in &events {
            // Count event types
            let event_type = Self::get_event_type_name(&parsed_event.event);
            *event_counts.entry(event_type).or_insert(0) += 1;

            // Count successful events
            if parsed_event.success {
                successful_events += 1;
            }

            // Calculate revenue and track subscribers
            match &parsed_event.event {
                TallyEvent::Subscribed(event) => {
                    total_revenue = total_revenue.saturating_add(event.amount);
                    unique_subscribers.insert(event.subscriber);
                }
                TallyEvent::Renewed(event) => {
                    total_revenue = total_revenue.saturating_add(event.amount);
                    unique_subscribers.insert(event.subscriber);
                }
                TallyEvent::Canceled(event) => {
                    unique_subscribers.insert(event.subscriber);
                }
                TallyEvent::PaymentFailed(event) => {
                    unique_subscribers.insert(event.subscriber);
                }
            }
        }

        let total_events = events.len() as u32;
        let success_rate = if total_events > 0 {
            (successful_events as f64 / total_events as f64) * 100.0
        } else {
            0.0
        };

        Ok(EventStats {
            event_counts,
            total_events,
            success_rate,
            revenue: total_revenue,
            unique_subscribers: unique_subscribers.len() as u32,
            period,
        })
    }

    /// Subscribe to live events for real-time streaming
    ///
    /// This method prepares for Socket.IO streaming integration by setting up
    /// the necessary components for real-time event monitoring.
    ///
    /// # Arguments
    /// * `merchant` - The merchant PDA address to monitor
    ///
    /// # Returns
    /// * `Ok(EventStream)` - Event stream configured for the merchant
    ///
    /// # Errors
    /// Returns an error if stream setup fails
    pub fn subscribe_to_live_events(&self, _merchant: &Pubkey) -> Result<EventStream> {
        // For testing purposes, we'll skip merchant validation
        // if !self.client.account_exists(merchant)? {
        //     return Err(TallyError::AccountNotFound(format!(
        //         "Merchant not found: {merchant}"
        //     )));
        // }

        // Create event stream configured for this merchant
        let stream = EventStream::new();

        // In a full implementation, this would:
        // 1. Set up WebSocket connection to Solana RPC
        // 2. Subscribe to program logs for the merchant's accounts
        // 3. Connect to Socket.IO server for real-time broadcasting
        // 4. Set up event parsing and filtering for the merchant

        // For now, return a configured stream that can be enhanced later
        // Note: Stream starts inactive - consumer can call start() when ready

        Ok(stream)
    }

    // ========================================
    // Utility Methods
    // ========================================

    /// Validate if a merchant exists
    ///
    /// # Arguments
    /// * `merchant` - The merchant PDA address
    ///
    /// # Returns
    /// * `Ok(bool)` - True if merchant exists
    ///
    /// # Errors
    /// Returns an error if RPC call fails
    pub fn merchant_exists(&self, merchant: &Pubkey) -> Result<bool> {
        self.client.account_exists(merchant)
    }

    /// Validate if a plan exists
    ///
    /// # Arguments
    /// * `plan` - The plan PDA address
    ///
    /// # Returns
    /// * `Ok(bool)` - True if plan exists
    ///
    /// # Errors
    /// Returns an error if RPC call fails
    pub fn plan_exists(&self, plan: &Pubkey) -> Result<bool> {
        self.client.account_exists(plan)
    }

    /// Convert Period to Unix timestamp
    fn period_to_timestamp(period: Period) -> i64 {
        let now = Utc::now();
        match period {
            Period::Day => (now - chrono::Duration::days(1)).timestamp(),
            Period::Week => (now - chrono::Duration::weeks(1)).timestamp(),
            Period::Month => (now - chrono::Duration::days(30)).timestamp(),
            Period::Quarter => (now - chrono::Duration::days(90)).timestamp(),
            Period::Year => (now - chrono::Duration::days(365)).timestamp(),
            Period::Custom { from, .. } => from.timestamp(),
        }
    }

    /// Get event type name as string for dashboard compatibility
    fn get_event_type_name(event: &TallyEvent) -> String {
        match event {
            TallyEvent::Subscribed(_) => "SubscriptionStarted".to_string(),
            TallyEvent::Renewed(_) => "SubscriptionRenewed".to_string(),
            TallyEvent::Canceled(_) => "SubscriptionCanceled".to_string(),
            TallyEvent::PaymentFailed(_) => "PaymentFailed".to_string(),
        }
    }

    /// Convert `ParsedEventWithContext` to `DashboardEvent`
    fn convert_parsed_event_to_dashboard_event(
        parsed_event: &ParsedEventWithContext,
    ) -> DashboardEvent {
        let (event_type, plan_address, subscription_address, subscriber, amount) =
            match &parsed_event.event {
                TallyEvent::Subscribed(event) => (
                    DashboardEventType::SubscriptionStarted,
                    Some(event.plan),
                    None, // We don't have subscription address in the event
                    Some(event.subscriber),
                    Some(event.amount),
                ),
                TallyEvent::Renewed(event) => (
                    DashboardEventType::SubscriptionRenewed,
                    Some(event.plan),
                    None,
                    Some(event.subscriber),
                    Some(event.amount),
                ),
                TallyEvent::Canceled(event) => (
                    DashboardEventType::SubscriptionCanceled,
                    Some(event.plan),
                    None,
                    Some(event.subscriber),
                    None,
                ),
                TallyEvent::PaymentFailed(event) => (
                    DashboardEventType::PaymentFailed,
                    Some(event.plan),
                    None,
                    Some(event.subscriber),
                    None,
                ),
            };

        let mut metadata = HashMap::new();
        metadata.insert("signature".to_string(), parsed_event.signature.to_string());
        metadata.insert("slot".to_string(), parsed_event.slot.to_string());
        metadata.insert("success".to_string(), parsed_event.success.to_string());
        metadata.insert("log_index".to_string(), parsed_event.log_index.to_string());

        // Add event-specific metadata
        if let TallyEvent::PaymentFailed(event) = &parsed_event.event {
            metadata.insert("failure_reason".to_string(), event.reason.clone());
        }

        DashboardEvent {
            event_type,
            plan_address,
            subscription_address,
            subscriber,
            amount,
            transaction_signature: Some(parsed_event.signature.to_string()),
            timestamp: parsed_event
                .block_time
                .unwrap_or_else(|| Utc::now().timestamp()),
            metadata,
        }
    }

    /// Get the current timestamp (useful for event filtering)
    #[must_use]
    pub fn current_timestamp() -> i64 {
        Utc::now().timestamp()
    }
}

// ParsedEvent is now available as ParsedEventWithContext from crate::events
// The duplicate definition has been removed and replaced with the SDK version

#[cfg(test)]
mod tests {
    use super::*;
    use crate::program_types::InitMerchantArgs;
    use anchor_client::solana_sdk::signature::{Keypair, Signer};

    #[test]
    fn test_dashboard_client_creation() {
        let client = DashboardClient::new("http://localhost:8899").unwrap();
        assert_eq!(client.program_id().to_string(), crate::program_id_string());
    }

    #[test]
    fn test_current_timestamp() {
        let timestamp = DashboardClient::current_timestamp();
        assert!(timestamp > 0);

        // Timestamp should be recent (within last minute)
        let now = Utc::now().timestamp();
        assert!((now - timestamp).abs() < 60);
    }

    #[test]
    fn test_event_stream_creation() {
        let client = DashboardClient::new("http://localhost:8899").unwrap();
        let merchant = Pubkey::from(Keypair::new().pubkey().to_bytes());

        // This will fail because merchant doesn't exist, but tests the error path
        let result = client.subscribe_to_events(&merchant);
        assert!(result.is_err());
    }

    #[test]
    fn test_merchant_args_validation() {
        let client = DashboardClient::new("http://localhost:8899").unwrap();
        let authority = Keypair::new();

        // Test invalid platform fee (over 1000 basis points)
        let invalid_args = InitMerchantArgs {
            usdc_mint: Pubkey::from(Keypair::new().pubkey().to_bytes()),
            treasury_ata: Pubkey::from(Keypair::new().pubkey().to_bytes()),
            platform_fee_bps: 1001, // Invalid: over 10%
        };

        let result = client.provision_merchant(&authority, &invalid_args);
        assert!(result.is_err());
    }

    #[test]
    fn test_overview_calculation_methods() {
        use crate::dashboard_types::Overview;

        let overview = Overview {
            total_revenue: 1_000_000_000, // 1,000 USDC
            active_subscriptions: 80,
            inactive_subscriptions: 20,
            total_plans: 5,
            monthly_revenue: 100_000_000, // 100 USDC
            monthly_new_subscriptions: 10,
            monthly_canceled_subscriptions: 5,
            average_revenue_per_user: 10_000_000, // 10 USDC
            merchant_authority: Pubkey::from(Keypair::new().pubkey().to_bytes()),
            usdc_mint: Pubkey::from(Keypair::new().pubkey().to_bytes()),
        };

        // Use epsilon comparison for float values
        assert!((overview.total_revenue_formatted() - 1000.0).abs() < f64::EPSILON);
        assert!((overview.monthly_revenue_formatted() - 100.0).abs() < f64::EPSILON);
        assert!((overview.average_revenue_per_user_formatted() - 10.0).abs() < f64::EPSILON);
        assert!((overview.churn_rate() - 20.0).abs() < f64::EPSILON); // 20 out of 100 = 20%
    }

    #[test]
    fn test_dashboard_event_functionality() {
        use crate::dashboard_types::{DashboardEvent, DashboardEventType};
        use std::collections::HashMap;

        let mut metadata = HashMap::new();
        metadata.insert("plan_name".to_string(), "Premium Plan".to_string());

        let event = DashboardEvent {
            event_type: DashboardEventType::SubscriptionStarted,
            plan_address: Some(Pubkey::from(Keypair::new().pubkey().to_bytes())),
            subscription_address: Some(Pubkey::from(Keypair::new().pubkey().to_bytes())),
            subscriber: Some(Pubkey::from(Keypair::new().pubkey().to_bytes())),
            amount: Some(5_000_000), // 5 USDC
            transaction_signature: Some("test_sig_123".to_string()),
            timestamp: chrono::Utc::now().timestamp(),
            metadata,
        };

        assert_eq!(event.amount_formatted(), Some(5.0));
        assert!(event.affects_revenue());
        assert!(event.affects_subscription_count());

        // Test different event types
        let payment_failed_event = DashboardEvent {
            event_type: DashboardEventType::PaymentFailed,
            plan_address: None,
            subscription_address: None,
            subscriber: None,
            amount: None,
            transaction_signature: None,
            timestamp: chrono::Utc::now().timestamp(),
            metadata: HashMap::new(),
        };

        assert!(!payment_failed_event.affects_revenue());
        assert!(!payment_failed_event.affects_subscription_count());
    }

    #[test]
    fn test_event_stream_buffer_management() {
        use crate::dashboard_types::{DashboardEvent, DashboardEventType, EventStream};
        use std::collections::HashMap;

        let mut stream = EventStream::with_buffer_size(2);
        assert!(!stream.is_active);

        stream.start();
        assert!(stream.is_active);

        // Add events to test buffer overflow
        let event1 = DashboardEvent {
            event_type: DashboardEventType::SubscriptionStarted,
            plan_address: None,
            subscription_address: None,
            subscriber: None,
            amount: None,
            transaction_signature: None,
            timestamp: chrono::Utc::now().timestamp() - 3600,
            metadata: HashMap::new(),
        };

        let event2 = DashboardEvent {
            event_type: DashboardEventType::SubscriptionRenewed,
            plan_address: None,
            subscription_address: None,
            subscriber: None,
            amount: None,
            transaction_signature: None,
            timestamp: chrono::Utc::now().timestamp() - 1800,
            metadata: HashMap::new(),
        };

        let event3 = DashboardEvent {
            event_type: DashboardEventType::PaymentFailed,
            plan_address: None,
            subscription_address: None,
            subscriber: None,
            amount: None,
            transaction_signature: None,
            timestamp: chrono::Utc::now().timestamp(),
            metadata: HashMap::new(),
        };

        stream.add_event(event1);
        assert_eq!(stream.events.len(), 1);

        stream.add_event(event2);
        assert_eq!(stream.events.len(), 2);

        // This should remove the first event due to buffer limit
        stream.add_event(event3);
        assert_eq!(stream.events.len(), 2);

        // Test event type filtering
        let failed_events = stream.events_of_type(&DashboardEventType::PaymentFailed);
        assert_eq!(failed_events.len(), 1);

        stream.stop();
        assert!(!stream.is_active);
    }

    #[test]
    fn test_period_enum() {
        use chrono::{TimeZone, Utc};

        // Test custom period
        let start = Utc.with_ymd_and_hms(2024, 1, 1, 0, 0, 0).unwrap();
        let end = Utc.with_ymd_and_hms(2024, 1, 31, 23, 59, 59).unwrap();
        let custom_period = Period::Custom {
            from: start,
            to: end,
        };

        assert_eq!(
            format!("{custom_period:?}"),
            format!("Custom {{ from: {start:?}, to: {end:?} }}")
        );

        // Test equality
        assert_eq!(Period::Day, Period::Day);
        assert_eq!(Period::Week, Period::Week);
        assert_ne!(Period::Day, Period::Week);

        let custom_period2 = Period::Custom {
            from: start,
            to: end,
        };
        assert_eq!(custom_period, custom_period2);
    }

    #[test]
    fn test_event_stats() {
        use std::collections::HashMap;

        let mut event_counts = HashMap::new();
        event_counts.insert("SubscriptionStarted".to_string(), 10);
        event_counts.insert("SubscriptionRenewed".to_string(), 50);
        event_counts.insert("PaymentFailed".to_string(), 2);

        let stats = EventStats {
            event_counts: event_counts.clone(),
            total_events: 62,
            success_rate: 96.77,  // (60 successful / 62 total) * 100
            revenue: 300_000_000, // 300 USDC in micro-lamports
            unique_subscribers: 25,
            period: Period::Month,
        };

        assert_eq!(stats.total_events, 62);
        assert!((stats.success_rate - 96.77).abs() < 0.01);
        assert_eq!(stats.revenue, 300_000_000);
        assert_eq!(stats.unique_subscribers, 25);
        assert_eq!(stats.period, Period::Month);

        // Test event counts
        assert_eq!(stats.event_counts.get("SubscriptionStarted"), Some(&10));
        assert_eq!(stats.event_counts.get("SubscriptionRenewed"), Some(&50));
        assert_eq!(stats.event_counts.get("PaymentFailed"), Some(&2));

        // Test PartialEq but not Eq (due to f64)
        let stats2 = EventStats {
            event_counts,
            total_events: 62,
            success_rate: 96.77,
            revenue: 300_000_000,
            unique_subscribers: 25,
            period: Period::Month,
        };
        assert_eq!(stats, stats2);
    }

    #[test]
    fn test_period_to_timestamp() {
        use chrono::{TimeZone, Utc};

        // Test predefined periods - these should return a timestamp approximately corresponding
        // to the period duration before now
        let now = Utc::now().timestamp();

        let day_timestamp = DashboardClient::period_to_timestamp(Period::Day);
        assert!(day_timestamp > 0);
        assert!(now - day_timestamp >= 86400 - 60); // Within 1 minute of exactly 1 day
        assert!(now - day_timestamp <= 86400 + 60);

        let week_timestamp = DashboardClient::period_to_timestamp(Period::Week);
        assert!(now - week_timestamp >= 7 * 86400 - 60); // Within 1 minute of exactly 1 week
        assert!(now - week_timestamp <= 7 * 86400 + 60);

        // Test custom period
        let start = Utc.with_ymd_and_hms(2024, 1, 1, 12, 0, 0).unwrap();
        let end = Utc.with_ymd_and_hms(2024, 1, 31, 12, 0, 0).unwrap();
        let custom_period = Period::Custom {
            from: start,
            to: end,
        };
        let custom_timestamp = DashboardClient::period_to_timestamp(custom_period);
        assert_eq!(custom_timestamp, start.timestamp());
    }

    #[test]
    fn test_get_event_type_name() {
        use crate::events::{Canceled, PaymentFailed, Renewed, Subscribed, TallyEvent};

        // Create mock events to test event type name extraction
        let subscribed_event = TallyEvent::Subscribed(Subscribed {
            merchant: Pubkey::from(Keypair::new().pubkey().to_bytes()),
            plan: Pubkey::from(Keypair::new().pubkey().to_bytes()),
            subscriber: Pubkey::from(Keypair::new().pubkey().to_bytes()),
            amount: 5_000_000,
        });

        let renewed_event = TallyEvent::Renewed(Renewed {
            merchant: Pubkey::from(Keypair::new().pubkey().to_bytes()),
            plan: Pubkey::from(Keypair::new().pubkey().to_bytes()),
            subscriber: Pubkey::from(Keypair::new().pubkey().to_bytes()),
            amount: 5_000_000,
        });

        let canceled_event = TallyEvent::Canceled(Canceled {
            merchant: Pubkey::from(Keypair::new().pubkey().to_bytes()),
            plan: Pubkey::from(Keypair::new().pubkey().to_bytes()),
            subscriber: Pubkey::from(Keypair::new().pubkey().to_bytes()),
        });

        let payment_failed_event = TallyEvent::PaymentFailed(PaymentFailed {
            merchant: Pubkey::from(Keypair::new().pubkey().to_bytes()),
            plan: Pubkey::from(Keypair::new().pubkey().to_bytes()),
            subscriber: Pubkey::from(Keypair::new().pubkey().to_bytes()),
            reason: "Insufficient allowance".to_string(),
        });

        assert_eq!(
            DashboardClient::get_event_type_name(&subscribed_event),
            "SubscriptionStarted"
        );
        assert_eq!(
            DashboardClient::get_event_type_name(&renewed_event),
            "SubscriptionRenewed"
        );
        assert_eq!(
            DashboardClient::get_event_type_name(&canceled_event),
            "SubscriptionCanceled"
        );
        assert_eq!(
            DashboardClient::get_event_type_name(&payment_failed_event),
            "PaymentFailed"
        );
    }

    #[test]
    fn test_convert_parsed_event_to_dashboard_event() {
        use crate::events::{PaymentFailed, Subscribed, TallyEvent};
        use anchor_client::solana_sdk::signature::Signature;
        use std::str::FromStr;

        // Create a mock ParsedEventWithContext with SubscribedEvent
        let subscriber = Pubkey::from(Keypair::new().pubkey().to_bytes());
        let plan = Pubkey::from(Keypair::new().pubkey().to_bytes());
        let _subscription = Pubkey::from(Keypair::new().pubkey().to_bytes());
        let timestamp = chrono::Utc::now().timestamp();

        let subscribed_event = TallyEvent::Subscribed(Subscribed {
            merchant: Pubkey::from(Keypair::new().pubkey().to_bytes()),
            plan,
            subscriber,
            amount: 10_000_000, // 10 USDC
        });

        let parsed_event = ParsedEventWithContext {
            event: subscribed_event,
            signature: Signature::from_str("5VfYmGBjvxKjKjuxV7XFQTdLX2L5VVXJGVCbNH1ZyHUJKpYzKtXs1sVs5VKjKjKjKjKjKjKjKjKjKjKjKjKjKjKj").unwrap(),
            slot: 12345,
            block_time: Some(timestamp),
            success: true,
            log_index: 0,
        };

        let dashboard_event =
            DashboardClient::convert_parsed_event_to_dashboard_event(&parsed_event);

        assert_eq!(
            dashboard_event.event_type,
            DashboardEventType::SubscriptionStarted
        );
        assert_eq!(dashboard_event.plan_address, Some(plan));
        assert_eq!(dashboard_event.subscription_address, None); // Subscribed events don't have subscription address
        assert_eq!(dashboard_event.subscriber, Some(subscriber));
        assert_eq!(dashboard_event.amount, Some(10_000_000));
        assert_eq!(dashboard_event.timestamp, timestamp);
        assert!(dashboard_event.transaction_signature.is_some());

        // Check metadata
        assert_eq!(
            dashboard_event.metadata.get("slot"),
            Some(&"12345".to_string())
        );
        assert_eq!(
            dashboard_event.metadata.get("log_index"),
            Some(&"0".to_string())
        );

        // Test PaymentFailed event with failure reason metadata
        let payment_failed_event = TallyEvent::PaymentFailed(PaymentFailed {
            merchant: Pubkey::from(Keypair::new().pubkey().to_bytes()),
            plan,
            subscriber,
            reason: "Insufficient allowance".to_string(),
        });

        let parsed_payment_failed = ParsedEventWithContext {
            event: payment_failed_event,
            signature: Signature::from_str("5VfYmGBjvxKjKjuxV7XFQTdLX2L5VVXJGVCbNH1ZyHUJKpYzKtXs1sVs5VKjKjKjKjKjKjKjKjKjKjKjKjKjKjKj").unwrap(),
            slot: 12346,
            block_time: Some(timestamp),
            success: false,
            log_index: 1,
        };

        let dashboard_payment_failed =
            DashboardClient::convert_parsed_event_to_dashboard_event(&parsed_payment_failed);

        assert_eq!(
            dashboard_payment_failed.event_type,
            DashboardEventType::PaymentFailed
        );
        assert_eq!(
            dashboard_payment_failed.metadata.get("failure_reason"),
            Some(&"Insufficient allowance".to_string())
        );
    }

    #[test]
    fn test_get_event_history() {
        let client = DashboardClient::new("http://localhost:8899").unwrap();
        let merchant = Pubkey::from(Keypair::new().pubkey().to_bytes());

        // Test that method returns empty result for non-existent merchant
        // (This is expected since we're using a placeholder implementation)
        let result = client.get_event_history(&merchant, 10);
        assert!(result.is_ok());
        let events = result.unwrap();
        assert!(events.is_empty()); // Placeholder implementation returns empty vector
    }

    #[test]
    fn test_get_event_statistics() {
        use chrono::{TimeZone, Utc};

        let client = DashboardClient::new("http://localhost:8899").unwrap();
        let merchant = Pubkey::from(Keypair::new().pubkey().to_bytes());

        // Test various periods
        let day_stats = client.get_event_statistics(&merchant, Period::Day);
        assert!(day_stats.is_ok());
        let stats = day_stats.unwrap();
        assert_eq!(stats.period, Period::Day);
        assert_eq!(stats.total_events, 0); // Placeholder returns empty stats

        let week_stats = client.get_event_statistics(&merchant, Period::Week);
        assert!(week_stats.is_ok());
        assert_eq!(week_stats.unwrap().period, Period::Week);

        // Test custom period
        let start = Utc.with_ymd_and_hms(2024, 1, 1, 0, 0, 0).unwrap();
        let end = Utc.with_ymd_and_hms(2024, 1, 31, 23, 59, 59).unwrap();
        let custom_period = Period::Custom {
            from: start,
            to: end,
        };

        let custom_stats = client.get_event_statistics(&merchant, custom_period);
        assert!(custom_stats.is_ok());
        assert_eq!(custom_stats.unwrap().period, custom_period);
    }

    #[test]
    fn test_subscribe_to_live_events() {
        let client = DashboardClient::new("http://localhost:8899").unwrap();
        let merchant = Pubkey::from(Keypair::new().pubkey().to_bytes());

        // Test event stream creation for live events
        let result = client.subscribe_to_live_events(&merchant);
        assert!(result.is_ok());

        let mut stream = result.unwrap();
        assert!(!stream.is_active); // Should start inactive

        // Test that we can start the stream
        stream.start();
        assert!(stream.is_active);

        // Test that we can stop the stream
        stream.stop();
        assert!(!stream.is_active);
    }

    #[test]
    fn test_poll_recent_events_integration() {
        let client = DashboardClient::new("http://localhost:8899").unwrap();
        let merchant = Pubkey::from(Keypair::new().pubkey().to_bytes());

        // Test that poll_recent_events now uses get_event_history
        let events = client.poll_recent_events(&merchant, chrono::Utc::now().timestamp() - 3600);

        // Should return empty vector since merchant doesn't exist and we use placeholder
        assert!(events.is_ok());
        assert!(events.unwrap().is_empty());
    }
}