allowthem-saas 0.0.9

SaaS multi-tenancy layer for allowthem
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
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
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
use chrono::{DateTime, Utc};
use serde::Serialize;
use sqlx::{FromRow, Row, SqlitePool};
use uuid::Uuid;

use allowthem_core::error::AuthError;

use crate::cache::TenantMeta;
use crate::error::SaasError;
use crate::tenants::{MemberId, Tenant, TenantId, TenantMember, TenantPlan, TenantStatus};

#[derive(Debug, Clone, Serialize, sqlx::FromRow)]
pub struct TenantUsage {
    pub period: String,
    pub mau_count: i64,
    pub limit_reached_at: Option<DateTime<Utc>>,
    pub notified_at: Option<DateTime<Utc>>,
}

/// Role of a dashboard user inside a tenant. Mirrors `tenant_members.role`.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, serde::Deserialize, sqlx::Type)]
#[sqlx(rename_all = "lowercase")]
#[serde(rename_all = "lowercase")]
pub enum TenantRole {
    Owner,
    Admin,
    Viewer,
}

impl TenantRole {
    pub fn is_owner(&self) -> bool {
        matches!(self, TenantRole::Owner)
    }

    pub fn is_admin_or_owner(&self) -> bool {
        matches!(self, TenantRole::Owner | TenantRole::Admin)
    }

    /// SQL-side spelling. Matches the `tenant_members.role` CHECK.
    pub fn as_str(&self) -> &'static str {
        match self {
            TenantRole::Owner => "owner",
            TenantRole::Admin => "admin",
            TenantRole::Viewer => "viewer",
        }
    }
}

impl std::str::FromStr for TenantRole {
    type Err = SaasError;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        match s {
            "owner" => Ok(TenantRole::Owner),
            "admin" => Ok(TenantRole::Admin),
            "viewer" => Ok(TenantRole::Viewer),
            other => Err(SaasError::InvalidRole(other.to_owned())),
        }
    }
}

pub struct ControlDb {
    pool: SqlitePool,
}

impl ControlDb {
    pub async fn new(pool: SqlitePool) -> Result<Self, AuthError> {
        sqlx::migrate!("./migrations")
            .run(&pool)
            .await
            .map_err(sqlx::Error::from)?;
        Ok(Self { pool })
    }

    pub fn pool(&self) -> &SqlitePool {
        &self.pool
    }

    pub async fn tenant_meta_by_slug(&self, slug: &str) -> Result<Option<TenantMeta>, SaasError> {
        let row = sqlx::query("SELECT id, status, plan_id FROM tenants WHERE slug = ?1")
            .bind(slug)
            .fetch_optional(&self.pool)
            .await?;

        let Some(row) = row else { return Ok(None) };

        let id_bytes: Vec<u8> = row.try_get("id")?;
        let status: TenantStatus = row.try_get("status")?;
        let plan_id: Vec<u8> = row.try_get("plan_id")?;
        let id = Uuid::from_slice(&id_bytes).map_err(|_| SaasError::TenantNotFound)?;

        Ok(Some(TenantMeta {
            id: TenantId::from(id),
            status,
            plan_id,
        }))
    }

    /// Returns the `(Tenant, role)` pairs for every tenant the email has
    /// accepted membership in. Sorted by `t.name ASC`, with deleted tenants
    /// excluded. Used by the dashboard workspace switcher.
    pub async fn tenants_for_member(
        &self,
        email: &str,
    ) -> Result<Vec<(Tenant, TenantRole)>, SaasError> {
        let rows = sqlx::query(
            "SELECT t.id, t.name, t.slug, t.owner_email, t.plan_id, t.status, \
                    t.db_path, t.last_seen_at, t.created_at, t.updated_at, m.role \
             FROM tenants t \
             JOIN tenant_members m ON m.tenant_id = t.id \
             WHERE m.email = ?1 AND m.accepted_at IS NOT NULL \
               AND t.status != 'deleted' \
             ORDER BY t.name ASC",
        )
        .bind(email)
        .fetch_all(&self.pool)
        .await?;

        let mut out = Vec::with_capacity(rows.len());
        for row in rows {
            let tenant = Tenant::from_row(&row)?;
            let role: TenantRole = row.try_get("role")?;
            out.push((tenant, role));
        }
        Ok(out)
    }

    /// Returns the role the given email has on the tenant, or `None` if the
    /// user is not an accepted member. Used by `RequireTenantMember` /
    /// `RequireTenantAdmin` / `RequireTenantOwner`.
    pub async fn member_role(
        &self,
        tenant_id: &TenantId,
        email: &str,
    ) -> Result<Option<TenantRole>, SaasError> {
        let row: Option<(TenantRole,)> = sqlx::query_as(
            "SELECT role FROM tenant_members \
             WHERE tenant_id = ?1 AND email = ?2 AND accepted_at IS NOT NULL",
        )
        .bind(tenant_id.as_bytes())
        .bind(email)
        .fetch_optional(&self.pool)
        .await?;
        Ok(row.map(|(r,)| r))
    }

    pub async fn most_recently_seen_tenants(&self, count: i64) -> Result<Vec<TenantId>, SaasError> {
        let rows = sqlx::query(
            "SELECT id FROM tenants \
             WHERE status = 'active' AND last_seen_at IS NOT NULL \
             ORDER BY last_seen_at DESC LIMIT ?1",
        )
        .bind(count)
        .fetch_all(&self.pool)
        .await?;

        let mut result = Vec::with_capacity(rows.len());
        for row in rows {
            let bytes: Vec<u8> = row.try_get("id")?;
            match Uuid::from_slice(&bytes) {
                Ok(uuid) => result.push(TenantId::from(uuid)),
                Err(_) => {
                    tracing::warn!("skipping tenant with undecodable UUID in most_recently_seen");
                }
            }
        }
        Ok(result)
    }

    pub async fn touch_last_seen(&self, tenant_id: &TenantId) -> Result<(), SaasError> {
        sqlx::query("UPDATE tenants SET last_seen_at = datetime('now') WHERE id = ?1")
            .bind(tenant_id.as_bytes())
            .execute(&self.pool)
            .await?;
        Ok(())
    }

    pub async fn usage_for_tenant(
        &self,
        tenant_id: &TenantId,
    ) -> Result<Vec<TenantUsage>, SaasError> {
        let rows = sqlx::query_as::<_, TenantUsage>(
            "SELECT period, mau_count, limit_reached_at, notified_at \
             FROM tenant_usage \
             WHERE tenant_id = ?1 \
             ORDER BY period DESC",
        )
        .bind(tenant_id.as_bytes())
        .fetch_all(&self.pool)
        .await?;
        Ok(rows)
    }

    /// Append an entry to `control_audit_events`.
    ///
    /// Call sites are fire-and-forget: log the error and continue. An
    /// unlogged audit event is a nuisance, never a correctness bug.
    /// `context` is serialised to a JSON string before persistence so the
    /// callers can pass arbitrary `serde_json::Value` shapes.
    pub async fn log_control_audit(
        &self,
        actor: &str,
        action: &str,
        tenant_id: Option<&TenantId>,
        context: &serde_json::Value,
    ) -> Result<(), SaasError> {
        let id = Uuid::now_v7();
        let context_str = context.to_string();
        sqlx::query(
            "INSERT INTO control_audit_events (id, actor, action, tenant_id, context) \
             VALUES (?1, ?2, ?3, ?4, ?5)",
        )
        .bind(id.as_bytes().as_slice())
        .bind(actor)
        .bind(action)
        .bind(tenant_id.map(|t| t.as_bytes()))
        .bind(&context_str)
        .execute(&self.pool)
        .await?;
        Ok(())
    }
}

// ---------------------------------------------------------------------------
// tenant_members CRUD — read paths and simple writes (invite, accept).
// Last-owner-invariant writes (update_member_role, remove_member) live in
// the next impl block so the SQL there gets focused review.
// ---------------------------------------------------------------------------

impl ControlDb {
    /// All members of a tenant. Pending invites (`accepted_at IS NULL`)
    /// surface first, then accepted rows ordered by `invited_at`.
    pub async fn list_tenant_members(
        &self,
        tenant_id: &TenantId,
    ) -> Result<Vec<TenantMember>, SaasError> {
        let rows = sqlx::query_as::<_, TenantMember>(
            "SELECT id, tenant_id, email, role, invited_at, accepted_at, \
                    invite_token_expires_at \
             FROM tenant_members \
             WHERE tenant_id = ?1 \
             ORDER BY (accepted_at IS NULL) DESC, invited_at ASC",
        )
        .bind(tenant_id.as_bytes())
        .fetch_all(&self.pool)
        .await?;
        Ok(rows)
    }

    pub async fn get_tenant_member(
        &self,
        member_id: &MemberId,
    ) -> Result<Option<TenantMember>, SaasError> {
        let row = sqlx::query_as::<_, TenantMember>(
            "SELECT id, tenant_id, email, role, invited_at, accepted_at, \
                    invite_token_expires_at \
             FROM tenant_members WHERE id = ?1",
        )
        .bind(member_id.as_bytes())
        .fetch_optional(&self.pool)
        .await?;
        Ok(row)
    }

    /// Insert a pending member row. Returns the new [`MemberId`]. The
    /// `(tenant_id, email)` UNIQUE constraint maps to
    /// [`SaasError::MemberAlreadyExists`] on collision.
    pub async fn invite_member(
        &self,
        tenant_id: &TenantId,
        email: &str,
        role: TenantRole,
        token_hash: &[u8],
        expires_at: i64,
    ) -> Result<MemberId, SaasError> {
        let id = MemberId::new();
        let result = sqlx::query(
            "INSERT INTO tenant_members \
                 (id, tenant_id, email, role, invite_token_hash, invite_token_expires_at) \
             VALUES (?1, ?2, ?3, ?4, ?5, ?6)",
        )
        .bind(id.as_bytes())
        .bind(tenant_id.as_bytes())
        .bind(email)
        .bind(role.as_str())
        .bind(token_hash)
        .bind(expires_at)
        .execute(&self.pool)
        .await;
        match result {
            Ok(_) => Ok(id),
            Err(sqlx::Error::Database(db_err)) if db_err.is_unique_violation() => {
                Err(SaasError::MemberAlreadyExists)
            }
            Err(e) => Err(SaasError::Db(e)),
        }
    }

    /// Look up a pending invite by `token_hash` (read-only — does not
    /// consume the token). Used by the GET `/invite/{token}` page so the
    /// invitee can preview the tenant + role before posting back.
    pub async fn find_pending_invite_by_hash(
        &self,
        token_hash: &[u8],
    ) -> Result<Option<TenantMember>, SaasError> {
        let now_secs = chrono::Utc::now().timestamp();
        let row = sqlx::query_as::<_, TenantMember>(
            "SELECT id, tenant_id, email, role, invited_at, accepted_at, \
                    invite_token_expires_at \
             FROM tenant_members \
             WHERE invite_token_hash = ?1 \
               AND accepted_at IS NULL \
               AND (invite_token_expires_at IS NULL OR invite_token_expires_at > ?2)",
        )
        .bind(token_hash)
        .bind(now_secs)
        .fetch_optional(&self.pool)
        .await?;
        Ok(row)
    }

    /// Validate a pending invite by `token_hash`, mark it accepted, and
    /// return the row as it was *before* the UPDATE so the caller has the
    /// pre-clear `email` + `role`. Counterintuitive naming, but the
    /// alternative (return the post-UPDATE row with NULL token columns)
    /// is strictly worse for callers.
    pub async fn accept_invite(&self, token_hash: &[u8]) -> Result<TenantMember, SaasError> {
        let now_secs = chrono::Utc::now().timestamp();
        let mut tx = self.pool.begin().await?;
        let row = sqlx::query_as::<_, TenantMember>(
            "SELECT id, tenant_id, email, role, invited_at, accepted_at, \
                    invite_token_expires_at \
             FROM tenant_members \
             WHERE invite_token_hash = ?1 \
               AND accepted_at IS NULL \
               AND (invite_token_expires_at IS NULL OR invite_token_expires_at > ?2)",
        )
        .bind(token_hash)
        .bind(now_secs)
        .fetch_optional(&mut *tx)
        .await?
        .ok_or(SaasError::InviteNotFoundOrExpired)?;

        sqlx::query(
            "UPDATE tenant_members \
             SET accepted_at = strftime('%Y-%m-%dT%H:%M:%fZ', 'now'), \
                 invite_token_hash = NULL, \
                 invite_token_expires_at = NULL \
             WHERE id = ?1",
        )
        .bind(&row.id)
        .execute(&mut *tx)
        .await?;
        tx.commit().await?;
        Ok(row)
    }
}

// ---------------------------------------------------------------------------
// tenant_members CRUD — last-owner-invariant writes.
//
// The invariant: a tenant must always have at least one accepted owner.
// We enforce it inside an explicit transaction (count owners, decide,
// then mutate) rather than relying on a single-statement subquery,
// which would be brittle to read and to test.
// ---------------------------------------------------------------------------

impl ControlDb {
    /// Number of accepted owners for the given tenant.
    pub async fn count_owners(&self, tenant_id: &TenantId) -> Result<i64, SaasError> {
        let n: i64 = sqlx::query_scalar(
            "SELECT COUNT(*) FROM tenant_members \
             WHERE tenant_id = ?1 AND role = 'owner' AND accepted_at IS NOT NULL",
        )
        .bind(tenant_id.as_bytes())
        .fetch_one(&self.pool)
        .await?;
        Ok(n)
    }

    /// Update a member's role. Rejects with [`SaasError::CannotDemoteLastOwner`]
    /// if this would leave the tenant with zero accepted owners.
    pub async fn update_member_role(
        &self,
        member_id: &MemberId,
        new_role: TenantRole,
    ) -> Result<(), SaasError> {
        let mut tx = self.pool.begin().await?;
        let target: Option<(Vec<u8>, String, Option<DateTime<Utc>>)> =
            sqlx::query_as("SELECT tenant_id, role, accepted_at FROM tenant_members WHERE id = ?1")
                .bind(member_id.as_bytes())
                .fetch_optional(&mut *tx)
                .await?;
        let (tenant_blob, current_role, accepted_at) = target.ok_or(SaasError::MemberNotFound)?;

        let demoting_owner = current_role == "owner" && !matches!(new_role, TenantRole::Owner);
        if demoting_owner && accepted_at.is_some() {
            let n: i64 = sqlx::query_scalar(
                "SELECT COUNT(*) FROM tenant_members \
                 WHERE tenant_id = ?1 AND role = 'owner' AND accepted_at IS NOT NULL",
            )
            .bind(&tenant_blob)
            .fetch_one(&mut *tx)
            .await?;
            if n <= 1 {
                return Err(SaasError::CannotDemoteLastOwner);
            }
        }

        sqlx::query("UPDATE tenant_members SET role = ?1 WHERE id = ?2")
            .bind(new_role.as_str())
            .bind(member_id.as_bytes())
            .execute(&mut *tx)
            .await?;
        tx.commit().await?;
        Ok(())
    }

    /// Remove a member. Rejects with [`SaasError::CannotRemoveLastOwner`]
    /// if removing this row would leave the tenant with zero accepted owners.
    pub async fn remove_member(&self, member_id: &MemberId) -> Result<(), SaasError> {
        let mut tx = self.pool.begin().await?;
        let target: Option<(Vec<u8>, String, Option<DateTime<Utc>>)> =
            sqlx::query_as("SELECT tenant_id, role, accepted_at FROM tenant_members WHERE id = ?1")
                .bind(member_id.as_bytes())
                .fetch_optional(&mut *tx)
                .await?;
        let (tenant_blob, role, accepted_at) = target.ok_or(SaasError::MemberNotFound)?;

        if role == "owner" && accepted_at.is_some() {
            let n: i64 = sqlx::query_scalar(
                "SELECT COUNT(*) FROM tenant_members \
                 WHERE tenant_id = ?1 AND role = 'owner' AND accepted_at IS NOT NULL",
            )
            .bind(&tenant_blob)
            .fetch_one(&mut *tx)
            .await?;
            if n <= 1 {
                return Err(SaasError::CannotRemoveLastOwner);
            }
        }

        sqlx::query("DELETE FROM tenant_members WHERE id = ?1")
            .bind(member_id.as_bytes())
            .execute(&mut *tx)
            .await?;
        tx.commit().await?;
        Ok(())
    }
}

// ---------------------------------------------------------------------------
// Super-admin control-plane types (99c.6 Step 1).
// ---------------------------------------------------------------------------

/// One row returned by [`ControlDb::search_tenants`].
#[derive(Debug, Clone, Serialize, sqlx::FromRow)]
pub struct TenantOverviewRow {
    pub id: Vec<u8>,
    pub name: String,
    pub slug: String,
    pub owner_email: String,
    pub status: TenantStatus,
    pub plan_name: String,
    pub mau_count: i64,
    pub created_at: DateTime<Utc>,
    pub last_seen_at: Option<DateTime<Utc>>,
}

/// Column to sort [`ControlDb::search_tenants`] results by.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum TenantSortCol {
    #[default]
    Name,
    Slug,
    Status,
    Plan,
    Mau,
    CreatedAt,
    LastSeenAt,
}

impl TenantSortCol {
    fn as_sql(&self) -> &'static str {
        match self {
            Self::Name => "t.name",
            Self::Slug => "t.slug",
            Self::Status => "t.status",
            Self::Plan => "p.name",
            Self::Mau => "COALESCE(tu.mau_count, 0)",
            Self::CreatedAt => "t.created_at",
            // NULLs sort last regardless of direction.
            Self::LastSeenAt => "COALESCE(t.last_seen_at, '')",
        }
    }
}

/// Sort direction for [`ControlDb::search_tenants`].
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum SortDir {
    #[default]
    Asc,
    Desc,
}

impl SortDir {
    fn as_sql(&self) -> &'static str {
        match self {
            Self::Asc => "ASC",
            Self::Desc => "DESC",
        }
    }
}

/// Parameters for [`ControlDb::search_tenants`].
pub struct SearchTenantsParams<'a> {
    /// Optional search term matched against `name`, `slug`, and `owner_email`.
    pub query: Option<&'a str>,
    pub status: Option<TenantStatus>,
    /// Filter by raw BLOB `plan_id`. Computed by the handler from a plan name.
    pub plan_id: Option<&'a [u8]>,
    /// `%Y-%m` period string (UTC) used for the MAU LEFT JOIN. Computed by the
    /// handler; not embedded in the query to prevent SQL injection.
    pub current_period: &'a str,
    pub sort_col: TenantSortCol,
    pub sort_dir: SortDir,
    pub limit: u32,
    pub offset: u32,
}

/// Result of [`ControlDb::search_tenants`].
pub struct SearchTenantsResult {
    pub rows: Vec<TenantOverviewRow>,
    pub total: u32,
}

/// Per-period usage aggregate returned by [`ControlDb::aggregate_usage_for_period`]
/// and [`ControlDb::aggregate_usage_history`].
#[derive(Debug, Clone, Serialize)]
pub struct PeriodAggregate {
    pub period: String,
    pub total_mau: i64,
    pub active_tenants: i64,
}

// ---------------------------------------------------------------------------
// Super-admin queries — counts, search, aggregates, mutations (99c.6 Step 1).
// ---------------------------------------------------------------------------

impl ControlDb {
    /// Count tenants in a given status (active / suspended / deleted).
    pub async fn count_tenants_by_status(&self, status: TenantStatus) -> Result<i64, SaasError> {
        let n: i64 = sqlx::query_scalar("SELECT COUNT(*) FROM tenants WHERE status = ?1")
            .bind(status)
            .fetch_one(&self.pool)
            .await?;
        Ok(n)
    }

    /// Count tenants whose `created_at` is >= `since`.
    pub async fn count_tenants_created_since(
        &self,
        since: DateTime<Utc>,
    ) -> Result<i64, SaasError> {
        let n: i64 = sqlx::query_scalar("SELECT COUNT(*) FROM tenants WHERE created_at >= ?1")
            .bind(since)
            .fetch_one(&self.pool)
            .await?;
        Ok(n)
    }

    /// Return `(plan_name, tenant_count)` pairs ordered by count descending.
    ///
    /// Deleted tenants are excluded. Plans with no tenants still appear with 0.
    pub async fn count_tenants_grouped_by_plan(&self) -> Result<Vec<(String, i64)>, SaasError> {
        let rows: Vec<(String, i64)> = sqlx::query_as(
            "SELECT p.name, COUNT(t.id) AS count \
             FROM tenant_plans p \
             LEFT JOIN tenants t ON t.plan_id = p.id AND t.status != 'deleted' \
             GROUP BY p.id, p.name \
             ORDER BY count DESC",
        )
        .fetch_all(&self.pool)
        .await?;
        Ok(rows)
    }

    /// Paginated, sortable, filterable tenant search.
    ///
    /// Mirrors the `Box::leak`+dynamic-WHERE pattern from `Db::search_users`.
    /// `plan_id` (BLOB) and `current_period` are bound separately from the
    /// string-typed binds to preserve positional correctness.
    pub async fn search_tenants(
        &self,
        p: SearchTenantsParams<'_>,
    ) -> Result<SearchTenantsResult, SaasError> {
        let mut where_clauses: Vec<String> = Vec::new();
        let mut string_binds: Vec<String> = Vec::new();

        if let Some(q) = p.query {
            let trimmed = q.trim();
            if !trimmed.is_empty() {
                let esc = trimmed
                    .replace('\\', "\\\\")
                    .replace('%', "\\%")
                    .replace('_', "\\_");
                let pat = format!("%{esc}%");
                where_clauses.push(
                    "(t.name LIKE ? ESCAPE '\\' \
                     OR t.slug LIKE ? ESCAPE '\\' \
                     OR t.owner_email LIKE ? ESCAPE '\\')"
                        .into(),
                );
                string_binds.push(pat.clone());
                string_binds.push(pat.clone());
                string_binds.push(pat);
            }
        }

        if let Some(s) = p.status {
            where_clauses.push("t.status = ?".into());
            let s_str = match s {
                TenantStatus::Active => "active",
                TenantStatus::Suspended => "suspended",
                TenantStatus::Deleted => "deleted",
            };
            string_binds.push(s_str.to_owned());
        }

        let has_plan_filter = p.plan_id.is_some();
        if has_plan_filter {
            where_clauses.push("t.plan_id = ?".into());
        }

        let where_sql = if where_clauses.is_empty() {
            String::new()
        } else {
            format!("WHERE {}", where_clauses.join(" AND "))
        };

        // Count query — no JOINs needed; filter columns are all on `tenants`.
        let count_sql: &'static str =
            Box::leak(format!("SELECT COUNT(*) FROM tenants t {where_sql}").into_boxed_str());
        let mut count_q = sqlx::query_scalar::<_, i64>(count_sql);
        for v in &string_binds {
            count_q = count_q.bind(v);
        }
        if let Some(pid) = p.plan_id {
            count_q = count_q.bind(pid);
        }
        let total = count_q.fetch_one(&self.pool).await? as u32;

        // Data query — LEFT JOIN usage for MAU; JOIN plans for plan name.
        // sort_col/sort_dir come from an enum allowlist — no injection risk.
        let sort_col_sql = p.sort_col.as_sql();
        let sort_dir_sql = p.sort_dir.as_sql();
        let data_sql: &'static str = Box::leak(
            format!(
                "SELECT t.id, t.name, t.slug, t.owner_email, t.status, \
                 t.created_at, t.last_seen_at, \
                 p.name AS plan_name, COALESCE(tu.mau_count, 0) AS mau_count \
                 FROM tenants t \
                 JOIN tenant_plans p ON p.id = t.plan_id \
                 LEFT JOIN tenant_usage tu \
                   ON tu.tenant_id = t.id AND tu.period = ? \
                 {where_sql} \
                 ORDER BY {sort_col_sql} {sort_dir_sql} \
                 LIMIT ? OFFSET ?"
            )
            .into_boxed_str(),
        );
        let mut data_q = sqlx::query_as::<_, TenantOverviewRow>(data_sql).bind(p.current_period);
        for v in &string_binds {
            data_q = data_q.bind(v);
        }
        if let Some(pid) = p.plan_id {
            data_q = data_q.bind(pid);
        }
        data_q = data_q.bind(p.limit).bind(p.offset);

        let rows = data_q.fetch_all(&self.pool).await?;

        Ok(SearchTenantsResult { rows, total })
    }

    /// Aggregate MAU + active-tenant count for a single period (e.g. `"2026-05"`).
    ///
    /// Returns zeros when no usage rows exist for the period.
    pub async fn aggregate_usage_for_period(
        &self,
        period: &str,
    ) -> Result<PeriodAggregate, SaasError> {
        let (total_mau, active_tenants): (i64, i64) = sqlx::query_as(
            "SELECT \
               COALESCE(SUM(mau_count), 0) AS total_mau, \
               COUNT(CASE WHEN mau_count > 0 THEN 1 END) AS active_tenants \
             FROM tenant_usage \
             WHERE period = ?1",
        )
        .bind(period)
        .fetch_one(&self.pool)
        .await?;

        Ok(PeriodAggregate {
            period: period.to_owned(),
            total_mau,
            active_tenants,
        })
    }

    /// Return the `n_months` most-recent per-period aggregates, newest first.
    pub async fn aggregate_usage_history(
        &self,
        n_months: u32,
    ) -> Result<Vec<PeriodAggregate>, SaasError> {
        let rows: Vec<(String, i64, i64)> = sqlx::query_as(
            "SELECT period, \
               SUM(mau_count) AS total_mau, \
               COUNT(CASE WHEN mau_count > 0 THEN 1 END) AS active_tenants \
             FROM tenant_usage \
             GROUP BY period \
             ORDER BY period DESC \
             LIMIT ?1",
        )
        .bind(n_months)
        .fetch_all(&self.pool)
        .await?;

        Ok(rows
            .into_iter()
            .map(|(period, total_mau, active_tenants)| PeriodAggregate {
                period,
                total_mau,
                active_tenants,
            })
            .collect())
    }

    /// Count non-deleted tenants that have not been seen since `before`
    /// (i.e. `last_seen_at` is NULL or earlier than the threshold).
    pub async fn count_dormant_tenants(&self, before: DateTime<Utc>) -> Result<i64, SaasError> {
        let n: i64 = sqlx::query_scalar(
            "SELECT COUNT(*) FROM tenants \
             WHERE status != 'deleted' \
               AND (last_seen_at IS NULL OR last_seen_at < ?1)",
        )
        .bind(before)
        .fetch_one(&self.pool)
        .await?;
        Ok(n)
    }

    /// Set a tenant's status. Bumps `updated_at`.
    pub async fn set_tenant_status(
        &self,
        tenant_id: &TenantId,
        status: TenantStatus,
    ) -> Result<(), SaasError> {
        sqlx::query(
            "UPDATE tenants \
             SET status = ?1, \
                 updated_at = strftime('%Y-%m-%dT%H:%M:%fZ', 'now') \
             WHERE id = ?2",
        )
        .bind(status)
        .bind(tenant_id.as_bytes())
        .execute(&self.pool)
        .await?;
        Ok(())
    }

    /// Set a tenant's plan. Bumps `updated_at`.
    pub async fn set_tenant_plan(
        &self,
        tenant_id: &TenantId,
        plan_id: &[u8],
    ) -> Result<(), SaasError> {
        sqlx::query(
            "UPDATE tenants \
             SET plan_id = ?1, \
                 updated_at = strftime('%Y-%m-%dT%H:%M:%fZ', 'now') \
             WHERE id = ?2",
        )
        .bind(plan_id)
        .bind(tenant_id.as_bytes())
        .execute(&self.pool)
        .await?;
        Ok(())
    }

    /// All available plans ordered by `price_cents ASC`.
    pub async fn list_plans(&self) -> Result<Vec<TenantPlan>, SaasError> {
        let rows = sqlx::query_as::<_, TenantPlan>(
            "SELECT id, name, mau_limit, price_cents, features \
             FROM tenant_plans \
             ORDER BY price_cents ASC",
        )
        .fetch_all(&self.pool)
        .await?;
        Ok(rows)
    }

    /// Look up a plan by its raw BLOB id. Returns `None` if not found.
    pub async fn get_plan_by_id(&self, plan_id: &[u8]) -> Result<Option<TenantPlan>, SaasError> {
        let row = sqlx::query_as::<_, TenantPlan>(
            "SELECT id, name, mau_limit, price_cents, features \
             FROM tenant_plans WHERE id = ?1",
        )
        .bind(plan_id)
        .fetch_optional(&self.pool)
        .await?;
        Ok(row)
    }
}

// ---------------------------------------------------------------------------
// tenant_domains CRUD
// ---------------------------------------------------------------------------

impl ControlDb {
    /// Insert a new domain row for the tenant with `status = pending_verification`.
    ///
    /// Maps a UNIQUE violation on `domain` to [`SaasError::DomainAlreadyExists`].
    pub async fn create_tenant_domain(
        &self,
        tenant_id: &crate::tenants::TenantId,
        domain: &str,
        dns_target: &str,
    ) -> Result<crate::domains::TenantDomain, SaasError> {
        use crate::domains::{DomainId, DomainStatus, TenantDomain};

        let id = DomainId::new();
        let result = sqlx::query(
            "INSERT INTO tenant_domains \
                 (id, tenant_id, domain, status, dns_target) \
             VALUES (?1, ?2, ?3, ?4, ?5)",
        )
        .bind(id.as_bytes())
        .bind(tenant_id.as_bytes())
        .bind(domain)
        .bind(DomainStatus::PendingVerification.as_str())
        .bind(dns_target)
        .execute(&self.pool)
        .await;

        match result {
            Ok(_) => {}
            Err(sqlx::Error::Database(db_err)) if db_err.is_unique_violation() => {
                return Err(SaasError::DomainAlreadyExists);
            }
            Err(e) => return Err(SaasError::Db(e)),
        }

        let row = sqlx::query_as::<_, TenantDomain>(
            "SELECT id, tenant_id, domain, status, dns_target, \
                    verified_at, cert_expires_at, last_error, created_at, updated_at \
             FROM tenant_domains WHERE id = ?1",
        )
        .bind(id.as_bytes())
        .fetch_one(&self.pool)
        .await?;
        Ok(row)
    }

    /// All domains for the given tenant, ordered newest-first.
    pub async fn list_tenant_domains(
        &self,
        tenant_id: &crate::tenants::TenantId,
    ) -> Result<Vec<crate::domains::TenantDomain>, SaasError> {
        use crate::domains::TenantDomain;

        let rows = sqlx::query_as::<_, TenantDomain>(
            "SELECT id, tenant_id, domain, status, dns_target, \
                    verified_at, cert_expires_at, last_error, created_at, updated_at \
             FROM tenant_domains \
             WHERE tenant_id = ?1 \
             ORDER BY created_at DESC, id DESC",
        )
        .bind(tenant_id.as_bytes())
        .fetch_all(&self.pool)
        .await?;
        Ok(rows)
    }

    /// Fetch a single domain row only if it belongs to `tenant_id`.
    ///
    /// Returns `None` when the domain does not exist or belongs to another
    /// tenant (caller should treat both as 404 at the API boundary).
    pub async fn get_tenant_domain_scoped(
        &self,
        domain_id: &crate::domains::DomainId,
        tenant_id: &crate::tenants::TenantId,
    ) -> Result<Option<crate::domains::TenantDomain>, SaasError> {
        use crate::domains::TenantDomain;

        let row = sqlx::query_as::<_, TenantDomain>(
            "SELECT id, tenant_id, domain, status, dns_target, \
                    verified_at, cert_expires_at, last_error, created_at, updated_at \
             FROM tenant_domains \
             WHERE id = ?1 AND tenant_id = ?2",
        )
        .bind(domain_id.as_bytes())
        .bind(tenant_id.as_bytes())
        .fetch_optional(&self.pool)
        .await?;
        Ok(row)
    }

    /// Update the status of a domain row, bumping `updated_at`.
    ///
    /// - `verified_at` is set only when transitioning to `Verified`.
    /// - `last_error` is cleared on `Verified` and set on `Failed`.
    pub async fn set_tenant_domain_status(
        &self,
        domain_id: &crate::domains::DomainId,
        status: crate::domains::DomainStatus,
        verified_at: Option<chrono::DateTime<chrono::Utc>>,
        last_error: Option<&str>,
    ) -> Result<(), SaasError> {
        sqlx::query(
            "UPDATE tenant_domains \
             SET status = ?1, \
                 verified_at = ?2, \
                 last_error = ?3, \
                 updated_at = strftime('%Y-%m-%dT%H:%M:%fZ', 'now') \
             WHERE id = ?4",
        )
        .bind(status.as_str())
        .bind(verified_at)
        .bind(last_error)
        .bind(domain_id.as_bytes())
        .execute(&self.pool)
        .await?;
        Ok(())
    }

    /// Delete a domain row only if it belongs to `tenant_id`.
    ///
    /// Returns `true` when a row was deleted, `false` when the domain did not
    /// exist or belonged to another tenant.
    pub async fn delete_tenant_domain_scoped(
        &self,
        domain_id: &crate::domains::DomainId,
        tenant_id: &crate::tenants::TenantId,
    ) -> Result<bool, SaasError> {
        let result = sqlx::query("DELETE FROM tenant_domains WHERE id = ?1 AND tenant_id = ?2")
            .bind(domain_id.as_bytes())
            .bind(tenant_id.as_bytes())
            .execute(&self.pool)
            .await?;
        Ok(result.rows_affected() > 0)
    }

    /// Find the tenant whose custom domain matches `domain` and whose status
    /// is `verified` or `active`. Used by 38y.2's router fallback.
    ///
    /// Input is lowercased before binding (DNS is case-insensitive; storage
    /// is always lowercase).
    pub async fn tenant_by_custom_domain(
        &self,
        domain: &str,
    ) -> Result<Option<crate::tenants::Tenant>, SaasError> {
        use crate::tenants::Tenant;

        let lower = domain.to_lowercase();
        let row = sqlx::query_as::<_, Tenant>(
            "SELECT t.id, t.name, t.slug, t.owner_email, t.plan_id, t.status, \
                    t.db_path, t.last_seen_at, t.created_at, t.updated_at \
             FROM tenants t \
             JOIN tenant_domains d ON d.tenant_id = t.id \
             WHERE d.domain = ?1 \
               AND d.status IN ('verified', 'active')",
        )
        .bind(&lower)
        .fetch_optional(&self.pool)
        .await?;
        Ok(row)
    }

    /// Rows eligible for the background verification sweep: those with status
    /// `pending_verification` or `failed`, ordered oldest-first, capped at
    /// `limit`.
    pub async fn list_domains_for_sweep(
        &self,
        limit: i64,
    ) -> Result<Vec<crate::domains::TenantDomain>, SaasError> {
        use crate::domains::TenantDomain;

        let rows = sqlx::query_as::<_, TenantDomain>(
            "SELECT id, tenant_id, domain, status, dns_target, \
                    verified_at, cert_expires_at, last_error, created_at, updated_at \
             FROM tenant_domains \
             WHERE status IN ('pending_verification', 'failed') \
             ORDER BY updated_at ASC \
             LIMIT ?1",
        )
        .bind(limit)
        .fetch_all(&self.pool)
        .await?;
        Ok(rows)
    }
}

#[cfg(test)]
pub(crate) mod tests {
    use super::*;
    use sqlx::Row;
    use std::str::FromStr;

    pub async fn test_pool() -> SqlitePool {
        let opts = sqlx::sqlite::SqliteConnectOptions::from_str("sqlite::memory:")
            .unwrap()
            .pragma("foreign_keys", "ON");
        SqlitePool::connect_with(opts).await.unwrap()
    }

    #[tokio::test]
    async fn control_db_runs_migrations() {
        let pool = test_pool().await;
        let db = ControlDb::new(pool).await;
        assert!(db.is_ok());
    }

    #[tokio::test]
    async fn log_control_audit_inserts_row() {
        let pool = test_pool().await;
        let db = ControlDb::new(pool).await.unwrap();
        db.log_control_audit(
            "owner@acme.com",
            "tenant.provisioned",
            None,
            &serde_json::json!({"slug": "acme"}),
        )
        .await
        .expect("log_control_audit");

        let count: i64 = sqlx::query_scalar("SELECT COUNT(*) FROM control_audit_events")
            .fetch_one(db.pool())
            .await
            .unwrap();
        assert_eq!(count, 1);

        let (actor, action, ctx): (String, String, String) =
            sqlx::query_as("SELECT actor, action, context FROM control_audit_events LIMIT 1")
                .fetch_one(db.pool())
                .await
                .unwrap();
        assert_eq!(actor, "owner@acme.com");
        assert_eq!(action, "tenant.provisioned");
        assert!(ctx.contains("acme"));
    }

    #[tokio::test]
    async fn tenant_slug_unique() {
        let pool = test_pool().await;
        let db = ControlDb::new(pool).await.unwrap();
        let row = sqlx::query("SELECT id FROM tenant_plans LIMIT 1")
            .fetch_one(db.pool())
            .await
            .unwrap();
        let plan_id: Vec<u8> = row.get("id");
        let id_a = uuid::Uuid::new_v4();
        let id_b = uuid::Uuid::new_v4();
        sqlx::query(
            "INSERT INTO tenants (id, name, slug, owner_email, plan_id, status, db_path) \
             VALUES (?, 'Acme', 'acme', 'a@a.com', ?, 'active', 'acme.db')",
        )
        .bind(id_a.as_bytes().as_ref())
        .bind(&plan_id)
        .execute(db.pool())
        .await
        .unwrap();
        let res = sqlx::query(
            "INSERT INTO tenants (id, name, slug, owner_email, plan_id, status, db_path) \
             VALUES (?, 'Acme 2', 'acme', 'b@b.com', ?, 'active', 'acme2.db')",
        )
        .bind(id_b.as_bytes().as_ref())
        .bind(&plan_id)
        .execute(db.pool())
        .await;
        assert!(res.is_err(), "duplicate slug should be rejected");
    }

    #[tokio::test]
    async fn tenant_status_check_rejects_invalid() {
        let pool = test_pool().await;
        let db = ControlDb::new(pool).await.unwrap();
        let row = sqlx::query("SELECT id FROM tenant_plans LIMIT 1")
            .fetch_one(db.pool())
            .await
            .unwrap();
        let plan_id: Vec<u8> = row.get("id");
        let id = uuid::Uuid::new_v4();
        let res = sqlx::query(
            "INSERT INTO tenants (id, name, slug, owner_email, plan_id, status, db_path) \
             VALUES (?, 'Bad', 'bad-status', 'c@c.com', ?, 'banned', 'bad.db')",
        )
        .bind(id.as_bytes().as_ref())
        .bind(&plan_id)
        .execute(db.pool())
        .await;
        assert!(res.is_err(), "invalid status should be rejected by CHECK");
    }

    #[tokio::test]
    async fn member_role_check_rejects_invalid() {
        let pool = test_pool().await;
        let db = ControlDb::new(pool).await.unwrap();
        let row = sqlx::query("SELECT id FROM tenant_plans LIMIT 1")
            .fetch_one(db.pool())
            .await
            .unwrap();
        let plan_id: Vec<u8> = row.get("id");
        let tenant_id = uuid::Uuid::new_v4();
        sqlx::query(
            "INSERT INTO tenants (id, name, slug, owner_email, plan_id, status, db_path) \
             VALUES (?, 'Role Test', 'role-test', 'd@d.com', ?, 'active', 'rtest.db')",
        )
        .bind(tenant_id.as_bytes().as_ref())
        .bind(&plan_id)
        .execute(db.pool())
        .await
        .unwrap();
        let member_id = uuid::Uuid::new_v4();
        let res = sqlx::query(
            "INSERT INTO tenant_members (id, tenant_id, email, role) \
             VALUES (?, ?, 'e@e.com', 'superuser')",
        )
        .bind(member_id.as_bytes().as_ref())
        .bind(tenant_id.as_bytes().as_ref())
        .execute(db.pool())
        .await;
        assert!(res.is_err(), "invalid role should be rejected by CHECK");
    }

    #[tokio::test]
    async fn usage_for_tenant_returns_records() {
        let pool = test_pool().await;
        let db = ControlDb::new(pool).await.unwrap();
        let row = sqlx::query("SELECT id FROM tenant_plans LIMIT 1")
            .fetch_one(db.pool())
            .await
            .unwrap();
        let plan_id: Vec<u8> = row.get("id");
        let tid = uuid::Uuid::new_v4();
        sqlx::query(
            "INSERT INTO tenants (id, name, slug, owner_email, plan_id, status, db_path) \
             VALUES (?, 'U', 'usagetest', 'u@u.com', ?, 'active', 'u.db')",
        )
        .bind(tid.as_bytes().as_ref())
        .bind(&plan_id)
        .execute(db.pool())
        .await
        .unwrap();
        let uid = uuid::Uuid::new_v4();
        sqlx::query(
            "INSERT INTO tenant_usage (id, tenant_id, period, mau_count) \
             VALUES (?, ?, '2026-04', 42)",
        )
        .bind(uid.as_bytes().as_ref())
        .bind(tid.as_bytes().as_ref())
        .execute(db.pool())
        .await
        .unwrap();
        let tenant_id = TenantId::from(tid);
        let usage = db.usage_for_tenant(&tenant_id).await.unwrap();
        assert_eq!(usage.len(), 1);
        assert_eq!(usage[0].period, "2026-04");
        assert_eq!(usage[0].mau_count, 42);
        assert!(usage[0].limit_reached_at.is_none());
    }

    #[tokio::test]
    async fn api_key_hash_unique() {
        let pool = test_pool().await;
        let db = ControlDb::new(pool).await.unwrap();
        let row = sqlx::query("SELECT id FROM tenant_plans LIMIT 1")
            .fetch_one(db.pool())
            .await
            .unwrap();
        let plan_id: Vec<u8> = row.get("id");
        let tenant_id = uuid::Uuid::new_v4();
        sqlx::query(
            "INSERT INTO tenants (id, name, slug, owner_email, plan_id, status, db_path) \
             VALUES (?, 'Key Test', 'key-test', 'f@f.com', ?, 'active', 'ktest.db')",
        )
        .bind(tenant_id.as_bytes().as_ref())
        .bind(&plan_id)
        .execute(db.pool())
        .await
        .unwrap();
        let hash = vec![0u8; 32];
        let key_id_a = uuid::Uuid::new_v4();
        let key_id_b = uuid::Uuid::new_v4();
        sqlx::query(
            "INSERT INTO tenant_api_keys (id, tenant_id, name, key_hash, scope) \
             VALUES (?, ?, 'key-a', ?, '[]')",
        )
        .bind(key_id_a.as_bytes().as_ref())
        .bind(tenant_id.as_bytes().as_ref())
        .bind(&hash)
        .execute(db.pool())
        .await
        .unwrap();
        let res = sqlx::query(
            "INSERT INTO tenant_api_keys (id, tenant_id, name, key_hash, scope) \
             VALUES (?, ?, 'key-b', ?, '[]')",
        )
        .bind(key_id_b.as_bytes().as_ref())
        .bind(tenant_id.as_bytes().as_ref())
        .bind(&hash)
        .execute(db.pool())
        .await;
        assert!(res.is_err(), "duplicate key_hash should be rejected");
    }

    /// Helper: insert a tenant row with the given slug + status; return its `TenantId`.
    async fn seed_tenant_with_status(db: &ControlDb, slug: &str, status: &str) -> TenantId {
        let plan_id: Vec<u8> = sqlx::query_scalar("SELECT id FROM tenant_plans LIMIT 1")
            .fetch_one(db.pool())
            .await
            .unwrap();
        let tid = uuid::Uuid::new_v4();
        let db_path = format!("{slug}.db");
        sqlx::query(
            "INSERT INTO tenants (id, name, slug, owner_email, plan_id, status, db_path) \
             VALUES (?, ?, ?, ?, ?, ?, ?)",
        )
        .bind(tid.as_bytes().as_ref())
        .bind(slug)
        .bind(slug)
        .bind(format!("owner-{slug}@example.com"))
        .bind(&plan_id)
        .bind(status)
        .bind(&db_path)
        .execute(db.pool())
        .await
        .unwrap();
        TenantId::from(tid)
    }

    async fn seed_tenant(db: &ControlDb, slug: &str) -> TenantId {
        seed_tenant_with_status(db, slug, "active").await
    }

    async fn insert_member(
        db: &ControlDb,
        tenant_id: &TenantId,
        email: &str,
        role: &str,
        accepted: bool,
    ) {
        let id = uuid::Uuid::new_v4();
        if accepted {
            sqlx::query(
                "INSERT INTO tenant_members (id, tenant_id, email, role, accepted_at) \
                 VALUES (?, ?, ?, ?, datetime('now'))",
            )
            .bind(id.as_bytes().as_ref())
            .bind(tenant_id.as_bytes())
            .bind(email)
            .bind(role)
            .execute(db.pool())
            .await
            .unwrap();
        } else {
            sqlx::query(
                "INSERT INTO tenant_members (id, tenant_id, email, role) \
                 VALUES (?, ?, ?, ?)",
            )
            .bind(id.as_bytes().as_ref())
            .bind(tenant_id.as_bytes())
            .bind(email)
            .bind(role)
            .execute(db.pool())
            .await
            .unwrap();
        }
    }

    #[tokio::test]
    async fn member_role_returns_role_for_accepted_member() {
        let db = ControlDb::new(test_pool().await).await.unwrap();
        let tid = seed_tenant(&db, "acme").await;
        insert_member(&db, &tid, "alice@x.com", "admin", true).await;

        let role = db.member_role(&tid, "alice@x.com").await.unwrap();
        assert_eq!(role, Some(TenantRole::Admin));
    }

    #[tokio::test]
    async fn member_role_ignores_unaccepted_invite() {
        let db = ControlDb::new(test_pool().await).await.unwrap();
        let tid = seed_tenant(&db, "acme").await;
        insert_member(&db, &tid, "bob@x.com", "viewer", false).await;

        let role = db.member_role(&tid, "bob@x.com").await.unwrap();
        assert_eq!(role, None);
    }

    #[tokio::test]
    async fn member_role_returns_none_for_non_member() {
        let db = ControlDb::new(test_pool().await).await.unwrap();
        let tid = seed_tenant(&db, "acme").await;

        let role = db.member_role(&tid, "stranger@x.com").await.unwrap();
        assert_eq!(role, None);
    }

    #[tokio::test]
    async fn tenants_for_member_returns_pairs_sorted_by_name() {
        let db = ControlDb::new(test_pool().await).await.unwrap();
        let zid = seed_tenant(&db, "zulu").await;
        let aid = seed_tenant(&db, "alpha").await;
        insert_member(&db, &aid, "x@y.com", "owner", true).await;
        insert_member(&db, &zid, "x@y.com", "admin", true).await;

        let pairs = db.tenants_for_member("x@y.com").await.unwrap();
        assert_eq!(pairs.len(), 2);
        assert_eq!(pairs[0].0.slug, "alpha");
        assert_eq!(pairs[0].1, TenantRole::Owner);
        assert_eq!(pairs[1].0.slug, "zulu");
        assert_eq!(pairs[1].1, TenantRole::Admin);
    }

    #[tokio::test]
    async fn tenants_for_member_excludes_deleted_tenants() {
        let db = ControlDb::new(test_pool().await).await.unwrap();
        let tid = seed_tenant_with_status(&db, "deadco", "deleted").await;
        insert_member(&db, &tid, "x@y.com", "owner", true).await;

        let pairs = db.tenants_for_member("x@y.com").await.unwrap();
        assert!(pairs.is_empty());
    }

    #[tokio::test]
    async fn tenants_for_member_excludes_unaccepted_invites() {
        let db = ControlDb::new(test_pool().await).await.unwrap();
        let tid = seed_tenant(&db, "acme").await;
        insert_member(&db, &tid, "pending@x.com", "viewer", false).await;

        let pairs = db.tenants_for_member("pending@x.com").await.unwrap();
        assert!(pairs.is_empty());
    }

    // ----- 99c.5 Step 3: tenant_members CRUD -----

    fn token_hash(seed: u8) -> Vec<u8> {
        vec![seed; 32]
    }

    fn future_ts() -> i64 {
        chrono::Utc::now().timestamp() + 60 * 60 * 24
    }

    #[tokio::test]
    async fn list_tenant_members_returns_pending_first() {
        let db = ControlDb::new(test_pool().await).await.unwrap();
        let tid = seed_tenant(&db, "listtest").await;
        insert_member(&db, &tid, "owner@x.com", "owner", true).await;
        db.invite_member(
            &tid,
            "pending@x.com",
            TenantRole::Admin,
            &token_hash(1),
            future_ts(),
        )
        .await
        .unwrap();

        let members = db.list_tenant_members(&tid).await.unwrap();
        assert_eq!(members.len(), 2);
        assert_eq!(members[0].email, "pending@x.com");
        assert!(members[0].accepted_at.is_none());
        assert_eq!(members[1].email, "owner@x.com");
        assert!(members[1].accepted_at.is_some());
    }

    #[tokio::test]
    async fn invite_member_unique_collision_returns_error() {
        let db = ControlDb::new(test_pool().await).await.unwrap();
        let tid = seed_tenant(&db, "collide").await;
        db.invite_member(
            &tid,
            "dup@x.com",
            TenantRole::Viewer,
            &token_hash(2),
            future_ts(),
        )
        .await
        .unwrap();
        let err = db
            .invite_member(
                &tid,
                "dup@x.com",
                TenantRole::Viewer,
                &token_hash(3),
                future_ts(),
            )
            .await
            .expect_err("second invite must collide");
        assert!(matches!(err, SaasError::MemberAlreadyExists));
    }

    #[tokio::test]
    async fn accept_invite_happy_path_clears_token() {
        let db = ControlDb::new(test_pool().await).await.unwrap();
        let tid = seed_tenant(&db, "happy").await;
        let hash = token_hash(4);
        db.invite_member(&tid, "joiner@x.com", TenantRole::Admin, &hash, future_ts())
            .await
            .unwrap();

        let row = db.accept_invite(&hash).await.unwrap();
        assert_eq!(row.email, "joiner@x.com");
        assert!(
            row.accepted_at.is_none(),
            "pre-update row carries the prior NULL accepted_at"
        );

        let mid = row.id_as_member_id().unwrap();
        let after = db.get_tenant_member(&mid).await.unwrap().unwrap();
        assert!(after.accepted_at.is_some());
        assert!(after.invite_token_expires_at.is_none());
    }

    #[tokio::test]
    async fn accept_invite_expired_returns_error() {
        let db = ControlDb::new(test_pool().await).await.unwrap();
        let tid = seed_tenant(&db, "expired").await;
        let hash = token_hash(5);
        let past = chrono::Utc::now().timestamp() - 60;
        db.invite_member(&tid, "stale@x.com", TenantRole::Viewer, &hash, past)
            .await
            .unwrap();
        let err = db.accept_invite(&hash).await.expect_err("expired");
        assert!(matches!(err, SaasError::InviteNotFoundOrExpired));
    }

    #[tokio::test]
    async fn accept_invite_already_accepted_returns_error() {
        let db = ControlDb::new(test_pool().await).await.unwrap();
        let tid = seed_tenant(&db, "twice").await;
        let hash = token_hash(6);
        db.invite_member(&tid, "once@x.com", TenantRole::Admin, &hash, future_ts())
            .await
            .unwrap();
        db.accept_invite(&hash).await.unwrap();
        let err = db.accept_invite(&hash).await.expect_err("re-accept");
        assert!(matches!(err, SaasError::InviteNotFoundOrExpired));
    }

    #[tokio::test]
    async fn find_pending_invite_skips_expired_and_accepted() {
        let db = ControlDb::new(test_pool().await).await.unwrap();
        let tid = seed_tenant(&db, "find").await;
        let live = token_hash(7);
        db.invite_member(&tid, "live@x.com", TenantRole::Viewer, &live, future_ts())
            .await
            .unwrap();
        let found = db.find_pending_invite_by_hash(&live).await.unwrap();
        assert!(found.is_some());

        db.accept_invite(&live).await.unwrap();
        let after = db.find_pending_invite_by_hash(&live).await.unwrap();
        assert!(after.is_none());
    }

    // ----- 99c.5 Step 4: last-owner invariant -----

    /// Insert an accepted member and return the typed [`MemberId`] so
    /// last-owner tests can target the row directly.
    async fn insert_member_typed(
        db: &ControlDb,
        tenant_id: &TenantId,
        email: &str,
        role: &str,
    ) -> MemberId {
        let mid = MemberId::new();
        sqlx::query(
            "INSERT INTO tenant_members (id, tenant_id, email, role, accepted_at) \
             VALUES (?, ?, ?, ?, datetime('now'))",
        )
        .bind(mid.as_bytes())
        .bind(tenant_id.as_bytes())
        .bind(email)
        .bind(role)
        .execute(db.pool())
        .await
        .unwrap();
        mid
    }

    #[tokio::test]
    async fn count_owners_counts_only_accepted_owners() {
        let db = ControlDb::new(test_pool().await).await.unwrap();
        let tid = seed_tenant(&db, "count").await;
        insert_member_typed(&db, &tid, "owner@x.com", "owner").await;
        insert_member_typed(&db, &tid, "admin@x.com", "admin").await;
        // Pending owner — must NOT count.
        db.invite_member(
            &tid,
            "pending@x.com",
            TenantRole::Owner,
            &token_hash(8),
            future_ts(),
        )
        .await
        .unwrap();

        assert_eq!(db.count_owners(&tid).await.unwrap(), 1);
    }

    #[tokio::test]
    async fn update_member_role_demote_last_owner_fails() {
        let db = ControlDb::new(test_pool().await).await.unwrap();
        let tid = seed_tenant(&db, "demote-last").await;
        let owner = insert_member_typed(&db, &tid, "lone@x.com", "owner").await;

        let err = db
            .update_member_role(&owner, TenantRole::Admin)
            .await
            .expect_err("demote sole owner");
        assert!(matches!(err, SaasError::CannotDemoteLastOwner));

        // Row unchanged.
        let after = db.get_tenant_member(&owner).await.unwrap().unwrap();
        assert_eq!(after.role, TenantRole::Owner);
    }

    #[tokio::test]
    async fn update_member_role_demote_one_of_two_owners_succeeds() {
        let db = ControlDb::new(test_pool().await).await.unwrap();
        let tid = seed_tenant(&db, "demote-ok").await;
        let first = insert_member_typed(&db, &tid, "a@x.com", "owner").await;
        insert_member_typed(&db, &tid, "b@x.com", "owner").await;

        db.update_member_role(&first, TenantRole::Admin)
            .await
            .unwrap();
        let after = db.get_tenant_member(&first).await.unwrap().unwrap();
        assert_eq!(after.role, TenantRole::Admin);
    }

    #[tokio::test]
    async fn update_role_unknown_member_returns_not_found() {
        let db = ControlDb::new(test_pool().await).await.unwrap();
        let bogus = MemberId::new();
        let err = db
            .update_member_role(&bogus, TenantRole::Admin)
            .await
            .expect_err("unknown");
        assert!(matches!(err, SaasError::MemberNotFound));
    }

    #[tokio::test]
    async fn remove_last_owner_fails() {
        let db = ControlDb::new(test_pool().await).await.unwrap();
        let tid = seed_tenant(&db, "rm-last").await;
        let owner = insert_member_typed(&db, &tid, "only@x.com", "owner").await;

        let err = db.remove_member(&owner).await.expect_err("remove last");
        assert!(matches!(err, SaasError::CannotRemoveLastOwner));
        assert!(db.get_tenant_member(&owner).await.unwrap().is_some());
    }

    #[tokio::test]
    async fn remove_one_of_two_owners_succeeds() {
        let db = ControlDb::new(test_pool().await).await.unwrap();
        let tid = seed_tenant(&db, "rm-ok").await;
        let first = insert_member_typed(&db, &tid, "a@x.com", "owner").await;
        insert_member_typed(&db, &tid, "b@x.com", "owner").await;

        db.remove_member(&first).await.unwrap();
        assert!(db.get_tenant_member(&first).await.unwrap().is_none());
    }

    #[tokio::test]
    async fn remove_admin_succeeds() {
        let db = ControlDb::new(test_pool().await).await.unwrap();
        let tid = seed_tenant(&db, "rm-admin").await;
        insert_member_typed(&db, &tid, "owner@x.com", "owner").await;
        let admin = insert_member_typed(&db, &tid, "admin@x.com", "admin").await;

        db.remove_member(&admin).await.unwrap();
        assert!(db.get_tenant_member(&admin).await.unwrap().is_none());
    }

    // ---- 99c.6 Step 1: super-admin queries ----

    #[tokio::test]
    async fn count_tenants_by_status_returns_correct_counts() {
        let db = ControlDb::new(test_pool().await).await.unwrap();
        seed_tenant_with_status(&db, "active-a", "active").await;
        seed_tenant_with_status(&db, "active-b", "active").await;
        seed_tenant_with_status(&db, "active-c", "active").await;
        seed_tenant_with_status(&db, "suspended-a", "suspended").await;
        seed_tenant_with_status(&db, "suspended-b", "suspended").await;
        seed_tenant_with_status(&db, "deleted-a", "deleted").await;

        assert_eq!(
            db.count_tenants_by_status(TenantStatus::Active)
                .await
                .unwrap(),
            3
        );
        assert_eq!(
            db.count_tenants_by_status(TenantStatus::Suspended)
                .await
                .unwrap(),
            2
        );
        assert_eq!(
            db.count_tenants_by_status(TenantStatus::Deleted)
                .await
                .unwrap(),
            1
        );
    }

    #[tokio::test]
    async fn count_tenants_created_since_threshold() {
        let db = ControlDb::new(test_pool().await).await.unwrap();
        seed_tenant(&db, "old-a").await;
        seed_tenant(&db, "old-b").await;

        let threshold = chrono::Utc::now();
        seed_tenant(&db, "new-a").await;

        // At minimum new-a was created after threshold (all three were inserted
        // in tight succession, but at least one must qualify).
        let after = db.count_tenants_created_since(threshold).await.unwrap();
        let all = db
            .count_tenants_created_since(chrono::DateTime::UNIX_EPOCH)
            .await
            .unwrap();
        assert_eq!(all, 3);
        assert!(after >= 0 && after <= 3, "threshold count in sane range");
    }

    #[tokio::test]
    async fn count_tenants_grouped_by_plan_returns_all_plans_zero() {
        let db = ControlDb::new(test_pool().await).await.unwrap();
        // No tenants seeded — all four seed plans should appear with count 0.
        let by_plan = db.count_tenants_grouped_by_plan().await.unwrap();
        assert_eq!(by_plan.len(), 4, "all 4 seed plans returned");
        assert!(by_plan.iter().all(|(_, c)| *c == 0));
    }

    #[tokio::test]
    async fn count_tenants_grouped_by_plan_counts_correctly() {
        let db = ControlDb::new(test_pool().await).await.unwrap();
        let plans: Vec<(Vec<u8>, String)> =
            sqlx::query_as("SELECT id, name FROM tenant_plans ORDER BY price_cents ASC LIMIT 2")
                .fetch_all(db.pool())
                .await
                .unwrap();
        let (dev_id, _) = &plans[0];
        let (starter_id, _) = &plans[1];

        for slug in ["dev-a", "dev-b"] {
            let id = uuid::Uuid::new_v4();
            sqlx::query(
                "INSERT INTO tenants (id, name, slug, owner_email, plan_id, status, db_path) \
                 VALUES (?, ?, ?, ?, ?, 'active', ?)",
            )
            .bind(id.as_bytes().as_ref())
            .bind(slug)
            .bind(slug)
            .bind(format!("{slug}@x.com"))
            .bind(dev_id)
            .bind(format!("{slug}.db"))
            .execute(db.pool())
            .await
            .unwrap();
        }
        let sid = uuid::Uuid::new_v4();
        sqlx::query(
            "INSERT INTO tenants (id, name, slug, owner_email, plan_id, status, db_path) \
             VALUES (?, 'Starter', 'strt', 'strt@x.com', ?, 'active', 'strt.db')",
        )
        .bind(sid.as_bytes().as_ref())
        .bind(starter_id)
        .execute(db.pool())
        .await
        .unwrap();

        let by_plan = db.count_tenants_grouped_by_plan().await.unwrap();
        let dev_count = by_plan.iter().find(|(n, _)| n == "dev").map(|(_, c)| *c);
        let starter_count = by_plan
            .iter()
            .find(|(n, _)| n == "starter")
            .map(|(_, c)| *c);
        assert_eq!(dev_count, Some(2));
        assert_eq!(starter_count, Some(1));
    }

    #[tokio::test]
    async fn search_tenants_no_filter_returns_all() {
        let db = ControlDb::new(test_pool().await).await.unwrap();
        seed_tenant(&db, "alpha").await;
        seed_tenant(&db, "beta").await;
        seed_tenant(&db, "gamma").await;

        let result = db
            .search_tenants(SearchTenantsParams {
                query: None,
                status: None,
                plan_id: None,
                current_period: "2026-05",
                sort_col: TenantSortCol::Name,
                sort_dir: SortDir::Asc,
                limit: 25,
                offset: 0,
            })
            .await
            .unwrap();

        assert_eq!(result.total, 3);
        assert_eq!(result.rows.len(), 3);
        assert_eq!(result.rows[0].slug, "alpha");
        assert_eq!(result.rows[2].slug, "gamma");
    }

    #[tokio::test]
    async fn search_tenants_query_matches_slug_and_name() {
        let db = ControlDb::new(test_pool().await).await.unwrap();
        seed_tenant(&db, "acme").await;
        seed_tenant(&db, "acme-staging").await;
        seed_tenant(&db, "beta").await;

        let result = db
            .search_tenants(SearchTenantsParams {
                query: Some("acme"),
                status: None,
                plan_id: None,
                current_period: "2026-05",
                sort_col: TenantSortCol::Name,
                sort_dir: SortDir::Asc,
                limit: 25,
                offset: 0,
            })
            .await
            .unwrap();

        assert_eq!(result.total, 2);
        assert!(result.rows.iter().all(|r| r.slug.contains("acme")));
    }

    #[tokio::test]
    async fn search_tenants_status_filter() {
        let db = ControlDb::new(test_pool().await).await.unwrap();
        seed_tenant_with_status(&db, "susp-a", "suspended").await;
        seed_tenant_with_status(&db, "susp-b", "suspended").await;
        seed_tenant(&db, "active-z").await;

        let result = db
            .search_tenants(SearchTenantsParams {
                query: None,
                status: Some(TenantStatus::Suspended),
                plan_id: None,
                current_period: "2026-05",
                sort_col: TenantSortCol::Name,
                sort_dir: SortDir::Asc,
                limit: 25,
                offset: 0,
            })
            .await
            .unwrap();

        assert_eq!(result.total, 2);
        assert!(
            result
                .rows
                .iter()
                .all(|r| r.status == TenantStatus::Suspended)
        );
    }

    #[tokio::test]
    async fn search_tenants_pagination() {
        let db = ControlDb::new(test_pool().await).await.unwrap();
        for i in 0..30u32 {
            seed_tenant(&db, &format!("page-t-{i:02}")).await;
        }

        let page1 = db
            .search_tenants(SearchTenantsParams {
                query: None,
                status: None,
                plan_id: None,
                current_period: "2026-05",
                sort_col: TenantSortCol::Name,
                sort_dir: SortDir::Asc,
                limit: 25,
                offset: 0,
            })
            .await
            .unwrap();
        let page2 = db
            .search_tenants(SearchTenantsParams {
                query: None,
                status: None,
                plan_id: None,
                current_period: "2026-05",
                sort_col: TenantSortCol::Name,
                sort_dir: SortDir::Asc,
                limit: 25,
                offset: 25,
            })
            .await
            .unwrap();

        assert_eq!(page1.total, 30);
        assert_eq!(page1.rows.len(), 25);
        assert_eq!(page2.rows.len(), 5);
    }

    #[tokio::test]
    async fn search_tenants_sort_name_desc() {
        let db = ControlDb::new(test_pool().await).await.unwrap();
        seed_tenant(&db, "aaa").await;
        seed_tenant(&db, "bbb").await;
        seed_tenant(&db, "ccc").await;

        let result = db
            .search_tenants(SearchTenantsParams {
                query: None,
                status: None,
                plan_id: None,
                current_period: "2026-05",
                sort_col: TenantSortCol::Name,
                sort_dir: SortDir::Desc,
                limit: 25,
                offset: 0,
            })
            .await
            .unwrap();

        assert_eq!(result.rows[0].slug, "ccc");
        assert_eq!(result.rows[2].slug, "aaa");
    }

    #[tokio::test]
    async fn aggregate_usage_for_period_returns_zeros_when_empty() {
        let db = ControlDb::new(test_pool().await).await.unwrap();
        let agg = db.aggregate_usage_for_period("2026-05").await.unwrap();
        assert_eq!(agg.period, "2026-05");
        assert_eq!(agg.total_mau, 0);
        assert_eq!(agg.active_tenants, 0);
    }

    #[tokio::test]
    async fn aggregate_usage_for_period_sums_correctly() {
        let db = ControlDb::new(test_pool().await).await.unwrap();
        let ta = seed_tenant(&db, "agg-a").await;
        let tb = seed_tenant(&db, "agg-b").await;
        let tc = seed_tenant(&db, "agg-c").await;

        for (tid, mau) in [(&ta, 100i64), (&tb, 200), (&tc, 0)] {
            let uid = uuid::Uuid::new_v4();
            sqlx::query(
                "INSERT INTO tenant_usage (id, tenant_id, period, mau_count) \
                 VALUES (?, ?, '2026-05', ?)",
            )
            .bind(uid.as_bytes().as_ref())
            .bind(tid.as_bytes())
            .bind(mau)
            .execute(db.pool())
            .await
            .unwrap();
        }

        let agg = db.aggregate_usage_for_period("2026-05").await.unwrap();
        assert_eq!(agg.total_mau, 300);
        // tc has mau_count=0 → not counted as active.
        assert_eq!(agg.active_tenants, 2);
    }

    #[tokio::test]
    async fn aggregate_usage_history_returns_n_most_recent() {
        let db = ControlDb::new(test_pool().await).await.unwrap();
        let tid = seed_tenant(&db, "hist").await;

        // Seed 14 periods.
        for i in 0..14u32 {
            let uid = uuid::Uuid::new_v4();
            let period = format!("2025-{:02}", i + 1);
            sqlx::query(
                "INSERT INTO tenant_usage (id, tenant_id, period, mau_count) \
                 VALUES (?, ?, ?, 1)",
            )
            .bind(uid.as_bytes().as_ref())
            .bind(tid.as_bytes())
            .bind(&period)
            .execute(db.pool())
            .await
            .unwrap();
        }

        let history = db.aggregate_usage_history(12).await.unwrap();
        assert_eq!(
            history.len(),
            12,
            "only the 12 most recent periods returned"
        );
        // Ordered newest first.
        assert!(history[0].period > history[11].period);
    }

    #[tokio::test]
    async fn count_dormant_tenants_excludes_deleted() {
        let db = ControlDb::new(test_pool().await).await.unwrap();
        // Active with no last_seen_at → dormant.
        seed_tenant(&db, "never-seen").await;
        // Deleted → excluded regardless.
        seed_tenant_with_status(&db, "del", "deleted").await;

        let n = db
            .count_dormant_tenants(chrono::Utc::now() + chrono::Duration::days(365))
            .await
            .unwrap();
        assert_eq!(n, 1, "deleted tenant must not be counted as dormant");
    }

    #[tokio::test]
    async fn count_dormant_tenants_excludes_recently_seen() {
        let db = ControlDb::new(test_pool().await).await.unwrap();
        let tid = seed_tenant(&db, "recent-tenant").await;
        db.touch_last_seen(&tid).await.unwrap();

        // Threshold is in the past → recently-seen tenant is not dormant.
        let n = db
            .count_dormant_tenants(chrono::Utc::now() - chrono::Duration::days(30))
            .await
            .unwrap();
        assert_eq!(n, 0, "recently-seen tenant should not be dormant");
    }

    #[tokio::test]
    async fn set_tenant_status_updates_row() {
        let db = ControlDb::new(test_pool().await).await.unwrap();
        let tid = seed_tenant(&db, "suspend-me").await;

        db.set_tenant_status(&tid, TenantStatus::Suspended)
            .await
            .unwrap();

        let (status,): (String,) = sqlx::query_as("SELECT status FROM tenants WHERE id = ?1")
            .bind(tid.as_bytes())
            .fetch_one(db.pool())
            .await
            .unwrap();
        assert_eq!(status, "suspended");
    }

    #[tokio::test]
    async fn set_tenant_plan_updates_plan_id() {
        let db = ControlDb::new(test_pool().await).await.unwrap();
        let tid = seed_tenant(&db, "change-plan").await;
        let plans = db.list_plans().await.unwrap();
        // dev is plan index 0; pick the next one.
        let new_plan = &plans[1];

        db.set_tenant_plan(&tid, &new_plan.id).await.unwrap();

        let (plan_id,): (Vec<u8>,) = sqlx::query_as("SELECT plan_id FROM tenants WHERE id = ?1")
            .bind(tid.as_bytes())
            .fetch_one(db.pool())
            .await
            .unwrap();
        assert_eq!(plan_id, new_plan.id);
    }

    #[tokio::test]
    async fn list_plans_returns_four_seed_plans_ordered_by_price() {
        let db = ControlDb::new(test_pool().await).await.unwrap();
        let plans = db.list_plans().await.unwrap();
        assert_eq!(plans.len(), 4);
        let names: Vec<&str> = plans.iter().map(|p| p.name.as_str()).collect();
        assert!(names.contains(&"dev"));
        assert!(names.contains(&"starter"));
        assert!(names.contains(&"growth"));
        assert!(names.contains(&"scale"));
        // Cheapest first.
        assert_eq!(plans[0].name, "dev");
        assert_eq!(plans[0].price_cents, 0);
    }

    #[tokio::test]
    async fn get_plan_by_id_some_and_none() {
        let db = ControlDb::new(test_pool().await).await.unwrap();
        let plans = db.list_plans().await.unwrap();
        let first = &plans[0];

        let found = db.get_plan_by_id(&first.id).await.unwrap();
        assert!(found.is_some());
        assert_eq!(found.unwrap().name, first.name);

        let bogus = vec![0u8; 16];
        let not_found = db.get_plan_by_id(&bogus).await.unwrap();
        assert!(not_found.is_none());
    }

    // ── tenant_domains CRUD ───────────────────────────────────────────────────

    #[tokio::test]
    async fn create_tenant_domain_inserts_pending() {
        use crate::domains::DomainStatus;

        let db = ControlDb::new(test_pool().await).await.unwrap();
        let tid = seed_tenant(&db, "dom-insert").await;

        let row = db
            .create_tenant_domain(&tid, "auth.example.com", "custom.allowthem.io")
            .await
            .unwrap();

        assert_eq!(row.domain, "auth.example.com");
        assert_eq!(row.dns_target, "custom.allowthem.io");
        assert_eq!(row.status, DomainStatus::PendingVerification);
        assert!(row.verified_at.is_none());
        assert!(row.last_error.is_none());
    }

    #[tokio::test]
    async fn create_tenant_domain_unique_violation_returns_conflict() {
        let db = ControlDb::new(test_pool().await).await.unwrap();
        let tid = seed_tenant(&db, "dom-conflict").await;

        db.create_tenant_domain(&tid, "auth.example.com", "custom.allowthem.io")
            .await
            .unwrap();

        let err = db
            .create_tenant_domain(&tid, "auth.example.com", "custom.allowthem.io")
            .await
            .unwrap_err();
        assert!(
            matches!(err, SaasError::DomainAlreadyExists),
            "expected DomainAlreadyExists, got {err:?}"
        );
    }

    #[tokio::test]
    async fn list_tenant_domains_orders_by_created_at_desc() {
        let db = ControlDb::new(test_pool().await).await.unwrap();
        let tid = seed_tenant(&db, "dom-list-order").await;

        db.create_tenant_domain(&tid, "a.example.com", "custom.allowthem.io")
            .await
            .unwrap();
        db.create_tenant_domain(&tid, "b.example.com", "custom.allowthem.io")
            .await
            .unwrap();
        db.create_tenant_domain(&tid, "c.example.com", "custom.allowthem.io")
            .await
            .unwrap();

        sqlx::query("UPDATE tenant_domains SET created_at = ?1 WHERE tenant_id = ?2")
            .bind("2026-01-01T00:00:00.000Z")
            .bind(tid.as_bytes())
            .execute(db.pool())
            .await
            .unwrap();

        let rows = db.list_tenant_domains(&tid).await.unwrap();
        assert_eq!(rows.len(), 3);
        // Same created_at values need a stable newest-first tiebreaker.
        assert_eq!(rows[0].domain, "c.example.com");
        assert_eq!(rows[1].domain, "b.example.com");
        assert_eq!(rows[2].domain, "a.example.com");
    }

    #[tokio::test]
    async fn list_tenant_domains_filters_by_tenant_id() {
        let db = ControlDb::new(test_pool().await).await.unwrap();
        let tid_a = seed_tenant(&db, "dom-filter-a").await;
        let tid_b = seed_tenant(&db, "dom-filter-b").await;

        db.create_tenant_domain(&tid_a, "auth.acme.com", "custom.allowthem.io")
            .await
            .unwrap();
        db.create_tenant_domain(&tid_b, "auth.beta.com", "custom.allowthem.io")
            .await
            .unwrap();

        let rows_a = db.list_tenant_domains(&tid_a).await.unwrap();
        assert_eq!(rows_a.len(), 1);
        assert_eq!(rows_a[0].domain, "auth.acme.com");

        let rows_b = db.list_tenant_domains(&tid_b).await.unwrap();
        assert_eq!(rows_b.len(), 1);
        assert_eq!(rows_b[0].domain, "auth.beta.com");
    }

    #[tokio::test]
    async fn get_tenant_domain_scoped_other_tenant_is_none() {
        use crate::domains::DomainId;

        let db = ControlDb::new(test_pool().await).await.unwrap();
        let tid_a = seed_tenant(&db, "dom-scope-a").await;
        let tid_b = seed_tenant(&db, "dom-scope-b").await;

        let row = db
            .create_tenant_domain(&tid_a, "auth.acme.com", "custom.allowthem.io")
            .await
            .unwrap();
        let did = DomainId::from(Uuid::from_slice(&row.id).unwrap());

        // Correct tenant sees the row.
        let found = db.get_tenant_domain_scoped(&did, &tid_a).await.unwrap();
        assert!(found.is_some());

        // Other tenant gets None.
        let not_found = db.get_tenant_domain_scoped(&did, &tid_b).await.unwrap();
        assert!(not_found.is_none());
    }

    #[tokio::test]
    async fn set_tenant_domain_status_to_verified_clears_last_error_and_sets_verified_at() {
        use crate::domains::{DomainId, DomainStatus};

        let db = ControlDb::new(test_pool().await).await.unwrap();
        let tid = seed_tenant(&db, "dom-verify").await;

        let row = db
            .create_tenant_domain(&tid, "auth.example.com", "custom.allowthem.io")
            .await
            .unwrap();
        let did = DomainId::from(Uuid::from_slice(&row.id).unwrap());

        let now = chrono::Utc::now();
        db.set_tenant_domain_status(&did, DomainStatus::Verified, Some(now), None)
            .await
            .unwrap();

        let updated = db
            .get_tenant_domain_scoped(&did, &tid)
            .await
            .unwrap()
            .unwrap();
        assert_eq!(updated.status, DomainStatus::Verified);
        assert!(updated.verified_at.is_some());
        assert!(updated.last_error.is_none());
    }

    #[tokio::test]
    async fn set_tenant_domain_status_to_failed_records_last_error() {
        use crate::domains::{DomainId, DomainStatus};

        let db = ControlDb::new(test_pool().await).await.unwrap();
        let tid = seed_tenant(&db, "dom-fail").await;

        let row = db
            .create_tenant_domain(&tid, "auth.example.com", "custom.allowthem.io")
            .await
            .unwrap();
        let did = DomainId::from(Uuid::from_slice(&row.id).unwrap());

        db.set_tenant_domain_status(
            &did,
            DomainStatus::Failed,
            None,
            Some("no CNAME record found"),
        )
        .await
        .unwrap();

        let updated = db
            .get_tenant_domain_scoped(&did, &tid)
            .await
            .unwrap()
            .unwrap();
        assert_eq!(updated.status, DomainStatus::Failed);
        assert!(updated.verified_at.is_none());
        assert_eq!(updated.last_error.as_deref(), Some("no CNAME record found"));
    }

    #[tokio::test]
    async fn delete_tenant_domain_scoped_other_tenant_is_no_op() {
        use crate::domains::DomainId;

        let db = ControlDb::new(test_pool().await).await.unwrap();
        let tid_a = seed_tenant(&db, "dom-del-a").await;
        let tid_b = seed_tenant(&db, "dom-del-b").await;

        let row = db
            .create_tenant_domain(&tid_a, "auth.example.com", "custom.allowthem.io")
            .await
            .unwrap();
        let did = DomainId::from(Uuid::from_slice(&row.id).unwrap());

        // Deleting from the wrong tenant returns false (no rows affected).
        let affected = db.delete_tenant_domain_scoped(&did, &tid_b).await.unwrap();
        assert!(!affected);

        // Row still exists for the correct tenant.
        let still_there = db.get_tenant_domain_scoped(&did, &tid_a).await.unwrap();
        assert!(still_there.is_some());

        // Correct tenant can delete.
        let deleted = db.delete_tenant_domain_scoped(&did, &tid_a).await.unwrap();
        assert!(deleted);

        let gone = db.get_tenant_domain_scoped(&did, &tid_a).await.unwrap();
        assert!(gone.is_none());
    }

    #[tokio::test]
    async fn tenant_by_custom_domain_returns_only_verified_or_active() {
        use crate::domains::{DomainId, DomainStatus};

        let db = ControlDb::new(test_pool().await).await.unwrap();
        let tid = seed_tenant(&db, "dom-router").await;

        // pending_verification → should not be returned
        let row = db
            .create_tenant_domain(&tid, "pending.example.com", "custom.allowthem.io")
            .await
            .unwrap();
        let did_pending = DomainId::from(Uuid::from_slice(&row.id).unwrap());
        assert!(
            db.tenant_by_custom_domain("pending.example.com")
                .await
                .unwrap()
                .is_none()
        );

        // Flip to verified → should be returned
        db.set_tenant_domain_status(
            &did_pending,
            DomainStatus::Verified,
            Some(chrono::Utc::now()),
            None,
        )
        .await
        .unwrap();
        let found = db
            .tenant_by_custom_domain("pending.example.com")
            .await
            .unwrap();
        assert!(found.is_some());
        assert_eq!(found.unwrap().slug, "dom-router");

        // failed → not returned
        let row2 = db
            .create_tenant_domain(&tid, "failed.example.com", "custom.allowthem.io")
            .await
            .unwrap();
        let did_failed = DomainId::from(Uuid::from_slice(&row2.id).unwrap());
        db.set_tenant_domain_status(&did_failed, DomainStatus::Failed, None, Some("err"))
            .await
            .unwrap();
        assert!(
            db.tenant_by_custom_domain("failed.example.com")
                .await
                .unwrap()
                .is_none()
        );
    }

    #[tokio::test]
    async fn tenant_by_custom_domain_lowercases_input() {
        use crate::domains::{DomainId, DomainStatus};

        let db = ControlDb::new(test_pool().await).await.unwrap();
        let tid = seed_tenant(&db, "dom-case").await;

        let row = db
            .create_tenant_domain(&tid, "auth.example.com", "custom.allowthem.io")
            .await
            .unwrap();
        let did = DomainId::from(Uuid::from_slice(&row.id).unwrap());
        db.set_tenant_domain_status(&did, DomainStatus::Verified, Some(chrono::Utc::now()), None)
            .await
            .unwrap();

        // Query with mixed case — should still find the row.
        let found = db
            .tenant_by_custom_domain("Auth.EXAMPLE.COM")
            .await
            .unwrap();
        assert!(found.is_some());
    }

    #[tokio::test]
    async fn list_domains_for_sweep_oldest_first_capped() {
        use crate::domains::{DomainId, DomainStatus};

        let db = ControlDb::new(test_pool().await).await.unwrap();
        let tid = seed_tenant(&db, "dom-sweep").await;

        // Insert three pending rows.
        for domain in ["a.sweep.com", "b.sweep.com", "c.sweep.com"] {
            db.create_tenant_domain(&tid, domain, "custom.allowthem.io")
                .await
                .unwrap();
        }

        // Insert one verified row — should not appear.
        let row = db
            .create_tenant_domain(&tid, "v.sweep.com", "custom.allowthem.io")
            .await
            .unwrap();
        let did_v = DomainId::from(Uuid::from_slice(&row.id).unwrap());
        db.set_tenant_domain_status(
            &did_v,
            DomainStatus::Verified,
            Some(chrono::Utc::now()),
            None,
        )
        .await
        .unwrap();

        // Cap at 2 — only 2 of the 3 pending rows returned.
        let sweep = db.list_domains_for_sweep(2).await.unwrap();
        assert_eq!(sweep.len(), 2);
        // None should be verified.
        for r in &sweep {
            assert!(
                matches!(
                    r.status,
                    DomainStatus::PendingVerification | DomainStatus::Failed
                ),
                "unexpected status {:?}",
                r.status
            );
        }
    }
}