Jake Vanderwerf
2025-11-04 42fa8304ddb811b0f725f245130f70c0f5e86a6c
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
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
<?php
namespace JVBase\managers;
 
use JVBase\managers\MagicLinkManager;
use JVBase\integrations\Cloudflare;
use JVBase\meta\MetaForm;
use JVBase\utility\Features;
use WP_User;
use WP_Error;
 
if (!defined('ABSPATH')) {
    exit;
}
 
/**
 * Referral Tracking Manager
 *
 * Handles user referral codes, tracking, rewards, and reporting.
 * Keeps referrals separate from invitations system.
 */
class ReferralManager
{
    protected $wpdb;
    protected MagicLinkManager $magic_link;
    protected CacheManager $cache;
    protected string $referrals_table;
    protected string $rewards_table;
 
    // Default reward settings
    protected array $default_settings = [
        'referrer_reward_applies_to' => 'per_user',  // 'per_user' or 'flat_total'
        'referrer_reward_amount' => 25.00,
        'referrer_reward_type'  => 'fixed',
        'referee_reward_type' => 'percentage',  // 'percentage' or 'fixed'
        'referee_reward_amount' => 20,  // 20% or $20
        'referee_reward_applies_to' => 'first_order'  // 'first_order' or 'all_orders'
    ];
 
    public function __construct()
    {
        global $wpdb;
        $this->wpdb = $wpdb;
        $this->cache = CacheManager::for('referrals', WEEK_IN_SECONDS);
        $this->referrals_table = BASE . 'referrals';
        $this->rewards_table = BASE . 'referral_rewards';
        $this->magic_link = new MagicLinkManager();
 
        // Hook into user registration to track referrals
        add_action('user_register', [$this, 'processReferral'], 10, 1);
 
        // Add meta boxes for admin to manage referrals
        add_action('show_user_profile', [$this, 'displayUserReferralInfo']);
        add_action('edit_user_profile', [$this, 'displayUserReferralInfo']);
 
        // Save referral code changes
        add_action('personal_options_update', [$this, 'saveUserReferralCode']);
        add_action('edit_user_profile_update', [$this, 'saveUserReferralCode']);
 
        add_filter(BASE.'new_user_email_content', [$this, 'addReferralToWelcomeEmail'], 99, 2);
 
 
        add_filter('jvbAdditionalActions', [$this, 'outputShareWidget']);
 
        add_action('wp_enqueue_scripts', [$this, 'enqueueScripts']);
        // Schedule cron jobs for reports
        $this->registerCronJobs();
 
        // Register admin subpage
        add_filter('jvbAdminSubpages', [$this, 'addSubpage'], 10, 1);
 
        // Add admin bar label for referral page
        add_action('admin_bar_menu', [$this, 'addReferralPageLabel'], 999);
 
        // Add admin notice to referral page edit screen
        add_action('admin_notices', [$this, 'showReferralPageNotice']);
 
 
        add_filter('jvbDashboardPage', [$this, 'renderDashPage'], 10, 2);
 
        // Handle settings save
        add_action('admin_init', [$this, 'registerSettings']);
    }
 
    public function enqueueScripts():void
    {
        $requirements = [
            'jvb-utility',
            'jvb-a11y',
            'jvb-popup',
            'jvb-tabs',
        ];
 
        if (Features::hasIntegration('cloudflare') && JVB()->connect('cloudflare')->isSetUp()) {
            $requirements[] = 'cloudflare-turnstile';
        }
        wp_enqueue_script(
            'jvb-referral',
            JVB_URL . 'assets/js/min/referral.min.js',
            $requirements,
            '1.0.0',
            true
        );
    }
 
    /**
     * Register cron jobs for automated reporting
     */
    protected function registerCronJobs(): void
    {
        add_action(BASE . 'referral_daily_report', [$this, 'sendDailyReport']);
        add_action(BASE . 'referral_weekly_report', [$this, 'sendWeeklyReport']);
 
        // Schedule if not already scheduled
        if (!wp_next_scheduled(BASE . 'referral_daily_report')) {
            wp_schedule_event(strtotime('tomorrow 9:00'), 'daily', BASE . 'referral_daily_report');
        }
 
        if (!wp_next_scheduled(BASE . 'referral_weekly_report')) {
            wp_schedule_event(strtotime('next Monday 9:00'), 'weekly', BASE . 'referral_weekly_report');
        }
    }
 
    /**
     * Generate or get existing referral code for a user
     *
     * @param int $user_id
     * @param string|null $custom_code Optional custom code
     * @return string|WP_Error
     */
    public function getUserReferralCode(int $user_id, ?string $custom_code = null)
    {
        $user = get_user_by('ID', $user_id);
        if (!$user) {
            return new WP_Error('invalid_user', 'User not found');
        }
 
        // Check if user already has a code
        $existing_code = get_user_meta($user_id, BASE . 'referral_code', true);
 
        if ($existing_code && !$custom_code) {
            return $existing_code;
        }
 
        // Generate new code if custom provided or none exists
        $code = $custom_code ?: $this->generateReferralCode($user);
 
        // Validate uniqueness
        if ($this->isCodeTaken($code, $user_id)) {
            return new WP_Error('code_taken', 'This referral code is already in use');
        }
 
        // Save the code
        update_user_meta($user_id, BASE . 'referral_code', $code);
 
        return $code;
    }
 
    /**
     * Generate a referral code based on user info
     *
     * @param WP_User $user
     * @return string
     */
    protected function generateReferralCode(WP_User $user): string
    {
        // Create code from first and last name
        $first = sanitize_title($user->first_name ?: 'user');
        $last = sanitize_title($user->last_name ?: wp_generate_password(4, false));
 
        $base_code = strtoupper(substr($first, 0, 4) . substr($last, 0, 4));
 
        // Ensure uniqueness
        $code = $base_code;
        $suffix = 1;
 
        while ($this->isCodeTaken($code)) {
            $code = $base_code . $suffix;
            $suffix++;
        }
 
        return $code;
    }
 
    /**
     * Check if a referral code is already taken
     *
     * @param string $code
     * @param int|null $exclude_user_id
     * @return bool
     */
    protected function isCodeTaken(string $code, ?int $exclude_user_id = null): bool
    {
        $args = [
            'meta_key' => BASE . 'referral_code',
            'meta_value' => $code,
            'fields' => 'ID',
            'number' => 1
        ];
 
        if ($exclude_user_id) {
            $args['exclude'] = [$exclude_user_id];
        }
 
        $users = get_users($args);
        return !empty($users);
    }
 
    /**
     * Track a new referral when user registers
     *
     * @param int $user_id
     */
    public function processReferral(int $user_id): void
    {
        // Check if user was created via referral magic link
        $referral_code = get_user_meta($user_id, BASE . 'pending_referral_code', true);
 
        if (!$referral_code) {
            return;
        }
 
        // Find the referrer
        $referrer = $this->getUserByReferralCode($referral_code);
 
        if (!$referrer) {
            delete_user_meta($user_id, BASE . 'pending_referral_code');
            return;
        }
 
        // Check for duplicates
        $existing = $this->getReferralByReferee($user_id);
        if ($existing) {
            delete_user_meta($user_id, BASE . 'pending_referral_code');
            return;
        }
 
        // Create referral record
        $result = $this->createReferral($referrer->ID, $user_id, $referral_code);
 
        if ($result) {
            // Clean up temp meta
            delete_user_meta($user_id, BASE . 'pending_referral_code');
 
            // Fire action for tracking
            do_action('jvb_referral_processed', $user_id, $referrer->ID, $referral_code);
        }
    }
 
    /**
     * Create a referral record in the database
     *
     * @param int $referrer_id
     * @param int $referee_id
     * @param string $code
     * @return int|false
     */
    protected function createReferral(int $referrer_id, int $referee_id, string $code)
    {
        $user = get_user_by('ID', $referee_id);
 
        return $this->wpdb->insert(
            $this->referrals_table,
            [
                'referrer_id' => $referrer_id,
                'referee_id' => $referee_id,
                'referee_name' => $user->display_name,
                'referee_email' => $user->user_email,
                'referee_phone' => get_user_meta($referee_id, BASE . 'phone', true) ?: '',
                'referral_code' => $code,
                'status' => 'pending',
                'referred_at' => current_time('mysql')
            ],
            ['%d', '%d', '%s', '%s', '%s', '%s', '%s', '%s']
        );
    }
 
    /**
     * Get user by referral code
     *
     * @param string $code
     * @return WP_User|null
     */
    public function getUserByReferralCode(string $code): ?WP_User
    {
        $users = get_users([
            'meta_key' => BASE . 'referral_code',
            'meta_value' => $code,
            'number' => 1
        ]);
 
        return $users[0] ?? null;
    }
 
    /**
     * Get referral record by referee ID
     *
     * @param int $referee_id
     * @return object|null
     */
    public function getReferralByReferee(int $referee_id): ?object
    {
        $result = $this->wpdb->get_row($this->wpdb->prepare(
            "SELECT * FROM {$this->referrals_table} WHERE referee_id = %d",
            $referee_id
        ));
 
        return $result ?: null;
    }
 
    /**
     * Mark a referral as treated/rewarded
     *
     * @param int $referral_id
     * @param bool $treated
     * @return bool
     */
    public function markAsTreated(int $referral_id, bool $treated = true): bool
    {
        $status = $treated ? 'treated' : 'pending';
 
        $result = $this->wpdb->update(
            $this->referrals_table,
            [
                'status' => $status,
                'treated_at' => $treated ? current_time('mysql') : null
            ],
            ['id' => $referral_id],
            ['%s', '%s'],
            ['%d']
        );
 
        if ($result && $treated) {
            // Create reward records when marking as treated
            $this->createRewardRecords($referral_id);
        }
 
        return $result !== false;
    }
 
    /**
     * Create reward records for both referrer and referee
     *
     * @param int $referral_id
     */
    protected function createRewardRecords(int $referral_id): void
    {
        $referral = $this->wpdb->get_row($this->wpdb->prepare(
            "SELECT * FROM {$this->referrals_table} WHERE id = %d",
            $referral_id
        ));
 
        if (!$referral) {
            return;
        }
 
        $settings = $this->getRewardSettings();
 
        // Create referrer reward
        $this->wpdb->insert(
            $this->rewards_table,
            [
                'referral_id' => $referral_id,
                'user_id' => $referral->referrer_id,
                'reward_type' => 'referrer',
                'amount' => $settings['referrer_reward_amount'],
                'status' => 'available',
                'created_at' => current_time('mysql')
            ],
            ['%d', '%d', '%s', '%f', '%s', '%s']
        );
 
        // Create referee reward
        $referee_amount = $settings['referee_reward_type'] === 'percentage'
            ? $settings['referee_reward_amount']  // Store as percentage
            : $settings['referee_reward_amount'];  // Store as fixed amount
 
        $this->wpdb->insert(
            $this->rewards_table,
            [
                'referral_id' => $referral_id,
                'user_id' => $referral->referee_id,
                'reward_type' => 'referee',
                'amount' => $referee_amount,
                'reward_calculation' => $settings['referee_reward_type'],
                'status' => 'available',
                'created_at' => current_time('mysql')
            ],
            ['%d', '%d', '%s', '%f', '%s', '%s', '%s']
        );
    }
 
    /**
     * Get referrals for a user
     *
     * @param int $user_id
     * @param array $args
     * @return array
     */
    public function getUserReferrals(int $user_id, array $args = []): array
    {
        $defaults = [
            'status' => 'all',
            'limit' => 100,
            'offset' => 0,
            'orderby' => 'referred_at',
            'order' => 'DESC'
        ];
 
        $args = wp_parse_args($args, $defaults);
 
        $where = $this->wpdb->prepare("WHERE referrer_id = %d", $user_id);
 
        if ($args['status'] !== 'all') {
            $where .= $this->wpdb->prepare(" AND status = %s", $args['status']);
        }
 
        $query = "SELECT * FROM {$this->referrals_table}
                  {$where}
                  ORDER BY {$args['orderby']} {$args['order']}
                  LIMIT {$args['limit']} OFFSET {$args['offset']}";
 
        return $this->wpdb->get_results($query);
    }
 
    /**
     * Get referral statistics for a user
     *
     * @param int $user_id
     * @return array
     */
    public function getUserStats(int $user_id): array
    {
        $cache_key = 'stats_' . $user_id;
        $cached = $this->cache->get($cache_key);
 
        if ($cached !== false) {
            return $cached;
        }
 
        $stats = $this->wpdb->get_row($this->wpdb->prepare(
            "SELECT
                COUNT(*) as total_referrals,
                SUM(CASE WHEN status = 'treated' THEN 1 ELSE 0 END) as treated_count,
                SUM(CASE WHEN status = 'pending' THEN 1 ELSE 0 END) as pending_count
            FROM {$this->referrals_table}
            WHERE referrer_id = %d",
            $user_id
        ), ARRAY_A);
 
        // Get total rewards
        $rewards = $this->wpdb->get_row($this->wpdb->prepare(
            "SELECT
                SUM(CASE WHEN status = 'available' THEN amount ELSE 0 END) as available_rewards,
                SUM(CASE WHEN status = 'redeemed' THEN amount ELSE 0 END) as redeemed_rewards
            FROM {$this->rewards_table}
            WHERE user_id = %d AND reward_type = 'referrer'",
            $user_id
        ), ARRAY_A);
 
        $stats = array_merge($stats, $rewards);
 
        $this->cache->set($cache_key, $stats, HOUR_IN_SECONDS);
 
        return $stats;
    }
 
    /**
     * Get top referrers for a time period
     *
     * @param int $limit
     * @param string $period 'day'|'week'|'month'|'all'
     * @return array
     */
    public function getTopReferrers(int $limit = 10, string $period = 'all'): array
    {
        $where = '';
 
        if ($period !== 'all') {
            $date_where = match($period) {
                'day' => "referred_at >= DATE_SUB(NOW(), INTERVAL 1 DAY)",
                'week' => "referred_at >= DATE_SUB(NOW(), INTERVAL 1 WEEK)",
                'month' => "referred_at >= DATE_SUB(NOW(), INTERVAL 1 MONTH)",
                default => "1=1"
            };
 
            $where = "WHERE {$date_where}";
        }
 
        $query = "SELECT
                    referrer_id,
                    COUNT(*) as referral_count,
                    SUM(CASE WHEN status = 'treated' THEN 1 ELSE 0 END) as treated_count
                  FROM {$this->referrals_table}
                  {$where}
                  GROUP BY referrer_id
                  ORDER BY referral_count DESC
                  LIMIT {$limit}";
 
        $results = $this->wpdb->get_results($query);
 
        // Enrich with user data
        foreach ($results as &$result) {
            $user = get_user_by('ID', $result->referrer_id);
            $result->user_name = $user ? $user->display_name : 'Unknown';
            $result->user_email = $user ? $user->user_email : '';
        }
 
        return $results;
    }
 
    /**
     * Send daily report if there are new referrals
     */
    public function sendDailyReport(): void
    {
        $yesterday = date('Y-m-d', strtotime('-1 day'));
 
        // Get new referrals from yesterday
        $new_referrals = $this->wpdb->get_results($this->wpdb->prepare(
            "SELECT
            r.*,
            u.display_name as referrer_name
        FROM {$this->referrals_table} r
        JOIN {$this->wpdb->users} u ON r.referrer_id = u.ID
        WHERE DATE(r.referred_at) = %s
        ORDER BY r.referred_at DESC",
            $yesterday
        ));
 
        // Only send if there's at least 1 new referral
        if (empty($new_referrals)) {
            return;
        }
 
        // Build email content
        $content = '<h2>Daily Referral Report</h2>';
        $content .= '<p><strong>' . count($new_referrals) . '</strong> new referral' .
            (count($new_referrals) !== 1 ? 's' : '') . ' yesterday (' . $yesterday . ')</p>';
 
        $content .= '<table style="width:100%; border-collapse: collapse; margin: 20px 0;">';
        $content .= '<thead><tr style="background: #f5f5f5;">';
        $content .= '<th style="padding: 10px; text-align: left; border: 1px solid #ddd;">Referee</th>';
        $content .= '<th style="padding: 10px; text-align: left; border: 1px solid #ddd;">Email</th>';
        $content .= '<th style="padding: 10px; text-align: left; border: 1px solid #ddd;">Referrer</th>';
        $content .= '<th style="padding: 10px; text-align: left; border: 1px solid #ddd;">Code</th>';
        $content .= '</tr></thead><tbody>';
 
        foreach ($new_referrals as $ref) {
            $content .= '<tr>';
            $content .= sprintf('<td style="padding: 10px; border: 1px solid #ddd;">%s</td>',
                esc_html($ref->referee_name));
            $content .= sprintf('<td style="padding: 10px; border: 1px solid #ddd;">%s</td>',
                esc_html($ref->referee_email));
            $content .= sprintf('<td style="padding: 10px; border: 1px solid #ddd;">%s</td>',
                esc_html($ref->referrer_name));
            $content .= sprintf('<td style="padding: 10px; border: 1px solid #ddd;">%s</td>',
                esc_html($ref->referral_code));
            $content .= '</tr>';
        }
 
        $content .= '</tbody></table>';
 
        // Get admin email
        $to = get_option('admin_email');
        $subject = sprintf('[%s] %d New Referral%s',
            get_bloginfo('name'),
            count($new_referrals),
            count($new_referrals) !== 1 ? 's' : '');
 
 
        jvbMail($to, $subject, $content);
    }
 
    /**
     * Send weekly report with top referrers
     */
    public function sendWeeklyReport(): void
    {
        $top_referrers = $this->getTopReferrers(10, 'week');
        $total_referrals = $this->wpdb->get_var(
            "SELECT COUNT(*) FROM {$this->referrals_table}
             WHERE referred_at >= DATE_SUB(NOW(), INTERVAL 1 WEEK)"
        );
 
        if ($total_referrals == 0) {
            return;
        }
 
        $to = get_option('admin_email');
        $subject = '[' . get_bloginfo('name') . '] Weekly Referral Summary - ' . date('F j, Y');
 
        $message = $this->generateWeeklyReportEmail($top_referrers, $total_referrals);
 
        wp_mail($to, $subject, $message, ['Content-Type: text/html; charset=UTF-8']);
    }
 
    /**
     * Generate CSV content from referrals
     *
     * @param array $referrals
     * @return string
     */
    protected function generateCSV(array $referrals): string
    {
        $csv = "Referred By,Referee Name,Referee Email,Referee Phone,Referral Code,Status,Referred At,Treated At\n";
 
        foreach ($referrals as $referral) {
            $csv .= sprintf(
                '"%s","%s","%s","%s","%s","%s","%s","%s"' . "\n",
                $referral->referrer_name ?? 'Unknown',
                $referral->referee_name,
                $referral->referee_email,
                $referral->referee_phone,
                $referral->referral_code,
                $referral->status,
                $referral->referred_at,
                $referral->treated_at ?? 'Not yet'
            );
        }
 
        return $csv;
    }
 
    /**
     * Generate HTML email for daily report
     *
     * @param array $referrals
     * @param string $period
     * @return string
     */
    protected function generateReportEmail(array $referrals, string $period): string
    {
        $count = count($referrals);
 
        $content = sprintf('<p>You have <strong>%d new referral%s</strong> today.</p>',
            $count,
            $count !== 1 ? 's' : ''
        );
 
        $content .= '<table style="width:100%; border-collapse: collapse; margin: 20px 0;">';
        $content .= '<thead><tr style="background: #f5f5f5; text-align: left;">';
        $content .= '<th style="padding: 10px; border: 1px solid #ddd;">Referred By</th>';
        $content .= '<th style="padding: 10px; border: 1px solid #ddd;">New User</th>';
        $content .= '<th style="padding: 10px; border: 1px solid #ddd;">Email</th>';
        $content .= '<th style="padding: 10px; border: 1px solid #ddd;">Status</th>';
        $content .= '<th style="padding: 10px; border: 1px solid #ddd;">Time</th>';
        $content .= '</tr></thead><tbody>';
 
        foreach ($referrals as $referral) {
            $content .= '<tr>';
            $content .= sprintf('<td style="padding: 10px; border: 1px solid #ddd;">%s</td>',
                esc_html($referral->referrer_name ?? 'Unknown'));
            $content .= sprintf('<td style="padding: 10px; border: 1px solid #ddd;">%s</td>',
                esc_html($referral->referee_name));
            $content .= sprintf('<td style="padding: 10px; border: 1px solid #ddd;">%s</td>',
                esc_html($referral->referee_email));
            $content .= sprintf('<td style="padding: 10px; border: 1px solid #ddd;">%s</td>',
                esc_html(ucfirst($referral->status)));
            $content .= sprintf('<td style="padding: 10px; border: 1px solid #ddd;">%s</td>',
                esc_html(date('g:i A', strtotime($referral->referred_at))));
            $content .= '</tr>';
        }
 
        $content .= '</tbody></table>';
        $content .= '<p><small>See attached CSV for full details.</small></p>';
 
        return jvbGetEmailTemplate($content, 'Daily Referral Report');
    }
 
    /**
     * Generate HTML email for weekly report
     *
     * @param array $top_referrers
     * @param int $total_referrals
     * @return string
     */
    protected function generateWeeklyReportEmail(array $top_referrers, int $total_referrals): string
    {
        $content = sprintf(
            '<p>This week you had <strong>%d total referral%s</strong>.</p>',
            $total_referrals,
            $total_referrals !== 1 ? 's' : ''
        );
 
        $content .= '<h3>Top 10 Referrers This Week</h3>';
        $content .= '<table style="width:100%; border-collapse: collapse; margin: 20px 0;">';
        $content .= '<thead><tr style="background: #f5f5f5; text-align: left;">';
        $content .= '<th style="padding: 10px; border: 1px solid #ddd;">Rank</th>';
        $content .= '<th style="padding: 10px; border: 1px solid #ddd;">User</th>';
        $content .= '<th style="padding: 10px; border: 1px solid #ddd;">Total Referrals</th>';
        $content .= '<th style="padding: 10px; border: 1px solid #ddd;">Treated</th>';
        $content .= '</tr></thead><tbody>';
 
        $rank = 1;
        foreach ($top_referrers as $referrer) {
            $content .= '<tr>';
            $content .= sprintf('<td style="padding: 10px; border: 1px solid #ddd;">%d</td>', $rank++);
            $content .= sprintf('<td style="padding: 10px; border: 1px solid #ddd;">%s</td>',
                esc_html($referrer->user_name));
            $content .= sprintf('<td style="padding: 10px; border: 1px solid #ddd;">%d</td>',
                $referrer->referral_count);
            $content .= sprintf('<td style="padding: 10px; border: 1px solid #ddd;">%d</td>',
                $referrer->treated_count);
            $content .= '</tr>';
        }
 
        $content .= '</tbody></table>';
 
        return jvbGetEmailTemplate($content, 'Weekly Referral Summary');
    }
 
    /**
     * Get reward settings
     *
     * @return array
     */
    public function getRewardSettings(): array
    {
        $saved = get_option(BASE . 'referral_settings', []);
        return wp_parse_args($saved, $this->default_settings);
    }
 
    /**
     * Display referral info in user profile
     *
     * @param WP_User $user
     */
    public function displayUserReferralInfo(WP_User $user): void
    {
        if (!current_user_can('edit_user', $user->ID)) {
            return;
        }
 
        $referral_code = get_user_meta($user->ID, BASE . 'referral_code', true);
        $stats = $this->getUserStats($user->ID);
        $referrals = $this->getUserReferrals($user->ID, ['limit' => 10]);
 
        ?>
        <h2>Referral Information</h2>
        <table class="form-table">
            <tr>
                <th><label for="referral_code">Referral Code</label></th>
                <td>
                    <input type="text"
                           name="referral_code"
                           id="referral_code"
                           value="<?php echo esc_attr($referral_code); ?>"
                           class="regular-text" />
                    <p class="description">
                        Users can sign up with this code.
                        <?php if ($referral_code): ?>
                            Share link: <?php echo home_url('/?ref=' . $referral_code); ?>
                        <?php endif; ?>
                    </p>
                </td>
            </tr>
        </table>
 
        <h3>Referral Statistics</h3>
        <table class="form-table">
            <tr>
                <th>Total Referrals:</th>
                <td><?php echo $stats['total_referrals'] ?? 0; ?></td>
            </tr>
            <tr>
                <th>Treated:</th>
                <td><?php echo $stats['treated_count'] ?? 0; ?></td>
            </tr>
            <tr>
                <th>Pending:</th>
                <td><?php echo $stats['pending_count'] ?? 0; ?></td>
            </tr>
            <tr>
                <th>Available Rewards:</th>
                <td>$<?php echo number_format($stats['available_rewards'] ?? 0, 2); ?></td>
            </tr>
            <tr>
                <th>Redeemed Rewards:</th>
                <td>$<?php echo number_format($stats['redeemed_rewards'] ?? 0, 2); ?></td>
            </tr>
        </table>
 
        <?php if (!empty($referrals)): ?>
        <h3>Recent Referrals</h3>
        <table class="widefat">
            <thead>
            <tr>
                <th>Name</th>
                <th>Email</th>
                <th>Status</th>
                <th>Referred At</th>
                <th>Actions</th>
            </tr>
            </thead>
            <tbody>
            <?php foreach ($referrals as $referral): ?>
                <tr>
                    <td><?php echo esc_html($referral->referee_name); ?></td>
                    <td><?php echo esc_html($referral->referee_email); ?></td>
                    <td><?php echo esc_html(ucfirst($referral->status)); ?></td>
                    <td><?php echo esc_html($referral->referred_at); ?></td>
                    <td>
                        <?php if ($referral->status === 'pending'): ?>
                            <button type="button"
                                    onclick="markReferralTreated(<?php echo $referral->id; ?>)">
                                Mark as Treated
                            </button>
                        <?php endif; ?>
                    </td>
                </tr>
            <?php endforeach; ?>
            </tbody>
        </table>
    <?php endif; ?>
 
        <script>
            function markReferralTreated(referralId) {
                if (!confirm('Mark this referral as treated? This will create reward records.')) {
                    return;
                }
 
                fetch('<?php echo rest_url(BASE . '/v1/referrals/' ); ?>' + referralId + '/treat', {
                    method: 'POST',
                    headers: {
                        'X-WP-Nonce': '<?php echo wp_create_nonce('wp_rest'); ?>'
                    }
                })
                    .then(response => response.json())
                    .then(data => {
                        if (data.success) {
                            alert('Referral marked as treated!');
                            location.reload();
                        } else {
                            alert('Error: ' + (data.message || 'Unknown error'));
                        }
                    });
            }
        </script>
        <?php
    }
 
    /**
     * Save custom referral code
     *
     * @param int $user_id
     */
    public function saveUserReferralCode(int $user_id): void
    {
        if (!current_user_can('edit_user', $user_id)) {
            return;
        }
 
        if (!isset($_POST['referral_code'])) {
            return;
        }
 
        $code = sanitize_text_field($_POST['referral_code']);
 
        if (empty($code)) {
            delete_user_meta($user_id, BASE . 'referral_code');
            return;
        }
 
        // Validate code format (alphanumeric only)
        if (!preg_match('/^[A-Z0-9]+$/i', $code)) {
            add_action('user_profile_update_errors', function($errors) {
                $errors->add('invalid_referral_code',
                    'Referral code can only contain letters and numbers.');
            });
            return;
        }
 
        // Check if code is unique
        if ($this->isCodeTaken($code, $user_id)) {
            add_action('user_profile_update_errors', function($errors) {
                $errors->add('referral_code_taken',
                    'This referral code is already in use.');
            });
            return;
        }
 
        update_user_meta($user_id, BASE . 'referral_code', strtoupper($code));
    }
 
 
    /**
     * Display user's referral code and share options
     * Use in templates or dashboard with: echo jvbReferralShareWidget();
     *
     * @return string HTML output
     */
    public function outputShareWidget(array $actions):array
    {
 
        $user_id = get_current_user_id();
        $content = '<aside class="jvb-referral right">';
        if (!$user_id) {
            $content .= $this->getUnloggedInReferral();
        } else {
            $content .= $this->getLoggedInReferral($user_id);
        }
        $content .= '</aside>';
 
        $actions[] =[
            'button' => '<button type="button" class="attn toggle-referral row" title="Your Referrals" data-action="toggle-referral" aria-label="Open Referral Sidebar" aria-controls="referral" aria-expanded="false">
                    '.jvbIcon('hand-heart').'<span class="screen-reader-text"></span>
                </button>',
            'content'   => $content
        ];
 
        return $actions;
    }
 
    function getUnloggedInReferral(): string
    {
        ob_start();
        JVB()->connect('cloudflare')->renderTurnstile();
        $turnstile = ob_get_clean();
        $meta = new MetaForm();
        $codeForm = '<form id="referral-code-form">
                    '.jvbFormStatus().$meta->return('referral_name', null, [
                        'required'  => true,
                        'type'      => 'text',
                        'label'     => 'Your Name',
                        'placeholder'=> 'Mister Meeseeks',
                        'autocomplete'=>'name'
                    ]).
                    $meta->return('referral_email', null, [
                        'required'  => true,
                        'type'      => 'email',
                        'label'     => 'Your Email',
                        'placeholder'=> 'look@me.com',
                        'autocomplete'=> 'email'
                    ]).
                    $meta->return('referral_code', null, [
                        'required'  => true,
                        'type'      => 'text',
                        'label'     => 'Referral Code',
                        'pattern'   => '[A-Za-z0-9]+',
                        'maxLength' => 20,
                        'autocomplete'=>'off'
                    ]).'
                    <button type="submit">
                        Get Started
                    </button>
 
                    <p class="helper-text">
                        We\'ll send you a link to complete your registration.
                    </p>
                    '.$turnstile.'
                </form><div class="success-content" hidden>
                    <h3>Check Your Email!</h3>
                    <p>We\'ve sent you a magic link to complete your registration. Click the link to activate your account and claim your reward!</p>
                    <p class="hint">Can\'t find it? Check your spam folder.</p>
                </div>';
 
        $loginForm = '<form id ="login-form">
        '.jvbFormStatus().$meta->return('login_email', null, [
                'required'  => true,
                'type'      => 'email',
                'label'     => 'Your Email',
                'autocomplete'=>'email'
            ]).'
        '.$turnstile.'
        <button type="submit">Login With Magic Link</button>
</form>
    <div class="success-content" hidden>
        <h3>Check Your Email!</h3>
        <p>We\'ve sent you a magic link to log in - no password required! Click the link in your email to log in.</p>
        <p class="hint">Can\'t find it? Check your spam folder.</p>
    </div>';
 
        $tabs = [
            'enterCode' => [
                'title' => 'Have a Code?',
                'description'   => [
                    'Enter the code given to you to get 20% off your first treatment!'
                ],
                'content'   => $codeForm
            ],
            'login' => [
                'title'     => 'Login',
                'description'   => [
                    'Login to see your rewards'
                ],
                'content'   => $loginForm
            ]
        ];
 
        return jvbRenderTabs($tabs, true);
    }
 
    protected function getReferralSuccessMessage(string $code): string
    {
        $referrer = $this->getUserByReferralCode($code);
 
        if (!$referrer) {
            return '';
        }
 
        $settings = $this->getRewardSettings();
        $reward_amount = $settings['referee_reward_amount'] ?? 20;
        $reward_type = $settings['referee_reward_type'] ?? 'percentage';
 
        $reward_text = $reward_type === 'percentage'
            ? $reward_amount . '% off'
            : '$' . number_format($reward_amount, 2) . ' off';
 
        $booking_url = apply_filters('jvb_referral_booking_url', home_url('/contact'));
 
        ob_start();
        ?>
        <div class="success-icon">
            ✓
        </div>
 
        <div class="success-content">
            <h3>Success! Your Reward is Ready!</h3>
 
            <div class="reward-highlight">
                <p style="margin: 0 0 8px 0;">You'll receive:</p>
                <p style="margin: 0;"><strong><?php echo esc_html($reward_text); ?> your first treatment!</strong></p>
            </div>
 
            <p>Your referral code <strong><?php echo esc_html($code); ?></strong> has been applied. Book your free consultation now to claim your reward!</p>
 
            <a href="<?php echo esc_url($booking_url); ?>" class="cta-button">
                Book Your Free Consultation
            </a>
 
            <div class="referred-by">
                Referred by <strong><?php echo esc_html($referrer->display_name); ?></strong>
            </div>
        </div>
        <?php
        return ob_get_clean();
    }
 
    function getLoggedInReferral(int $user_id):string
    {
        // Logged-in user widget
        $referral_code = get_user_meta($user_id, BASE . 'referral_code', true);
 
        // Generate code if user doesn't have one
        if (empty($referral_code)) {
            $referral_code = $this->getUserReferralCode($user_id);
            if (is_wp_error($referral_code)) {
                return '';
            }
        }
 
        $share_url = home_url('/?ref=' . $referral_code);
 
        ob_start();
        ?>
            <header>
                <h3>Share the ♡</h3>
                <p>Invite your friends.</p>
                <p>Earn rewards when they book!</p>
            </header>
 
        <div class="row even share-buttons">
            <a href="mailto:?subject=<?php echo urlencode('Check out ' . get_bloginfo('name')); ?>&body=<?php echo urlencode('I thought you might be interested: ' . $share_url); ?>"
               class="share-btn email-share">
                <?php echo jvbIcon('envelope', ['size' => 20]); ?>
                Email
            </a>
            <a href="https://www.facebook.com/sharer/sharer.php?u=<?php echo urlencode($share_url); ?>"
               target="_blank"
               rel="noopener noreferrer"
               class="share-btn facebook-share">
                <?php echo jvbIcon('facebook', ['size' => 20]); ?>
                Facebook
            </a>
            <a href="https://twitter.com/intent/tweet?url=<?php echo urlencode($share_url); ?>&text=<?php echo urlencode('Check this out!'); ?>"
               target="_blank"
               rel="noopener noreferrer"
               class="share-btn twitter-share">
                <?php echo jvbIcon('twitter', ['size' => 20]); ?>
                Twitter
            </a>
        </div>
 
            <h4>Your Referral Link</h4>
            <div class="row btw">
                <code id="your-referral-link"><?= esc_url($share_url)?></code>
                <button type="button" class="copy" data-target="your-referral-link">
                    Copy Link
                </button>
            </div>
 
 
            <h4>Your Code</h4>
            <div class="row btw">
                <code id="your-referral-code"><?=esc_html($referral_code)?></code>
                <button type="button" class="copy" data-target="your-referral-code">
                    Copy Code
                </button>
            </div>
 
            <div class="row btw referral-stats">
                <div class="stat-item">
                    <span class="stat-value" data-stat="total">-</span>
                    <span class="stat-label">Total Referrals</span>
                </div>
                <div class="stat-item">
                    <span class="stat-value" data-stat="treated">-</span>
                    <span class="stat-label">Successful</span>
                </div>
                <div class="stat-item">
                    <span class="stat-value" data-stat="pending">-</span>
                    <span class="stat-label">Pending</span>
                </div>
                <div class="stat-item">
                    <span class="stat-value" data-stat="rewards">$0.00</span>
                    <span class="stat-label">Available Rewards</span>
                </div>
            </div>
 
        <?php
        return ob_get_clean();
    }
 
    function addReferralToWelcomeEmail(string $content, WP_User $user): string
    {
        $referral = $this->getReferralByReferee($user->ID);
 
        if (!$referral) {
            return $content;
        }
 
        $settings = get_option(BASE . 'referral_settings', []);
        $reward_amount = $settings['referee_reward_amount'] ?? 20;
        $reward_type = $settings['referee_reward_type'] ?? 'percentage';
 
        $reward_text = $reward_type === 'percentage'
            ? $reward_amount . '% off'
            : '$' . number_format($reward_amount, 2) . ' off';
 
        $bonus_content = '<div style="background: #e7f5ff; padding: 20px; border-radius: 8px; margin: 20px 0;">';
        $bonus_content .= '<h3 style="margin-top: 0; color: #2271b1;">🎉 Welcome Bonus!</h3>';
        $bonus_content .= '<p>Since you were referred by a friend, you\'ve earned <strong>' . $reward_text . '</strong> your first booking!</p>';
        $bonus_content .= '<p>Your reward will be automatically applied when you book.</p>';
        $bonus_content .= '</div>';
 
        // Insert bonus content after the first paragraph
        $parts = explode('</p>', $content, 2);
        if (count($parts) === 2) {
            return $parts[0] . '</p>' . $bonus_content . $parts[1];
        }
 
        return $content . $bonus_content;
    }
 
    /**
     * Send referral invitation via email with magic link
     *
     * @param int $user_id Referrer's user ID
     * @param string $invitee_email Email of person to invite
     * @param string $invitee_name Name of person to invite
     * @return array|WP_Error Result with success/error
     */
    public function sendReferralInvitation(int $user_id, string $invitee_email, string $invitee_name):array|WP_Error
    {
        // Verify user exists
        if (!$this->checkUser($user_id)) {
            return new WP_Error('invalid_user', 'Invalid user ID');
        }
 
        // Check email rate limit (15/hour)
        $rate_check = $this->checkEmailRateLimit($user_id);
        if ($rate_check !== true) {
            return new WP_Error('rate_limit', 'You can only send 15 invitations per hour. Please try again later.');
        }
 
        // Validate email
        $invitee_email = sanitize_email($invitee_email);
        if (!is_email($invitee_email)) {
            return new WP_Error('invalid_email', 'Invalid email address');
        }
 
        // Check if this email has already been invited or registered
        if ($this->isEmailInvited($invitee_email)) {
            return new WP_Error('already_invited', 'This person has already been invited');
        }
 
        if (email_exists($invitee_email)) {
            return new WP_Error('user_exists', 'This person already has an account');
        }
 
        // Get referrer info
        $referrer = get_user_by('ID', $user_id);
        $referral_code = $this->getUserReferralCode($user_id);
 
        if (is_wp_error($referral_code)) {
            return $referral_code;
        }
 
        // Get reward text for email
        $settings = $this->getRewardSettings();
        $reward_text = $settings['referee_reward_type'] === 'percentage'
            ? "Get {$settings['referee_reward_amount']}% off your first treatment!"
            : "Get \${$settings['referee_reward_amount']} off your first treatment!";
 
        // Record the invitation attempt (for tracking)
        $this->recordInvitationAttempt($user_id, $invitee_email, $invitee_name);
 
        // Send magic link via MagicLinkManager
        $result = $this->magic_link->sendMagicLink(
            $invitee_email,
            MagicLinkManager::TYPE_REFERRAL,
            [
                'name' => sanitize_text_field($invitee_name),
                'referral_code' => $referral_code,
                'referrer_id' => $user_id,
                'referrer_name' => $referrer->display_name,
                'reward_text' => $reward_text
            ]
        );
 
        if (is_wp_error($result)) {
            return $result;
        }
 
        return [
            'success' => true,
            'message' => 'Invitation sent successfully',
            'email' => $invitee_email,
            'name' => $invitee_name
        ];
    }
 
    /**
     * Send multiple referral invitations
     *
     * @param int $user_id Referrer's user ID
     * @param array $invitations Array of ['email' => '', 'name' => '']
     * @return array Results with success/failed arrays
     */
    public function sendBatchReferralInvitations(int $user_id, array $invitations): array
    {
        $results = [
            'success' => [],
            'failed' => []
        ];
 
        foreach ($invitations as $invite) {
            $email = $invite['email'] ?? '';
            $name = $invite['name'] ?? '';
 
            if (empty($email) || empty($name)) {
                $results['failed'][] = [
                    'email' => $email,
                    'name' => $name,
                    'reason' => 'Missing email or name'
                ];
                continue;
            }
 
            $result = $this->sendReferralInvitation($user_id, $email, $name);
 
            if (is_wp_error($result)) {
                $results['failed'][] = [
                    'email' => $email,
                    'name' => $name,
                    'reason' => $result->get_error_message()
                ];
            } else {
                $results['success'][] = [
                    'email' => $email,
                    'name' => $name
                ];
            }
 
            // Small delay between sends to be respectful
            usleep(100000); // 0.1 seconds
        }
 
        return [
            'success' => !empty($results['success']),
            'results' => $results,
            'summary' => sprintf(
                'Sent %d invitations, %d failed',
                count($results['success']),
                count($results['failed'])
            )
        ];
    }
 
    /**
     * Check email invitation rate limit (15 per hour)
     *
     * @param int $user_id
     * @return true|string True if allowed, error message if limited
     */
    protected function checkEmailRateLimit(int $user_id):bool|string
    {
        $hourly_key = 'referral_invites_hour_' . $user_id;
        $count = (int) get_transient($hourly_key);
 
        if ($count >= 15) {
            return 'hourly_limit_reached';
        }
 
        set_transient($hourly_key, $count + 1, HOUR_IN_SECONDS);
        return true;
    }
 
    /**
     * Check if an email has already been invited
     *
     * @param string $email
     * @return bool
     */
    protected function isEmailInvited(string $email): bool
    {
        // Check invitation tracking table
        $invitation_key = 'referral_invite_' . md5($email);
        $invited = get_transient($invitation_key);
 
        if ($invited) {
            return true;
        }
 
        // Check if there's a pending referral for this email
        $existing = $this->wpdb->get_var($this->wpdb->prepare(
            "SELECT id FROM {$this->referrals_table} WHERE referee_email = %s",
            $email
        ));
 
        return !empty($existing);
    }
 
    /**
     * Record invitation attempt (for tracking and preventing duplicates)
     *
     * @param int $user_id
     * @param string $email
     * @param string $name
     */
    protected function recordInvitationAttempt(int $user_id, string $email, string $name): void
    {
        // Store for 30 days (same as magic link invitation validity)
        $invitation_key = 'referral_invite_' . md5($email);
        $data = [
            'inviter_id' => $user_id,
            'email' => $email,
            'name' => $name,
            'sent_at' => current_time('mysql')
        ];
 
        set_transient($invitation_key, $data, 30 * DAY_IN_SECONDS);
 
        // Also log in user meta for tracking
        $sent_invites = get_user_meta($user_id, BASE . 'referral_invites_sent', true) ?: [];
        $sent_invites[] = [
            'email' => $email,
            'name' => $name,
            'sent_at' => current_time('mysql')
        ];
        update_user_meta($user_id, BASE . 'referral_invites_sent', $sent_invites);
    }
 
    /**
     * Get user's invitation stats
     *
     * @param int $user_id
     * @return array
     */
    public function getUserInvitationStats(int $user_id): array
    {
        $sent_invites = get_user_meta($user_id, BASE . 'referral_invites_sent', true) ?: [];
 
        // Count invites sent in last hour
        $one_hour_ago = strtotime('-1 hour');
        $recent_count = 0;
 
        foreach ($sent_invites as $invite) {
            if (strtotime($invite['sent_at']) > $one_hour_ago) {
                $recent_count++;
            }
        }
 
        return [
            'total_sent' => count($sent_invites),
            'sent_last_hour' => $recent_count,
            'remaining_this_hour' => max(0, 15 - $recent_count),
            'can_send_more' => $recent_count < 15
        ];
    }
 
    /**
     * Export referrals for Jane App cross-reference
     *
     * @param string $start_date Y-m-d format
     * @param string $end_date Y-m-d format
     * @return string CSV content
     */
    public function exportReferrals(string $start_date, string $end_date): string
    {
        $referrals = $this->wpdb->get_results($this->wpdb->prepare(
            "SELECT
            r.id,
            r.referee_name,
            r.referee_email,
            r.referee_phone,
            r.referral_code,
            r.referred_at,
            r.status,
            r.treated_at,
            u.display_name as referrer_name,
            u.user_email as referrer_email
        FROM {$this->referrals_table} r
        JOIN {$this->wpdb->users} u ON r.referrer_id = u.ID
        WHERE DATE(r.referred_at) BETWEEN %s AND %s
        ORDER BY r.referred_at DESC",
            $start_date,
            $end_date
        ));
 
        // Build CSV
        $csv_lines = [];
 
        // Headers
        $csv_lines[] = [
            'Referral ID',
            'Referee Name',
            'Referee Email',
            'Referee Phone',
            'Referral Code',
            'Referred Date',
            'Status',
            'Treated Date',
            'Referrer Name',
            'Referrer Email'
        ];
 
        // Data rows
        foreach ($referrals as $ref) {
            $csv_lines[] = [
                $ref->id,
                $ref->referee_name,
                $ref->referee_email,
                $ref->referee_phone ?: 'N/A',
                $ref->referral_code,
                $ref->referred_at,
                ucfirst($ref->status),
                $ref->treated_at ?: 'N/A',
                $ref->referrer_name,
                $ref->referrer_email
            ];
        }
 
        // Convert to CSV string
        $output = fopen('php://temp', 'r+');
        foreach ($csv_lines as $line) {
            fputcsv($output, $line);
        }
        rewind($output);
        $csv_content = stream_get_contents($output);
        fclose($output);
 
        return $csv_content;
    }
 
    /**
     * Add referral settings subpage to admin menu
     *
     * @param array $subpages
     * @return array
     */
    public function addSubpage(array $subpages): array
    {
        $subpages[] = [
            'page_title' => 'Referral Settings',
            'menu_title' => 'Referrals',
            'capability' => 'manage_options',
            'menu_slug'  => 'jvb-referrals',
            'callback'   => [$this, 'renderAdminPage'],
            'icon'       => 'users',
        ];
 
        return $subpages;
    }
 
    /**
     * Register settings
     */
    public function registerSettings(): void
    {
        register_setting(
            BASE . 'referral_settings',
            BASE . 'referral_page_id',
            [
                'type' => 'integer',
                'sanitize_callback' => 'absint',
                'default' => 0
            ]
        );
 
        register_setting(
            BASE . 'referral_settings',
            BASE . 'referral_reward_settings',
            [
                'type' => 'array',
                'sanitize_callback' => [$this, 'sanitizeRewardSettings'],
                'default' => $this->default_settings
            ]
        );
    }
 
    /**
     * Sanitize reward settings
     */
    public function sanitizeRewardSettings(array $settings): array
    {
        return [
            'referrer_reward_applies_to' => in_array($settings['referrer_reward_applies_to'] ?? '', ['per_user', 'flat_total'])
                ? $settings['referrer_reward_applies_to']
                : 'per_user',
            'referrer_reward_amount' => floatval($settings['referrer_reward_amount'] ?? 25.00),
            'referrer_reward_type' => in_array($settings['referrer_reward_type'] ?? '', ['fixed', 'percentage'])
                ? $settings['referrer_reward_type']
                : 'fixed',
            'referee_reward_type' => in_array($settings['referee_reward_type'] ?? '', ['percentage', 'fixed'])
                ? $settings['referee_reward_type']
                : 'percentage',
            'referee_reward_amount' => floatval($settings['referee_reward_amount'] ?? 20),
            'referee_reward_applies_to' => in_array($settings['referee_reward_applies_to'] ?? '', ['first_order', 'all_orders'])
                ? $settings['referee_reward_applies_to']
                : 'first_order',
        ];
    }
 
    /**
     * Render the admin settings page
     */
    public function renderAdminPage(): void
    {
        // Handle form submission
        if (isset($_POST['submit']) && check_admin_referer(BASE . 'referral_settings_nonce')) {
            update_option(BASE . 'referral_page_id', absint($_POST[BASE . 'referral_page_id'] ?? 0));
 
            $reward_settings = [
                'referrer_reward_applies_to' => sanitize_text_field($_POST['referrer_reward_applies_to'] ?? 'per_user'),
                'referrer_reward_amount' => floatval($_POST['referrer_reward_amount'] ?? 25.00),
                'referrer_reward_type' => sanitize_text_field($_POST['referrer_reward_type'] ?? 'fixed'),
                'referee_reward_type' => sanitize_text_field($_POST['referee_reward_type'] ?? 'percentage'),
                'referee_reward_amount' => floatval($_POST['referee_reward_amount'] ?? 20),
                'referee_reward_applies_to' => sanitize_text_field($_POST['referee_reward_applies_to'] ?? 'first_order'),
            ];
 
            update_option(BASE . 'referral_reward_settings', $this->sanitizeRewardSettings($reward_settings));
 
            echo '<div class="notice notice-success is-dismissible"><p>Settings saved successfully.</p></div>';
        }
 
        $referral_page_id = $this->getReferralPageId();
        $settings = $this->getRewardSettings();
 
        echo $this->renderAdminHTML();
    }
 
    protected function renderAdminHTML():string
    {
        ob_start();
        ?>
        <div class="wrap">
            <h1>Referral Settings</h1>
 
            <form method="post" action="">
                <?php wp_nonce_field(BASE . 'referral_settings_nonce'); ?>
 
                <div class="card">
                    <h2>Referral Page</h2>
                    <p>Select the page where users can access their referral dashboard.</p>
 
                    <table class="form-table">
                        <tr>
                            <th scope="row">
                                <label for="<?= BASE ?>referral_page_id">Referral Page</label>
                            </th>
                            <td>
                                <?php
                                wp_dropdown_pages([
                                    'name' => BASE . 'referral_page_id',
                                    'id' => BASE . 'referral_page_id',
                                    'selected' => $referral_page_id,
                                    'show_option_none' => __('— Select —', 'jvbase'),
                                    'option_none_value' => '0'
                                ]);
                                ?>
                                <p class="description">
                                    This page will show "Referral Page" in the admin bar when editing.
                                </p>
                            </td>
                        </tr>
                    </table>
                </div>
 
                <div class="card">
                    <h2>Reward Settings</h2>
 
                    <table class="form-table">
                        <tr>
                            <th colspan="2"><h3>Referrer Rewards</h3></th>
                        </tr>
                        <tr>
                            <th scope="row">
                                <label for="referrer_reward_type">Reward Type</label>
                            </th>
                            <td>
                                <select name="referrer_reward_type" id="referrer_reward_type">
                                    <option value="fixed" <?php selected($settings['referrer_reward_type'], 'fixed'); ?>>Fixed Amount</option>
                                    <option value="percentage" <?php selected($settings['referrer_reward_type'], 'percentage'); ?>>Percentage</option>
                                </select>
                            </td>
                        </tr>
                        <tr>
                            <th scope="row">
                                <label for="referrer_reward_amount">Reward Amount</label>
                            </th>
                            <td>
                                <input type="number"
                                       name="referrer_reward_amount"
                                       id="referrer_reward_amount"
                                       value="<?= esc_attr($settings['referrer_reward_amount']) ?>"
                                       step="0.01"
                                       min="0">
                                <p class="description">Amount in dollars or percentage</p>
                            </td>
                        </tr>
                        <tr>
                            <th scope="row">
                                <label for="referrer_reward_applies_to">Applies To</label>
                            </th>
                            <td>
                                <select name="referrer_reward_applies_to" id="referrer_reward_applies_to">
                                    <option value="per_user" <?php selected($settings['referrer_reward_applies_to'], 'per_user'); ?>>Per User Referred</option>
                                    <option value="flat_total" <?php selected($settings['referrer_reward_applies_to'], 'flat_total'); ?>>Flat Total</option>
                                </select>
                            </td>
                        </tr>
 
                        <tr>
                            <th colspan="2"><h3>Referee (New User) Rewards</h3></th>
                        </tr>
                        <tr>
                            <th scope="row">
                                <label for="referee_reward_type">Reward Type</label>
                            </th>
                            <td>
                                <select name="referee_reward_type" id="referee_reward_type">
                                    <option value="percentage" <?php selected($settings['referee_reward_type'], 'percentage'); ?>>Percentage</option>
                                    <option value="fixed" <?php selected($settings['referee_reward_type'], 'fixed'); ?>>Fixed Amount</option>
                                </select>
                            </td>
                        </tr>
                        <tr>
                            <th scope="row">
                                <label for="referee_reward_amount">Reward Amount</label>
                            </th>
                            <td>
                                <input type="number"
                                       name="referee_reward_amount"
                                       id="referee_reward_amount"
                                       value="<?= esc_attr($settings['referee_reward_amount']) ?>"
                                       step="0.01"
                                       min="0">
                                <p class="description">Amount in dollars or percentage</p>
                            </td>
                        </tr>
                        <tr>
                            <th scope="row">
                                <label for="referee_reward_applies_to">Applies To</label>
                            </th>
                            <td>
                                <select name="referee_reward_applies_to" id="referee_reward_applies_to">
                                    <option value="first_order" <?php selected($settings['referee_reward_applies_to'], 'first_order'); ?>>First Order Only</option>
                                    <option value="all_orders" <?php selected($settings['referee_reward_applies_to'], 'all_orders'); ?>>All Orders</option>
                                </select>
                            </td>
                        </tr>
                    </table>
                </div>
 
                <p class="submit">
                    <button type="submit" name="submit" class="button button-primary">Save Settings</button>
                </p>
            </form>
 
            <?= $this->renderReferralStats(true) ?>
        </div>
        <?php
        return ob_get_clean();
    }
 
    /**
     * Render referral statistics
     */
    protected function renderReferralStats(bool $wrapCard = false):string
    {
        ob_start();
        global $wpdb;
 
        $total_referrals = $wpdb->get_var("SELECT COUNT(*) FROM {$this->referrals_table}");
        $pending_referrals = $wpdb->get_var("SELECT COUNT(*) FROM {$this->referrals_table} WHERE status = 'pending'");
        $treated_referrals = $wpdb->get_var("SELECT COUNT(*) FROM {$this->referrals_table} WHERE status = 'treated'");
 
        ?>
        <table class="widefat">
            <tr>
                <th>Total Referrals</th>
                <td><?= esc_html($total_referrals) ?></td>
            </tr>
            <tr>
                <th>Pending</th>
                <td><?= esc_html($pending_referrals) ?></td>
            </tr>
            <tr>
                <th>Treated</th>
                <td><?= esc_html($treated_referrals) ?></td>
            </tr>
        </table>
        <?php
        $table = ob_get_clean();
        if ($wrapCard) {
            $table = '<div class="card">
                <h2>Referral Statistics</h2>
                '.$table.'
            </div>';
        }
        return $table;
    }
 
    /**
     * Add "Referral Page" label to admin bar
     *
     * @param WP_Admin_Bar $wp_admin_bar
     */
    public function addReferralPageLabel($wp_admin_bar): void
    {
        if (!is_admin()) {
            return;
        }
 
        $referral_page_id = $this->getReferralPageId();
 
        if (!$referral_page_id) {
            return;
        }
 
        global $pagenow, $post;
 
        // Check if we're editing the referral page
        if ('post.php' === $pagenow && $post && $post->ID === $referral_page_id) {
            $wp_admin_bar->add_node([
                'id' => 'referral-page',
                'parent' => 'top-secondary',
                'title' => __('Referral Page', 'jvbase'),
                'meta' => [
                    'class' => 'referral-page-notice'
                ]
            ]);
        }
    }
 
    /**
     * Get the referral page ID
     *
     * @return int|null
     */
    public function getReferralPageId(): ?int
    {
        $page_id = get_option(BASE . 'referral_page_id');
        return $page_id ? (int) $page_id : null;
    }
 
    /**
     * Show admin notice on referral page edit screen
     */
    public function showReferralPageNotice(): void
    {
        global $pagenow, $post;
 
        if ('post.php' !== $pagenow || !$post) {
            return;
        }
 
        $referral_page_id = $this->getReferralPageId();
 
        if ($post->ID === $referral_page_id) {
            echo '<div class="notice notice-info">';
            echo '<p>' . __('This page is designated as the <strong>Referral Page</strong>.', 'jvbase') . '</p>';
            echo '</div>';
        }
    }
 
    public function renderDashPage(string $content, string $page):string
    {
        if ($page !== 'referrals') {
            return $content;
        }
        $out = '';
        if (current_user_can('manage_options')) {
            $out .= $this->renderAdminHTML();
        } else {
            $out .= $this->renderReferralStats(true);
        }
        return ($out === '') ? $content : '<form id="referrals" class="col" data-save="referrals">'.$out.'</form>';
    }
}