xmaster 1.6.3

Enterprise-grade X/Twitter CLI — post, reply, like, retweet, DM, search, and more
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
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
use chrono::{Datelike, Timelike, Utc};
use rusqlite::{params, Connection, OptionalExtension};
use serde::{Deserialize, Serialize};
use std::path::PathBuf;

use crate::config::config_dir;

// ---------------------------------------------------------------------------
// Types
// ---------------------------------------------------------------------------

#[derive(Debug, Clone, Serialize)]
pub struct DiscoveredPostRow {
    pub tweet_id: String,
    pub author_username: String,
    pub text: String,
    pub like_count: i64,
    pub impression_count: i64,
    pub last_source: String,
    pub first_discovered_at: i64,
}

#[derive(Debug, Clone, Serialize)]
pub struct WatchlistEntry {
    pub username: String,
    pub user_id: Option<String>,
    pub topic: Option<String>,
    pub followers: i64,
    pub added_at: i64,
}

/// A reply target that earned a spot in the watchlist based on reply outcomes.
/// Returned by `find_hot_reply_targets` for automatic promotion.
#[derive(Debug, Clone, Serialize)]
pub struct HotReplyTarget {
    pub username: String,
    pub user_id: Option<String>,
    pub target_followers: i64,
}

/// A post that outperformed enough to be worth repurposing on another
/// platform. Returned by `find_cross_post_candidates`, surfaced as a
/// handoff suggestion in `xmaster track run`.
#[derive(Debug, Clone, Serialize)]
pub struct CrossPostCandidate {
    pub tweet_id: String,
    pub text_preview: String,
    pub posted_at: i64,
    pub impressions: i64,
    pub likes: i64,
    pub bookmarks: i64,
    pub suggested_action: String,
}

/// Aggregated stats for a reply target across all replies in a time window.
/// Returned by `rank_hot_reply_targets` for the `engage hot-targets` command.
#[derive(Debug, Clone, Serialize)]
pub struct HotTargetStats {
    pub username: String,
    pub sample_count: i64,
    pub avg_impressions: f64,
    pub avg_profile_clicks: f64,
    pub reply_back_rate: f64,
    pub last_reply_at: i64,
    pub score: f64,
}

#[derive(Debug, Clone)]
pub struct PendingReply {
    pub id: i64,
    pub reply_tweet_id: String,
    pub target_username: Option<String>,
    pub performed_at: i64,
}

/// Full metric snapshot row — used by the metrics command to compute deltas.
/// Unlike `SnapshotRecord` (which is a summary), this includes snapshot_at + all counters.
#[derive(Debug, Clone)]
pub struct FullSnapshot {
    pub snapshot_at: i64,
    pub minutes_since_post: i64,
    pub likes: i64,
    pub retweets: i64,
    pub replies: i64,
    pub impressions: i64,
    pub bookmarks: i64,
    pub quotes: i64,
    pub profile_clicks: i64,
}

#[derive(Debug, Clone, Serialize)]
pub struct PostRecord {
    pub tweet_id: String,
    pub text: String,
    pub content_type: String,
    pub posted_at: i64,
    pub preflight_score: Option<f64>,
    pub latest_metrics: Option<SnapshotRecord>,
}

#[derive(Debug, Clone, Serialize)]
pub struct SnapshotRecord {
    pub likes: i64,
    pub retweets: i64,
    pub replies: i64,
    pub impressions: i64,
    pub bookmarks: i64,
    pub engagement_rate: f64,
}

#[derive(Debug, Clone, Serialize)]
pub struct TimingSlot {
    pub day_of_week: i32,
    pub hour_of_day: i32,
    pub avg_impressions: f64,
    pub avg_engagement_rate: f64,
    pub sample_count: i64,
}

#[derive(Debug, Clone, Serialize)]
pub struct RecentVelocity {
    pub posts_1h: i64,
    pub posts_6h: i64,
    pub posts_24h: i64,
    /// Original posts (non-replies) in the last 24h. The 2026 algorithm treats
    /// >4 standalone posts/day as a spam-flag risk, but heavy replying is fine.
    pub standalone_24h: i64,
    pub accelerating_post: Option<String>,
}

#[derive(Debug, Clone, Serialize)]
pub struct ReciprocityInfo {
    pub total_engagements: i64,
    pub replies_received: i64,
    pub reply_rate: f64,
}

#[derive(Debug, Clone, Serialize)]
pub struct ReciprocatorInfo {
    pub username: String,
    pub total_engagements: i64,
    pub replies_received: i64,
    pub reply_rate: f64,
    pub avg_followers: i64,
}

// ---------------------------------------------------------------------------
// ReplyStyle classification
// ---------------------------------------------------------------------------

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub enum ReplyStyle {
    Question,
    DataPoint,
    Counterpoint,
    Anecdote,
    Humor,
    Agreement,
}

impl ReplyStyle {
    pub fn as_str(&self) -> &'static str {
        match self {
            Self::Question => "question",
            Self::DataPoint => "data_point",
            Self::Counterpoint => "counterpoint",
            Self::Anecdote => "anecdote",
            Self::Humor => "humor",
            Self::Agreement => "agreement",
        }
    }
}

impl std::fmt::Display for ReplyStyle {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.write_str(self.as_str())
    }
}

/// Classify a reply's rhetorical style using simple heuristics.
pub fn classify_reply(text: &str) -> ReplyStyle {
    let lower = text.to_lowercase();

    if text.contains('?') {
        return ReplyStyle::Question;
    }

    // DataPoint: numbers, percentages, stats
    if lower.contains('%')
        || lower.chars().any(|c| c.is_ascii_digit())
            && (lower.contains("study") || lower.contains("data") || lower.contains("stat"))
    {
        return ReplyStyle::DataPoint;
    }

    // Counterpoint: disagreement markers
    let counterpoint_markers = ["but ", "however", "actually", "disagree", "on the other hand"];
    if counterpoint_markers.iter().any(|m| lower.contains(m)) {
        return ReplyStyle::Counterpoint;
    }

    // Anecdote: personal experience markers
    let anecdote_markers = [
        "i've ", "i tested", "in my experience", "i found", "i noticed",
        "i tried", "personally", "my own",
    ];
    if anecdote_markers.iter().any(|m| lower.contains(m)) {
        return ReplyStyle::Anecdote;
    }

    // Humor: casual tone markers
    let humor_markers = ["lol", "lmao", "haha", "rofl", "😂", "🤣", "💀"];
    if humor_markers.iter().any(|m| lower.contains(m)) {
        return ReplyStyle::Humor;
    }

    ReplyStyle::Agreement
}

// ---------------------------------------------------------------------------
// IntelStore
// ---------------------------------------------------------------------------

pub struct IntelStore {
    conn: Connection,
}

impl IntelStore {
    /// Open (or create) the learning database at `~/.config/xmaster/xmaster.db`.
    pub fn open() -> Result<Self, rusqlite::Error> {
        let dir = config_dir();
        std::fs::create_dir_all(&dir).ok();
        let db_path: PathBuf = dir.join("xmaster.db");
        Self::open_at(&db_path)
    }

    /// Open the database at an explicit path. Used by tests to avoid the
    /// process-wide `XMASTER_CONFIG_DIR` env var that causes race conditions
    /// when `cargo test` runs in parallel.
    pub fn open_at(path: &std::path::Path) -> Result<Self, rusqlite::Error> {
        let conn = Connection::open(path)?;
        conn.pragma_update(None, "journal_mode", "wal")?;
        conn.pragma_update(None, "busy_timeout", 5000)?;
        conn.pragma_update(None, "synchronous", "NORMAL")?;
        let store = Self { conn };
        store.init_tables()?;
        Ok(store)
    }

    // -- schema --------------------------------------------------------------

    fn init_tables(&self) -> Result<(), rusqlite::Error> {
        self.conn.execute_batch(
            "
            CREATE TABLE IF NOT EXISTS posts (
                id              INTEGER PRIMARY KEY AUTOINCREMENT,
                tweet_id        TEXT UNIQUE NOT NULL,
                text            TEXT NOT NULL,
                content_type    TEXT NOT NULL DEFAULT 'text',
                char_count      INTEGER NOT NULL,
                has_link        BOOLEAN NOT NULL DEFAULT 0,
                has_media       BOOLEAN NOT NULL DEFAULT 0,
                has_poll        BOOLEAN NOT NULL DEFAULT 0,
                hashtag_count   INTEGER NOT NULL DEFAULT 0,
                hook_text       TEXT,
                posted_at       INTEGER NOT NULL,
                day_of_week     INTEGER NOT NULL,
                hour_of_day     INTEGER NOT NULL,
                reply_to_id     TEXT,
                quote_of_id     TEXT,
                preflight_score REAL,
                analysis_json   TEXT,
                analysis_version INTEGER DEFAULT 1,
                scheduled_post_id TEXT,
                local_day_of_week INTEGER,
                local_hour_of_day INTEGER,
                tz_offset_minutes INTEGER
            );

            CREATE TABLE IF NOT EXISTS metric_snapshots (
                id                 INTEGER PRIMARY KEY AUTOINCREMENT,
                tweet_id           TEXT NOT NULL REFERENCES posts(tweet_id),
                snapshot_at        INTEGER NOT NULL,
                minutes_since_post INTEGER NOT NULL,
                likes              INTEGER NOT NULL DEFAULT 0,
                retweets           INTEGER NOT NULL DEFAULT 0,
                replies            INTEGER NOT NULL DEFAULT 0,
                impressions        INTEGER NOT NULL DEFAULT 0,
                bookmarks          INTEGER NOT NULL DEFAULT 0,
                quotes             INTEGER NOT NULL DEFAULT 0,
                profile_clicks     INTEGER NOT NULL DEFAULT 0,
                url_clicks         INTEGER
            );

            CREATE TABLE IF NOT EXISTS engagement_actions (
                id               INTEGER PRIMARY KEY AUTOINCREMENT,
                action_type      TEXT NOT NULL,
                target_tweet_id  TEXT,
                target_user_id   TEXT,
                target_username  TEXT,
                performed_at     INTEGER NOT NULL,
                got_reply_back   BOOLEAN DEFAULT NULL,
                target_followers INTEGER
            );

            CREATE TABLE IF NOT EXISTS watchlist_accounts (
                id          INTEGER PRIMARY KEY AUTOINCREMENT,
                username    TEXT UNIQUE NOT NULL,
                user_id     TEXT,
                topic       TEXT,
                followers   INTEGER NOT NULL DEFAULT 0,
                added_at    INTEGER NOT NULL
            );

            -- Add reply_tweet_id if not exists (safe for existing DBs)
            -- SQLite doesn't support IF NOT EXISTS for ALTER, so we use a pragma check
            CREATE TABLE IF NOT EXISTS timing_stats (
                id                  INTEGER PRIMARY KEY AUTOINCREMENT,
                day_of_week         INTEGER NOT NULL,
                hour_of_day         INTEGER NOT NULL,
                content_type        TEXT NOT NULL DEFAULT 'all',
                avg_impressions     REAL,
                avg_engagement_rate REAL,
                sample_count        INTEGER NOT NULL DEFAULT 0,
                last_updated        INTEGER NOT NULL
            );

            CREATE TABLE IF NOT EXISTS discovered_posts (
                id                     INTEGER PRIMARY KEY AUTOINCREMENT,
                tweet_id               TEXT UNIQUE NOT NULL,
                text                   TEXT NOT NULL,
                author_id              TEXT,
                author_username        TEXT,
                created_at             TEXT,
                conversation_id        TEXT,
                referenced_tweets_json TEXT NOT NULL DEFAULT '[]',
                like_count             INTEGER,
                retweet_count          INTEGER,
                reply_count            INTEGER,
                impression_count       INTEGER,
                bookmark_count         INTEGER,
                author_followers       INTEGER,
                media_urls_json        TEXT NOT NULL DEFAULT '[]',
                first_source           TEXT NOT NULL,
                last_source            TEXT NOT NULL,
                first_discovered_at    INTEGER NOT NULL,
                last_seen_at           INTEGER NOT NULL
            );

            CREATE INDEX IF NOT EXISTS idx_discovered_author
                ON discovered_posts(author_username);
            CREATE INDEX IF NOT EXISTS idx_discovered_last_seen
                ON discovered_posts(last_seen_at DESC);
            CREATE INDEX IF NOT EXISTS idx_discovered_impressions
                ON discovered_posts(impression_count DESC);
            ",
        )?;

        // Safe migrations: add columns if not present (transaction for atomicity)
        self.conn.execute_batch("BEGIN IMMEDIATE;")?;
        let migrate_result = (|| -> Result<(), rusqlite::Error> {
            let cols: Vec<String> = self.conn
                .prepare("PRAGMA table_info(engagement_actions)")?
                .query_map([], |row| row.get::<_, String>(1))?
                .collect::<Result<Vec<_>, _>>()?;
            if !cols.iter().any(|c| c == "reply_tweet_id") {
                self.conn.execute_batch("ALTER TABLE engagement_actions ADD COLUMN reply_tweet_id TEXT;")?;
            }
            if !cols.iter().any(|c| c == "reply_style") {
                self.conn.execute_batch("ALTER TABLE engagement_actions ADD COLUMN reply_style TEXT;")?;
            }

            let post_cols: Vec<String> = self.conn
                .prepare("PRAGMA table_info(posts)")?
                .query_map([], |row| row.get::<_, String>(1))?
                .collect::<Result<Vec<_>, _>>()?;
            if !post_cols.iter().any(|c| c == "analysis_json") {
                self.conn.execute_batch("ALTER TABLE posts ADD COLUMN analysis_json TEXT;")?;
            }
            if !post_cols.iter().any(|c| c == "analysis_version") {
                self.conn.execute_batch("ALTER TABLE posts ADD COLUMN analysis_version INTEGER DEFAULT 1;")?;
            }
            if !post_cols.iter().any(|c| c == "scheduled_post_id") {
                self.conn.execute_batch("ALTER TABLE posts ADD COLUMN scheduled_post_id TEXT;")?;
            }

            // Add url_clicks to metric_snapshots if missing (legacy DBs predating the field)
            let ms_cols: Vec<String> = self.conn
                .prepare("PRAGMA table_info(metric_snapshots)")?
                .query_map([], |row| row.get::<_, String>(1))?
                .collect::<Result<Vec<_>, _>>()?;
            if !ms_cols.iter().any(|c| c == "url_clicks") {
                self.conn.execute_batch("ALTER TABLE metric_snapshots ADD COLUMN url_clicks INTEGER;")?;
            }

            // NOTE: engagement_actions.performed_at has TEXT column affinity
            // (declared TEXT in tracker.rs original CREATE TABLE). SQLite's
            // UPDATE ... SET col = CAST(col AS INTEGER) does NOT change the
            // stored type when the declared column affinity is TEXT. The only
            // real fix is DROP + CREATE TABLE with INTEGER affinity + INSERT
            // from old. That's tracked in issue #3 and too risky for an
            // additive migration. Queries on performed_at MUST use CAST
            // guards until the table rebuild lands.
            //
            // metric_snapshots.snapshot_at was declared INTEGER in store.rs
            // (correct affinity) so the IntelStore path stores INTEGER values.
            // However the tracker.rs CREATE TABLE declared it differently in
            // older versions, so existing rows may be text. The CAST in
            // latest_snapshot_full handles this case.

            Ok(())
        })();
        match migrate_result {
            Ok(()) => self.conn.execute_batch("COMMIT;")?,
            Err(e) => { self.conn.execute_batch("ROLLBACK;").ok(); return Err(e); }
        }

        // reply_outcomes view v2: extended with profile_clicks, quotes,
        // url_clicks, snapshot_at, minutes_since_post, and target_user_id.
        // DROP + CREATE because views hold no data. Safe on every open.
        self.conn.execute_batch("DROP VIEW IF EXISTS reply_outcomes;")?;
        self.conn.execute_batch(
            "CREATE VIEW reply_outcomes AS
             SELECT ea.id AS action_id,
                    ea.target_tweet_id,
                    ea.target_user_id,
                    ea.target_username,
                    ea.target_followers,
                    ea.reply_tweet_id,
                    ea.reply_style,
                    ea.performed_at,
                    ea.got_reply_back,
                    ms.likes,
                    ms.retweets,
                    ms.replies,
                    ms.impressions,
                    ms.bookmarks,
                    ms.quotes,
                    ms.profile_clicks,
                    ms.url_clicks,
                    CAST(ms.snapshot_at AS INTEGER) AS snapshot_at,
                    ms.minutes_since_post
             FROM engagement_actions ea
             LEFT JOIN metric_snapshots ms
               ON ms.tweet_id = ea.reply_tweet_id
               AND ms.id = (
                   SELECT MAX(ms2.id) FROM metric_snapshots ms2
                   WHERE ms2.tweet_id = ea.reply_tweet_id
               )
             WHERE ea.action_type = 'reply'
               AND ea.reply_tweet_id IS NOT NULL;"
        )?;

        Ok(())
    }

    /// Static helper: classify a reply's rhetorical style.
    pub fn classify_reply_style(text: &str) -> ReplyStyle {
        classify_reply(text)
    }

    // -- watchlist ------------------------------------------------------------

    pub fn add_watchlist(&self, username: &str, user_id: Option<&str>, topic: Option<&str>, followers: i64) -> Result<(), rusqlite::Error> {
        let now = Utc::now().timestamp();
        self.conn.execute(
            "INSERT OR REPLACE INTO watchlist_accounts (username, user_id, topic, followers, added_at)
             VALUES (LOWER(?1), ?2, ?3, ?4, ?5)",
            params![username.to_lowercase(), user_id, topic, followers, now],
        )?;
        Ok(())
    }

    pub fn remove_watchlist(&self, username: &str) -> Result<bool, rusqlite::Error> {
        let changed = self.conn.execute(
            "DELETE FROM watchlist_accounts WHERE username = LOWER(?1)",
            params![username.to_lowercase()],
        )?;
        Ok(changed > 0)
    }

    pub fn list_watchlist(&self) -> Result<Vec<WatchlistEntry>, rusqlite::Error> {
        let mut stmt = self.conn.prepare(
            "SELECT username, user_id, topic, followers, added_at FROM watchlist_accounts ORDER BY added_at DESC"
        )?;
        let rows = stmt.query_map([], |row| {
            Ok(WatchlistEntry {
                username: row.get(0)?,
                user_id: row.get(1)?,
                topic: row.get(2)?,
                followers: row.get(3)?,
                added_at: row.get(4)?,
            })
        })?;
        rows.collect()
    }

    /// Log a reply with the reply tweet ID and style for tracking reply-backs.
    pub fn log_reply(
        &self,
        target_tweet_id: &str,
        target_user_id: Option<&str>,
        target_username: Option<&str>,
        target_followers: Option<i64>,
        reply_tweet_id: &str,
        reply_style: Option<&ReplyStyle>,
    ) -> Result<(), rusqlite::Error> {
        let performed_at = Utc::now().timestamp();
        self.conn.execute(
            "INSERT INTO engagement_actions
                (action_type, target_tweet_id, target_user_id, target_username,
                 performed_at, target_followers, reply_tweet_id, reply_style)
             VALUES ('reply', ?1, ?2, LOWER(?3), ?4, ?5, ?6, ?7)",
            params![
                target_tweet_id,
                target_user_id,
                target_username.map(|u| u.to_lowercase()),
                performed_at,
                target_followers,
                reply_tweet_id,
                reply_style.map(|s| s.as_str()),
            ],
        )?;
        Ok(())
    }

    /// Get pending replies that haven't been checked for reply-back yet.
    pub fn get_pending_replies(&self, max_age_hours: i64) -> Result<Vec<PendingReply>, rusqlite::Error> {
        let cutoff = Utc::now().timestamp() - (max_age_hours * 3600);
        let mut stmt = self.conn.prepare(
            "SELECT id, reply_tweet_id, target_username, performed_at
             FROM engagement_actions
             WHERE action_type = 'reply' AND got_reply_back IS NULL
               AND reply_tweet_id IS NOT NULL AND performed_at > ?1
             ORDER BY performed_at DESC"
        )?;
        let rows = stmt.query_map(params![cutoff], |row| {
            Ok(PendingReply {
                id: row.get(0)?,
                reply_tweet_id: row.get(1)?,
                target_username: row.get(2)?,
                performed_at: row.get(3)?,
            })
        })?;
        rows.collect()
    }

    /// Mark a reply as having received (or not) a reply-back.
    pub fn set_reply_back(&self, action_id: i64, got_reply: bool) -> Result<(), rusqlite::Error> {
        self.conn.execute(
            "UPDATE engagement_actions SET got_reply_back = ?1 WHERE id = ?2",
            params![got_reply as i32, action_id],
        )?;
        Ok(())
    }

    // -- writes --------------------------------------------------------------

    /// Log a published post, extracting features automatically.
    pub fn log_post(
        &self,
        tweet_id: &str,
        text: &str,
        content_type: &str,
        reply_to: Option<&str>,
        quote_of: Option<&str>,
        preflight_score: Option<f64>,
        analysis_json: Option<&str>,
        scheduled_post_id: Option<&str>,
    ) -> Result<(), rusqlite::Error> {
        let now = Utc::now();
        let local_now = chrono::Local::now();
        let posted_at = now.timestamp();
        let day_of_week = now.weekday().num_days_from_monday() as i32; // 0=Mon (UTC)
        let hour_of_day = now.hour() as i32; // UTC
        let local_day = local_now.weekday().num_days_from_monday() as i32;
        let local_hour = local_now.hour() as i32;
        let tz_offset = local_now.offset().local_minus_utc() / 60;
        let char_count = text.chars().count() as i32;
        let has_link = text.contains("http://") || text.contains("https://");
        let hashtag_count = text.matches('#').count() as i32;
        let hook_text: String = text.chars().take(140).collect();

        self.conn.execute(
            "INSERT OR IGNORE INTO posts
                (tweet_id, text, content_type, char_count, has_link, has_media, has_poll,
                 hashtag_count, hook_text, posted_at, day_of_week, hour_of_day,
                 reply_to_id, quote_of_id, preflight_score,
                 analysis_json, analysis_version, scheduled_post_id,
                 local_day_of_week, local_hour_of_day, tz_offset_minutes)
             VALUES (?1,?2,?3,?4,?5,0,0,?6,?7,?8,?9,?10,?11,?12,?13,?14,1,?15,?16,?17,?18)",
            params![
                tweet_id,
                text,
                content_type,
                char_count,
                has_link,
                hashtag_count,
                hook_text,
                posted_at,
                day_of_week,
                hour_of_day,
                reply_to,
                quote_of,
                preflight_score,
                analysis_json,
                scheduled_post_id,
                local_day,
                local_hour,
                tz_offset,
            ],
        )?;
        Ok(())
    }

    /// Convenience function to record a published post with full metadata.
    /// Centralizes post recording logic: writes the post row, attaches analysis,
    /// links to scheduled_posts when applicable, and logs reply metadata for replies.
    #[allow(clippy::too_many_arguments)]
    pub fn record_published_post(
        &self,
        tweet_id: &str,
        text: &str,
        content_type: &str,
        reply_to: Option<&str>,
        quote_of: Option<&str>,
        preflight_score: Option<f64>,
        analysis_json: Option<&str>,
        scheduled_post_id: Option<&str>,
    ) -> Result<(), rusqlite::Error> {
        self.log_post(
            tweet_id,
            text,
            content_type,
            reply_to,
            quote_of,
            preflight_score,
            analysis_json,
            scheduled_post_id,
        )
    }

    // -- discovered posts library ---------------------------------------------

    /// Cache external posts encountered during search/timeline/read commands.
    /// UPSERT: first encounter preserves source/timestamp, re-encounters update metrics.
    pub fn record_discovered_posts(
        &self,
        source: &str,
        tweets: &[crate::providers::xapi::TweetData],
    ) -> Result<(), rusqlite::Error> {
        if tweets.is_empty() {
            return Ok(());
        }
        let now = Utc::now().timestamp();
        let tx = self.conn.unchecked_transaction()?;
        {
            let mut stmt = tx.prepare_cached(
                "INSERT INTO discovered_posts (
                    tweet_id, text, author_id, author_username, created_at,
                    conversation_id, referenced_tweets_json,
                    like_count, retweet_count, reply_count, impression_count,
                    bookmark_count, author_followers, media_urls_json,
                    first_source, last_source, first_discovered_at, last_seen_at
                ) VALUES (?1,?2,?3,?4,?5,?6,?7,?8,?9,?10,?11,?12,?13,?14,?15,?15,?16,?16)
                ON CONFLICT(tweet_id) DO UPDATE SET
                    text = excluded.text,
                    author_id = COALESCE(excluded.author_id, discovered_posts.author_id),
                    author_username = COALESCE(excluded.author_username, discovered_posts.author_username),
                    created_at = COALESCE(excluded.created_at, discovered_posts.created_at),
                    conversation_id = COALESCE(excluded.conversation_id, discovered_posts.conversation_id),
                    referenced_tweets_json = CASE
                        WHEN excluded.referenced_tweets_json <> '[]' THEN excluded.referenced_tweets_json
                        ELSE discovered_posts.referenced_tweets_json END,
                    like_count = COALESCE(excluded.like_count, discovered_posts.like_count),
                    retweet_count = COALESCE(excluded.retweet_count, discovered_posts.retweet_count),
                    reply_count = COALESCE(excluded.reply_count, discovered_posts.reply_count),
                    impression_count = COALESCE(excluded.impression_count, discovered_posts.impression_count),
                    bookmark_count = COALESCE(excluded.bookmark_count, discovered_posts.bookmark_count),
                    author_followers = COALESCE(excluded.author_followers, discovered_posts.author_followers),
                    media_urls_json = CASE
                        WHEN excluded.media_urls_json <> '[]' THEN excluded.media_urls_json
                        ELSE discovered_posts.media_urls_json END,
                    last_source = excluded.last_source,
                    last_seen_at = excluded.last_seen_at",
            )?;
            for t in tweets {
                let m = t.public_metrics.as_ref();
                let refs_json = serde_json::to_string(
                    &t.referenced_tweets.as_deref().unwrap_or(&[])
                ).unwrap_or_else(|_| "[]".into());
                let media_json = serde_json::to_string(&t.media_urls)
                    .unwrap_or_else(|_| "[]".into());
                stmt.execute(params![
                    t.id,
                    t.text,
                    t.author_id,
                    t.author_username,
                    t.created_at,
                    t.conversation_id,
                    refs_json,
                    m.map(|m| m.like_count as i64),
                    m.map(|m| m.retweet_count as i64),
                    m.map(|m| m.reply_count as i64),
                    m.map(|m| m.impression_count as i64),
                    m.map(|m| m.bookmark_count as i64),
                    t.author_followers.map(|f| f as i64),
                    media_json,
                    source,
                    now,
                ])?;
            }
        }
        tx.commit()?;
        Ok(())
    }

    /// Cache a single discovered post.
    pub fn record_discovered_post(
        &self,
        source: &str,
        tweet: &crate::providers::xapi::TweetData,
    ) -> Result<(), rusqlite::Error> {
        self.record_discovered_posts(source, std::slice::from_ref(tweet))
    }

    /// Query the discovered posts library with optional filters.
    pub fn query_discovered_posts(
        &self,
        topic: Option<&str>,
        author: Option<&str>,
        min_likes: Option<i64>,
        limit: usize,
    ) -> Result<Vec<DiscoveredPostRow>, rusqlite::Error> {
        let mut sql = String::from(
            "SELECT tweet_id, COALESCE(author_username,''), text,
                    COALESCE(like_count,0), COALESCE(impression_count,0),
                    last_source, first_discovered_at
             FROM discovered_posts WHERE 1=1"
        );
        // Build dynamic WHERE clauses — params are positional
        let mut param_idx = 1usize;
        let topic_idx = if topic.is_some() {
            sql.push_str(&format!(" AND text LIKE '%' || ?{param_idx} || '%'"));
            let idx = param_idx; param_idx += 1; Some(idx)
        } else { None };
        let author_idx = if author.is_some() {
            sql.push_str(&format!(" AND author_username LIKE '%' || ?{param_idx} || '%'"));
            let idx = param_idx; param_idx += 1; Some(idx)
        } else { None };
        let likes_idx = if min_likes.is_some() {
            sql.push_str(&format!(" AND like_count >= ?{param_idx}"));
            let idx = param_idx; param_idx += 1; Some(idx)
        } else { None };
        sql.push_str(&format!(" ORDER BY COALESCE(impression_count,0) DESC LIMIT ?{param_idx}"));

        let mut stmt = self.conn.prepare(&sql)?;
        let mut bind_idx = 1usize;
        if let Some(_) = topic_idx { stmt.raw_bind_parameter(bind_idx, topic.unwrap())?; bind_idx += 1; }
        if let Some(_) = author_idx { stmt.raw_bind_parameter(bind_idx, author.unwrap())?; bind_idx += 1; }
        if let Some(_) = likes_idx { stmt.raw_bind_parameter(bind_idx, min_likes.unwrap())?; bind_idx += 1; }
        stmt.raw_bind_parameter(bind_idx, limit as i64)?;

        let mut rows = Vec::new();
        let mut raw = stmt.raw_query();
        while let Some(row) = raw.next()? {
            rows.push(DiscoveredPostRow {
                tweet_id: row.get(0)?,
                author_username: row.get(1)?,
                text: row.get(2)?,
                like_count: row.get(3)?,
                impression_count: row.get(4)?,
                last_source: row.get(5)?,
                first_discovered_at: row.get(6)?,
            });
        }
        Ok(rows)
    }

    /// Count total posts in the discovered library.
    pub fn discovered_posts_count(&self) -> Result<i64, rusqlite::Error> {
        self.conn.query_row("SELECT COUNT(*) FROM discovered_posts", [], |r| r.get(0))
    }

    // -- metric snapshots -----------------------------------------------------

    /// Record a metric snapshot for a tweet. `url_clicks` is `Option<i64>` because
    /// the value comes from `non_public_metrics` which is only visible for posts you own;
    /// callers pass `None` when the data is unavailable.
    #[allow(clippy::too_many_arguments)]
    pub fn log_metric_snapshot(
        &self,
        tweet_id: &str,
        likes: i64,
        retweets: i64,
        replies: i64,
        impressions: i64,
        bookmarks: i64,
        quotes: i64,
        profile_clicks: i64,
        minutes_since_post: i64,
        url_clicks: Option<i64>,
    ) -> Result<(), rusqlite::Error> {
        let snapshot_at = Utc::now().timestamp();
        self.conn.execute(
            "INSERT INTO metric_snapshots
                (tweet_id, snapshot_at, minutes_since_post, likes, retweets, replies,
                 impressions, bookmarks, quotes, profile_clicks, url_clicks)
             VALUES (?1,?2,?3,?4,?5,?6,?7,?8,?9,?10,?11)",
            params![
                tweet_id,
                snapshot_at,
                minutes_since_post,
                likes,
                retweets,
                replies,
                impressions,
                bookmarks,
                quotes,
                profile_clicks,
                url_clicks,
            ],
        )?;
        Ok(())
    }

    /// Fetch the most recent metric snapshot for a tweet with all counters + timestamps.
    /// Returns None if no snapshot exists yet for this tweet_id.
    ///
    /// Fetch the most recent metric snapshot for a tweet with all counters + timestamps.
    /// Returns None if no snapshot exists yet for this tweet_id.
    pub fn latest_snapshot_full(
        &self,
        tweet_id: &str,
    ) -> Result<Option<FullSnapshot>, rusqlite::Error> {
        self.conn
            .query_row(
                "SELECT snapshot_at, minutes_since_post,
                        likes, retweets, replies,
                        impressions, bookmarks, quotes, profile_clicks
                 FROM metric_snapshots
                 WHERE tweet_id = ?1
                 ORDER BY snapshot_at DESC
                 LIMIT 1",
                params![tweet_id],
                |row| {
                    Ok(FullSnapshot {
                        snapshot_at: row.get(0)?,
                        minutes_since_post: row.get(1)?,
                        likes: row.get(2)?,
                        retweets: row.get(3)?,
                        replies: row.get(4)?,
                        impressions: row.get(5)?,
                        bookmarks: row.get(6)?,
                        quotes: row.get(7)?,
                        profile_clicks: row.get(8)?,
                    })
                },
            )
            .optional()
    }

    /// Log an engagement action (like, reply, retweet, etc.).
    pub fn log_engagement(
        &self,
        action_type: &str,
        target_tweet_id: Option<&str>,
        target_user_id: Option<&str>,
        target_username: Option<&str>,
        target_followers: Option<i64>,
    ) -> Result<(), rusqlite::Error> {
        let performed_at = Utc::now().timestamp();
        self.conn.execute(
            "INSERT INTO engagement_actions
                (action_type, target_tweet_id, target_user_id, target_username,
                 performed_at, target_followers)
             VALUES (?1,?2,?3,?4,?5,?6)",
            params![
                action_type,
                target_tweet_id,
                target_user_id,
                target_username,
                performed_at,
                target_followers,
            ],
        )?;
        Ok(())
    }

    // -- reads ---------------------------------------------------------------

    /// Recent posts with their latest metric snapshot.
    pub fn get_post_history(&self, limit: i64) -> Result<Vec<PostRecord>, rusqlite::Error> {
        let mut stmt = self.conn.prepare(
            "SELECT tweet_id, text, content_type, posted_at, preflight_score
             FROM posts ORDER BY posted_at DESC LIMIT ?1",
        )?;

        let rows = stmt.query_map(params![limit], |row| {
            Ok(PostRecord {
                tweet_id: row.get(0)?,
                text: row.get(1)?,
                content_type: row.get(2)?,
                posted_at: row.get(3)?,
                preflight_score: row.get(4)?,
                latest_metrics: None, // filled below
            })
        })?;

        let mut posts: Vec<PostRecord> = rows.collect::<Result<Vec<_>, _>>()?;

        for post in &mut posts {
            post.latest_metrics = self.latest_snapshot(&post.tweet_id)?;
        }

        Ok(posts)
    }

    fn latest_snapshot(&self, tweet_id: &str) -> Result<Option<SnapshotRecord>, rusqlite::Error> {
        self.conn
            .query_row(
                "SELECT likes, retweets, replies, impressions, bookmarks
                 FROM metric_snapshots
                 WHERE tweet_id = ?1
                 ORDER BY snapshot_at DESC LIMIT 1",
                params![tweet_id],
                |row| {
                    let likes: i64 = row.get(0)?;
                    let retweets: i64 = row.get(1)?;
                    let replies: i64 = row.get(2)?;
                    let impressions: i64 = row.get(3)?;
                    let bookmarks: i64 = row.get(4)?;
                    let engagement_rate = if impressions > 0 {
                        (likes + retweets + replies) as f64 / impressions as f64
                    } else {
                        0.0
                    };
                    Ok(SnapshotRecord {
                        likes,
                        retweets,
                        replies,
                        impressions,
                        bookmarks,
                        engagement_rate,
                    })
                },
            )
            .optional()
    }

    /// Aggregated timing heatmap: avg engagement by day-of-week / hour-of-day.
    pub fn get_timing_heatmap(&self) -> Result<Vec<TimingSlot>, rusqlite::Error> {
        let mut stmt = self.conn.prepare(
            "SELECT p.day_of_week, p.hour_of_day,
                    AVG(ms.impressions)                                         AS avg_imp,
                    AVG(CASE WHEN ms.impressions > 0
                         THEN (ms.likes + ms.retweets + ms.replies + ms.quotes) * 1.0
                              / ms.impressions ELSE 0 END)                      AS avg_er,
                    COUNT(DISTINCT p.tweet_id)                                  AS cnt
             FROM posts p
             JOIN metric_snapshots ms ON ms.tweet_id = p.tweet_id
             GROUP BY p.day_of_week, p.hour_of_day
             ORDER BY avg_er DESC",
        )?;

        let rows = stmt.query_map([], |row| {
            Ok(TimingSlot {
                day_of_week: row.get(0)?,
                hour_of_day: row.get(1)?,
                avg_impressions: row.get(2)?,
                avg_engagement_rate: row.get(3)?,
                sample_count: row.get(4)?,
            })
        })?;

        rows.collect()
    }

    /// Top N best posting time slots, optionally filtered by content type.
    pub fn get_best_posting_times(
        &self,
        content_type: Option<&str>,
        top_n: i64,
    ) -> Result<Vec<TimingSlot>, rusqlite::Error> {
        let (sql, use_filter) = match content_type {
            Some(_) => (
                "SELECT p.day_of_week, p.hour_of_day,
                        AVG(ms.impressions) AS avg_imp,
                        AVG(CASE WHEN ms.impressions > 0
                             THEN (ms.likes+ms.retweets+ms.replies+ms.quotes)*1.0
                                  / ms.impressions ELSE 0 END) AS avg_er,
                        COUNT(DISTINCT p.tweet_id) AS cnt
                 FROM posts p
                 JOIN metric_snapshots ms ON ms.tweet_id = p.tweet_id
                 WHERE p.content_type = ?1
                 GROUP BY p.day_of_week, p.hour_of_day
                 HAVING cnt >= 2
                 ORDER BY avg_er DESC
                 LIMIT ?2",
                true,
            ),
            None => (
                "SELECT p.day_of_week, p.hour_of_day,
                        AVG(ms.impressions) AS avg_imp,
                        AVG(CASE WHEN ms.impressions > 0
                             THEN (ms.likes+ms.retweets+ms.replies+ms.quotes)*1.0
                                  / ms.impressions ELSE 0 END) AS avg_er,
                        COUNT(DISTINCT p.tweet_id) AS cnt
                 FROM posts p
                 JOIN metric_snapshots ms ON ms.tweet_id = p.tweet_id
                 GROUP BY p.day_of_week, p.hour_of_day
                 HAVING cnt >= 2
                 ORDER BY avg_er DESC
                 LIMIT ?1",
                false,
            ),
        };

        let mut stmt = self.conn.prepare(sql)?;

        let map_row = |row: &rusqlite::Row| -> rusqlite::Result<TimingSlot> {
            Ok(TimingSlot {
                day_of_week: row.get(0)?,
                hour_of_day: row.get(1)?,
                avg_impressions: row.get(2)?,
                avg_engagement_rate: row.get(3)?,
                sample_count: row.get(4)?,
            })
        };

        let results: Vec<TimingSlot> = if use_filter {
            stmt.query_map(params![content_type.unwrap(), top_n], map_row)?
                .collect::<Result<Vec<_>, _>>()?
        } else {
            stmt.query_map(params![top_n], map_row)?
                .collect::<Result<Vec<_>, _>>()?
        };

        Ok(results)
    }

    /// Posts in the last 1h, 6h, 24h and whether any recent post is accelerating.
    pub fn get_recent_post_velocity(&self) -> Result<RecentVelocity, rusqlite::Error> {
        let now = Utc::now().timestamp();

        let posts_1h: i64 = self.conn.query_row(
            "SELECT COUNT(*) FROM posts
             WHERE posted_at > ?1 - 3600",
            params![now],
            |r| r.get(0),
        )?;
        let posts_6h: i64 = self.conn.query_row(
            "SELECT COUNT(*) FROM posts
             WHERE posted_at > ?1 - 21600",
            params![now],
            |r| r.get(0),
        )?;
        let posts_24h: i64 = self.conn.query_row(
            "SELECT COUNT(*) FROM posts
             WHERE posted_at > ?1 - 86400",
            params![now],
            |r| r.get(0),
        )?;
        // Standalone = everything except replies. Threads count (thread_hook
        // and thread_reply are both original-authored feed items). Replies to
        // other people's posts are excluded — the 2026 "2-4/day" cap applies
        // only to new impressions in the main feed.
        let standalone_24h: i64 = self.conn.query_row(
            "SELECT COUNT(*) FROM posts
             WHERE posted_at > ?1 - 86400
               AND content_type != 'reply'",
            params![now],
            |r| r.get(0),
        )?;

        // A post is "accelerating" if its latest two snapshots show increasing impressions
        let accelerating_post: Option<String> = self
            .conn
            .query_row(
                "SELECT s1.tweet_id
                 FROM metric_snapshots s1
                 JOIN metric_snapshots s2 ON s1.tweet_id = s2.tweet_id
                   AND s2.id = (SELECT MAX(id) FROM metric_snapshots
                                WHERE tweet_id = s1.tweet_id AND id < s1.id)
                 JOIN posts p ON p.tweet_id = s1.tweet_id
                 WHERE s1.id = (SELECT MAX(id) FROM metric_snapshots
                                WHERE tweet_id = s1.tweet_id)
                   AND p.posted_at > ?1 - 86400
                   AND s1.impressions > s2.impressions * 1.5
                 ORDER BY (s1.impressions - s2.impressions) DESC
                 LIMIT 1",
                params![now],
                |r| r.get(0),
            )
            .optional()?;

        Ok(RecentVelocity {
            posts_1h,
            posts_6h,
            posts_24h,
            standalone_24h,
            accelerating_post,
        })
    }

    /// Recalculate the `timing_stats` table from raw posts + snapshots.
    pub fn update_timing_stats(&self) -> Result<(), rusqlite::Error> {
        let now = Utc::now().timestamp();
        let tx = self.conn.unchecked_transaction()?;
        tx.execute_batch("DELETE FROM timing_stats")?;
        tx.execute(
            "INSERT INTO timing_stats
                (day_of_week, hour_of_day, content_type,
                 avg_impressions, avg_engagement_rate, sample_count, last_updated)
             SELECT p.day_of_week, p.hour_of_day, p.content_type,
                    AVG(ms.impressions),
                    AVG(CASE WHEN ms.impressions > 0
                         THEN (ms.likes+ms.retweets+ms.replies+ms.quotes)*1.0
                              / ms.impressions ELSE 0 END),
                    COUNT(DISTINCT p.tweet_id),
                    ?1
             FROM posts p
             JOIN metric_snapshots ms ON ms.tweet_id = p.tweet_id
             GROUP BY p.day_of_week, p.hour_of_day, p.content_type",
            params![now],
        )?;
        // Also insert an 'all' row per slot
        tx.execute(
            "INSERT INTO timing_stats
                (day_of_week, hour_of_day, content_type,
                 avg_impressions, avg_engagement_rate, sample_count, last_updated)
             SELECT p.day_of_week, p.hour_of_day, 'all',
                    AVG(ms.impressions),
                    AVG(CASE WHEN ms.impressions > 0
                         THEN (ms.likes+ms.retweets+ms.replies+ms.quotes)*1.0
                              / ms.impressions ELSE 0 END),
                    COUNT(DISTINCT p.tweet_id),
                    ?1
             FROM posts p
             JOIN metric_snapshots ms ON ms.tweet_id = p.tweet_id
             GROUP BY p.day_of_week, p.hour_of_day",
            params![now],
        )?;
        tx.commit()?;
        Ok(())
    }

    /// How often a user replies back after we engage with them.
    pub fn get_engagement_reciprocity(
        &self,
        username: &str,
    ) -> Result<Option<ReciprocityInfo>, rusqlite::Error> {
        self.conn
            .query_row(
                "SELECT COUNT(*) AS total,
                        SUM(CASE WHEN got_reply_back = 1 THEN 1 ELSE 0 END) AS replies_back
                 FROM engagement_actions
                 WHERE target_username = ?1",
                params![username],
                |row| {
                    let total: i64 = row.get(0)?;
                    if total == 0 {
                        return Ok(None);
                    }
                    let replies_received: i64 = row.get(1)?;
                    Ok(Some(ReciprocityInfo {
                        total_engagements: total,
                        replies_received,
                        reply_rate: if total > 0 {
                            replies_received as f64 / total as f64
                        } else {
                            0.0
                        },
                    }))
                },
            )
    }

    /// Find reply targets that deserve promotion to watchlist based on recent reply outcomes.
    ///
    /// Selection criteria (per-row OR): impressions ≥ min, profile_clicks ≥ min, or got_reply_back = 1.
    /// Guardrail: target_followers ≥ min_target_followers.
    /// Freshness: only considers replies in the last `max_age_hours`.
    /// Excludes targets already on the watchlist.
    ///
    /// Find reply targets that deserve promotion to watchlist based on recent reply outcomes.
    ///
    /// Same TEXT-affinity caveat as `rank_hot_reply_targets` — CAST guards
    /// on `performed_at` remain until a full table rebuild migration.
    pub fn find_hot_reply_targets(
        &self,
        min_impressions: i64,
        min_profile_clicks: i64,
        min_target_followers: i64,
        max_age_hours: i64,
    ) -> Result<Vec<HotReplyTarget>, rusqlite::Error> {
        let cutoff = Utc::now().timestamp() - max_age_hours * 3600;
        let mut stmt = self.conn.prepare(
            "SELECT DISTINCT LOWER(ea.target_username),
                    ea.target_user_id,
                    COALESCE(ea.target_followers, 0)
             FROM engagement_actions ea
             LEFT JOIN metric_snapshots ms
               ON ms.tweet_id = ea.reply_tweet_id
              AND ms.id = (
                  SELECT MAX(ms2.id) FROM metric_snapshots ms2
                  WHERE ms2.tweet_id = ea.reply_tweet_id
              )
             LEFT JOIN watchlist_accounts wa
               ON wa.username = LOWER(ea.target_username)
             WHERE ea.action_type = 'reply'
               AND ea.target_username IS NOT NULL
               AND ea.reply_tweet_id IS NOT NULL
               AND CAST(ea.performed_at AS INTEGER) >= ?1
               AND COALESCE(ea.target_followers, 0) >= ?2
               AND wa.username IS NULL
               AND (COALESCE(ms.impressions, 0) >= ?3
                 OR COALESCE(ms.profile_clicks, 0) >= ?4
                 OR COALESCE(ea.got_reply_back, 0) = 1)
             ORDER BY COALESCE(ea.target_followers, 0) DESC"
        )?;
        let rows = stmt.query_map(
            params![cutoff, min_target_followers, min_impressions, min_profile_clicks],
            |row| {
                Ok(HotReplyTarget {
                    username: row.get(0)?,
                    user_id: row.get(1)?,
                    target_followers: row.get(2)?,
                })
            },
        )?;
        rows.collect()
    }

    /// Find standalone posts (not replies) that crossed the impressions
    /// threshold and are fresh enough to repurpose on other platforms
    /// (e.g., screenshot → Instagram still, LinkedIn carousel, etc).
    ///
    /// This is the output surface for the cross-platform amplification
    /// pattern observed in @jackmoses777's 198K-X-views → 325K-IG-views
    /// case (Apr 2026): high-performing X text posts repurposed as IG
    /// stills outperform native IG content for X writers.
    ///
    /// Returns at most `max_results` posts, ordered by latest impressions
    /// descending. `min_impressions` defaults to 5000 in the caller.
    pub fn find_cross_post_candidates(
        &self,
        min_impressions: i64,
        max_age_days: i64,
        max_results: i64,
    ) -> Result<Vec<CrossPostCandidate>, rusqlite::Error> {
        let cutoff = Utc::now().timestamp() - max_age_days * 24 * 3600;
        let mut stmt = self.conn.prepare(
            "SELECT p.tweet_id,
                    p.text,
                    p.posted_at,
                    MAX(ms.impressions) AS peak_impressions,
                    MAX(ms.likes) AS peak_likes,
                    MAX(ms.bookmarks) AS peak_bookmarks
             FROM posts p
             JOIN metric_snapshots ms ON ms.tweet_id = p.tweet_id
             WHERE p.content_type NOT IN ('reply', 'thread_reply')
               AND p.posted_at >= ?1
             GROUP BY p.tweet_id, p.text, p.posted_at
             HAVING MAX(ms.impressions) >= ?2
             ORDER BY MAX(ms.impressions) DESC
             LIMIT ?3"
        )?;
        let rows = stmt.query_map(
            params![cutoff, min_impressions, max_results],
            |row| {
                let tweet_id: String = row.get(0)?;
                let text: String = row.get(1)?;
                let posted_at: i64 = row.get(2)?;
                let impressions: i64 = row.get(3)?;
                let likes: i64 = row.get(4)?;
                let bookmarks: i64 = row.get(5)?;
                Ok(CrossPostCandidate {
                    tweet_id: tweet_id.clone(),
                    text_preview: if text.chars().count() > 120 {
                        let truncated: String = text.chars().take(120).collect();
                        format!("{truncated}...")
                    } else {
                        text
                    },
                    posted_at,
                    impressions,
                    likes,
                    bookmarks,
                    suggested_action: format!(
                        "Screenshot https://x.com/i/status/{tweet_id} and repost as an Instagram still via the clinstagram skill"
                    ),
                })
            },
        )?;
        rows.collect()
    }

    /// Aggregate reply-outcome stats per target username over the last `days`.
    ///
    /// Filters by min_samples, min_avg_impressions, min_avg_profile_clicks (HAVING).
    /// Returns rows sorted by composite score descending. Caller can re-sort.
    ///
    /// Aggregate reply-outcome stats per target username over the last `days`.
    ///
    /// NOTE: `performed_at` still has TEXT affinity in production DBs (the column
    /// was declared TEXT in the original tracker.rs CREATE TABLE and SQLite's
    /// CAST-in-UPDATE trick doesn't fix cell types when the declared affinity
    /// disagrees). We must keep CAST in comparisons and MAX() until a full table
    /// rebuild migration lands.
    pub fn rank_hot_reply_targets(
        &self,
        days: i64,
        min_samples: i64,
        min_avg_impressions: f64,
        min_avg_profile_clicks: f64,
    ) -> Result<Vec<HotTargetStats>, rusqlite::Error> {
        let cutoff = Utc::now().timestamp() - days * 24 * 3600;
        let mut stmt = self.conn.prepare(
            "SELECT LOWER(ea.target_username) AS username,
                    COUNT(*) AS sample_count,
                    AVG(COALESCE(ms.impressions, 0)) AS avg_imps,
                    AVG(COALESCE(ms.profile_clicks, 0)) AS avg_clicks,
                    AVG(CASE WHEN ea.got_reply_back = 1 THEN 1.0 ELSE 0.0 END) AS reply_back_rate,
                    CAST(MAX(CAST(ea.performed_at AS INTEGER)) AS INTEGER) AS last_reply_at
             FROM engagement_actions ea
             LEFT JOIN metric_snapshots ms
               ON ms.tweet_id = ea.reply_tweet_id
              AND ms.id = (
                  SELECT MAX(ms2.id) FROM metric_snapshots ms2
                  WHERE ms2.tweet_id = ea.reply_tweet_id
              )
             WHERE ea.action_type = 'reply'
               AND ea.target_username IS NOT NULL
               AND ea.reply_tweet_id IS NOT NULL
               AND CAST(ea.performed_at AS INTEGER) >= ?1
             GROUP BY LOWER(ea.target_username)
             HAVING sample_count >= ?2
                AND avg_imps >= ?3
                AND avg_clicks >= ?4"
        )?;
        let mut rows: Vec<HotTargetStats> = stmt
            .query_map(
                params![cutoff, min_samples, min_avg_impressions, min_avg_profile_clicks],
                |row| {
                    let avg_imps: f64 = row.get(2)?;
                    let avg_clicks: f64 = row.get(3)?;
                    let reply_back_rate: f64 = row.get(4)?;
                    // Composite score: normalized blend of reach + profile clicks + reciprocity.
                    // Normalization caps avoid one huge outlier dominating.
                    let imps_norm = (avg_imps / 1000.0).min(1.0);
                    let clicks_norm = (avg_clicks / 10.0).min(1.0);
                    let score = 0.55 * imps_norm + 0.25 * clicks_norm + 0.20 * reply_back_rate;
                    Ok(HotTargetStats {
                        username: row.get(0)?,
                        sample_count: row.get(1)?,
                        avg_impressions: avg_imps,
                        avg_profile_clicks: avg_clicks,
                        reply_back_rate,
                        last_reply_at: row.get(5)?,
                        score,
                    })
                },
            )?
            .collect::<Result<Vec<_>, _>>()?;
        rows.sort_by(|a, b| {
            b.score
                .partial_cmp(&a.score)
                .unwrap_or(std::cmp::Ordering::Equal)
        });
        Ok(rows)
    }

    /// Top reciprocators: accounts that reply back most often, filtered by min followers.
    pub fn get_top_reciprocators(
        &self,
        min_followers: i64,
        limit: i64,
    ) -> Result<Vec<ReciprocatorInfo>, rusqlite::Error> {
        let mut stmt = self.conn.prepare(
            "SELECT target_username,
                    COUNT(*) AS total,
                    SUM(CASE WHEN got_reply_back = 1 THEN 1 ELSE 0 END) AS replies_back,
                    AVG(target_followers) AS avg_fol
             FROM engagement_actions
             WHERE target_username IS NOT NULL
               AND target_followers >= ?1
             GROUP BY target_username
             HAVING replies_back > 0
             ORDER BY (CAST(replies_back AS REAL) / total) DESC
             LIMIT ?2",
        )?;

        let rows = stmt.query_map(params![min_followers, limit], |row| {
            let total: i64 = row.get(1)?;
            let replies_received: i64 = row.get(2)?;
            let avg_fol: f64 = row.get(3)?;
            Ok(ReciprocatorInfo {
                username: row.get(0)?,
                total_engagements: total,
                replies_received,
                reply_rate: if total > 0 {
                    replies_received as f64 / total as f64
                } else {
                    0.0
                },
                avg_followers: avg_fol as i64,
            })
        })?;

        rows.collect()
    }
}

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

    fn test_store() -> IntelStore {
        let dir = tempdir().unwrap();
        let db_path = dir.path().join("test.db");
        let store = IntelStore::open_at(&db_path).unwrap();
        // Keep tempdir alive by leaking it (tests are short-lived)
        std::mem::forget(dir);
        store
    }

    #[test]
    fn log_and_retrieve_post() {
        let store = test_store();
        store
            .log_post("tweet_001", "Hello world!", "opinion", None, None, Some(85.0), None, None)
            .unwrap();

        let posts = store.get_post_history(10).unwrap();
        assert_eq!(posts.len(), 1);
        assert_eq!(posts[0].tweet_id, "tweet_001");
        assert_eq!(posts[0].text, "Hello world!");
        assert_eq!(posts[0].content_type, "opinion");
        assert_eq!(posts[0].preflight_score, Some(85.0));
        assert!(posts[0].latest_metrics.is_none());
    }

    #[test]
    fn duplicate_tweet_id_ignored() {
        let store = test_store();
        store
            .log_post("tweet_dup", "First", "opinion", None, None, None, None, None)
            .unwrap();
        // INSERT OR IGNORE — second insert should not fail or create duplicate
        store
            .log_post("tweet_dup", "Second", "opinion", None, None, None, None, None)
            .unwrap();

        let posts = store.get_post_history(10).unwrap();
        assert_eq!(posts.len(), 1);
        assert_eq!(posts[0].text, "First"); // original text preserved
    }

    #[test]
    fn engagement_logging() {
        let store = test_store();
        store
            .log_engagement("like", Some("t_100"), None, Some("testuser"), Some(5000))
            .unwrap();
        store
            .log_engagement("reply", Some("t_101"), None, Some("testuser"), Some(5000))
            .unwrap();

        let info = store.get_engagement_reciprocity("testuser").unwrap();
        assert!(info.is_some());
        let info = info.unwrap();
        assert_eq!(info.total_engagements, 2);
    }

    #[test]
    fn timing_heatmap_empty_db() {
        let store = test_store();
        let heatmap = store.get_timing_heatmap().unwrap();
        assert!(heatmap.is_empty());
    }

    #[test]
    fn recent_velocity_empty_db() {
        let store = test_store();
        let velocity = store.get_recent_post_velocity().unwrap();
        assert_eq!(velocity.posts_1h, 0);
        assert_eq!(velocity.posts_6h, 0);
        assert_eq!(velocity.posts_24h, 0);
        assert_eq!(velocity.standalone_24h, 0);
        assert!(velocity.accelerating_post.is_none());
    }

    #[test]
    fn cross_post_candidates_filters_and_excludes_replies() {
        let store = test_store();
        // Standalone post that hit 12k imps — a candidate
        store
            .log_post("big_hit", "a viral thought", "text", None, None, None, None, None)
            .unwrap();
        store
            .log_metric_snapshot("big_hit", 400, 50, 30, 12_000, 100, 10, 5, 30, None)
            .unwrap();
        // Standalone post with only 1k imps — below threshold
        store
            .log_post("small_post", "meh", "text", None, None, None, None, None)
            .unwrap();
        store
            .log_metric_snapshot("small_post", 10, 1, 0, 1_000, 5, 0, 0, 20, None)
            .unwrap();
        // Reply that happened to go viral — must be excluded
        store
            .log_post("viral_reply", "nailed it", "reply", Some("other"), None, None, None, None)
            .unwrap();
        store
            .log_metric_snapshot("viral_reply", 500, 60, 40, 15_000, 80, 15, 20, 40, None)
            .unwrap();

        let candidates = store.find_cross_post_candidates(5_000, 14, 10).unwrap();
        assert_eq!(candidates.len(), 1, "only big_hit qualifies");
        assert_eq!(candidates[0].tweet_id, "big_hit");
        assert_eq!(candidates[0].impressions, 12_000);
        assert!(candidates[0].suggested_action.contains("clinstagram"));
    }

    #[test]
    fn standalone_24h_excludes_replies() {
        let store = test_store();
        store
            .log_post("p_1", "a standalone post", "text", None, None, None, None, None)
            .unwrap();
        store
            .log_post("p_2", "reply to something", "reply", Some("other_id"), None, None, None, None)
            .unwrap();
        store
            .log_post("p_3", "thread opener", "thread_hook", None, None, None, None, None)
            .unwrap();
        let v = store.get_recent_post_velocity().unwrap();
        assert_eq!(v.posts_24h, 3, "all three rows count toward posts_24h");
        assert_eq!(v.standalone_24h, 2, "reply must be excluded from standalone_24h");
    }

    #[test]
    fn metric_snapshot_persists_url_clicks() {
        let store = test_store();
        store
            .log_post("tweet_url", "Check this link", "text", None, None, None, None, None)
            .unwrap();
        store
            .log_metric_snapshot("tweet_url", 5, 1, 0, 200, 0, 0, 3, 10, Some(42))
            .unwrap();
        let clicks: Option<i64> = store
            .conn
            .query_row(
                "SELECT url_clicks FROM metric_snapshots WHERE tweet_id = 'tweet_url'",
                [],
                |r| r.get(0),
            )
            .unwrap();
        assert_eq!(clicks, Some(42));
    }

    #[test]
    fn metric_snapshot_persists_url_clicks_null_when_none() {
        let store = test_store();
        store
            .log_post("tweet_null", "no link", "text", None, None, None, None, None)
            .unwrap();
        store
            .log_metric_snapshot("tweet_null", 1, 0, 0, 10, 0, 0, 0, 5, None)
            .unwrap();
        let clicks: Option<i64> = store
            .conn
            .query_row(
                "SELECT url_clicks FROM metric_snapshots WHERE tweet_id = 'tweet_null'",
                [],
                |r| r.get(0),
            )
            .unwrap();
        assert_eq!(clicks, None);
    }

    #[test]
    fn metric_snapshot_and_retrieval() {
        let store = test_store();
        store
            .log_post("tweet_metrics", "Test post", "opinion", None, None, None, None, None)
            .unwrap();
        store
            .log_metric_snapshot("tweet_metrics", 10, 5, 3, 1000, 2, 1, 50, 60, None)
            .unwrap();

        let posts = store.get_post_history(10).unwrap();
        assert_eq!(posts.len(), 1);
        let metrics = posts[0].latest_metrics.as_ref().unwrap();
        assert_eq!(metrics.likes, 10);
        assert_eq!(metrics.retweets, 5);
        assert_eq!(metrics.replies, 3);
        assert_eq!(metrics.impressions, 1000);
        assert_eq!(metrics.bookmarks, 2);
        assert!(metrics.engagement_rate > 0.0);
    }

    #[test]
    fn engagement_reciprocity_unknown_user() {
        let store = test_store();
        let info = store.get_engagement_reciprocity("nobody").unwrap();
        assert!(info.is_none());
    }

    #[test]
    fn update_timing_stats_empty_db() {
        let store = test_store();
        // Should not fail on empty database
        store.update_timing_stats().unwrap();
    }

    #[test]
    fn classify_reply_question() {
        assert_eq!(classify_reply("What do you think about this?"), ReplyStyle::Question);
    }

    #[test]
    fn classify_reply_counterpoint() {
        assert_eq!(classify_reply("However, the evidence suggests otherwise"), ReplyStyle::Counterpoint);
    }

    #[test]
    fn classify_reply_anecdote() {
        assert_eq!(classify_reply("I've been using this protocol for 6 months"), ReplyStyle::Anecdote);
    }

    #[test]
    fn classify_reply_humor() {
        assert_eq!(classify_reply("lol that's incredible"), ReplyStyle::Humor);
    }

    #[test]
    fn classify_reply_agreement_fallback() {
        assert_eq!(classify_reply("Great insight, totally agree"), ReplyStyle::Agreement);
    }

    #[test]
    fn record_published_post_works() {
        let store = test_store();
        store
            .record_published_post(
                "tweet_pub_001",
                "Test published post",
                "text",
                None,
                None,
                Some(75.0),
                Some(r#"{"score":75}"#),
                Some("sched_123"),
            )
            .unwrap();

        let posts = store.get_post_history(10).unwrap();
        assert_eq!(posts.len(), 1);
        assert_eq!(posts[0].tweet_id, "tweet_pub_001");
    }

    #[test]
    fn log_reply_with_style() {
        let store = test_store();
        let style = classify_reply("What makes you think that?");
        store
            .log_reply("target_001", None, Some("testuser"), Some(5000), "reply_001", Some(&style))
            .unwrap();

        let pending = store.get_pending_replies(24).unwrap();
        assert_eq!(pending.len(), 1);
        assert_eq!(pending[0].reply_tweet_id, "reply_001");
    }

    #[test]
    fn discovered_posts_upsert_and_query() {
        let store = test_store();
        let tweet = crate::providers::xapi::TweetData {
            id: "dp_123".into(),
            text: "Longevity research is the future".into(),
            author_id: Some("user1".into()),
            author_username: Some("testuser".into()),
            created_at: Some("2026-01-01T00:00:00Z".into()),
            conversation_id: None,
            referenced_tweets: None,
            public_metrics: Some(crate::providers::xapi::TweetMetrics {
                like_count: 42,
                retweet_count: 5,
                reply_count: 3,
                impression_count: 2000,
                bookmark_count: 1,
            }),
            author_followers: Some(500),
            media_urls: vec![],
        };
        // First insert
        store.record_discovered_post("search", &tweet).unwrap();
        assert_eq!(store.discovered_posts_count().unwrap(), 1);

        // Upsert: same tweet, different source — updates last_source, preserves first_source
        store.record_discovered_post("timeline", &tweet).unwrap();
        assert_eq!(store.discovered_posts_count().unwrap(), 1);
        let last: String = store.conn.query_row(
            "SELECT last_source FROM discovered_posts WHERE tweet_id = 'dp_123'", [], |r| r.get(0)
        ).unwrap();
        assert_eq!(last, "timeline");
        let first: String = store.conn.query_row(
            "SELECT first_source FROM discovered_posts WHERE tweet_id = 'dp_123'", [], |r| r.get(0)
        ).unwrap();
        assert_eq!(first, "search");

        // Query: by topic
        let results = store.query_discovered_posts(Some("longevity"), None, None, 10).unwrap();
        assert_eq!(results.len(), 1);
        assert_eq!(results[0].tweet_id, "dp_123");

        // Query: by min_likes
        let results = store.query_discovered_posts(None, None, Some(100), 10).unwrap();
        assert_eq!(results.len(), 0); // 42 < 100
        let results = store.query_discovered_posts(None, None, Some(10), 10).unwrap();
        assert_eq!(results.len(), 1);

        // Query: by author
        let results = store.query_discovered_posts(None, Some("testuser"), None, 10).unwrap();
        assert_eq!(results.len(), 1);
    }

    // -- hot reply targets (items D + E) -------------------------------------

    /// Helper: log an outgoing reply-post + the matching engagement_action.
    /// Mirrors the production flow where `record_published_post` writes the post row.
    fn log_outgoing_reply(
        store: &IntelStore,
        target_tweet_id: &str,
        target_user_id: Option<&str>,
        target_username: &str,
        target_followers: i64,
        reply_tweet_id: &str,
    ) {
        store
            .log_post(reply_tweet_id, "reply body", "text", Some(target_tweet_id), None, None, None, None)
            .unwrap();
        store
            .log_reply(
                target_tweet_id,
                target_user_id,
                Some(target_username),
                Some(target_followers),
                reply_tweet_id,
                None,
            )
            .unwrap();
    }

    #[test]
    fn find_hot_reply_targets_returns_high_impression_reply() {
        let store = test_store();
        log_outgoing_reply(&store, "target_tweet_1", Some("uid_42"), "hottarget", 5000, "my_reply_1");
        store
            .log_metric_snapshot("my_reply_1", 10, 2, 1, 500, 0, 0, 3, 15, None)
            .unwrap();

        let promoted = store
            .find_hot_reply_targets(100, 1, 1_000, 24 * 14)
            .unwrap();
        assert_eq!(promoted.len(), 1);
        assert_eq!(promoted[0].username, "hottarget");
        assert_eq!(promoted[0].user_id.as_deref(), Some("uid_42"));
        assert_eq!(promoted[0].target_followers, 5000);
    }

    #[test]
    fn find_hot_reply_targets_skips_low_follower_targets() {
        let store = test_store();
        log_outgoing_reply(&store, "t1", None, "smallfry", 500, "r1");
        store
            .log_metric_snapshot("r1", 0, 0, 0, 1000, 0, 0, 5, 10, None)
            .unwrap();

        let promoted = store
            .find_hot_reply_targets(100, 1, 1_000, 24 * 14)
            .unwrap();
        assert!(promoted.is_empty(), "should filter target below min_target_followers");
    }

    #[test]
    fn find_hot_reply_targets_skips_already_watchlisted() {
        let store = test_store();
        store
            .add_watchlist("alreadyhere", Some("uid_1"), None, 10_000)
            .unwrap();
        log_outgoing_reply(&store, "t1", Some("uid_1"), "alreadyhere", 10_000, "r1");
        store
            .log_metric_snapshot("r1", 0, 0, 0, 1000, 0, 0, 5, 10, None)
            .unwrap();

        let promoted = store
            .find_hot_reply_targets(100, 1, 1_000, 24 * 14)
            .unwrap();
        assert!(promoted.is_empty(), "should not re-promote watchlist members");
    }

    #[test]
    fn find_hot_reply_targets_promotes_on_profile_clicks_alone() {
        let store = test_store();
        log_outgoing_reply(&store, "t1", None, "clickytarget", 2000, "r1");
        // Low impressions but 2 profile_clicks
        store
            .log_metric_snapshot("r1", 0, 0, 0, 10, 0, 0, 2, 5, None)
            .unwrap();

        let promoted = store
            .find_hot_reply_targets(100, 1, 1_000, 24 * 14)
            .unwrap();
        assert_eq!(promoted.len(), 1);
        assert_eq!(promoted[0].username, "clickytarget");
    }

    #[test]
    fn find_hot_reply_targets_promotes_on_reply_back() {
        let store = test_store();
        log_outgoing_reply(&store, "t1", None, "replier", 5000, "r1");
        store
            .log_metric_snapshot("r1", 0, 0, 0, 10, 0, 0, 0, 3, None)
            .unwrap();
        let action_id: i64 = store
            .conn
            .query_row(
                "SELECT id FROM engagement_actions WHERE reply_tweet_id = 'r1'",
                [],
                |r| r.get(0),
            )
            .unwrap();
        store.set_reply_back(action_id, true).unwrap();

        let promoted = store
            .find_hot_reply_targets(100, 1, 1_000, 24 * 14)
            .unwrap();
        assert_eq!(promoted.len(), 1);
        assert_eq!(promoted[0].username, "replier");
    }

    #[test]
    fn find_hot_reply_targets_skips_stale_replies() {
        let store = test_store();
        store
            .log_post("r1", "old reply body", "text", Some("t1"), None, None, None, None)
            .unwrap();
        let old_ts = chrono::Utc::now().timestamp() - 30 * 24 * 3600;
        store
            .conn
            .execute(
                "INSERT INTO engagement_actions
                    (action_type, target_tweet_id, target_user_id, target_username,
                     performed_at, target_followers, reply_tweet_id, reply_style)
                 VALUES ('reply', 't1', NULL, LOWER('oldtarget'), ?1, 5000, 'r1', NULL)",
                params![old_ts],
            )
            .unwrap();
        store
            .log_metric_snapshot("r1", 0, 0, 0, 10_000, 0, 0, 10, 30, None)
            .unwrap();

        let promoted = store
            .find_hot_reply_targets(100, 1, 1_000, 24 * 14)
            .unwrap();
        assert!(promoted.is_empty(), "should skip replies older than max_age_hours");
    }

    #[test]
    fn rank_hot_reply_targets_aggregates_by_username() {
        let store = test_store();
        log_outgoing_reply(&store, "t1", None, "target_a", 3000, "r1");
        store
            .log_metric_snapshot("r1", 0, 0, 0, 100, 0, 0, 1, 5, None)
            .unwrap();
        log_outgoing_reply(&store, "t2", None, "target_a", 3000, "r2");
        store
            .log_metric_snapshot("r2", 0, 0, 0, 300, 0, 0, 3, 5, None)
            .unwrap();

        let ranked = store.rank_hot_reply_targets(7, 1, 0.0, 0.0).unwrap();
        assert_eq!(ranked.len(), 1);
        assert_eq!(ranked[0].username, "target_a");
        assert_eq!(ranked[0].sample_count, 2);
        assert!((ranked[0].avg_impressions - 200.0).abs() < 0.01);
        assert!((ranked[0].avg_profile_clicks - 2.0).abs() < 0.01);
    }

    #[test]
    fn rank_hot_reply_targets_respects_min_samples() {
        let store = test_store();
        log_outgoing_reply(&store, "t1", None, "target_a", 3000, "r1");
        store
            .log_metric_snapshot("r1", 0, 0, 0, 1000, 0, 0, 5, 5, None)
            .unwrap();

        let ranked = store.rank_hot_reply_targets(7, 2, 0.0, 0.0).unwrap();
        assert!(ranked.is_empty(), "single sample should be filtered by min_samples=2");
    }

    #[test]
    fn rank_hot_reply_targets_computes_reply_back_rate() {
        let store = test_store();
        log_outgoing_reply(&store, "t1", None, "target_a", 3000, "r1");
        store
            .log_metric_snapshot("r1", 0, 0, 0, 100, 0, 0, 0, 5, None)
            .unwrap();
        let id1: i64 = store
            .conn
            .query_row(
                "SELECT id FROM engagement_actions WHERE reply_tweet_id = 'r1'",
                [],
                |r| r.get(0),
            )
            .unwrap();
        store.set_reply_back(id1, true).unwrap();

        log_outgoing_reply(&store, "t2", None, "target_a", 3000, "r2");
        store
            .log_metric_snapshot("r2", 0, 0, 0, 100, 0, 0, 0, 5, None)
            .unwrap();
        let id2: i64 = store
            .conn
            .query_row(
                "SELECT id FROM engagement_actions WHERE reply_tweet_id = 'r2'",
                [],
                |r| r.get(0),
            )
            .unwrap();
        store.set_reply_back(id2, false).unwrap();

        let ranked = store.rank_hot_reply_targets(7, 1, 0.0, 0.0).unwrap();
        assert_eq!(ranked.len(), 1);
        assert!((ranked[0].reply_back_rate - 0.5).abs() < 0.01);
    }

    #[test]
    fn hot_reply_targets_end_to_end_promotion_loop() {
        let store = test_store();
        log_outgoing_reply(&store, "t1", Some("uid_new"), "newhot", 5000, "r1");
        store
            .log_metric_snapshot("r1", 0, 0, 0, 500, 0, 0, 3, 15, None)
            .unwrap();

        let hot = store
            .find_hot_reply_targets(100, 1, 1_000, 24 * 14)
            .unwrap();
        assert_eq!(hot.len(), 1);

        for row in &hot {
            store
                .add_watchlist(&row.username, row.user_id.as_deref(), None, row.target_followers)
                .unwrap();
        }

        let wl = store.list_watchlist().unwrap();
        assert_eq!(wl.len(), 1);
        assert_eq!(wl[0].username, "newhot");
        assert_eq!(wl[0].user_id.as_deref(), Some("uid_new"));
        assert_eq!(wl[0].followers, 5000);
        assert!(wl[0].topic.is_none(), "auto-promotion should preserve NULL topic");

        // Second call should skip because target is now watchlisted
        let hot_again = store
            .find_hot_reply_targets(100, 1, 1_000, 24 * 14)
            .unwrap();
        assert!(hot_again.is_empty());
    }

    #[test]
    fn rank_hot_reply_targets_applies_days_filter() {
        let store = test_store();
        log_outgoing_reply(&store, "t1", None, "fresh_target", 3000, "r1");
        store
            .log_metric_snapshot("r1", 0, 0, 0, 1000, 0, 0, 5, 5, None)
            .unwrap();
        store
            .log_post("r2", "stale reply body", "text", Some("t2"), None, None, None, None)
            .unwrap();
        let old_ts = chrono::Utc::now().timestamp() - 20 * 24 * 3600;
        store
            .conn
            .execute(
                "INSERT INTO engagement_actions
                    (action_type, target_tweet_id, target_user_id, target_username,
                     performed_at, target_followers, reply_tweet_id, reply_style)
                 VALUES ('reply', 't2', NULL, LOWER('stale_target'), ?1, 3000, 'r2', NULL)",
                params![old_ts],
            )
            .unwrap();
        store
            .log_metric_snapshot("r2", 0, 0, 0, 1000, 0, 0, 5, 5, None)
            .unwrap();

        let ranked = store.rank_hot_reply_targets(7, 1, 0.0, 0.0).unwrap();
        assert_eq!(ranked.len(), 1);
        assert_eq!(ranked[0].username, "fresh_target");
    }
}