varpulis-db 0.6.4

PostgreSQL database layer for Varpulis Cloud
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
use chrono::{Datelike, NaiveDate, Utc};
use sqlx::PgPool;
use uuid::Uuid;

use crate::models::{ApiKey, GlobalPipelineTemplate, Organization, Pipeline, UsageDaily, User};
use crate::DbError;

// ---------------------------------------------------------------------------
// Users
// ---------------------------------------------------------------------------

const USER_COLUMNS: &str = "id, github_id, email, name, avatar_url, created_at, \
                            username, password_hash, display_name, role, disabled, updated_at, \
                            email_verified, verification_token, verification_expires_at";

/// Create a new user or update an existing one (upsert on `github_id`).
pub async fn create_or_update_user(
    pool: &PgPool,
    github_id: &str,
    email: &str,
    name: &str,
    avatar_url: &str,
) -> Result<User, DbError> {
    let query = format!(
        "INSERT INTO users (github_id, email, name, avatar_url)
         VALUES ($1, $2, $3, $4)
         ON CONFLICT (github_id) DO UPDATE
             SET email      = EXCLUDED.email,
                 name       = EXCLUDED.name,
                 avatar_url = EXCLUDED.avatar_url
         RETURNING {USER_COLUMNS}"
    );
    let user = sqlx::query_as::<_, User>(&query)
        .bind(github_id)
        .bind(email)
        .bind(name)
        .bind(avatar_url)
        .fetch_one(pool)
        .await?;

    Ok(user)
}

/// Look up a user by their GitHub ID.
pub async fn get_user_by_github_id(
    pool: &PgPool,
    github_id: &str,
) -> Result<Option<User>, DbError> {
    let query = format!("SELECT {USER_COLUMNS} FROM users WHERE github_id = $1");
    let user = sqlx::query_as::<_, User>(&query)
        .bind(github_id)
        .fetch_optional(pool)
        .await?;

    Ok(user)
}

/// Create a local user with username and password hash.
pub async fn create_local_user(
    pool: &PgPool,
    username: &str,
    password_hash: &str,
    display_name: &str,
    email: &str,
    role: &str,
) -> Result<User, DbError> {
    let query = format!(
        "INSERT INTO users (username, password_hash, display_name, email, role, name, avatar_url, email_verified)
         VALUES ($1, $2, $3, $4, $5, $3, '', true)
         RETURNING {USER_COLUMNS}"
    );
    let user = sqlx::query_as::<_, User>(&query)
        .bind(username)
        .bind(password_hash)
        .bind(display_name)
        .bind(email)
        .bind(role)
        .fetch_one(pool)
        .await?;

    Ok(user)
}

/// Look up a user by username (for login).
pub async fn get_user_by_username(pool: &PgPool, username: &str) -> Result<Option<User>, DbError> {
    let query = format!("SELECT {USER_COLUMNS} FROM users WHERE username = $1");
    let user = sqlx::query_as::<_, User>(&query)
        .bind(username)
        .fetch_optional(pool)
        .await?;

    Ok(user)
}

/// Look up a user by ID.
pub async fn get_user_by_id(pool: &PgPool, id: Uuid) -> Result<Option<User>, DbError> {
    let query = format!("SELECT {USER_COLUMNS} FROM users WHERE id = $1");
    let user = sqlx::query_as::<_, User>(&query)
        .bind(id)
        .fetch_optional(pool)
        .await?;

    Ok(user)
}

/// Update a user's details (admin operation). Only non-None fields are updated.
pub async fn update_user(
    pool: &PgPool,
    id: Uuid,
    display_name: Option<&str>,
    email: Option<&str>,
    role: Option<&str>,
    disabled: Option<bool>,
) -> Result<Option<User>, DbError> {
    // Build dynamic SET clause
    let mut sets = Vec::new();
    let mut bind_idx = 1u32;
    if display_name.is_some() {
        sets.push(format!("display_name = ${bind_idx}"));
        bind_idx += 1;
    }
    if email.is_some() {
        sets.push(format!("email = ${bind_idx}"));
        bind_idx += 1;
    }
    if role.is_some() {
        sets.push(format!("role = ${bind_idx}"));
        bind_idx += 1;
    }
    if disabled.is_some() {
        sets.push(format!("disabled = ${bind_idx}"));
        bind_idx += 1;
    }
    if sets.is_empty() {
        return get_user_by_id(pool, id).await;
    }
    sets.push("updated_at = now()".to_string());
    let query = format!(
        "UPDATE users SET {} WHERE id = ${bind_idx} RETURNING {USER_COLUMNS}",
        sets.join(", "),
    );

    let mut q = sqlx::query_as::<_, User>(&query);
    if let Some(v) = display_name {
        q = q.bind(v);
    }
    if let Some(v) = email {
        q = q.bind(v);
    }
    if let Some(v) = role {
        q = q.bind(v);
    }
    if let Some(v) = disabled {
        q = q.bind(v);
    }
    q = q.bind(id);

    let user = q.fetch_optional(pool).await?;
    Ok(user)
}

/// Update a user's password hash.
pub async fn update_password_hash(pool: &PgPool, id: Uuid, new_hash: &str) -> Result<(), DbError> {
    sqlx::query("UPDATE users SET password_hash = $1, updated_at = now() WHERE id = $2")
        .bind(new_hash)
        .bind(id)
        .execute(pool)
        .await?;
    Ok(())
}

/// List all users (for admin panel).
pub async fn list_users(pool: &PgPool) -> Result<Vec<User>, DbError> {
    let query = format!("SELECT {USER_COLUMNS} FROM users ORDER BY created_at");
    let users = sqlx::query_as::<_, User>(&query).fetch_all(pool).await?;
    Ok(users)
}

/// Delete a user by ID.
pub async fn delete_user(pool: &PgPool, id: Uuid) -> Result<(), DbError> {
    sqlx::query("DELETE FROM users WHERE id = $1")
        .bind(id)
        .execute(pool)
        .await?;
    Ok(())
}

/// Create a local user with email verification required (self-service signup).
#[allow(clippy::too_many_arguments)]
pub async fn create_local_user_with_verification(
    pool: &PgPool,
    username: &str,
    password_hash: &str,
    display_name: &str,
    email: &str,
    role: &str,
    token: &str,
    expires_at: chrono::DateTime<Utc>,
) -> Result<User, DbError> {
    let query = format!(
        "INSERT INTO users (username, password_hash, display_name, email, role, name, avatar_url, \
         email_verified, verification_token, verification_expires_at)
         VALUES ($1, $2, $3, $4, $5, $3, '', false, $6, $7)
         RETURNING {USER_COLUMNS}"
    );
    let user = sqlx::query_as::<_, User>(&query)
        .bind(username)
        .bind(password_hash)
        .bind(display_name)
        .bind(email)
        .bind(role)
        .bind(token)
        .bind(expires_at)
        .fetch_one(pool)
        .await?;

    Ok(user)
}

/// Look up a user by their verification token.
pub async fn get_user_by_verification_token(
    pool: &PgPool,
    token: &str,
) -> Result<Option<User>, DbError> {
    let query = format!("SELECT {USER_COLUMNS} FROM users WHERE verification_token = $1");
    let user = sqlx::query_as::<_, User>(&query)
        .bind(token)
        .fetch_optional(pool)
        .await?;

    Ok(user)
}

/// Mark a user's email as verified and clear the verification token.
pub async fn verify_user_email(pool: &PgPool, user_id: Uuid) -> Result<(), DbError> {
    sqlx::query(
        "UPDATE users SET email_verified = true, verification_token = NULL, \
         verification_expires_at = NULL, updated_at = now() WHERE id = $1",
    )
    .bind(user_id)
    .execute(pool)
    .await?;
    Ok(())
}

/// Look up a user by email address.
pub async fn get_user_by_email(pool: &PgPool, email: &str) -> Result<Option<User>, DbError> {
    let query = format!("SELECT {USER_COLUMNS} FROM users WHERE email = $1");
    let user = sqlx::query_as::<_, User>(&query)
        .bind(email)
        .fetch_optional(pool)
        .await?;

    Ok(user)
}

/// Check if any user with admin role exists.
pub async fn has_admin_user(pool: &PgPool) -> Result<bool, DbError> {
    let row: Option<(i64,)> =
        sqlx::query_as("SELECT COUNT(*) FROM users WHERE role = 'admin' AND username IS NOT NULL")
            .fetch_optional(pool)
            .await?;
    Ok(row.map(|r| r.0).unwrap_or(0) > 0)
}

// ---------------------------------------------------------------------------
// Organizations
// ---------------------------------------------------------------------------

const ORG_COLUMNS: &str = "id, owner_id, name, tier, stripe_customer_id, trial_expires_at, status, pipeline_limit, events_per_second_limit, monthly_event_limit, notes, created_at, updated_at, slug, org_type, parent_org_id, db_schema, k8s_namespace, kafka_topic_prefix";

/// Create a new organization owned by the given user.
/// Also inserts an `org_members` row with role `owner`.
/// Auto-generates a slug and provisions a tenant schema.
pub async fn create_organization(
    pool: &PgPool,
    owner_id: Uuid,
    name: &str,
) -> Result<Organization, DbError> {
    let base_slug = generate_slug(name);
    let slug = ensure_unique_slug(pool, &base_slug).await?;

    let query = format!(
        "INSERT INTO organizations (owner_id, name, slug) \
         VALUES ($1, $2, $3) RETURNING {ORG_COLUMNS}"
    );
    let org = sqlx::query_as::<_, Organization>(&query)
        .bind(owner_id)
        .bind(name)
        .bind(&slug)
        .fetch_one(pool)
        .await?;

    // Ensure the owner is also a member
    let _ = add_org_member(pool, org.id, owner_id, "owner").await;

    // Provision tenant schema (best-effort, log error)
    if let Err(e) = provision_tenant_schema(pool, org.id).await {
        tracing::error!("Failed to provision schema for org {}: {}", org.id, e);
    }

    // Re-read to get updated db_schema
    let org = get_organization(pool, org.id).await?.unwrap_or(org);

    Ok(org)
}

/// Create a new organization with trial status (30-day free trial).
/// Also inserts an `org_members` row with role `owner`.
/// Auto-generates a slug and provisions a tenant schema.
pub async fn create_trial_organization(
    pool: &PgPool,
    owner_id: Uuid,
    name: &str,
) -> Result<Organization, DbError> {
    let base_slug = generate_slug(name);
    let slug = ensure_unique_slug(pool, &base_slug).await?;

    let query = format!(
        "INSERT INTO organizations (owner_id, name, slug, status, trial_expires_at) \
         VALUES ($1, $2, $3, 'trial', now() + interval '30 days') \
         RETURNING {ORG_COLUMNS}"
    );
    let org = sqlx::query_as::<_, Organization>(&query)
        .bind(owner_id)
        .bind(name)
        .bind(&slug)
        .fetch_one(pool)
        .await?;

    // Ensure the owner is also a member
    let _ = add_org_member(pool, org.id, owner_id, "owner").await;

    // Provision tenant schema (best-effort, log error)
    if let Err(e) = provision_tenant_schema(pool, org.id).await {
        tracing::error!("Failed to provision schema for org {}: {}", org.id, e);
    }

    // Re-read to get updated db_schema
    let org = get_organization(pool, org.id).await?.unwrap_or(org);

    Ok(org)
}

/// Get an organization by its ID.
pub async fn get_organization(pool: &PgPool, id: Uuid) -> Result<Option<Organization>, DbError> {
    let query = format!("SELECT {ORG_COLUMNS} FROM organizations WHERE id = $1");
    let org = sqlx::query_as::<_, Organization>(&query)
        .bind(id)
        .fetch_optional(pool)
        .await?;

    Ok(org)
}

/// List all organizations a user is a member of (via `org_members` JOIN).
/// Falls back to `owner_id` match for backward compatibility.
pub async fn get_user_organizations(
    pool: &PgPool,
    user_id: Uuid,
) -> Result<Vec<Organization>, DbError> {
    let query = format!(
        "SELECT {ORG_COLUMNS} FROM organizations \
         WHERE id IN (\
             SELECT org_id FROM org_members WHERE user_id = $1 AND status = 'active' \
             UNION \
             SELECT id FROM organizations WHERE owner_id = $1\
         ) ORDER BY created_at"
    );
    let orgs = sqlx::query_as::<_, Organization>(&query)
        .bind(user_id)
        .fetch_all(pool)
        .await?;

    Ok(orgs)
}

/// Update the Stripe customer ID for an organization.
pub async fn update_org_stripe_customer(
    pool: &PgPool,
    org_id: Uuid,
    customer_id: &str,
) -> Result<(), DbError> {
    sqlx::query("UPDATE organizations SET stripe_customer_id = $1 WHERE id = $2")
        .bind(customer_id)
        .bind(org_id)
        .execute(pool)
        .await?;
    Ok(())
}

/// Update the tier of an organization.
pub async fn update_org_tier(pool: &PgPool, org_id: Uuid, tier: &str) -> Result<(), DbError> {
    sqlx::query("UPDATE organizations SET tier = $1, updated_at = now() WHERE id = $2")
        .bind(tier)
        .bind(org_id)
        .execute(pool)
        .await?;
    Ok(())
}

/// Get an organization by its Stripe customer ID.
pub async fn get_org_by_stripe_customer(
    pool: &PgPool,
    customer_id: &str,
) -> Result<Option<Organization>, DbError> {
    let query = format!("SELECT {ORG_COLUMNS} FROM organizations WHERE stripe_customer_id = $1");
    let org = sqlx::query_as::<_, Organization>(&query)
        .bind(customer_id)
        .fetch_optional(pool)
        .await?;
    Ok(org)
}

// ---------------------------------------------------------------------------
// Tenant Hierarchy
// ---------------------------------------------------------------------------

/// Create a sub-tenant under a parent tenant.
/// The parent must be org_type 'tenant' (no sub-sub-tenants allowed).
/// Sub-tenants inherit the parent's db_schema (shared schema, isolated by org_id).
pub async fn create_sub_tenant(
    pool: &PgPool,
    parent_org_id: Uuid,
    owner_id: Uuid,
    name: &str,
) -> Result<Organization, DbError> {
    // Verify parent is a tenant (not global, not sub_tenant)
    let parent = get_organization(pool, parent_org_id)
        .await?
        .ok_or_else(|| DbError::Pool("Parent organization not found".to_string()))?;
    if parent.org_type != "tenant" {
        return Err(DbError::Pool(
            "Sub-tenants can only be created under tenant-type organizations".to_string(),
        ));
    }

    let base_slug = generate_slug(name);
    let slug = ensure_unique_slug(pool, &base_slug).await?;

    // Sub-tenant inherits parent's db_schema (shared schema, RLS isolation)
    let inherited_schema = parent.db_schema.as_deref();

    let query = format!(
        "INSERT INTO organizations (owner_id, name, slug, org_type, parent_org_id, db_schema, status) \
         VALUES ($1, $2, $3, 'sub_tenant', $4, $5, 'active') \
         RETURNING {ORG_COLUMNS}"
    );
    let org = sqlx::query_as::<_, Organization>(&query)
        .bind(owner_id)
        .bind(name)
        .bind(&slug)
        .bind(parent_org_id)
        .bind(inherited_schema)
        .fetch_one(pool)
        .await?;

    // Ensure the owner is also a member
    let _ = add_org_member(pool, org.id, owner_id, "owner").await;

    Ok(org)
}

/// Get or create the singleton global organization.
pub async fn get_or_create_global_org(
    pool: &PgPool,
    owner_id: Uuid,
) -> Result<Organization, DbError> {
    let query =
        format!("SELECT {ORG_COLUMNS} FROM organizations WHERE org_type = 'global' LIMIT 1");
    if let Some(org) = sqlx::query_as::<_, Organization>(&query)
        .fetch_optional(pool)
        .await?
    {
        return Ok(org);
    }

    let query = format!(
        "INSERT INTO organizations (owner_id, name, slug, org_type, tier, status, db_schema) \
         VALUES ($1, 'Global', 'global', 'global', 'enterprise', 'active', 'tenant_global') \
         RETURNING {ORG_COLUMNS}"
    );
    let org = sqlx::query_as::<_, Organization>(&query)
        .bind(owner_id)
        .fetch_one(pool)
        .await?;

    let _ = add_org_member(pool, org.id, owner_id, "owner").await;

    // Provision global schema (best-effort)
    if let Err(e) = provision_tenant_schema(pool, org.id).await {
        tracing::error!("Failed to provision global schema: {}", e);
    }

    Ok(org)
}

/// Get the global organization (if it exists).
pub async fn get_global_org(pool: &PgPool) -> Result<Option<Organization>, DbError> {
    let query =
        format!("SELECT {ORG_COLUMNS} FROM organizations WHERE org_type = 'global' LIMIT 1");
    let org = sqlx::query_as::<_, Organization>(&query)
        .fetch_optional(pool)
        .await?;
    Ok(org)
}

/// List direct child organizations of a parent.
pub async fn list_child_organizations(
    pool: &PgPool,
    parent_id: Uuid,
) -> Result<Vec<Organization>, DbError> {
    let query = format!(
        "SELECT {ORG_COLUMNS} FROM organizations WHERE parent_org_id = $1 ORDER BY created_at"
    );
    let orgs = sqlx::query_as::<_, Organization>(&query)
        .bind(parent_id)
        .fetch_all(pool)
        .await?;
    Ok(orgs)
}

/// List all tenants (org_type = 'tenant'). For global admin view.
pub async fn list_tenants(pool: &PgPool) -> Result<Vec<Organization>, DbError> {
    let query = format!(
        "SELECT {ORG_COLUMNS} FROM organizations WHERE org_type = 'tenant' ORDER BY created_at"
    );
    let orgs = sqlx::query_as::<_, Organization>(&query)
        .fetch_all(pool)
        .await?;
    Ok(orgs)
}

/// List all sub-tenants of a given tenant.
pub async fn list_sub_tenants(
    pool: &PgPool,
    tenant_id: Uuid,
) -> Result<Vec<Organization>, DbError> {
    let query = format!(
        "SELECT {ORG_COLUMNS} FROM organizations \
         WHERE parent_org_id = $1 AND org_type = 'sub_tenant' ORDER BY created_at"
    );
    let orgs = sqlx::query_as::<_, Organization>(&query)
        .bind(tenant_id)
        .fetch_all(pool)
        .await?;
    Ok(orgs)
}

/// List pipelines visible to an org, including inherited ones from parent and global.
/// Returns own pipelines + inherited from parent tenant + inherited from global org.
pub async fn list_visible_pipelines(pool: &PgPool, org_id: Uuid) -> Result<Vec<Pipeline>, DbError> {
    // Get the org to determine its type and parent
    let org = get_organization(pool, org_id)
        .await?
        .ok_or_else(|| DbError::Pool("Organization not found".to_string()))?;

    match org.org_type.as_str() {
        "global" => {
            // Global sees only its own pipelines
            list_pipelines(pool, org_id).await
        }
        "tenant" => {
            // Tenant sees own pipelines (includes inherited global copies with org_id = tenant)
            let query = format!(
                "SELECT {PIPELINE_COLUMNS} FROM pipelines \
                 WHERE org_id = $1 \
                 ORDER BY scope_level DESC, created_at"
            );
            let pipelines = sqlx::query_as::<_, Pipeline>(&query)
                .bind(org_id)
                .fetch_all(pool)
                .await?;
            Ok(pipelines)
        }
        "sub_tenant" => {
            // Sub-tenant sees own pipelines (includes inherited copies with org_id = sub_tenant)
            // Inherited pipelines are copied to sub-tenant via propagate_pipeline_to_sub_tenants
            let query = format!(
                "SELECT {PIPELINE_COLUMNS} FROM pipelines \
                 WHERE org_id = $1 \
                 ORDER BY scope_level DESC, created_at"
            );
            let pipelines = sqlx::query_as::<_, Pipeline>(&query)
                .bind(org_id)
                .fetch_all(pool)
                .await?;
            Ok(pipelines)
        }
        _ => list_pipelines(pool, org_id).await,
    }
}

/// Create a pipeline with scope level and optional inheritance.
pub async fn create_scoped_pipeline(
    pool: &PgPool,
    org_id: Uuid,
    name: &str,
    vpl_source: &str,
    scope_level: &str,
) -> Result<Pipeline, DbError> {
    let query = format!(
        "INSERT INTO pipelines (org_id, name, vpl_source, scope_level) \
         VALUES ($1, $2, $3, $4) RETURNING {PIPELINE_COLUMNS}"
    );
    let pipeline = sqlx::query_as::<_, Pipeline>(&query)
        .bind(org_id)
        .bind(name)
        .bind(vpl_source)
        .bind(scope_level)
        .fetch_one(pool)
        .await?;
    Ok(pipeline)
}

/// Propagate a tenant-scoped pipeline to all sub-tenants as inherited.
pub async fn propagate_pipeline_to_sub_tenants(
    pool: &PgPool,
    pipeline: &Pipeline,
) -> Result<u64, DbError> {
    let sub_tenants = list_sub_tenants(pool, pipeline.org_id).await?;
    let mut count = 0u64;
    for sub in &sub_tenants {
        let query = format!(
            "INSERT INTO pipelines (org_id, name, vpl_source, scope_level, inherited_from_org_id) \
             VALUES ($1, $2, $3, $4, $5) \
             ON CONFLICT DO NOTHING \
             RETURNING {PIPELINE_COLUMNS}"
        );
        let result = sqlx::query_as::<_, Pipeline>(&query)
            .bind(sub.id)
            .bind(&pipeline.name)
            .bind(&pipeline.vpl_source)
            .bind(&pipeline.scope_level)
            .bind(pipeline.org_id)
            .fetch_optional(pool)
            .await?;
        if result.is_some() {
            count += 1;
        }
    }
    Ok(count)
}

// ---------------------------------------------------------------------------
// Slug Generation & Schema Provisioning
// ---------------------------------------------------------------------------

/// Generate a URL-safe slug from an org name.
/// Lowercases, replaces non-alphanumeric with underscore, collapses runs.
pub fn generate_slug(name: &str) -> String {
    let mut result = String::with_capacity(name.len());
    let mut prev_underscore = true; // start true to skip leading underscores
    for c in name.chars() {
        if c.is_ascii_alphanumeric() {
            result.push(c.to_ascii_lowercase());
            prev_underscore = false;
        } else if !prev_underscore {
            result.push('_');
            prev_underscore = true;
        }
    }
    result.trim_end_matches('_').to_string()
}

/// Ensure a slug is unique in the organizations table, appending a numeric suffix if needed.
pub async fn ensure_unique_slug(pool: &PgPool, base_slug: &str) -> Result<String, DbError> {
    let count: (i64,) = sqlx::query_as("SELECT COUNT(*) FROM organizations WHERE slug = $1")
        .bind(base_slug)
        .fetch_one(pool)
        .await?;
    if count.0 == 0 {
        return Ok(base_slug.to_string());
    }
    for i in 1..1000 {
        let candidate = format!("{base_slug}_{i}");
        let count: (i64,) = sqlx::query_as("SELECT COUNT(*) FROM organizations WHERE slug = $1")
            .bind(&candidate)
            .fetch_one(pool)
            .await?;
        if count.0 == 0 {
            return Ok(candidate);
        }
    }
    Err(DbError::Pool("Could not generate unique slug".to_string()))
}

/// Provision a PostgreSQL schema for a tenant organization.
/// Creates `tenant_{slug}` schema with data plane tables (pipelines, usage_daily).
/// Updates the org's `db_schema` column.
pub async fn provision_tenant_schema(pool: &PgPool, org_id: Uuid) -> Result<String, DbError> {
    let org = get_organization(pool, org_id)
        .await?
        .ok_or_else(|| DbError::Pool("Organization not found".to_string()))?;

    let slug = org
        .slug
        .as_ref()
        .ok_or_else(|| DbError::Pool("Organization has no slug".to_string()))?;

    let schema_name = format!("tenant_{slug}");

    // Validate schema name characters (prevent SQL injection)
    if !schema_name
        .chars()
        .all(|c| c.is_ascii_alphanumeric() || c == '_')
    {
        return Err(DbError::Pool("Invalid schema name".to_string()));
    }

    // Create schema
    sqlx::query(&format!("CREATE SCHEMA IF NOT EXISTS \"{schema_name}\""))
        .execute(pool)
        .await?;

    // Create data plane tables in tenant schema
    sqlx::query(&format!(
        "CREATE TABLE IF NOT EXISTS \"{schema_name}\".pipelines ( \
            id UUID PRIMARY KEY DEFAULT gen_random_uuid(), \
            org_id UUID NOT NULL REFERENCES public.organizations(id) ON DELETE CASCADE, \
            name TEXT NOT NULL, \
            vpl_source TEXT NOT NULL DEFAULT '', \
            status TEXT NOT NULL DEFAULT 'stopped', \
            created_at TIMESTAMPTZ NOT NULL DEFAULT now(), \
            updated_at TIMESTAMPTZ NOT NULL DEFAULT now(), \
            global_template_id UUID, \
            scope_level TEXT NOT NULL DEFAULT 'own', \
            inherited_from_org_id UUID REFERENCES public.organizations(id) ON DELETE CASCADE \
        )"
    ))
    .execute(pool)
    .await?;

    sqlx::query(&format!(
        "CREATE TABLE IF NOT EXISTS \"{schema_name}\".usage_daily ( \
            org_id UUID NOT NULL REFERENCES public.organizations(id) ON DELETE CASCADE, \
            date DATE NOT NULL, \
            events_processed BIGINT NOT NULL DEFAULT 0, \
            output_events BIGINT NOT NULL DEFAULT 0, \
            PRIMARY KEY (org_id, date) \
        )"
    ))
    .execute(pool)
    .await?;

    // Create indexes
    sqlx::query(&format!(
        "CREATE INDEX IF NOT EXISTS idx_pipelines_org ON \"{schema_name}\".pipelines(org_id)"
    ))
    .execute(pool)
    .await?;

    sqlx::query(&format!(
        "CREATE INDEX IF NOT EXISTS idx_usage_daily_org_date \
         ON \"{schema_name}\".usage_daily(org_id, date)"
    ))
    .execute(pool)
    .await?;

    // Update org with schema name
    sqlx::query("UPDATE organizations SET db_schema = $1 WHERE id = $2")
        .bind(&schema_name)
        .bind(org_id)
        .execute(pool)
        .await?;

    tracing::info!(
        "Provisioned tenant schema '{}' for org {}",
        schema_name,
        org_id
    );
    Ok(schema_name)
}

/// Verify that a PostgreSQL schema exists.
pub async fn verify_schema_exists(pool: &PgPool, schema_name: &str) -> Result<bool, DbError> {
    let result: Option<(String,)> = sqlx::query_as(
        "SELECT schema_name FROM information_schema.schemata WHERE schema_name = $1",
    )
    .bind(schema_name)
    .fetch_optional(pool)
    .await?;
    Ok(result.is_some())
}

/// Get the effective schema for an org (own db_schema or parent's).
pub async fn get_effective_schema(pool: &PgPool, org_id: Uuid) -> Result<Option<String>, DbError> {
    let org = get_organization(pool, org_id)
        .await?
        .ok_or_else(|| DbError::Pool("Organization not found".to_string()))?;

    if let Some(ref schema) = org.db_schema {
        return Ok(Some(schema.clone()));
    }

    // Sub-tenant: inherit from parent
    if let Some(parent_id) = org.parent_org_id {
        let parent = get_organization(pool, parent_id)
            .await?
            .ok_or_else(|| DbError::Pool("Parent organization not found".to_string()))?;
        return Ok(parent.db_schema);
    }

    Ok(None)
}

/// List tables in a tenant schema.
pub async fn list_schema_tables(pool: &PgPool, schema_name: &str) -> Result<Vec<String>, DbError> {
    let rows: Vec<(String,)> = sqlx::query_as(
        "SELECT table_name FROM information_schema.tables \
         WHERE table_schema = $1 ORDER BY table_name",
    )
    .bind(schema_name)
    .fetch_all(pool)
    .await?;
    Ok(rows.into_iter().map(|r| r.0).collect())
}

/// Acquire a connection with `search_path` set to the tenant's schema.
///
/// Returns a `PoolConnection` whose session has `SET search_path TO "tenant_slug", public`.
/// This allows queries to resolve unqualified table names to the tenant schema first,
/// falling back to `public` for shared tables (organizations, users, etc.).
///
/// The search_path reverts when the connection is returned to the pool (PostgreSQL
/// resets session state on connection reuse in sqlx).
pub async fn with_tenant_schema(
    pool: &PgPool,
    org_id: Uuid,
) -> Result<sqlx::pool::PoolConnection<sqlx::Postgres>, DbError> {
    let schema = get_effective_schema(pool, org_id).await?;

    let schema_name = match schema {
        Some(s) => s,
        None => {
            return pool
                .acquire()
                .await
                .map_err(|e| DbError::Pool(e.to_string()))
        }
    };

    // Validate schema name (prevent SQL injection)
    if !schema_name
        .chars()
        .all(|c| c.is_ascii_alphanumeric() || c == '_')
    {
        return Err(DbError::Pool(format!(
            "Invalid schema name: {}",
            schema_name
        )));
    }

    let mut conn = pool
        .acquire()
        .await
        .map_err(|e| DbError::Pool(e.to_string()))?;

    sqlx::query(&format!("SET search_path TO \"{schema_name}\", public"))
        .execute(conn.as_mut())
        .await?;

    Ok(conn)
}

// ---------------------------------------------------------------------------
// Organization Members
// ---------------------------------------------------------------------------

use crate::models::OrgMember;

/// Add a user as a member of an organization.
pub async fn add_org_member(
    pool: &PgPool,
    org_id: Uuid,
    user_id: Uuid,
    role: &str,
) -> Result<OrgMember, DbError> {
    let member = sqlx::query_as::<_, OrgMember>(
        "INSERT INTO org_members (org_id, user_id, role, status, accepted_at) \
         VALUES ($1, $2, $3, 'active', now()) \
         ON CONFLICT (org_id, user_id) DO UPDATE SET role = EXCLUDED.role \
         RETURNING id, org_id, user_id, role, status, invited_at, accepted_at",
    )
    .bind(org_id)
    .bind(user_id)
    .bind(role)
    .fetch_one(pool)
    .await?;

    Ok(member)
}

/// Get a user's membership in a specific organization.
pub async fn get_user_org_membership(
    pool: &PgPool,
    user_id: Uuid,
    org_id: Uuid,
) -> Result<Option<OrgMember>, DbError> {
    let member = sqlx::query_as::<_, OrgMember>(
        "SELECT id, org_id, user_id, role, status, invited_at, accepted_at \
         FROM org_members WHERE user_id = $1 AND org_id = $2",
    )
    .bind(user_id)
    .bind(org_id)
    .fetch_optional(pool)
    .await?;

    Ok(member)
}

/// List all memberships for a user (with the associated organization).
pub async fn get_user_memberships(
    pool: &PgPool,
    user_id: Uuid,
) -> Result<Vec<(OrgMember, Organization)>, DbError> {
    let members = sqlx::query_as::<_, OrgMember>(
        "SELECT id, org_id, user_id, role, status, invited_at, accepted_at \
         FROM org_members WHERE user_id = $1 AND status = 'active' ORDER BY invited_at",
    )
    .bind(user_id)
    .fetch_all(pool)
    .await?;

    let mut results = Vec::with_capacity(members.len());
    for member in members {
        if let Ok(Some(org)) = get_organization(pool, member.org_id).await {
            results.push((member, org));
        }
    }

    Ok(results)
}

/// List all members of an organization (with associated user info).
pub async fn list_org_members(
    pool: &PgPool,
    org_id: Uuid,
) -> Result<Vec<(OrgMember, User)>, DbError> {
    let members = sqlx::query_as::<_, OrgMember>(
        "SELECT id, org_id, user_id, role, status, invited_at, accepted_at \
         FROM org_members WHERE org_id = $1 ORDER BY invited_at",
    )
    .bind(org_id)
    .fetch_all(pool)
    .await?;

    let mut results = Vec::with_capacity(members.len());
    for member in members {
        if let Ok(Some(user)) = get_user_by_id(pool, member.user_id).await {
            results.push((member, user));
        }
    }

    Ok(results)
}

/// Remove a user from an organization.
pub async fn remove_org_member(pool: &PgPool, org_id: Uuid, user_id: Uuid) -> Result<(), DbError> {
    sqlx::query("DELETE FROM org_members WHERE org_id = $1 AND user_id = $2")
        .bind(org_id)
        .bind(user_id)
        .execute(pool)
        .await?;
    Ok(())
}

// ---------------------------------------------------------------------------
// Admin organization management
// ---------------------------------------------------------------------------

/// List all organizations (admin query).
pub async fn list_all_organizations(pool: &PgPool) -> Result<Vec<Organization>, DbError> {
    let query = format!("SELECT {ORG_COLUMNS} FROM organizations ORDER BY created_at");
    let orgs = sqlx::query_as::<_, Organization>(&query)
        .fetch_all(pool)
        .await?;
    Ok(orgs)
}

/// Update the status of an organization (active, trial, suspended, revoked).
pub async fn update_org_status(pool: &PgPool, org_id: Uuid, status: &str) -> Result<(), DbError> {
    sqlx::query("UPDATE organizations SET status = $1, updated_at = now() WHERE id = $2")
        .bind(status)
        .bind(org_id)
        .execute(pool)
        .await?;
    Ok(())
}

/// Update per-tenant resource limits.
pub async fn update_org_limits(
    pool: &PgPool,
    org_id: Uuid,
    pipeline_limit: i32,
    eps_limit: i32,
    monthly_limit: i64,
) -> Result<(), DbError> {
    sqlx::query(
        "UPDATE organizations SET pipeline_limit = $1, events_per_second_limit = $2, \
         monthly_event_limit = $3, updated_at = now() WHERE id = $4",
    )
    .bind(pipeline_limit)
    .bind(eps_limit)
    .bind(monthly_limit)
    .bind(org_id)
    .execute(pool)
    .await?;
    Ok(())
}

/// Extend a trial expiration date.
pub async fn extend_trial(
    pool: &PgPool,
    org_id: Uuid,
    new_expiry: chrono::DateTime<Utc>,
) -> Result<(), DbError> {
    sqlx::query(
        "UPDATE organizations SET trial_expires_at = $1, status = 'trial', updated_at = now() WHERE id = $2",
    )
    .bind(new_expiry)
    .bind(org_id)
    .execute(pool)
    .await?;
    Ok(())
}

/// Get all trial orgs whose trial has expired (for background expiry task).
pub async fn get_expiring_trials(
    pool: &PgPool,
    before: chrono::DateTime<Utc>,
) -> Result<Vec<Organization>, DbError> {
    let query = format!(
        "SELECT {ORG_COLUMNS} FROM organizations \
         WHERE status = 'trial' AND trial_expires_at IS NOT NULL AND trial_expires_at < $1"
    );
    let orgs = sqlx::query_as::<_, Organization>(&query)
        .bind(before)
        .fetch_all(pool)
        .await?;
    Ok(orgs)
}

/// Aggregated usage summary for an organization (current month).
pub async fn get_org_usage_summary(pool: &PgPool, org_id: Uuid) -> Result<i64, DbError> {
    let today = Utc::now().date_naive();
    let start = chrono::NaiveDate::from_ymd_opt(today.year(), today.month(), 1).unwrap_or(today);
    let row: Option<(i64,)> = sqlx::query_as(
        "SELECT COALESCE(SUM(events_processed), 0) FROM usage_daily \
         WHERE org_id = $1 AND date >= $2 AND date <= $3",
    )
    .bind(org_id)
    .bind(start)
    .bind(today)
    .fetch_optional(pool)
    .await?;
    Ok(row.map(|r| r.0).unwrap_or(0))
}

// ---------------------------------------------------------------------------
// API Keys
// ---------------------------------------------------------------------------

const API_KEY_COLUMNS: &str = "id, org_id, key_hash, name, created_at, last_used_at, \
                               key_prefix, scopes, expires_at, revoked_at, created_by";

/// Create a new API key for an organization.
pub async fn create_api_key(
    pool: &PgPool,
    org_id: Uuid,
    key_hash: &str,
    name: &str,
) -> Result<ApiKey, DbError> {
    let query = format!(
        "INSERT INTO api_keys (org_id, key_hash, name) VALUES ($1, $2, $3) RETURNING {API_KEY_COLUMNS}"
    );
    let key = sqlx::query_as::<_, ApiKey>(&query)
        .bind(org_id)
        .bind(key_hash)
        .bind(name)
        .fetch_one(pool)
        .await?;

    Ok(key)
}

/// Create an API key with extended attributes (prefix, scopes, expiry, creator).
#[allow(clippy::too_many_arguments)]
pub async fn create_api_key_extended(
    pool: &PgPool,
    org_id: Uuid,
    key_hash: &str,
    name: &str,
    key_prefix: &str,
    scopes: &str,
    expires_at: Option<chrono::DateTime<Utc>>,
    created_by: Option<Uuid>,
) -> Result<ApiKey, DbError> {
    let query = format!(
        "INSERT INTO api_keys (org_id, key_hash, name, key_prefix, scopes, expires_at, created_by) \
         VALUES ($1, $2, $3, $4, $5, $6, $7) RETURNING {API_KEY_COLUMNS}"
    );
    let key = sqlx::query_as::<_, ApiKey>(&query)
        .bind(org_id)
        .bind(key_hash)
        .bind(name)
        .bind(key_prefix)
        .bind(scopes)
        .bind(expires_at)
        .bind(created_by)
        .fetch_one(pool)
        .await?;

    Ok(key)
}

/// Look up an active API key by its hash (excludes revoked and expired keys).
pub async fn get_api_key_by_hash(pool: &PgPool, hash: &str) -> Result<Option<ApiKey>, DbError> {
    let query = format!(
        "SELECT {API_KEY_COLUMNS} FROM api_keys \
         WHERE key_hash = $1 AND revoked_at IS NULL \
         AND (expires_at IS NULL OR expires_at > now())"
    );
    let key = sqlx::query_as::<_, ApiKey>(&query)
        .bind(hash)
        .fetch_optional(pool)
        .await?;

    Ok(key)
}

/// List all active API keys for an organization (excludes revoked).
pub async fn list_api_keys(pool: &PgPool, org_id: Uuid) -> Result<Vec<ApiKey>, DbError> {
    let query = format!(
        "SELECT {API_KEY_COLUMNS} FROM api_keys \
         WHERE org_id = $1 AND revoked_at IS NULL ORDER BY created_at"
    );
    let keys = sqlx::query_as::<_, ApiKey>(&query)
        .bind(org_id)
        .fetch_all(pool)
        .await?;

    Ok(keys)
}

/// Delete an API key by its ID, scoped to an organization.
pub async fn delete_api_key(pool: &PgPool, id: Uuid, org_id: Uuid) -> Result<(), DbError> {
    let result = sqlx::query("DELETE FROM api_keys WHERE id = $1 AND org_id = $2")
        .bind(id)
        .bind(org_id)
        .execute(pool)
        .await?;

    if result.rows_affected() == 0 {
        return Err(sqlx::Error::RowNotFound.into());
    }

    Ok(())
}

/// Soft-delete (revoke) an API key by setting `revoked_at`, scoped to an org.
pub async fn revoke_api_key(pool: &PgPool, id: Uuid, org_id: Uuid) -> Result<(), DbError> {
    let result = sqlx::query(
        "UPDATE api_keys SET revoked_at = now() WHERE id = $1 AND org_id = $2 AND revoked_at IS NULL",
    )
    .bind(id)
    .bind(org_id)
    .execute(pool)
    .await?;

    if result.rows_affected() == 0 {
        return Err(sqlx::Error::RowNotFound.into());
    }

    Ok(())
}

/// Update the `last_used_at` timestamp of an API key to now.
pub async fn touch_api_key(pool: &PgPool, id: Uuid) -> Result<(), DbError> {
    sqlx::query("UPDATE api_keys SET last_used_at = $1 WHERE id = $2")
        .bind(Utc::now())
        .bind(id)
        .execute(pool)
        .await?;

    Ok(())
}

// ---------------------------------------------------------------------------
// Pipelines
// ---------------------------------------------------------------------------

const PIPELINE_COLUMNS: &str =
    "id, org_id, name, vpl_source, status, created_at, updated_at, global_template_id, scope_level, inherited_from_org_id";

/// Create a new pipeline for an organization.
pub async fn create_pipeline(
    pool: &PgPool,
    org_id: Uuid,
    name: &str,
    vpl_source: &str,
) -> Result<Pipeline, DbError> {
    let query = format!(
        "INSERT INTO pipelines (org_id, name, vpl_source) \
         VALUES ($1, $2, $3) RETURNING {PIPELINE_COLUMNS}"
    );
    let pipeline = sqlx::query_as::<_, Pipeline>(&query)
        .bind(org_id)
        .bind(name)
        .bind(vpl_source)
        .fetch_one(pool)
        .await?;

    Ok(pipeline)
}

/// Get a pipeline by its ID, scoped to an organization.
pub async fn get_pipeline(
    pool: &PgPool,
    id: Uuid,
    org_id: Uuid,
) -> Result<Option<Pipeline>, DbError> {
    let query = format!("SELECT {PIPELINE_COLUMNS} FROM pipelines WHERE id = $1 AND org_id = $2");
    let pipeline = sqlx::query_as::<_, Pipeline>(&query)
        .bind(id)
        .bind(org_id)
        .fetch_optional(pool)
        .await?;

    Ok(pipeline)
}

/// List all pipelines belonging to an organization.
pub async fn list_pipelines(pool: &PgPool, org_id: Uuid) -> Result<Vec<Pipeline>, DbError> {
    let query =
        format!("SELECT {PIPELINE_COLUMNS} FROM pipelines WHERE org_id = $1 ORDER BY created_at");
    let pipelines = sqlx::query_as::<_, Pipeline>(&query)
        .bind(org_id)
        .fetch_all(pool)
        .await?;

    Ok(pipelines)
}

/// Update the status of a pipeline, scoped to an organization.
pub async fn update_pipeline_status(
    pool: &PgPool,
    id: Uuid,
    org_id: Uuid,
    status: &str,
) -> Result<(), DbError> {
    sqlx::query("UPDATE pipelines SET status = $1, updated_at = $2 WHERE id = $3 AND org_id = $4")
        .bind(status)
        .bind(Utc::now())
        .bind(id)
        .bind(org_id)
        .execute(pool)
        .await?;

    Ok(())
}

/// Update the VPL source of a pipeline, scoped to an organization.
pub async fn update_pipeline_source(
    pool: &PgPool,
    id: Uuid,
    org_id: Uuid,
    vpl_source: &str,
) -> Result<(), DbError> {
    sqlx::query(
        "UPDATE pipelines SET vpl_source = $1, updated_at = $2 WHERE id = $3 AND org_id = $4",
    )
    .bind(vpl_source)
    .bind(Utc::now())
    .bind(id)
    .bind(org_id)
    .execute(pool)
    .await?;

    Ok(())
}

/// Delete a pipeline by its ID, scoped to an organization.
pub async fn delete_pipeline(pool: &PgPool, id: Uuid, org_id: Uuid) -> Result<(), DbError> {
    let result = sqlx::query("DELETE FROM pipelines WHERE id = $1 AND org_id = $2")
        .bind(id)
        .bind(org_id)
        .execute(pool)
        .await?;

    if result.rows_affected() == 0 {
        return Err(sqlx::Error::RowNotFound.into());
    }

    Ok(())
}

/// Delete a pipeline by name for a given org (used to sync runtime deletions to DB).
pub async fn delete_pipeline_by_name(
    pool: &PgPool,
    org_id: Uuid,
    name: &str,
) -> Result<(), DbError> {
    sqlx::query("DELETE FROM pipelines WHERE org_id = $1 AND name = $2")
        .bind(org_id)
        .bind(name)
        .execute(pool)
        .await?;
    Ok(())
}

// ---------------------------------------------------------------------------
// Kubernetes Namespace Management
// ---------------------------------------------------------------------------

/// Set the k8s_namespace for a tenant org.
pub async fn set_k8s_namespace(
    pool: &PgPool,
    org_id: Uuid,
    namespace: &str,
) -> Result<(), DbError> {
    sqlx::query("UPDATE organizations SET k8s_namespace = $1 WHERE id = $2")
        .bind(namespace)
        .bind(org_id)
        .execute(pool)
        .await?;
    Ok(())
}

/// Clear the k8s_namespace for a tenant org.
pub async fn clear_k8s_namespace(pool: &PgPool, org_id: Uuid) -> Result<(), DbError> {
    sqlx::query("UPDATE organizations SET k8s_namespace = NULL WHERE id = $1")
        .bind(org_id)
        .execute(pool)
        .await?;
    Ok(())
}

/// Get the effective k8s namespace for an org (own or parent's).
pub async fn get_effective_namespace(
    pool: &PgPool,
    org_id: Uuid,
) -> Result<Option<String>, DbError> {
    let org = get_organization(pool, org_id)
        .await?
        .ok_or_else(|| DbError::Pool("Organization not found".to_string()))?;

    if let Some(ns) = org.k8s_namespace {
        return Ok(Some(ns));
    }

    // Sub-tenants inherit parent's namespace
    if let Some(parent_id) = org.parent_org_id {
        if let Some(parent) = get_organization(pool, parent_id).await? {
            return Ok(parent.k8s_namespace);
        }
    }

    Ok(None)
}

// ---------------------------------------------------------------------------
// Kafka Topic Prefix Management
// ---------------------------------------------------------------------------

/// Set the kafka_topic_prefix for a tenant org.
pub async fn set_kafka_topic_prefix(
    pool: &PgPool,
    org_id: Uuid,
    prefix: &str,
) -> Result<(), DbError> {
    sqlx::query("UPDATE organizations SET kafka_topic_prefix = $1 WHERE id = $2")
        .bind(prefix)
        .bind(org_id)
        .execute(pool)
        .await?;
    Ok(())
}

/// Clear the kafka_topic_prefix for a tenant org.
pub async fn clear_kafka_topic_prefix(pool: &PgPool, org_id: Uuid) -> Result<(), DbError> {
    sqlx::query("UPDATE organizations SET kafka_topic_prefix = NULL WHERE id = $1")
        .bind(org_id)
        .execute(pool)
        .await?;
    Ok(())
}

/// Get the effective Kafka topic prefix for an org (own or parent's).
pub async fn get_effective_topic_prefix(
    pool: &PgPool,
    org_id: Uuid,
) -> Result<Option<String>, DbError> {
    let org = get_organization(pool, org_id)
        .await?
        .ok_or_else(|| DbError::Pool("Organization not found".to_string()))?;

    if let Some(prefix) = org.kafka_topic_prefix {
        return Ok(Some(prefix));
    }

    // Sub-tenants inherit parent's topic prefix
    if let Some(parent_id) = org.parent_org_id {
        if let Some(parent) = get_organization(pool, parent_id).await? {
            return Ok(parent.kafka_topic_prefix);
        }
    }

    Ok(None)
}

// ---------------------------------------------------------------------------
// Global Pipeline Templates
// ---------------------------------------------------------------------------

const GLOBAL_TEMPLATE_COLUMNS: &str =
    "id, name, vpl_source, status, deployed_by, created_at, updated_at";

/// Create a new global pipeline template.
pub async fn create_global_template(
    pool: &PgPool,
    name: &str,
    vpl_source: &str,
    deployed_by: Option<Uuid>,
) -> Result<GlobalPipelineTemplate, DbError> {
    let query = format!(
        "INSERT INTO global_pipeline_templates (name, vpl_source, deployed_by) \
         VALUES ($1, $2, $3) RETURNING {GLOBAL_TEMPLATE_COLUMNS}"
    );
    let template = sqlx::query_as::<_, GlobalPipelineTemplate>(&query)
        .bind(name)
        .bind(vpl_source)
        .bind(deployed_by)
        .fetch_one(pool)
        .await?;

    Ok(template)
}

/// List all global pipeline templates.
pub async fn list_global_templates(pool: &PgPool) -> Result<Vec<GlobalPipelineTemplate>, DbError> {
    let query = format!(
        "SELECT {GLOBAL_TEMPLATE_COLUMNS} FROM global_pipeline_templates ORDER BY created_at"
    );
    let templates = sqlx::query_as::<_, GlobalPipelineTemplate>(&query)
        .fetch_all(pool)
        .await?;

    Ok(templates)
}

/// Get a single global template by ID.
pub async fn get_global_template(
    pool: &PgPool,
    id: Uuid,
) -> Result<Option<GlobalPipelineTemplate>, DbError> {
    let query =
        format!("SELECT {GLOBAL_TEMPLATE_COLUMNS} FROM global_pipeline_templates WHERE id = $1");
    let template = sqlx::query_as::<_, GlobalPipelineTemplate>(&query)
        .bind(id)
        .fetch_optional(pool)
        .await?;

    Ok(template)
}

/// Update a global template's VPL source.
pub async fn update_global_template_source(
    pool: &PgPool,
    id: Uuid,
    vpl_source: &str,
) -> Result<(), DbError> {
    sqlx::query(
        "UPDATE global_pipeline_templates SET vpl_source = $1, updated_at = now() WHERE id = $2",
    )
    .bind(vpl_source)
    .bind(id)
    .execute(pool)
    .await?;

    Ok(())
}

/// Delete a global template (CASCADE removes all tenant copies).
pub async fn delete_global_template(pool: &PgPool, id: Uuid) -> Result<(), DbError> {
    sqlx::query("DELETE FROM global_pipeline_templates WHERE id = $1")
        .bind(id)
        .execute(pool)
        .await?;

    Ok(())
}

/// List only deployed global templates.
pub async fn list_deployed_global_templates(
    pool: &PgPool,
) -> Result<Vec<GlobalPipelineTemplate>, DbError> {
    let query = format!(
        "SELECT {GLOBAL_TEMPLATE_COLUMNS} FROM global_pipeline_templates \
         WHERE status = 'deployed' ORDER BY created_at"
    );
    let templates = sqlx::query_as::<_, GlobalPipelineTemplate>(&query)
        .fetch_all(pool)
        .await?;

    Ok(templates)
}

/// Create a pipeline copy linked to a global template.
pub async fn create_global_pipeline_copy(
    pool: &PgPool,
    org_id: Uuid,
    template_id: Uuid,
    name: &str,
    vpl_source: &str,
) -> Result<Pipeline, DbError> {
    // Find the global org to set inherited_from_org_id
    let global_org_id = get_global_org(pool).await?.map(|o| o.id);
    let query = format!(
        "INSERT INTO pipelines (org_id, name, vpl_source, global_template_id, scope_level, inherited_from_org_id) \
         VALUES ($1, $2, $3, $4, 'global', $5) RETURNING {PIPELINE_COLUMNS}"
    );
    let pipeline = sqlx::query_as::<_, Pipeline>(&query)
        .bind(org_id)
        .bind(name)
        .bind(vpl_source)
        .bind(template_id)
        .bind(global_org_id)
        .fetch_one(pool)
        .await?;

    Ok(pipeline)
}

/// List all pipeline copies of a given global template.
pub async fn list_global_template_copies(
    pool: &PgPool,
    template_id: Uuid,
) -> Result<Vec<Pipeline>, DbError> {
    let query = format!(
        "SELECT {PIPELINE_COLUMNS} FROM pipelines WHERE global_template_id = $1 ORDER BY created_at"
    );
    let pipelines = sqlx::query_as::<_, Pipeline>(&query)
        .bind(template_id)
        .fetch_all(pool)
        .await?;

    Ok(pipelines)
}

/// Update all pipeline copies of a global template with new VPL source.
pub async fn update_global_template_copies_source(
    pool: &PgPool,
    template_id: Uuid,
    vpl_source: &str,
) -> Result<u64, DbError> {
    let result = sqlx::query(
        "UPDATE pipelines SET vpl_source = $1, updated_at = now() WHERE global_template_id = $2",
    )
    .bind(vpl_source)
    .bind(template_id)
    .execute(pool)
    .await?;

    Ok(result.rows_affected())
}

// ---------------------------------------------------------------------------
// Usage
// ---------------------------------------------------------------------------

/// Record (upsert) daily usage metrics for an organization.
pub async fn record_usage(
    pool: &PgPool,
    org_id: Uuid,
    date: NaiveDate,
    events_processed: i64,
    output_events: i64,
) -> Result<(), DbError> {
    sqlx::query(
        r"
        INSERT INTO usage_daily (org_id, date, events_processed, output_events)
        VALUES ($1, $2, $3, $4)
        ON CONFLICT (org_id, date) DO UPDATE
            SET events_processed = usage_daily.events_processed + EXCLUDED.events_processed,
                output_events    = usage_daily.output_events    + EXCLUDED.output_events
        ",
    )
    .bind(org_id)
    .bind(date)
    .bind(events_processed)
    .bind(output_events)
    .execute(pool)
    .await?;

    Ok(())
}

/// Query daily usage metrics for an organization over a date range (inclusive).
pub async fn get_usage(
    pool: &PgPool,
    org_id: Uuid,
    start_date: NaiveDate,
    end_date: NaiveDate,
) -> Result<Vec<UsageDaily>, DbError> {
    let rows = sqlx::query_as::<_, UsageDaily>(
        r"
        SELECT org_id, date, events_processed, output_events
        FROM usage_daily
        WHERE org_id = $1 AND date >= $2 AND date <= $3
        ORDER BY date
        ",
    )
    .bind(org_id)
    .bind(start_date)
    .bind(end_date)
    .fetch_all(pool)
    .await?;

    Ok(rows)
}