autumn-admin-plugin 0.5.0

Out-of-the-box admin panel plugin for autumn-web applications
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
//! Core traits that models implement to participate in the admin panel.

use std::future::Future;
use std::pin::Pin;

use chrono::{DateTime, Utc};
use serde::Deserialize;
use serde_json::Value;

// ── Field metadata ──────────────────────────────────────────────────

/// The kind of a model field, used to select the appropriate form widget.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum AdminFieldKind {
    /// Single-line text input.
    Text,
    /// Multi-line textarea.
    TextArea,
    /// Integer input.
    Integer,
    /// Floating-point input.
    Float,
    /// Boolean checkbox.
    Boolean,
    /// Date picker (no time).
    Date,
    /// Date + time picker.
    DateTime,
    /// Select dropdown with fixed choices.
    Select(Vec<SelectOption>),
    /// Hidden field (shown in detail, not editable).
    Hidden,
    /// Password field (write-only, never displayed).
    Password,
    /// JSON editor.
    Json,
}

/// A single option in a [`AdminFieldKind::Select`] dropdown.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct SelectOption {
    pub value: String,
    pub label: String,
}

/// Metadata for a single model field.
#[derive(Debug, Clone)]
#[allow(clippy::struct_excessive_bools)] // orthogonal flags on a plain config record
pub struct AdminField {
    /// Column name in the database / struct field name.
    pub name: &'static str,
    /// Human-readable label for the UI.
    pub label: String,
    /// Widget type.
    pub kind: AdminFieldKind,
    /// Whether this field appears in the list view table.
    pub list_display: bool,
    /// Whether this field is searchable (included in text search).
    pub searchable: bool,
    /// Whether this field can be used as a filter.
    pub filterable: bool,
    /// Whether this field is required on create/edit forms.
    pub required: bool,
    /// Whether this field is editable (false for IDs, timestamps, etc.).
    pub editable: bool,
    /// Sort priority in list view (None = not sortable).
    pub sortable: bool,
    /// Whether this column is encrypted at rest (#805). When set, the field is
    /// rendered as a disabled, redacted, unsubmitted control in forms (so its
    /// plaintext is never placed into the HTML and a save never overwrites the
    /// stored ciphertext), and — unless [`Self::encrypted_visible`] is also set —
    /// redacted (`••••••••`) in list and detail views.
    ///
    /// This is a per-field flag rather than a global column-name lookup so that an
    /// unrelated resource with a same-named plaintext field stays fully editable.
    pub encrypted: bool,
    /// For an [`Self::encrypted`] column, show its decrypted plaintext in list and
    /// detail (read) views — the `#[encrypted(admin_visible)]` opt-in. Edit forms
    /// still never pre-fill the plaintext. Has no effect unless `encrypted` is set.
    pub encrypted_visible: bool,
}

impl AdminField {
    /// Create a new field with sensible defaults.
    ///
    /// By default: displayed in list, not searchable, not filterable,
    /// required, and sortable. Editable defaults to `true` except for
    /// [`AdminFieldKind::Hidden`], which is read-only by contract
    /// (and is therefore excluded from `strip_meta_fields` acceptance
    /// even if a caller later flips `editable` back to `true`).
    #[must_use]
    pub fn new(name: &'static str, kind: AdminFieldKind) -> Self {
        let editable = !matches!(kind, AdminFieldKind::Hidden);
        Self {
            name,
            label: humanize_field_name(name),
            kind,
            list_display: true,
            searchable: false,
            filterable: false,
            required: true,
            editable,
            sortable: true,
            encrypted: false,
            encrypted_visible: false,
        }
    }

    /// Mark this column as encrypted at rest (#805): redacted in read views and
    /// rendered as a disabled, unsubmitted control in forms.
    #[must_use]
    pub const fn encrypted(mut self) -> Self {
        self.encrypted = true;
        self
    }

    /// Mark this column as encrypted at rest but show its decrypted plaintext in
    /// read views (the `#[encrypted(admin_visible)]` opt-in). Implies
    /// [`Self::encrypted`]; edit forms still never pre-fill the plaintext.
    #[must_use]
    pub const fn encrypted_visible(mut self) -> Self {
        self.encrypted = true;
        self.encrypted_visible = true;
        self
    }

    /// Set the human-readable label.
    #[must_use]
    pub fn label(mut self, label: impl Into<String>) -> Self {
        self.label = label.into();
        self
    }

    /// Mark this field as searchable.
    #[must_use]
    pub const fn searchable(mut self) -> Self {
        self.searchable = true;
        self
    }

    /// Mark this field as filterable.
    #[must_use]
    pub const fn filterable(mut self) -> Self {
        self.filterable = true;
        self
    }

    /// Mark this field as optional.
    #[must_use]
    pub const fn optional(mut self) -> Self {
        self.required = false;
        self
    }

    /// Mark this field as read-only.
    #[must_use]
    pub const fn readonly(mut self) -> Self {
        self.editable = false;
        self
    }

    /// Hide this field from the list view.
    #[must_use]
    pub const fn hide_from_list(mut self) -> Self {
        self.list_display = false;
        self
    }
}

// ── Bulk actions ────────────────────────────────────────────────────

/// A named bulk action that can be performed on selected records.
pub struct AdminAction {
    /// Machine name (used in form values).
    pub name: &'static str,
    /// Human-readable label for the button.
    pub label: String,
    /// CSS class for styling (e.g., "danger" for destructive actions).
    pub style: ActionStyle,
    /// Whether a confirmation dialog is shown before executing.
    pub confirm: bool,
}

/// Visual style for an admin action button.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ActionStyle {
    /// Default/neutral style.
    Default,
    /// Primary/positive action.
    Primary,
    /// Destructive/dangerous action (red).
    Danger,
}

// ── Version history ──────────────────────────────────────────────────

/// A single entry in the admin History pane for an opted-in model.
///
/// Mirrors [`autumn_web::VersionEntry`] but is decoupled from the runtime
/// type so the admin plugin has no compile-time dependency on the DB feature.
#[derive(Debug, Clone)]
pub struct AdminHistoryEntry {
    /// Auto-incrementing PK in the history table.
    pub id: i64,
    /// Actor identifier (`user_id` or `"system"`).
    pub actor: String,
    /// Operation: `"insert"`, `"update"`, or `"delete"`.
    pub op: String,
    /// Request / trace correlation ID.
    pub request_id: Option<String>,
    /// Column-level changes, serialized as JSON for template rendering.
    pub changes: Vec<Value>,
    /// When this entry was recorded.
    pub recorded_at: DateTime<Utc>,
}

/// Paginated history result for the admin History pane.
#[derive(Debug, Clone)]
pub struct AdminHistoryPage {
    pub entries: Vec<AdminHistoryEntry>,
    pub total: u64,
    pub page: u64,
    pub per_page: u64,
}

impl AdminHistoryPage {
    /// Total number of pages.
    #[must_use]
    pub const fn total_pages(&self) -> u64 {
        if self.per_page == 0 {
            return 0;
        }
        self.total.div_ceil(self.per_page)
    }

    /// Whether there is a next page.
    #[must_use]
    pub const fn has_next_page(&self) -> bool {
        self.page < self.total_pages()
    }
}

// ── The core trait ──────────────────────────────────────────────────

/// Type alias for the boxed future returned by async `AdminModel` methods.
pub type AdminFuture<'a, T> = Pin<Box<dyn Future<Output = Result<T, AdminError>> + Send + 'a>>;

/// Error type for admin operations.
#[derive(Debug, thiserror::Error)]
pub enum AdminError {
    #[error("Record not found")]
    NotFound,

    #[error("Validation failed: {0}")]
    Validation(String),

    #[error("Database error: {0}")]
    Database(String),

    #[error("{0}")]
    Other(String),
}

// ── CSV import types ────────────────────────────────────────────────

/// Mode for an admin CSV import operation.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum CsvImportMode {
    /// Insert every row as a new record.
    #[default]
    Insert,
    /// Dry-run: validate rows but do not write anything.
    DryRun,
}

impl CsvImportMode {
    /// Parse from a form field value.
    ///
    /// Returns `None` for unrecognised values so callers can reject them with a
    /// 400 instead of silently falling back to the destructive `Insert` path.
    /// An *absent* mode field should default to `Insert` without calling this.
    #[must_use]
    pub fn from_form_value(s: &str) -> Option<Self> {
        match s {
            "insert" | "Insert" => Some(Self::Insert),
            "dry_run" | "DryRun" | "dry-run" => Some(Self::DryRun),
            _ => None,
        }
    }
}

/// The outcome of processing a single imported CSV row.
#[derive(Debug)]
pub enum AdminImportRowResult {
    /// The row was inserted as a new record.
    Inserted,
    /// The row updated an existing record.
    Updated,
    /// The row was intentionally skipped.
    Skipped,
    /// A row-level error (no specific column).
    RowError(String),
    /// A field-level error with a column name.
    FieldError { column: String, message: String },
}

/// Summary of a completed (or dry-run) admin CSV import.
#[derive(Debug, Default, Clone)]
pub struct AdminImportReport {
    pub inserted: u64,
    pub updated: u64,
    pub skipped: u64,
    pub errors: Vec<AdminImportError>,
}

/// A single parse/validation error from an admin CSV import.
#[derive(Debug, Clone)]
pub struct AdminImportError {
    /// 1-based CSV line number (header = line 1).
    pub line: u64,
    /// Column name, if known.
    pub column: Option<String>,
    /// Human-readable description.
    pub message: String,
}

/// The core trait that enables a model to be managed in the admin panel.
///
/// Implementors provide field metadata, CRUD operations, and display
/// configuration. The admin plugin uses this trait to generate all views
/// dynamically at runtime.
///
/// # Design notes
///
/// All data flows through `serde_json::Value` to keep the trait object-safe.
/// The admin panel doesn't need to know concrete types — it renders fields
/// based on [`AdminField`] metadata and passes values as JSON.
pub trait AdminModel: Send + Sync + 'static {
    /// URL-safe slug for this model (e.g., "projects", "tickets").
    /// Used in admin URLs: `/admin/projects/`, `/admin/projects/42/`.
    fn slug(&self) -> &'static str;

    /// Human-readable singular name (e.g., "Project").
    fn display_name(&self) -> &'static str;

    /// Human-readable plural name (e.g., "Projects").
    fn display_name_plural(&self) -> &'static str;

    /// Field metadata for this model.
    fn fields(&self) -> Vec<AdminField>;

    /// Available bulk actions.
    ///
    /// Defaults to "Delete selected". When `supports_soft_delete()` returns
    /// `true`, also includes "Restore selected" and "Purge selected" so that
    /// the admin route validator can dispatch those action names.
    fn actions(&self) -> Vec<AdminAction> {
        let mut acts = vec![AdminAction {
            name: "delete",
            label: "Delete selected".to_owned(),
            style: ActionStyle::Danger,
            confirm: true,
        }];
        if self.supports_soft_delete() {
            acts.push(AdminAction {
                name: "restore",
                label: "Restore selected".to_owned(),
                style: ActionStyle::Default,
                confirm: false,
            });
            acts.push(AdminAction {
                name: "purge",
                label: "Purge selected".to_owned(),
                style: ActionStyle::Danger,
                confirm: true,
            });
        }
        acts
    }

    // ── CRUD operations ─────────────────────────────────────────

    /// List records with pagination, search, sort, and filters.
    fn list(
        &self,
        pool: &diesel_async::pooled_connection::deadpool::Pool<diesel_async::AsyncPgConnection>,
        params: ListParams,
    ) -> AdminFuture<'_, ListResult>;

    /// Get a single record by ID.
    fn get(
        &self,
        pool: &diesel_async::pooled_connection::deadpool::Pool<diesel_async::AsyncPgConnection>,
        id: i64,
    ) -> AdminFuture<'_, Option<Value>>;

    /// Create a new record from form data.
    fn create(
        &self,
        pool: &diesel_async::pooled_connection::deadpool::Pool<diesel_async::AsyncPgConnection>,
        data: Value,
    ) -> AdminFuture<'_, Value>;

    /// Update an existing record.
    fn update(
        &self,
        pool: &diesel_async::pooled_connection::deadpool::Pool<diesel_async::AsyncPgConnection>,
        id: i64,
        data: Value,
    ) -> AdminFuture<'_, Value>;

    /// Delete a record by ID.
    fn delete(
        &self,
        pool: &diesel_async::pooled_connection::deadpool::Pool<diesel_async::AsyncPgConnection>,
        id: i64,
    ) -> AdminFuture<'_, ()>;

    /// Whether this model supports soft-delete (defaults to `false`).
    ///
    /// When `true`, the admin panel shows a Trash tab with restore/purge.
    fn supports_soft_delete(&self) -> bool {
        false
    }

    /// Restore a soft-deleted record (set `deleted_at = NULL`).
    ///
    /// The default returns `AdminError::Other` when `supports_soft_delete()` is
    /// `false`, so models that opt in must override this method.
    fn restore<'a>(
        &'a self,
        _pool: &'a diesel_async::pooled_connection::deadpool::Pool<diesel_async::AsyncPgConnection>,
        _id: i64,
    ) -> AdminFuture<'a, ()> {
        Box::pin(async move {
            Err(AdminError::Other(
                "this model does not support soft delete; \
                 override supports_soft_delete() to return true and implement restore()"
                    .to_owned(),
            ))
        })
    }

    /// Permanently delete (purge) a soft-deleted record.
    ///
    /// The default returns `AdminError::Other` when `supports_soft_delete()` is
    /// `false`.
    fn purge<'a>(
        &'a self,
        _pool: &'a diesel_async::pooled_connection::deadpool::Pool<diesel_async::AsyncPgConnection>,
        _id: i64,
    ) -> AdminFuture<'a, ()> {
        Box::pin(async move {
            Err(AdminError::Other(
                "this model does not support soft delete; \
                 override supports_soft_delete() to return true and implement purge()"
                    .to_owned(),
            ))
        })
    }

    /// List soft-deleted records (where `deleted_at IS NOT NULL`).
    ///
    /// The default returns `AdminError::Other` when `supports_soft_delete()` is
    /// `false`.
    fn list_deleted<'a>(
        &'a self,
        _pool: &'a diesel_async::pooled_connection::deadpool::Pool<diesel_async::AsyncPgConnection>,
        _params: ListParams,
    ) -> AdminFuture<'a, ListResult> {
        Box::pin(async move {
            Err(AdminError::Other(
                "this model does not support soft delete; \
                 override supports_soft_delete() to return true and implement list_deleted()"
                    .to_owned(),
            ))
        })
    }

    /// Execute a bulk action on the given IDs.
    fn execute_action(
        &self,
        pool: &diesel_async::pooled_connection::deadpool::Pool<diesel_async::AsyncPgConnection>,
        action: &str,
        ids: Vec<i64>,
    ) -> AdminFuture<'_, u64> {
        // Default implementation: dispatch the built-in `"delete"`, `"restore"`,
        // and `"purge"` actions. Any other action name returns an error so it
        // doesn't silently no-op — overriders that declare custom actions must
        // implement them here.
        //
        // We clone the pool (deadpool::Pool is Arc-backed, cheap) so the
        // returned future only borrows from `&self` and avoids the
        // lifetime mismatch between `&self` and `&pool` that would
        // otherwise show up in the trait's elided `'_` return signature.
        let action = action.to_owned();
        let pool = pool.clone();
        Box::pin(async move {
            match action.as_str() {
                "delete" => {
                    let mut count: u64 = 0;
                    for id in ids {
                        self.delete(&pool, id).await?;
                        count += 1;
                    }
                    Ok(count)
                }
                "restore" => {
                    let mut count: u64 = 0;
                    for id in ids {
                        self.restore(&pool, id).await?;
                        count += 1;
                    }
                    Ok(count)
                }
                "purge" => {
                    let mut count: u64 = 0;
                    for id in ids {
                        self.purge(&pool, id).await?;
                        count += 1;
                    }
                    Ok(count)
                }
                other => Err(AdminError::Other(format!(
                    "unhandled bulk action '{other}'; \
                     override AdminModel::execute_action to support it"
                ))),
            }
        })
    }

    /// Return a display string for a record (used in breadcrumbs, titles).
    ///
    /// Defaults to `"ModelName #id"` (or `"ModelName <no id>"` when the
    /// record has no numeric `id`).
    fn record_display(&self, record: &Value) -> String {
        record_id(record).map_or_else(
            || format!("{} <no id>", self.display_name()),
            |id| format!("{} #{id}", self.display_name()),
        )
    }

    /// Records per page in the list view. Override to taste.
    fn per_page(&self) -> u64 {
        25
    }

    /// Count records matching a list query (defaults to `list(..., per_page: 0).total`).
    ///
    /// Override if the backend can count without materializing records.
    fn count(
        &self,
        pool: &diesel_async::pooled_connection::deadpool::Pool<diesel_async::AsyncPgConnection>,
    ) -> AdminFuture<'_, u64> {
        let params = ListParams {
            page: 1,
            per_page: 0,
            ..Default::default()
        };
        let fut = self.list(pool, params);
        Box::pin(async move { fut.await.map(|r| r.total) })
    }

    // ── CSV import / export ─────────────────────────────────────────

    /// Whether this model exposes a `GET /admin/{slug}/export.csv` link.
    ///
    /// Defaults to `false` — export must be explicitly opted into to avoid
    /// silently exposing model data on upgrade. Override to `true` to enable
    /// the CSV export button and route.
    fn supports_csv_export(&self) -> bool {
        false
    }

    /// Column names written to the CSV header row during export.
    ///
    /// Defaults to the ordered names of all non-password, non-hidden fields
    /// declared in [`fields`]. Override to add computed columns (e.g. a
    /// joined display value) or to omit sensitive columns (PII redaction).
    ///
    /// # PII redaction strategy
    ///
    /// To redact a column: remove it from this list. To include a placeholder
    /// instead of the real value, add the column here and override
    /// [`AdminModel::csv_export_row`] to return `"[REDACTED]"` for that key.
    ///
    /// [`fields`]: AdminModel::fields
    fn csv_export_columns(&self) -> Vec<&'static str> {
        self.fields()
            .into_iter()
            .filter(|f| {
                !matches!(f.kind, AdminFieldKind::Password | AdminFieldKind::Hidden) && !f.encrypted
            })
            .map(|f| f.name)
            .collect()
    }

    /// Serialize a single record (as returned by [`list`]) into an ordered
    /// list of string values for CSV export.
    ///
    /// The default implementation extracts values for each column in
    /// [`csv_export_columns`] from the JSON record. Override to add computed
    /// columns (joined values, formatted timestamps, etc.).
    ///
    /// [`list`]: AdminModel::list
    /// [`csv_export_columns`]: AdminModel::csv_export_columns
    fn csv_export_row(&self, columns: &[&str], record: &Value) -> Vec<String> {
        columns
            .iter()
            .map(|col| {
                record
                    .get(*col)
                    .map(|v| match v {
                        Value::String(s) => escape_csv_formula(s),
                        Value::Null => String::new(),
                        other => other.to_string(),
                    })
                    .unwrap_or_default()
            })
            .collect()
    }

    /// Whether this model accepts `POST /admin/{slug}/import` CSV uploads.
    ///
    /// Defaults to `false` — import must be explicitly opted into because it
    /// performs bulk writes. Override to `true` and implement
    /// [`import_csv_row`] to enable the import UI.
    ///
    /// [`import_csv_row`]: AdminModel::import_csv_row
    fn supports_csv_import(&self) -> bool {
        false
    }

    /// Process a single CSV row during an admin import.
    ///
    /// Receives the **1-based line number** in the CSV file and a map of
    /// `column_name → value` (all strings; coerce as needed). Return the
    /// appropriate [`AdminImportRowResult`].
    ///
    /// The default always returns `AdminImportRowResult::Skipped` — models
    /// that set [`supports_csv_import`] to `true` should override this.
    ///
    /// [`supports_csv_import`]: AdminModel::supports_csv_import
    fn import_csv_row<'a>(
        &'a self,
        _pool: &'a diesel_async::pooled_connection::deadpool::Pool<diesel_async::AsyncPgConnection>,
        _line: u64,
        _row: std::collections::HashMap<String, String>,
        _mode: CsvImportMode,
    ) -> AdminFuture<'a, AdminImportRowResult> {
        Box::pin(async move { Ok(AdminImportRowResult::Skipped) })
    }

    /// Whether this model has automatic record version history enabled.
    ///
    /// When `true`, the admin panel renders a **History** affordance on the
    /// detail page and serves `/{slug}/{id}/history`.
    fn has_history(&self) -> bool {
        false
    }

    /// Retrieve a paginated page of version history entries for a record.
    ///
    /// The default implementation returns [`AdminError::Other`] so models
    /// that do not opt in get a clear error instead of a silent no-op.
    fn get_history<'a>(
        &'a self,
        _pool: &'a diesel_async::pooled_connection::deadpool::Pool<diesel_async::AsyncPgConnection>,
        _record_id: i64,
        _page: u64,
        _per_page: u64,
    ) -> AdminFuture<'a, AdminHistoryPage> {
        Box::pin(async move {
            Err(AdminError::Other(
                "this model does not have version history enabled; \
                 use #[repository(Model, versioned = true)] to opt in"
                    .to_owned(),
            ))
        })
    }
}

// ── VersionPage → AdminHistoryPage conversion ──────────────────────

impl From<autumn_web::version_history::VersionEntry> for AdminHistoryEntry {
    fn from(e: autumn_web::version_history::VersionEntry) -> Self {
        Self {
            id: e.id,
            actor: e.actor,
            op: e.op.to_string(),
            request_id: e.request_id,
            changes: e
                .changes
                .into_iter()
                .map(|c| serde_json::to_value(&c).unwrap_or(serde_json::Value::Null))
                .collect(),
            recorded_at: e.recorded_at,
        }
    }
}

impl From<autumn_web::version_history::VersionPage> for AdminHistoryPage {
    /// Convert a [`autumn_web::version_history::VersionPage`] returned by a
    /// versioned repository's `version_history()` method into an
    /// [`AdminHistoryPage`] for the admin panel.
    ///
    /// ```rust,ignore
    /// fn get_history<'a>(
    ///     &'a self, pool: &'a Pool<AsyncPgConnection>,
    ///     record_id: i64, page: u64, per_page: u64,
    /// ) -> AdminFuture<'a, AdminHistoryPage> {
    ///     let pool = pool.clone();
    ///     Box::pin(async move {
    ///         let repo = PgPostRepository::from_pool(pool);
    ///         let filter = autumn_web::VersionFilter { page, per_page, ..Default::default() };
    ///         repo.version_history(record_id, filter).await
    ///             .map(AdminHistoryPage::from)
    ///             .map_err(|e| AdminError::Database(e.to_string()))
    ///     })
    /// }
    /// ```
    fn from(vp: autumn_web::version_history::VersionPage) -> Self {
        Self {
            entries: vp
                .entries
                .into_iter()
                .map(AdminHistoryEntry::from)
                .collect(),
            total: vp.total,
            page: vp.page,
            per_page: vp.per_page,
        }
    }
}

/// Extract the `"id"` field of a record as `i64`.
///
/// Returns `None` when the field is missing or non-numeric. Callers in
/// mutation paths should treat `None` as an error (the model returned a
/// payload without a routable identifier); display contexts may fall back
/// to a placeholder like `"#?"`.
#[must_use]
pub fn record_id(record: &Value) -> Option<i64> {
    record.get("id").and_then(Value::as_i64)
}

// ── Query parameters ────────────────────────────────────────────────

/// Parameters for a list query.
#[derive(Debug, Clone, Default)]
pub struct ListParams {
    /// Page number (1-indexed).
    pub page: u64,
    /// Records per page.
    pub per_page: u64,
    /// Full-text search query.
    pub search: Option<String>,
    /// Column to sort by.
    pub sort_by: Option<String>,
    /// Sort direction.
    pub sort_dir: SortDirection,
    /// Active filters (`field_name` → value).
    pub filters: Vec<(String, String)>,
}

/// Sort direction for list queries.
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum SortDirection {
    #[default]
    Asc,
    Desc,
}

impl SortDirection {
    /// URL-friendly representation (`"asc"` / `"desc"`).
    #[must_use]
    pub const fn as_str(self) -> &'static str {
        match self {
            Self::Asc => "asc",
            Self::Desc => "desc",
        }
    }

    /// The opposite direction (used to flip sort on re-click).
    #[must_use]
    pub const fn flipped(self) -> Self {
        match self {
            Self::Asc => Self::Desc,
            Self::Desc => Self::Asc,
        }
    }
}

/// Result of a list query, containing records and pagination metadata.
#[derive(Debug, Clone)]
pub struct ListResult {
    /// The records for the current page (as JSON objects).
    pub records: Vec<Value>,
    /// Total number of records matching the query (for pagination).
    pub total: u64,
    /// Current page number.
    pub page: u64,
    /// Records per page.
    pub per_page: u64,
}

impl ListResult {
    /// Total number of pages.
    #[must_use]
    pub const fn total_pages(&self) -> u64 {
        if self.per_page == 0 {
            return 0;
        }
        self.total.div_ceil(self.per_page)
    }
}

// ── Helpers ─────────────────────────────────────────────────────────

/// Prefix a CSV cell value with `'` when it starts with a formula-triggering
/// character (`=`, `+`, `-`, `@`, tab, or CR) to prevent spreadsheet formula
/// injection.
fn escape_csv_formula(s: &str) -> String {
    match s.bytes().next() {
        Some(b'=' | b'+' | b'-' | b'@' | b'\t' | b'\r') => {
            let mut out = String::with_capacity(s.len() + 1);
            out.push('\'');
            out.push_str(s);
            out
        }
        _ => s.to_owned(),
    }
}

/// Convert a `snake_case` field name to a human-readable label.
///
/// `"created_at"` → `"Created At"`, `"user_id"` → `"User Id"`.
fn humanize_field_name(name: &str) -> String {
    let mut s = String::with_capacity(name.len());
    for (i, word) in name.split('_').enumerate() {
        if i > 0 {
            s.push(' ');
        }
        let mut chars = word.chars();
        if let Some(c) = chars.next() {
            s.extend(c.to_uppercase());
            s.extend(chars);
        }
    }
    s
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::sync::Mutex;

    /// Test fixture: an `AdminModel` whose `delete` records the id it was
    /// asked to delete. Doesn't override `execute_action` — that's the
    /// behaviour under test.
    struct DeletingModel {
        deleted: Mutex<Vec<i64>>,
        fail_on: Option<i64>,
    }

    impl AdminModel for DeletingModel {
        fn slug(&self) -> &'static str {
            "tracked"
        }
        fn display_name(&self) -> &'static str {
            "Tracked"
        }
        fn display_name_plural(&self) -> &'static str {
            "Tracked"
        }
        fn fields(&self) -> Vec<AdminField> {
            vec![]
        }
        fn list(
            &self,
            _pool: &diesel_async::pooled_connection::deadpool::Pool<
                diesel_async::AsyncPgConnection,
            >,
            _params: ListParams,
        ) -> AdminFuture<'_, ListResult> {
            Box::pin(async {
                Ok(ListResult {
                    records: vec![],
                    total: 0,
                    page: 1,
                    per_page: 25,
                })
            })
        }
        fn get(
            &self,
            _pool: &diesel_async::pooled_connection::deadpool::Pool<
                diesel_async::AsyncPgConnection,
            >,
            _id: i64,
        ) -> AdminFuture<'_, Option<Value>> {
            Box::pin(async { Ok(None) })
        }
        fn create(
            &self,
            _pool: &diesel_async::pooled_connection::deadpool::Pool<
                diesel_async::AsyncPgConnection,
            >,
            data: Value,
        ) -> AdminFuture<'_, Value> {
            Box::pin(async move { Ok(data) })
        }
        fn update(
            &self,
            _pool: &diesel_async::pooled_connection::deadpool::Pool<
                diesel_async::AsyncPgConnection,
            >,
            _id: i64,
            data: Value,
        ) -> AdminFuture<'_, Value> {
            Box::pin(async move { Ok(data) })
        }
        fn delete(
            &self,
            _pool: &diesel_async::pooled_connection::deadpool::Pool<
                diesel_async::AsyncPgConnection,
            >,
            id: i64,
        ) -> AdminFuture<'_, ()> {
            let deleted = &self.deleted;
            let fail_on = self.fail_on;
            Box::pin(async move {
                if Some(id) == fail_on {
                    return Err(AdminError::Database("simulated failure".into()));
                }
                deleted.lock().unwrap().push(id);
                Ok(())
            })
        }
    }

    /// Test fixture: an `AdminModel` that supports soft delete and records
    /// which ids were restored/purged. Overrides `supports_soft_delete()` and
    /// the three soft-delete methods.
    #[derive(Default)]
    struct SoftDeleteModel {
        restored: Mutex<Vec<i64>>,
        purged: Mutex<Vec<i64>>,
    }

    impl AdminModel for SoftDeleteModel {
        fn slug(&self) -> &'static str {
            "soft"
        }
        fn display_name(&self) -> &'static str {
            "Soft"
        }
        fn display_name_plural(&self) -> &'static str {
            "Softs"
        }
        fn fields(&self) -> Vec<AdminField> {
            vec![]
        }
        fn list(
            &self,
            _pool: &diesel_async::pooled_connection::deadpool::Pool<
                diesel_async::AsyncPgConnection,
            >,
            _params: ListParams,
        ) -> AdminFuture<'_, ListResult> {
            Box::pin(async {
                Ok(ListResult {
                    records: vec![],
                    total: 0,
                    page: 1,
                    per_page: 25,
                })
            })
        }
        fn get(
            &self,
            _pool: &diesel_async::pooled_connection::deadpool::Pool<
                diesel_async::AsyncPgConnection,
            >,
            _id: i64,
        ) -> AdminFuture<'_, Option<Value>> {
            Box::pin(async { Ok(None) })
        }
        fn create(
            &self,
            _pool: &diesel_async::pooled_connection::deadpool::Pool<
                diesel_async::AsyncPgConnection,
            >,
            data: Value,
        ) -> AdminFuture<'_, Value> {
            Box::pin(async move { Ok(data) })
        }
        fn update(
            &self,
            _pool: &diesel_async::pooled_connection::deadpool::Pool<
                diesel_async::AsyncPgConnection,
            >,
            _id: i64,
            data: Value,
        ) -> AdminFuture<'_, Value> {
            Box::pin(async move { Ok(data) })
        }
        fn delete(
            &self,
            _pool: &diesel_async::pooled_connection::deadpool::Pool<
                diesel_async::AsyncPgConnection,
            >,
            _id: i64,
        ) -> AdminFuture<'_, ()> {
            Box::pin(async { Ok(()) })
        }
        fn supports_soft_delete(&self) -> bool {
            true
        }
        fn restore<'a>(
            &'a self,
            _pool: &'a diesel_async::pooled_connection::deadpool::Pool<
                diesel_async::AsyncPgConnection,
            >,
            id: i64,
        ) -> AdminFuture<'a, ()> {
            Box::pin(async move {
                self.restored.lock().unwrap().push(id);
                Ok(())
            })
        }
        fn purge<'a>(
            &'a self,
            _pool: &'a diesel_async::pooled_connection::deadpool::Pool<
                diesel_async::AsyncPgConnection,
            >,
            id: i64,
        ) -> AdminFuture<'a, ()> {
            Box::pin(async move {
                self.purged.lock().unwrap().push(id);
                Ok(())
            })
        }
        fn list_deleted<'a>(
            &'a self,
            _pool: &'a diesel_async::pooled_connection::deadpool::Pool<
                diesel_async::AsyncPgConnection,
            >,
            _params: ListParams,
        ) -> AdminFuture<'a, ListResult> {
            Box::pin(async {
                Ok(ListResult {
                    records: vec![],
                    total: 0,
                    page: 1,
                    per_page: 25,
                })
            })
        }
    }

    /// Build a `Pool` whose manager would fail to connect — the test models
    /// never call `pool.get()`, so the pool itself just sits unused.
    fn dummy_pool()
    -> diesel_async::pooled_connection::deadpool::Pool<diesel_async::AsyncPgConnection> {
        use diesel_async::pooled_connection::AsyncDieselConnectionManager;
        use diesel_async::pooled_connection::deadpool::Pool;
        let mgr = AsyncDieselConnectionManager::<diesel_async::AsyncPgConnection>::new(
            "postgresql://test",
        );
        Pool::builder(mgr).build().expect("build pool")
    }

    #[tokio::test]
    async fn default_execute_action_delete_invokes_delete_for_each_id() {
        let model = DeletingModel {
            deleted: Mutex::new(vec![]),
            fail_on: None,
        };
        let pool = dummy_pool();
        let count = model
            .execute_action(&pool, "delete", vec![10, 20, 30])
            .await
            .expect("default delete should succeed");
        assert_eq!(count, 3);
        assert_eq!(*model.deleted.lock().unwrap(), vec![10, 20, 30]);
    }

    #[tokio::test]
    async fn default_execute_action_delete_aborts_on_first_failure() {
        let model = DeletingModel {
            deleted: Mutex::new(vec![]),
            fail_on: Some(20),
        };
        let pool = dummy_pool();
        let err = model
            .execute_action(&pool, "delete", vec![10, 20, 30])
            .await
            .expect_err("delete should propagate failure");
        assert!(matches!(err, AdminError::Database(_)));
        // Only the pre-failure id was committed.
        assert_eq!(*model.deleted.lock().unwrap(), vec![10]);
    }

    #[tokio::test]
    async fn default_execute_action_rejects_unknown_action() {
        let model = DeletingModel {
            deleted: Mutex::new(vec![]),
            fail_on: None,
        };
        let pool = dummy_pool();
        let err = model
            .execute_action(&pool, "promote", vec![1])
            .await
            .expect_err("unknown actions must error, not silently no-op");
        assert!(
            matches!(err, AdminError::Other(msg) if msg.contains("promote")),
            "error should name the unhandled action"
        );
        assert!(model.deleted.lock().unwrap().is_empty());
    }

    #[test]
    fn humanize_converts_snake_case() {
        assert_eq!(humanize_field_name("created_at"), "Created At");
        assert_eq!(humanize_field_name("user_id"), "User Id");
        assert_eq!(humanize_field_name("name"), "Name");
        assert_eq!(humanize_field_name(""), "");
    }

    #[test]
    fn list_result_total_pages() {
        let result = ListResult {
            records: vec![],
            total: 25,
            page: 1,
            per_page: 10,
        };
        assert_eq!(result.total_pages(), 3);
    }

    #[test]
    fn list_result_total_pages_exact() {
        let result = ListResult {
            records: vec![],
            total: 20,
            page: 1,
            per_page: 10,
        };
        assert_eq!(result.total_pages(), 2);
    }

    #[test]
    fn list_result_total_pages_zero_per_page() {
        let result = ListResult {
            records: vec![],
            total: 20,
            page: 1,
            per_page: 0,
        };
        assert_eq!(result.total_pages(), 0);
    }

    #[test]
    fn admin_field_builder() {
        let field = AdminField::new("email", AdminFieldKind::Text)
            .label("Email Address")
            .searchable()
            .filterable()
            .optional();

        assert_eq!(field.name, "email");
        assert_eq!(field.label, "Email Address");
        assert!(field.searchable);
        assert!(field.filterable);
        assert!(!field.required);
        assert!(field.editable);
    }

    #[test]
    fn record_id_extracts_numeric_id() {
        assert_eq!(record_id(&serde_json::json!({"id": 42})), Some(42));
    }

    #[test]
    fn record_id_returns_none_for_missing_or_non_numeric() {
        assert_eq!(record_id(&serde_json::json!({})), None);
        assert_eq!(record_id(&serde_json::json!({"id": null})), None);
        assert_eq!(record_id(&serde_json::json!({"id": "abc"})), None);
        // Floats aren't valid IDs either — only integers.
        assert_eq!(record_id(&serde_json::json!({"id": 1.5})), None);
    }

    #[test]
    fn hidden_fields_default_to_not_editable() {
        // AdminFieldKind::Hidden is documented as "not editable". Ensure the
        // default matches the contract so admins who skip `.readonly()` still
        // get safe behaviour.
        let hidden = AdminField::new("owner_id", AdminFieldKind::Hidden);
        assert!(
            !hidden.editable,
            "Hidden fields must default to editable=false"
        );

        // Other kinds remain editable by default.
        let text = AdminField::new("name", AdminFieldKind::Text);
        assert!(text.editable);
    }

    // ── Soft-delete admin support (issue #689) ────────────────────

    #[test]
    fn admin_model_supports_soft_delete_defaults_to_false() {
        let model = DeletingModel {
            deleted: Mutex::new(vec![]),
            fail_on: None,
        };
        assert!(
            !model.supports_soft_delete(),
            "AdminModel::supports_soft_delete() must default to false"
        );
    }

    #[tokio::test]
    async fn admin_model_restore_returns_error_when_soft_delete_not_supported() {
        let model = DeletingModel {
            deleted: Mutex::new(vec![]),
            fail_on: None,
        };
        let pool = dummy_pool();
        let err = model
            .restore(&pool, 1)
            .await
            .expect_err("restore must error when supports_soft_delete() is false");
        assert!(
            matches!(err, AdminError::Other(_)),
            "restore on non-soft-delete model must return AdminError::Other: {err:?}"
        );
    }

    #[tokio::test]
    async fn admin_model_purge_returns_error_when_soft_delete_not_supported() {
        let model = DeletingModel {
            deleted: Mutex::new(vec![]),
            fail_on: None,
        };
        let pool = dummy_pool();
        let err = model
            .purge(&pool, 1)
            .await
            .expect_err("purge must error when supports_soft_delete() is false");
        assert!(
            matches!(err, AdminError::Other(_)),
            "purge on non-soft-delete model must return AdminError::Other: {err:?}"
        );
    }

    #[tokio::test]
    async fn admin_model_list_deleted_returns_error_when_soft_delete_not_supported() {
        let model = DeletingModel {
            deleted: Mutex::new(vec![]),
            fail_on: None,
        };
        let pool = dummy_pool();
        let params = ListParams {
            page: 1,
            per_page: 25,
            ..Default::default()
        };
        let err = model
            .list_deleted(&pool, params)
            .await
            .expect_err("list_deleted must error when supports_soft_delete() is false");
        assert!(
            matches!(err, AdminError::Other(_)),
            "list_deleted on non-soft-delete model must return AdminError::Other: {err:?}"
        );
    }

    #[test]
    fn default_actions_returns_only_delete_when_soft_delete_not_supported() {
        let model = DeletingModel {
            deleted: Mutex::new(vec![]),
            fail_on: None,
        };
        let acts = model.actions();
        assert_eq!(
            acts.len(),
            1,
            "default model must advertise exactly one action"
        );
        assert_eq!(acts[0].name, "delete");
    }

    #[test]
    fn actions_includes_restore_and_purge_when_soft_delete_supported() {
        let model = SoftDeleteModel::default();
        let acts = model.actions();
        let names: Vec<&str> = acts.iter().map(|a| a.name).collect();
        assert!(
            names.contains(&"restore"),
            "soft-delete model must advertise restore action; got: {names:?}"
        );
        assert!(
            names.contains(&"purge"),
            "soft-delete model must advertise purge action; got: {names:?}"
        );
    }

    #[tokio::test]
    async fn execute_action_restore_dispatches_to_restore_method() {
        let model = SoftDeleteModel::default();
        let pool = dummy_pool();
        let count = model
            .execute_action(&pool, "restore", vec![10, 20])
            .await
            .expect("restore action should succeed on soft-delete model");
        assert_eq!(
            count, 2,
            "restore action must return count of restored records"
        );
        assert_eq!(*model.restored.lock().unwrap(), vec![10, 20]);
    }

    #[tokio::test]
    async fn execute_action_purge_dispatches_to_purge_method() {
        let model = SoftDeleteModel::default();
        let pool = dummy_pool();
        let count = model
            .execute_action(&pool, "purge", vec![5])
            .await
            .expect("purge action should succeed on soft-delete model");
        assert_eq!(count, 1, "purge action must return count of purged records");
        assert_eq!(*model.purged.lock().unwrap(), vec![5]);
    }

    // ── Version history tests (issue #700) ────────────────────────

    #[test]
    fn admin_model_has_history_defaults_to_false() {
        let model = DeletingModel {
            deleted: Mutex::new(vec![]),
            fail_on: None,
        };
        assert!(
            !model.has_history(),
            "AdminModel::has_history() must default to false"
        );
    }

    #[tokio::test]
    async fn admin_model_get_history_returns_error_when_not_opted_in() {
        let model = DeletingModel {
            deleted: Mutex::new(vec![]),
            fail_on: None,
        };
        let pool = dummy_pool();
        let err = model
            .get_history(&pool, 42, 1, 25)
            .await
            .expect_err("get_history must error when has_history() is false");
        assert!(
            matches!(err, AdminError::Other(_)),
            "get_history on non-versioned model must return AdminError::Other: {err:?}"
        );
    }

    #[test]
    fn admin_history_page_total_pages() {
        let page = AdminHistoryPage {
            entries: vec![],
            total: 51,
            page: 1,
            per_page: 25,
        };
        assert_eq!(page.total_pages(), 3);
    }

    #[test]
    fn admin_history_page_has_next_page() {
        let page = AdminHistoryPage {
            entries: vec![],
            total: 50,
            page: 1,
            per_page: 25,
        };
        assert!(page.has_next_page());
    }

    #[test]
    fn admin_history_page_no_next_on_last() {
        let page = AdminHistoryPage {
            entries: vec![],
            total: 50,
            page: 2,
            per_page: 25,
        };
        assert!(!page.has_next_page());
    }

    #[test]
    fn admin_history_page_zero_per_page() {
        let page = AdminHistoryPage {
            entries: vec![],
            total: 10,
            page: 1,
            per_page: 0,
        };
        assert_eq!(page.total_pages(), 0);
    }

    // ── SortDirection and AdminField builder coverage ─────────────

    #[test]
    fn sort_direction_as_str_returns_correct_values() {
        assert_eq!(SortDirection::Asc.as_str(), "asc");
        assert_eq!(SortDirection::Desc.as_str(), "desc");
    }

    #[test]
    fn sort_direction_flipped_returns_opposite() {
        assert_eq!(SortDirection::Asc.flipped(), SortDirection::Desc);
        assert_eq!(SortDirection::Desc.flipped(), SortDirection::Asc);
    }

    #[test]
    fn admin_field_readonly_sets_editable_false() {
        let field = AdminField::new("created_at", AdminFieldKind::DateTime).readonly();
        assert!(!field.editable, "readonly() must set editable = false");
    }

    #[test]
    fn admin_field_hide_from_list_sets_list_display_false() {
        let field = AdminField::new("internal_token", AdminFieldKind::Text).hide_from_list();
        assert!(
            !field.list_display,
            "hide_from_list() must set list_display = false"
        );
    }

    #[test]
    fn admin_model_record_display_includes_display_name_and_id() {
        let model = DeletingModel {
            deleted: Mutex::new(vec![]),
            fail_on: None,
        };
        let record = serde_json::json!({"id": 7, "name": "foo"});
        assert_eq!(model.record_display(&record), "Tracked #7");
    }

    #[test]
    fn admin_model_record_display_placeholder_when_no_id() {
        let model = DeletingModel {
            deleted: Mutex::new(vec![]),
            fail_on: None,
        };
        let record = serde_json::json!({"name": "bar"});
        assert_eq!(model.record_display(&record), "Tracked <no id>");
    }

    #[test]
    fn admin_model_per_page_default_is_25() {
        let model = DeletingModel {
            deleted: Mutex::new(vec![]),
            fail_on: None,
        };
        assert_eq!(model.per_page(), 25);
    }

    #[test]
    fn version_page_converts_to_admin_history_page() {
        use autumn_web::version_history::{ColumnChange, VersionEntry, VersionOp, VersionPage};
        use chrono::Utc;

        let entry = VersionEntry {
            id: 1,
            table_name: "posts".to_owned(),
            record_id: 42,
            op: VersionOp::Update,
            actor: "admin".to_owned(),
            request_id: Some("req-1".to_owned()),
            changes: vec![ColumnChange::new(
                "title",
                Some(serde_json::json!("old")),
                Some(serde_json::json!("new")),
            )],
            recorded_at: Utc::now(),
        };
        let vp = VersionPage {
            entries: vec![entry],
            total: 1,
            page: 1,
            per_page: 25,
        };

        let ap = AdminHistoryPage::from(vp);
        assert_eq!(ap.total, 1);
        assert_eq!(ap.page, 1);
        assert_eq!(ap.per_page, 25);
        assert_eq!(ap.entries.len(), 1);
        let e = &ap.entries[0];
        assert_eq!(e.id, 1);
        assert_eq!(e.actor, "admin");
        assert_eq!(e.op, "update");
        assert_eq!(e.request_id.as_deref(), Some("req-1"));
        assert_eq!(e.changes.len(), 1);
    }

    #[test]
    fn version_entry_converts_to_admin_history_entry() {
        use autumn_web::version_history::{ColumnChange, VersionEntry, VersionOp};
        use chrono::Utc;

        let entry = VersionEntry {
            id: 7,
            table_name: "users".to_owned(),
            record_id: 3,
            op: VersionOp::Delete,
            actor: "system".to_owned(),
            request_id: None,
            changes: vec![ColumnChange::sensitive("password_digest")],
            recorded_at: Utc::now(),
        };

        let admin_entry = AdminHistoryEntry::from(entry);
        assert_eq!(admin_entry.id, 7);
        assert_eq!(admin_entry.actor, "system");
        assert_eq!(admin_entry.op, "delete");
        assert!(admin_entry.request_id.is_none());
        assert_eq!(admin_entry.changes.len(), 1);
    }

    // ── CsvImportMode ────────────────────────────────────────────────

    #[test]
    fn csv_import_mode_from_form_value_recognises_insert() {
        assert_eq!(
            CsvImportMode::from_form_value("insert"),
            Some(CsvImportMode::Insert)
        );
        assert_eq!(
            CsvImportMode::from_form_value("Insert"),
            Some(CsvImportMode::Insert)
        );
    }

    #[test]
    fn csv_import_mode_from_form_value_recognises_dry_run() {
        assert_eq!(
            CsvImportMode::from_form_value("dry_run"),
            Some(CsvImportMode::DryRun)
        );
        assert_eq!(
            CsvImportMode::from_form_value("DryRun"),
            Some(CsvImportMode::DryRun)
        );
        assert_eq!(
            CsvImportMode::from_form_value("dry-run"),
            Some(CsvImportMode::DryRun)
        );
    }

    #[test]
    fn csv_import_mode_from_form_value_rejects_unknown() {
        assert_eq!(CsvImportMode::from_form_value("upsert"), None);
        assert_eq!(CsvImportMode::from_form_value(""), None);
        assert_eq!(CsvImportMode::from_form_value("INSERT"), None);
        assert_eq!(CsvImportMode::from_form_value("DRY_RUN"), None);
    }

    #[test]
    fn csv_import_mode_default_is_insert() {
        assert_eq!(CsvImportMode::default(), CsvImportMode::Insert);
    }

    // ── escape_csv_formula ──────────────────────────────────────────

    #[test]
    fn escape_csv_formula_prefixes_equals_sign() {
        assert_eq!(escape_csv_formula("=SUM(A1)"), "'=SUM(A1)");
    }

    #[test]
    fn escape_csv_formula_prefixes_plus_and_minus_and_at() {
        assert_eq!(escape_csv_formula("+cmd"), "'+cmd");
        assert_eq!(escape_csv_formula("-1+1"), "'-1+1");
        assert_eq!(escape_csv_formula("@A1"), "'@A1");
    }

    #[test]
    fn escape_csv_formula_prefixes_tab_and_cr() {
        assert_eq!(escape_csv_formula("\thello"), "'\thello");
        assert_eq!(escape_csv_formula("\rhello"), "'\rhello");
    }

    #[test]
    fn escape_csv_formula_leaves_normal_strings_unchanged() {
        assert_eq!(escape_csv_formula("hello world"), "hello world");
        assert_eq!(escape_csv_formula("123"), "123");
        assert_eq!(escape_csv_formula(""), "");
        assert_eq!(escape_csv_formula("normal,value"), "normal,value");
    }

    // ── AdminModel CSV defaults ──────────────────────────────────────

    #[test]
    fn admin_model_supports_csv_export_defaults_to_false() {
        let model = DeletingModel {
            deleted: Mutex::new(vec![]),
            fail_on: None,
        };
        assert!(
            !model.supports_csv_export(),
            "supports_csv_export must default to false to require explicit opt-in"
        );
    }

    #[test]
    fn admin_model_supports_csv_import_defaults_to_false() {
        let model = DeletingModel {
            deleted: Mutex::new(vec![]),
            fail_on: None,
        };
        assert!(
            !model.supports_csv_import(),
            "supports_csv_import must default to false"
        );
    }

    #[test]
    fn csv_export_row_extracts_columns_and_escapes_formulas() {
        let model = DeletingModel {
            deleted: Mutex::new(vec![]),
            fail_on: None,
        };
        let record = serde_json::json!({
            "id": 1,
            "name": "Alice",
            "formula": "=EVIL()",
            "amount": 42.5,
            "active": true,
            "notes": null,
        });
        let columns = &[
            "id", "name", "formula", "amount", "active", "notes", "missing",
        ];
        let row = model.csv_export_row(columns, &record);
        assert_eq!(row[0], "1");
        assert_eq!(row[1], "Alice");
        assert_eq!(row[2], "'=EVIL()", "formula-leading value must be escaped");
        assert_eq!(row[3], "42.5");
        assert_eq!(row[4], "true");
        assert_eq!(row[5], "", "null becomes empty string");
        assert_eq!(row[6], "", "missing column becomes empty string");
    }
}