ironclaw 0.24.0

Secure personal AI assistant that protects your data and expands its capabilities on the fly
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
//! UserStore implementation for LibSqlBackend.

use async_trait::async_trait;
use chrono::{DateTime, Utc};
use libsql::params;
use uuid::Uuid;

use super::{fmt_opt_ts, fmt_ts, get_opt_text, get_opt_ts, get_text, get_ts, opt_text};
use crate::db::libsql::LibSqlBackend;
use crate::db::{ApiTokenRecord, DatabaseError, UserRecord, UserStore};

fn row_to_user(row: &libsql::Row) -> Result<UserRecord, DatabaseError> {
    let metadata_str = get_text(row, 9);
    let metadata: serde_json::Value = serde_json::from_str(&metadata_str)
        .map_err(|e| DatabaseError::Serialization(e.to_string()))?;
    Ok(UserRecord {
        id: get_text(row, 0),
        email: get_opt_text(row, 1),
        display_name: get_text(row, 2),
        status: get_text(row, 3),
        role: get_text(row, 4),
        created_at: get_ts(row, 5),
        updated_at: get_ts(row, 6),
        last_login_at: get_opt_ts(row, 7),
        created_by: get_opt_text(row, 8),
        metadata,
    })
}

fn row_to_api_token(row: &libsql::Row) -> Result<ApiTokenRecord, DatabaseError> {
    let id_str = get_text(row, 0);
    let id: Uuid = id_str
        .parse()
        .map_err(|e| DatabaseError::Serialization(format!("invalid UUID: {e}")))?;
    Ok(ApiTokenRecord {
        id,
        user_id: get_text(row, 1),
        name: get_text(row, 2),
        token_prefix: get_text(row, 3),
        expires_at: get_opt_ts(row, 4),
        last_used_at: get_opt_ts(row, 5),
        created_at: get_ts(row, 6),
        revoked_at: get_opt_ts(row, 7),
    })
}

#[async_trait]
impl UserStore for LibSqlBackend {
    async fn create_user(&self, user: &UserRecord) -> Result<(), DatabaseError> {
        let conn = self.connect().await?;
        let metadata_json = serde_json::to_string(&user.metadata)
            .map_err(|e| DatabaseError::Serialization(e.to_string()))?;

        conn.execute(
            r#"
            INSERT INTO users (id, email, display_name, status, role, created_at, updated_at, last_login_at, created_by, metadata)
            VALUES (?1, ?2, ?3, ?4, ?5, ?6, ?7, ?8, ?9, ?10)
            "#,
            params![
                user.id.as_str(),
                opt_text(user.email.as_deref()),
                user.display_name.as_str(),
                user.status.as_str(),
                user.role.as_str(),
                fmt_ts(&user.created_at),
                fmt_ts(&user.updated_at),
                fmt_opt_ts(&user.last_login_at),
                opt_text(user.created_by.as_deref()),
                metadata_json,
            ],
        )
        .await
        .map_err(|e| DatabaseError::Query(e.to_string()))?;
        Ok(())
    }

    async fn get_user(&self, id: &str) -> Result<Option<UserRecord>, DatabaseError> {
        let conn = self.connect().await?;
        let mut rows = conn
            .query(
                r#"
                SELECT id, email, display_name, status, role, created_at, updated_at,
                       last_login_at, created_by, metadata
                FROM users WHERE id = ?1
                "#,
                params![id],
            )
            .await
            .map_err(|e| DatabaseError::Query(e.to_string()))?;

        match rows
            .next()
            .await
            .map_err(|e| DatabaseError::Query(e.to_string()))?
        {
            Some(row) => Ok(Some(row_to_user(&row)?)),
            None => Ok(None),
        }
    }

    async fn get_user_by_email(&self, email: &str) -> Result<Option<UserRecord>, DatabaseError> {
        let conn = self.connect().await?;
        let mut rows = conn
            .query(
                r#"
                SELECT id, email, display_name, status, role, created_at, updated_at,
                       last_login_at, created_by, metadata
                FROM users WHERE email = ?1
                "#,
                params![email],
            )
            .await
            .map_err(|e| DatabaseError::Query(e.to_string()))?;

        match rows
            .next()
            .await
            .map_err(|e| DatabaseError::Query(e.to_string()))?
        {
            Some(row) => Ok(Some(row_to_user(&row)?)),
            None => Ok(None),
        }
    }

    async fn list_users(&self, status: Option<&str>) -> Result<Vec<UserRecord>, DatabaseError> {
        let conn = self.connect().await?;
        let mut users = Vec::new();

        let mut rows = if let Some(status) = status {
            conn.query(
                r#"
                SELECT id, email, display_name, status, role, created_at, updated_at,
                       last_login_at, created_by, metadata
                FROM users WHERE status = ?1
                ORDER BY created_at DESC
                "#,
                params![status],
            )
            .await
            .map_err(|e| DatabaseError::Query(e.to_string()))?
        } else {
            conn.query(
                r#"
                SELECT id, email, display_name, status, role, created_at, updated_at,
                       last_login_at, created_by, metadata
                FROM users
                ORDER BY created_at DESC
                "#,
                (),
            )
            .await
            .map_err(|e| DatabaseError::Query(e.to_string()))?
        };

        while let Some(row) = rows
            .next()
            .await
            .map_err(|e| DatabaseError::Query(e.to_string()))?
        {
            users.push(row_to_user(&row)?);
        }
        Ok(users)
    }

    async fn update_user_status(&self, id: &str, status: &str) -> Result<(), DatabaseError> {
        let conn = self.connect().await?;
        let now = fmt_ts(&Utc::now());
        conn.execute(
            "UPDATE users SET status = ?2, updated_at = ?3 WHERE id = ?1",
            params![id, status, now],
        )
        .await
        .map_err(|e| DatabaseError::Query(e.to_string()))?;
        Ok(())
    }

    async fn update_user_role(&self, id: &str, role: &str) -> Result<(), DatabaseError> {
        let conn = self.connect().await?;
        let now = fmt_ts(&Utc::now());
        conn.execute(
            "UPDATE users SET role = ?2, updated_at = ?3 WHERE id = ?1",
            params![id, role, now],
        )
        .await
        .map_err(|e| DatabaseError::Query(e.to_string()))?;
        Ok(())
    }

    async fn update_user_profile(
        &self,
        id: &str,
        display_name: &str,
        metadata: &serde_json::Value,
    ) -> Result<(), DatabaseError> {
        let conn = self.connect().await?;
        let now = fmt_ts(&Utc::now());
        let metadata_json = serde_json::to_string(metadata)
            .map_err(|e| DatabaseError::Serialization(e.to_string()))?;
        conn.execute(
            "UPDATE users SET display_name = ?2, metadata = ?3, updated_at = ?4 WHERE id = ?1",
            params![id, display_name, metadata_json, now],
        )
        .await
        .map_err(|e| DatabaseError::Query(e.to_string()))?;
        Ok(())
    }

    async fn record_login(&self, id: &str) -> Result<(), DatabaseError> {
        let conn = self.connect().await?;
        let now = fmt_ts(&Utc::now());
        conn.execute(
            "UPDATE users SET last_login_at = ?2, updated_at = ?2 WHERE id = ?1",
            params![id, now],
        )
        .await
        .map_err(|e| DatabaseError::Query(e.to_string()))?;
        Ok(())
    }

    async fn create_api_token(
        &self,
        user_id: &str,
        name: &str,
        token_hash: &[u8; 32],
        token_prefix: &str,
        expires_at: Option<DateTime<Utc>>,
    ) -> Result<ApiTokenRecord, DatabaseError> {
        let conn = self.connect().await?;
        let id = Uuid::new_v4();
        let now = Utc::now();

        conn.execute(
            r#"
            INSERT INTO api_tokens (id, user_id, token_hash, token_prefix, name, expires_at, created_at)
            VALUES (?1, ?2, ?3, ?4, ?5, ?6, ?7)
            "#,
            params![
                id.to_string(),
                user_id,
                libsql::Value::Blob(token_hash.to_vec()),
                token_prefix,
                name,
                fmt_opt_ts(&expires_at),
                fmt_ts(&now),
            ],
        )
        .await
        .map_err(|e| DatabaseError::Query(e.to_string()))?;

        Ok(ApiTokenRecord {
            id,
            user_id: user_id.to_string(),
            name: name.to_string(),
            token_prefix: token_prefix.to_string(),
            expires_at,
            last_used_at: None,
            created_at: now,
            revoked_at: None,
        })
    }

    async fn list_api_tokens(&self, user_id: &str) -> Result<Vec<ApiTokenRecord>, DatabaseError> {
        let conn = self.connect().await?;
        let mut rows = conn
            .query(
                r#"
                SELECT id, user_id, name, token_prefix, expires_at, last_used_at, created_at, revoked_at
                FROM api_tokens WHERE user_id = ?1
                ORDER BY created_at DESC
                "#,
                params![user_id],
            )
            .await
            .map_err(|e| DatabaseError::Query(e.to_string()))?;

        let mut tokens = Vec::new();
        while let Some(row) = rows
            .next()
            .await
            .map_err(|e| DatabaseError::Query(e.to_string()))?
        {
            tokens.push(row_to_api_token(&row)?);
        }
        Ok(tokens)
    }

    async fn revoke_api_token(&self, token_id: Uuid, user_id: &str) -> Result<bool, DatabaseError> {
        let conn = self.connect().await?;
        let now = fmt_ts(&Utc::now());
        let rows_affected = conn
            .execute(
                r#"
                UPDATE api_tokens SET revoked_at = ?3
                WHERE id = ?1 AND user_id = ?2 AND revoked_at IS NULL
                "#,
                params![token_id.to_string(), user_id, now],
            )
            .await
            .map_err(|e| DatabaseError::Query(e.to_string()))?;
        Ok(rows_affected > 0)
    }

    async fn authenticate_token(
        &self,
        token_hash: &[u8; 32],
    ) -> Result<Option<(ApiTokenRecord, UserRecord)>, DatabaseError> {
        let conn = self.connect().await?;
        let now = fmt_ts(&Utc::now());

        let mut rows = conn
            .query(
                r#"
                SELECT
                    t.id, t.user_id, t.name, t.token_prefix, t.expires_at,
                    t.last_used_at, t.created_at, t.revoked_at,
                    u.id, u.email, u.display_name, u.status, u.role, u.created_at,
                    u.updated_at, u.last_login_at, u.created_by, u.metadata
                FROM api_tokens t
                JOIN users u ON u.id = t.user_id
                WHERE t.token_hash = ?1
                  AND t.revoked_at IS NULL
                  AND (t.expires_at IS NULL OR t.expires_at > ?2)
                  AND u.status = 'active'
                "#,
                params![libsql::Value::Blob(token_hash.to_vec()), now],
            )
            .await
            .map_err(|e| DatabaseError::Query(e.to_string()))?;

        match rows
            .next()
            .await
            .map_err(|e| DatabaseError::Query(e.to_string()))?
        {
            Some(row) => {
                let id_str = get_text(&row, 0);
                let token_id: Uuid = id_str
                    .parse()
                    .map_err(|e| DatabaseError::Serialization(format!("invalid UUID: {e}")))?;
                let token = ApiTokenRecord {
                    id: token_id,
                    user_id: get_text(&row, 1),
                    name: get_text(&row, 2),
                    token_prefix: get_text(&row, 3),
                    expires_at: get_opt_ts(&row, 4),
                    last_used_at: get_opt_ts(&row, 5),
                    created_at: get_ts(&row, 6),
                    revoked_at: get_opt_ts(&row, 7),
                };

                let metadata_str = get_text(&row, 17);
                let metadata: serde_json::Value = serde_json::from_str(&metadata_str)
                    .map_err(|e| DatabaseError::Serialization(e.to_string()))?;

                let user = UserRecord {
                    id: get_text(&row, 8),
                    email: get_opt_text(&row, 9),
                    display_name: get_text(&row, 10),
                    status: get_text(&row, 11),
                    role: get_text(&row, 12),
                    created_at: get_ts(&row, 13),
                    updated_at: get_ts(&row, 14),
                    last_login_at: get_opt_ts(&row, 15),
                    created_by: get_opt_text(&row, 16),
                    metadata,
                };

                Ok(Some((token, user)))
            }
            None => Ok(None),
        }
    }

    async fn record_token_usage(&self, token_id: Uuid) -> Result<(), DatabaseError> {
        let conn = self.connect().await?;
        let now = fmt_ts(&Utc::now());
        conn.execute(
            "UPDATE api_tokens SET last_used_at = ?2 WHERE id = ?1",
            params![token_id.to_string(), now],
        )
        .await
        .map_err(|e| DatabaseError::Query(e.to_string()))?;
        Ok(())
    }

    async fn has_any_users(&self) -> Result<bool, DatabaseError> {
        let conn = self.connect().await?;
        let mut rows = conn
            .query("SELECT 1 FROM users LIMIT 1", ())
            .await
            .map_err(|e| DatabaseError::Query(e.to_string()))?;

        let has_users = rows
            .next()
            .await
            .map_err(|e| DatabaseError::Query(e.to_string()))?
            .is_some();
        Ok(has_users)
    }

    async fn delete_user(&self, id: &str) -> Result<bool, DatabaseError> {
        let conn = self.connect().await?;

        conn.execute("BEGIN", ())
            .await
            .map_err(|e| DatabaseError::Query(e.to_string()))?;

        let result = async {
            // Delete from child tables first to avoid FK violations.
            // agent_jobs cascades to job_actions, llm_calls, estimation_snapshots
            // conversations cascades to conversation_messages
            // memory_documents cascades to memory_chunks
            // routines cascades to routine_runs
            for table in &[
                "settings",
                "heartbeat_state",
                "tool_rate_limit_state",
                "secret_usage_log",
                "leak_detection_events",
                "secrets",
                "wasm_tools",
                "routines",
                "memory_documents",
                "conversations",
                "api_tokens",
            ] {
                conn.execute(
                    &format!("DELETE FROM {} WHERE user_id = ?1", table),
                    params![id],
                )
                .await
                .map_err(|e| DatabaseError::Query(e.to_string()))?;
            }
            // job_events references agent_jobs(id) without CASCADE — delete via subquery.
            conn.execute(
                "DELETE FROM job_events WHERE job_id IN (SELECT id FROM agent_jobs WHERE user_id = ?1)",
                params![id],
            )
            .await
            .map_err(|e| DatabaseError::Query(e.to_string()))?;
            conn.execute("DELETE FROM agent_jobs WHERE user_id = ?1", params![id])
                .await
                .map_err(|e| DatabaseError::Query(e.to_string()))?;
            // Nullify self-referencing created_by before deleting the user
            conn.execute(
                "UPDATE users SET created_by = NULL WHERE created_by = ?1",
                params![id],
            )
            .await
            .map_err(|e| DatabaseError::Query(e.to_string()))?;
            let rows = conn
                .execute("DELETE FROM users WHERE id = ?1", params![id])
                .await
                .map_err(|e| DatabaseError::Query(e.to_string()))?;
            Ok::<_, DatabaseError>(rows > 0)
        }
        .await;

        match result {
            Ok(deleted) => {
                conn.execute("COMMIT", ())
                    .await
                    .map_err(|e| DatabaseError::Query(e.to_string()))?;
                Ok(deleted)
            }
            Err(e) => {
                let _ = conn.execute("ROLLBACK", ()).await;
                Err(e)
            }
        }
    }

    async fn user_usage_stats(
        &self,
        user_id: Option<&str>,
        since: DateTime<Utc>,
    ) -> Result<Vec<crate::db::UserUsageStats>, DatabaseError> {
        let conn = self.connect().await?;
        let since_str = fmt_ts(&since);
        let mut rows = if let Some(uid) = user_id {
            conn.query(
                r#"
                SELECT COALESCE(j.user_id, c.user_id) as user_id,
                       l.model, COUNT(*) as call_count,
                       COALESCE(SUM(l.input_tokens), 0) as input_tokens,
                       COALESCE(SUM(l.output_tokens), 0) as output_tokens,
                       CAST(COALESCE(SUM(l.cost), 0) AS TEXT) as total_cost
                FROM llm_calls l
                LEFT JOIN agent_jobs j ON l.job_id = j.id
                LEFT JOIN conversations c ON l.conversation_id = c.id
                WHERE l.created_at >= ?1
                  AND COALESCE(j.user_id, c.user_id) = ?2
                GROUP BY COALESCE(j.user_id, c.user_id), l.model
                ORDER BY total_cost DESC
                "#,
                params![since_str, uid],
            )
            .await
            .map_err(|e| DatabaseError::Query(e.to_string()))?
        } else {
            conn.query(
                r#"
                SELECT COALESCE(j.user_id, c.user_id) as user_id,
                       l.model, COUNT(*) as call_count,
                       COALESCE(SUM(l.input_tokens), 0) as input_tokens,
                       COALESCE(SUM(l.output_tokens), 0) as output_tokens,
                       CAST(COALESCE(SUM(l.cost), 0) AS TEXT) as total_cost
                FROM llm_calls l
                LEFT JOIN agent_jobs j ON l.job_id = j.id
                LEFT JOIN conversations c ON l.conversation_id = c.id
                WHERE l.created_at >= ?1
                GROUP BY COALESCE(j.user_id, c.user_id), l.model
                ORDER BY total_cost DESC
                "#,
                params![since_str],
            )
            .await
            .map_err(|e| DatabaseError::Query(e.to_string()))?
        };
        let mut stats = Vec::new();
        while let Some(row) = rows
            .next()
            .await
            .map_err(|e| DatabaseError::Query(e.to_string()))?
        {
            let cost_str = get_text(&row, 5);
            let total_cost = rust_decimal::Decimal::from_str_exact(&cost_str).map_err(|e| {
                DatabaseError::Query(format!("invalid cost value '{}': {}", cost_str, e))
            })?;
            stats.push(crate::db::UserUsageStats {
                user_id: get_text(&row, 0),
                model: get_text(&row, 1),
                call_count: row
                    .get::<i64>(2)
                    .map_err(|e| DatabaseError::Query(e.to_string()))?,
                input_tokens: row
                    .get::<i64>(3)
                    .map_err(|e| DatabaseError::Query(e.to_string()))?,
                output_tokens: row
                    .get::<i64>(4)
                    .map_err(|e| DatabaseError::Query(e.to_string()))?,
                total_cost,
            });
        }
        Ok(stats)
    }

    async fn create_user_with_token(
        &self,
        user: &UserRecord,
        token_name: &str,
        token_hash: &[u8; 32],
        token_prefix: &str,
        expires_at: Option<DateTime<Utc>>,
    ) -> Result<ApiTokenRecord, DatabaseError> {
        let conn = self.connect().await?;
        let metadata_json = serde_json::to_string(&user.metadata)
            .map_err(|e| DatabaseError::Serialization(e.to_string()))?;

        conn.execute("BEGIN", ())
            .await
            .map_err(|e| DatabaseError::Query(e.to_string()))?;

        // Insert user
        if let Err(e) = conn
            .execute(
                r#"
                INSERT INTO users (id, email, display_name, status, role, created_at, updated_at, last_login_at, created_by, metadata)
                VALUES (?1, ?2, ?3, ?4, ?5, ?6, ?7, ?8, ?9, ?10)
                "#,
                params![
                    user.id.as_str(),
                    opt_text(user.email.as_deref()),
                    user.display_name.as_str(),
                    user.status.as_str(),
                    user.role.as_str(),
                    fmt_ts(&user.created_at),
                    fmt_ts(&user.updated_at),
                    fmt_opt_ts(&user.last_login_at),
                    opt_text(user.created_by.as_deref()),
                    metadata_json,
                ],
            )
            .await
        {
            let _ = conn.execute("ROLLBACK", ()).await;
            return Err(DatabaseError::Query(e.to_string()));
        }

        // Insert token
        let id = Uuid::new_v4();
        let now = Utc::now();
        if let Err(e) = conn
            .execute(
                r#"
                INSERT INTO api_tokens (id, user_id, token_hash, token_prefix, name, expires_at, created_at)
                VALUES (?1, ?2, ?3, ?4, ?5, ?6, ?7)
                "#,
                params![
                    id.to_string(),
                    user.id.as_str(),
                    libsql::Value::Blob(token_hash.to_vec()),
                    token_prefix,
                    token_name,
                    fmt_opt_ts(&expires_at),
                    fmt_ts(&now),
                ],
            )
            .await
        {
            let _ = conn.execute("ROLLBACK", ()).await;
            return Err(DatabaseError::Query(e.to_string()));
        }

        conn.execute("COMMIT", ())
            .await
            .map_err(|e| DatabaseError::Query(e.to_string()))?;

        Ok(ApiTokenRecord {
            id,
            user_id: user.id.clone(),
            name: token_name.to_string(),
            token_prefix: token_prefix.to_string(),
            expires_at,
            last_used_at: None,
            created_at: now,
            revoked_at: None,
        })
    }

    async fn user_summary_stats(
        &self,
        user_id: Option<&str>,
    ) -> Result<Vec<crate::db::UserSummaryStats>, DatabaseError> {
        let conn = self.connect().await?;
        // Aggregate from llm_calls, resolving user_id via either agent_jobs
        // (for background job calls) or conversations (for chat calls where
        // job_id is NULL). Also count distinct agent_jobs per user.
        let mut rows = if let Some(uid) = user_id {
            conn.query(
                r#"
                SELECT
                    COALESCE(j.user_id, c.user_id) AS user_id,
                    COUNT(DISTINCT j.id) AS job_count,
                    CAST(COALESCE(SUM(l.cost), 0) AS TEXT) AS total_cost,
                    MAX(l.created_at) AS last_active_at
                FROM llm_calls l
                LEFT JOIN agent_jobs j ON l.job_id = j.id
                LEFT JOIN conversations c ON l.conversation_id = c.id
                WHERE COALESCE(j.user_id, c.user_id) = ?1
                GROUP BY COALESCE(j.user_id, c.user_id)
                "#,
                params![uid],
            )
            .await
            .map_err(|e| DatabaseError::Query(e.to_string()))?
        } else {
            conn.query(
                r#"
                SELECT
                    COALESCE(j.user_id, c.user_id) AS user_id,
                    COUNT(DISTINCT j.id) AS job_count,
                    CAST(COALESCE(SUM(l.cost), 0) AS TEXT) AS total_cost,
                    MAX(l.created_at) AS last_active_at
                FROM llm_calls l
                LEFT JOIN agent_jobs j ON l.job_id = j.id
                LEFT JOIN conversations c ON l.conversation_id = c.id
                GROUP BY COALESCE(j.user_id, c.user_id)
                "#,
                (),
            )
            .await
            .map_err(|e| DatabaseError::Query(e.to_string()))?
        };
        let mut stats = Vec::new();
        while let Some(row) = rows
            .next()
            .await
            .map_err(|e| DatabaseError::Query(e.to_string()))?
        {
            let cost_str = get_text(&row, 2);
            let total_cost = rust_decimal::Decimal::from_str_exact(&cost_str).map_err(|e| {
                DatabaseError::Query(format!("invalid cost value '{}': {}", cost_str, e))
            })?;
            stats.push(crate::db::UserSummaryStats {
                user_id: get_text(&row, 0),
                job_count: row
                    .get::<i64>(1)
                    .map_err(|e| DatabaseError::Query(e.to_string()))?,
                total_cost,
                last_active_at: get_opt_ts(&row, 3),
            });
        }
        Ok(stats)
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::db::libsql::LibSqlBackend;
    use crate::db::{Database, UserStore};
    use sha2::{Digest, Sha256};

    fn hash(s: &str) -> [u8; 32] {
        let mut h = Sha256::new();
        h.update(s.as_bytes());
        h.finalize().into()
    }

    async fn setup() -> (LibSqlBackend, tempfile::TempDir) {
        let dir = tempfile::tempdir().unwrap();
        let db_path = dir.path().join("test_users.db");
        let db = LibSqlBackend::new_local(&db_path).await.unwrap();
        db.run_migrations().await.unwrap();
        (db, dir) // keep dir alive so the DB file isn't deleted
    }

    fn test_user(id: &str) -> UserRecord {
        UserRecord {
            id: id.to_string(),
            email: Some(format!("{}@test.com", id)),
            display_name: id.to_string(),
            status: "active".to_string(),
            role: "member".to_string(),
            created_at: Utc::now(),
            updated_at: Utc::now(),
            last_login_at: None,
            created_by: None,
            metadata: serde_json::json!({}),
        }
    }

    #[tokio::test]
    async fn test_has_any_users_empty() {
        let (db, _dir) = setup().await;
        assert!(!db.has_any_users().await.unwrap());
    }

    #[tokio::test]
    async fn test_create_and_get_user() {
        let (db, _dir) = setup().await;
        let user = test_user("alice");
        db.create_user(&user).await.unwrap();

        assert!(db.has_any_users().await.unwrap());

        let found = db.get_user("alice").await.unwrap().unwrap();
        assert_eq!(found.id, "alice");
        assert_eq!(found.email, Some("alice@test.com".to_string()));
        assert_eq!(found.status, "active");
    }

    #[tokio::test]
    async fn test_get_user_by_email() {
        let (db, _dir) = setup().await;
        db.create_user(&test_user("bob")).await.unwrap();

        let found = db.get_user_by_email("bob@test.com").await.unwrap();
        assert!(found.is_some());
        assert_eq!(found.unwrap().id, "bob");

        assert!(
            db.get_user_by_email("nobody@test.com")
                .await
                .unwrap()
                .is_none()
        );
    }

    #[tokio::test]
    async fn test_list_users_with_status_filter() {
        let (db, _dir) = setup().await;
        db.create_user(&test_user("alice")).await.unwrap();
        db.create_user(&test_user("bob")).await.unwrap();
        db.update_user_status("bob", "suspended").await.unwrap();

        let all = db.list_users(None).await.unwrap();
        assert_eq!(all.len(), 2);

        let active = db.list_users(Some("active")).await.unwrap();
        assert_eq!(active.len(), 1);
        assert_eq!(active[0].id, "alice");

        let suspended = db.list_users(Some("suspended")).await.unwrap();
        assert_eq!(suspended.len(), 1);
        assert_eq!(suspended[0].id, "bob");
    }

    #[tokio::test]
    async fn test_update_user_profile() {
        let (db, _dir) = setup().await;
        db.create_user(&test_user("alice")).await.unwrap();

        let meta = serde_json::json!({"role": "admin"});
        db.update_user_profile("alice", "Alice Smith", &meta)
            .await
            .unwrap();

        let user = db.get_user("alice").await.unwrap().unwrap();
        assert_eq!(user.display_name, "Alice Smith");
        assert_eq!(user.metadata["role"], "admin");
    }

    #[tokio::test]
    async fn test_token_lifecycle_create_authenticate_revoke() {
        let (db, _dir) = setup().await;
        db.create_user(&test_user("alice")).await.unwrap();

        // Create token
        let token_hash = hash("secret-token-123");
        let record = db
            .create_api_token("alice", "laptop", &token_hash, "secret-t", None)
            .await
            .unwrap();
        assert_eq!(record.user_id, "alice");
        assert_eq!(record.name, "laptop");
        assert_eq!(record.token_prefix, "secret-t");

        // Authenticate
        let (tok, user) = db.authenticate_token(&token_hash).await.unwrap().unwrap();
        assert_eq!(tok.id, record.id);
        assert_eq!(user.id, "alice");

        // List tokens
        let tokens = db.list_api_tokens("alice").await.unwrap();
        assert_eq!(tokens.len(), 1);

        // Revoke
        assert!(db.revoke_api_token(record.id, "alice").await.unwrap());

        // Auth should fail after revoke
        assert!(db.authenticate_token(&token_hash).await.unwrap().is_none());
    }

    #[tokio::test]
    async fn test_token_auth_fails_for_suspended_user() {
        let (db, _dir) = setup().await;
        db.create_user(&test_user("alice")).await.unwrap();

        let token_hash = hash("token-abc");
        db.create_api_token("alice", "test", &token_hash, "token-ab", None)
            .await
            .unwrap();

        // Auth works while active
        assert!(db.authenticate_token(&token_hash).await.unwrap().is_some());

        // Suspend user
        db.update_user_status("alice", "suspended").await.unwrap();

        // Auth should fail
        assert!(db.authenticate_token(&token_hash).await.unwrap().is_none());
    }

    #[tokio::test]
    async fn test_token_revoke_wrong_user_returns_false() {
        let (db, _dir) = setup().await;
        db.create_user(&test_user("alice")).await.unwrap();
        db.create_user(&test_user("bob")).await.unwrap();

        let token_hash = hash("alice-token");
        let record = db
            .create_api_token("alice", "test", &token_hash, "alice-to", None)
            .await
            .unwrap();

        // Bob can't revoke Alice's token
        assert!(!db.revoke_api_token(record.id, "bob").await.unwrap());

        // Alice can
        assert!(db.revoke_api_token(record.id, "alice").await.unwrap());
    }

    #[tokio::test]
    async fn test_record_login_and_token_usage() {
        let (db, _dir) = setup().await;
        db.create_user(&test_user("alice")).await.unwrap();

        let token_hash = hash("tok");
        let record = db
            .create_api_token("alice", "test", &token_hash, "tok", None)
            .await
            .unwrap();

        // Record usage
        db.record_token_usage(record.id).await.unwrap();
        db.record_login("alice").await.unwrap();

        // Verify timestamps updated
        let user = db.get_user("alice").await.unwrap().unwrap();
        assert!(user.last_login_at.is_some());

        let tokens = db.list_api_tokens("alice").await.unwrap();
        assert!(tokens[0].last_used_at.is_some());
    }

    #[tokio::test]
    async fn test_delete_user_removes_api_tokens() {
        let (db, _dir) = setup().await;
        db.create_user(&test_user("alice")).await.unwrap();

        let token_hash = hash("alice-tok");
        db.create_api_token("alice", "primary", &token_hash, "alice-to", None)
            .await
            .unwrap();

        // Verify token exists before deletion.
        let tokens = db.list_api_tokens("alice").await.unwrap();
        assert_eq!(tokens.len(), 1);

        // Delete user — should also remove their api_tokens.
        assert!(db.delete_user("alice").await.unwrap());

        // api_tokens must be gone (not orphaned).
        let tokens = db.list_api_tokens("alice").await.unwrap();
        assert!(
            tokens.is_empty(),
            "expected api_tokens to be deleted with user, found {}",
            tokens.len()
        );
    }

    /// Helper: insert a minimal agent_job row for testing usage stats.
    async fn insert_test_job(db: &LibSqlBackend, job_id: &str, user_id: &str) {
        let conn = db.connect().await.unwrap();
        conn.execute(
            "INSERT INTO agent_jobs (id, title, description, status, source, user_id, created_at) \
             VALUES (?1, 'test', 'test job', 'completed', 'test', ?2, ?3)",
            params![job_id, user_id, fmt_ts(&Utc::now())],
        )
        .await
        .unwrap();
    }

    /// Helper: insert a minimal llm_call row for testing usage stats.
    async fn insert_test_llm_call(db: &LibSqlBackend, job_id: &str, model: &str, cost: &str) {
        let conn = db.connect().await.unwrap();
        let call_id = uuid::Uuid::new_v4().to_string();
        conn.execute(
            "INSERT INTO llm_calls (id, job_id, provider, model, input_tokens, output_tokens, cost, created_at) \
             VALUES (?1, ?2, 'test', ?3, 100, 50, ?4, ?5)",
            params![call_id, job_id, model, cost, fmt_ts(&Utc::now())],
        )
        .await
        .unwrap();
    }

    #[tokio::test]
    async fn test_user_summary_stats_empty() {
        let (db, _dir) = setup().await;
        let stats = db.user_summary_stats(None).await.unwrap();
        assert!(stats.is_empty());
    }

    #[tokio::test]
    async fn test_user_summary_stats_with_jobs_and_costs() {
        let (db, _dir) = setup().await;
        db.create_user(&test_user("alice")).await.unwrap();
        db.create_user(&test_user("bob")).await.unwrap();

        // Alice: 2 jobs, 2 LLM calls
        insert_test_job(&db, "job-a1", "alice").await;
        insert_test_job(&db, "job-a2", "alice").await;
        insert_test_llm_call(&db, "job-a1", "gpt-4", "0.05").await;
        insert_test_llm_call(&db, "job-a2", "gpt-4", "0.10").await;

        // Bob: 1 job, no LLM calls
        insert_test_job(&db, "job-b1", "bob").await;

        // All users with LLM calls (Bob has a job but no LLM calls, so no stats)
        let stats = db.user_summary_stats(None).await.unwrap();
        assert_eq!(stats.len(), 1);

        let alice_stats = stats.iter().find(|s| s.user_id == "alice").unwrap();
        assert_eq!(alice_stats.job_count, 2);
        assert_eq!(
            alice_stats.total_cost,
            rust_decimal::Decimal::from_str_exact("0.15").unwrap()
        );
        assert!(alice_stats.last_active_at.is_some());

        // Bob has no LLM calls so doesn't appear in summary stats
        assert!(!stats.iter().any(|s| s.user_id == "bob"));

        // Filter to single user
        let alice_only = db.user_summary_stats(Some("alice")).await.unwrap();
        assert_eq!(alice_only.len(), 1);
        assert_eq!(alice_only[0].job_count, 2);

        // Bob returns empty when filtered
        let bob_only = db.user_summary_stats(Some("bob")).await.unwrap();
        assert!(bob_only.is_empty());
    }

    #[tokio::test]
    async fn test_user_usage_stats_with_calls() {
        let (db, _dir) = setup().await;
        db.create_user(&test_user("alice")).await.unwrap();

        insert_test_job(&db, "job-a1", "alice").await;
        insert_test_llm_call(&db, "job-a1", "gpt-4", "0.05").await;
        insert_test_llm_call(&db, "job-a1", "gpt-4", "0.10").await;
        insert_test_llm_call(&db, "job-a1", "gpt-3.5", "0.01").await;

        let since = chrono::Utc::now() - chrono::Duration::hours(1);
        let stats = db.user_usage_stats(None, since).await.unwrap();

        // Two models used
        assert_eq!(stats.len(), 2);

        let gpt4 = stats.iter().find(|s| s.model == "gpt-4").unwrap();
        assert_eq!(gpt4.call_count, 2);
        assert_eq!(gpt4.input_tokens, 200);
        assert_eq!(gpt4.output_tokens, 100);
        assert_eq!(
            gpt4.total_cost,
            rust_decimal::Decimal::from_str_exact("0.15").unwrap()
        );

        let gpt35 = stats.iter().find(|s| s.model == "gpt-3.5").unwrap();
        assert_eq!(gpt35.call_count, 1);
    }
}