BREP_plm 0.24.0

The next.BREP.io PLM server for the Rust B-rep CAD kernel: parts, revisions, lifecycle, checkout, BOMs and configurable workflows over an embedded SQLite store, with a browser UI and the document API the CAD app stores through.
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
//! The PLM record types, and nothing that acts on them.
//!
//! The shape follows the four identity layers the architecture settled on:
//! **Part** (permanent, owns the number) → **Revision** (the released unit) →
//! the CAD **document** the revision points at → the **occurrence**, which
//! lives in the CAD document and never in this server.
//!
//! # Slots that are deliberately inert
//!
//! [`Origin`] and [`Part::external_ref`] are recorded and served but nothing
//! here branches on them yet. They are the seams the later
//! work needs — the KiCad importer stamps `Origin::Imported` and an
//! `external_ref` of `Timer:NE555D`, a family stamps `Origin::Generated` — and
//! a field added now costs one line, while a field added after parts exist
//! costs a migration. Behaviour built ON them is NOT here.

use std::collections::BTreeMap;

use serde::{Deserialize, Serialize};

/// Whole Unix seconds. The only time representation in the server: it
/// serializes as a number, sorts correctly, and needs no date library.
pub type Timestamp = u64;

// ===========================================================================
// Users, groups and sessions
// ===========================================================================

/// The built-in group names. Groups are plain strings on the user so an
/// operator can invent more without a migration; these four are the ones the
/// server itself checks.
pub mod groups {
    /// Full authority, including user and part-type administration.
    pub const ADMIN: &str = "admin";
    /// May release revisions and BREAK another user's lock — the "check-in
    /// group" in the architecture.
    pub const CHECKIN: &str = "checkin";
    /// May create parts and drafts, and check out their own work.
    pub const AUTHOR: &str = "author";
    /// Read-only.
    pub const VIEWER: &str = "viewer";
    /// The shop floor: reads, and signs the steps of manufacturing jobs
    /// ([`crate::jobs`]). Nothing else.
    pub const OPERATOR: &str = "operator";
    /// A headless CAD worker: may take bake jobs from the queue and write
    /// their results. Nothing else — a worker
    /// account cannot create parts or release anything.
    pub const WORKER: &str = "worker";
}

/// One account. The password is never stored — only the PBKDF2 verifier in
/// [`crate::auth`], which carries its own salt and iteration count so a later
/// change of cost does not invalidate existing accounts.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct User {
    pub id: String,
    pub username: String,
    #[serde(default)]
    pub display_name: String,
    #[serde(default)]
    pub email: String,
    /// `pbkdf2$<iterations>$<b64 salt>$<b64 hash>` — see [`crate::auth`].
    pub password: String,
    #[serde(default)]
    pub groups: Vec<String>,
    #[serde(default = "yes")]
    pub active: bool,
    pub created_at: Timestamp,
}

fn yes() -> bool {
    true
}

impl User {
    /// Whether this user is in `group`. `admin` answers yes to everything, so
    /// no call site has to remember to check for it as well.
    pub fn in_group(&self, group: &str) -> bool {
        self.active
            && (self.groups.iter().any(|g| g == group)
                || self.groups.iter().any(|g| g == groups::ADMIN))
    }

    /// May create parts and revisions.
    pub fn can_author(&self) -> bool {
        self.in_group(groups::AUTHOR)
    }

    /// May release a revision and break another user's lock.
    pub fn can_checkin(&self) -> bool {
        self.in_group(groups::CHECKIN)
    }

    /// May administer users and part types.
    pub fn is_admin(&self) -> bool {
        self.in_group(groups::ADMIN)
    }

    /// May claim bake jobs and write their results.
    pub fn can_bake(&self) -> bool {
        self.in_group(groups::WORKER)
    }
}

/// A logged-in browser. Sessions live in the database file like everything
/// else, so a restarted server does not sign everyone out.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct Session {
    pub token: String,
    pub user_id: String,
    pub created_at: Timestamp,
    pub last_seen: Timestamp,
    /// The CSRF token of this session (synchronizer pattern): handed to the
    /// page at sign-in and by `/api/me`, and required back in `X-CSRF-Token`
    /// on every request that changes anything.
    pub csrf: String,
}

/// What an API token may do. A token is how a caller that is not a browser —
/// the CAD app, a bake worker, an organization's own script — signs in. It
/// acts as the user who owns it, narrowed by its scope; it never acts as more.
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum TokenScope {
    /// Everything its owner may do, except managing tokens and passwords.
    #[default]
    Full,
    /// Only reads (`GET`).
    Read,
    /// Reads, the bake queue and the document store: what a headless CAD
    /// worker needs and nothing more.
    Worker,
}

impl TokenScope {
    pub fn as_str(self) -> &'static str {
        match self {
            TokenScope::Full => "full",
            TokenScope::Read => "read",
            TokenScope::Worker => "worker",
        }
    }
}

/// A bearer token for a non-browser caller. The secret is shown ONCE, when it
/// is created; the server keeps only its SHA-256 (the secret is 256 random
/// bits, so a slow hash buys nothing) and its first characters, so a person
/// can tell tokens apart in a list.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ApiToken {
    pub id: String,
    pub user_id: String,
    /// What the owner called it ("bake worker on cad-01").
    pub name: String,
    pub scope: TokenScope,
    /// Hex SHA-256 of the whole secret.
    pub hash: String,
    /// The secret's first characters, e.g. `plm_3fQ9`.
    pub prefix: String,
    pub created_at: Timestamp,
    #[serde(default)]
    pub last_used_at: Option<Timestamp>,
    /// When it stops working; `None` never expires.
    #[serde(default)]
    pub expires_at: Option<Timestamp>,
    /// The desktop app a browser sign-in made this token for
    /// (`/api/desktop/link`), listed under Linked desktops; `None` for a token
    /// a person made.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub desktop: Option<DesktopDevice>,
}

/// What a desktop app said about itself when it asked to sign in.
#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize)]
pub struct DesktopDevice {
    #[serde(default)]
    pub host: String,
    #[serde(default)]
    pub os: String,
    #[serde(default)]
    pub app_version: String,
}

// ===========================================================================
// Part types — the configurable numbering scheme
// ===========================================================================

/// A class of part with its OWN numbering scheme. `CPART` + 9 digits is the
/// seeded default, not a hard-coded rule: prefix and width are data, an
/// operator adds more types, and [`format_number`](Self::format_number) is the
/// one place a number is spelled.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PartType {
    /// Stable key used in URLs and on parts (`"component"`).
    pub id: String,
    /// What the UI shows (`"Component"`).
    pub name: String,
    /// Literal text before the digits (`"CPART"`).
    pub prefix: String,
    /// How many digits follow the prefix. The formatted number is ALWAYS this
    /// long — zero-padded — so every number of a type sorts as text.
    pub digits: u32,
    /// The next sequence value to hand out. Allocation bumps it. Used only in
    /// [`NumberMode::Counter`]; the other modes keep it so switching back to a
    /// counter resumes where it left off.
    pub next: u64,
    pub created_at: Timestamp,
    /// How this type's numbers are made. Absent in a file written before modes
    /// existed, which reads as the counter every type used to be.
    #[serde(default)]
    pub mode: NumberMode,
}

/// How a part type's numbers are made. In EVERY mode a
/// number is unique across all parts, not only within its type — that check
/// lives inside the store's write, not here.
#[derive(Debug, Clone, PartialEq, Eq, Default, Serialize, Deserialize)]
#[serde(tag = "kind", rename_all = "lowercase")]
pub enum NumberMode {
    /// `prefix` + `digits` + an auto-incrementing counter. A typed number is
    /// refused: the counter owns the numbers of a counter type.
    #[default]
    Counter,
    /// Any number the user types — e.g. the ERP purchasing number of a library
    /// part.
    Free,
    /// A typed number that must match `regex` in full.
    Pattern { regex: String },
    /// The admin script at `script` (relative to the scripts directory)
    /// validates the typed number or allocates one, through its `partNumber`
    /// function.
    Script { script: String },
}

impl NumberMode {
    pub fn kind(&self) -> &'static str {
        match self {
            NumberMode::Counter => "counter",
            NumberMode::Free => "free",
            NumberMode::Pattern { .. } => "pattern",
            NumberMode::Script { .. } => "script",
        }
    }
}

impl PartType {
    /// Spell `sequence` as a part number of this type. The width comes from
    /// `digits`, never from a literal.
    pub fn format_number(&self, sequence: u64) -> String {
        format!(
            "{prefix}{sequence:0width$}",
            prefix = self.prefix,
            sequence = sequence,
            width = self.digits as usize
        )
    }

    /// The highest sequence this type can spell. `digits` 9 → 999_999_999.
    pub fn capacity(&self) -> u64 {
        10u64.saturating_pow(self.digits).saturating_sub(1)
    }
}

// ===========================================================================
// Parts and revisions
// ===========================================================================

/// Where a revision's content came from. Inert today (see the module header).
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum Origin {
    /// A person made it in the CAD app.
    #[default]
    Authored,
    /// A library import produced it (KiCad today).
    Imported,
    /// A part family generated it from a parameter tuple.
    Generated,
}

/// What a part's document is for, shown by its file extension. Chosen when
/// the part is created and NEVER changed: turning a normal part into a family
/// means a new part.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum DocumentClass {
    /// `.nBREP` — placed as-is. Imported parts and family members are normal.
    #[default]
    Normal,
    /// `.fBREP` — a family seed: the model plus the table of its members.
    Family,
    /// `.tBREP` — a template whose values are driven by expressions.
    Template,
    /// `.iBREP` — manufacturing instructions for one assembly revision
    /// ([`Revision::instructs`]), revised on their own schedule.
    Instructions,
}

impl DocumentClass {
    pub fn as_str(self) -> &'static str {
        match self {
            DocumentClass::Normal => "normal",
            DocumentClass::Family => "family",
            DocumentClass::Template => "template",
            DocumentClass::Instructions => "instructions",
        }
    }

    pub fn parse(text: &str) -> Option<Self> {
        match text.trim().to_ascii_lowercase().trim_start_matches('.') {
            "" | "normal" | "nbrep" => Some(DocumentClass::Normal),
            "family" | "fbrep" => Some(DocumentClass::Family),
            "template" | "tbrep" => Some(DocumentClass::Template),
            "instructions" | "ibrep" => Some(DocumentClass::Instructions),
            _ => None,
        }
    }
}

/// The lifecycle states, in the order a revision moves through them.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum Lifecycle {
    Draft,
    InReview,
    Released,
    Superseded,
    Obsolete,
}

impl Lifecycle {
    /// Whether the revision's document may still be written. This is the ONE
    /// predicate immutability rests on — see [`crate::lifecycle`].
    pub fn is_editable(self) -> bool {
        matches!(self, Lifecycle::Draft | Lifecycle::InReview)
    }

    pub fn as_str(self) -> &'static str {
        match self {
            Lifecycle::Draft => "draft",
            Lifecycle::InReview => "inreview",
            Lifecycle::Released => "released",
            Lifecycle::Superseded => "superseded",
            Lifecycle::Obsolete => "obsolete",
        }
    }
}

/// Who holds a draft revision checked out.
///
/// There is no lease and no expiry: the architecture chose MANUAL break, so a
/// lock persists until its holder checks in or someone in the check-in group
/// breaks it. `acquired_at` is what makes a stale lock visible to the person
/// deciding whether to break it.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Lock {
    pub user_id: String,
    /// Which client of that user holds it — the same person can be in the
    /// browser and the desktop build at once.
    #[serde(default)]
    pub client_id: String,
    pub acquired_at: Timestamp,
}

/// One revision of a part: the released unit of engineering intent, and the
/// thing a CAD document hangs off.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Revision {
    /// Whether the saved document produces BREP faces. Derived, never user editable.
    #[serde(default)]
    pub has_geometry: bool,
    /// Hash of the document used to derive geometry metadata.
    #[serde(default)]
    pub geometry_content_hash: String,
    /// Values defined by this part's numbered PLM type, owned by this revision.
    #[serde(default)]
    pub attributes: BTreeMap<String, serde_json::Value>,
    #[serde(default)]
    pub attributes_initialized: bool,
    /// Individual placements, identified by their CAD feature ids.
    #[serde(default)]
    pub occurrences: Vec<crate::bom_config::Occurrence>,
    /// The revision label itself; its database key is (part_id, id).
    pub id: String,
    /// What people call it. FREE TEXT: whoever creates
    /// the revision may type it, and the store enforces only that it is unique
    /// within its part. `A, B, C` from [`crate::lifecycle::next_revision_label`]
    /// is the suggestion when nobody types one. Revisions are ordered by
    /// creation — their position in [`Part::revisions`] — never by label.
    pub label: String,
    pub lifecycle: Lifecycle,
    #[serde(default)]
    pub origin: Origin,
    /// SHA-256 of the document as last written, so a client can detect drift
    /// without fetching the payload. Empty until something is written.
    #[serde(default)]
    pub content_hash: String,
    #[serde(default)]
    pub size: u64,
    /// `None` when nobody has it checked out.
    #[serde(default)]
    pub lock: Option<Lock>,
    pub created_by: String,
    pub created_at: Timestamp,
    #[serde(default)]
    pub modified_at: Timestamp,
    #[serde(default)]
    pub released_by: Option<String>,
    #[serde(default)]
    pub released_at: Option<Timestamp>,
    /// Set on a family member's revision by Generate: which family row made
    /// it, and the fingerprints the next Generate compares to skip an
    /// unchanged row. `None` for anything a family did not write.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub family: Option<FamilyStamp>,
    /// Set on the first revision of a part spun out of a template.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub template: Option<TemplateStamp>,
    /// The bake this revision's document is waiting for, or had. `None` for
    /// a document nothing asked to be baked.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub bake: Option<Bake>,
    /// What this revision is built from: the "uses" list the CAD app
    /// publishes on save and release. The server
    /// never reads geometry — this list IS the assembly structure, and the
    /// BOM, where-used and the release gate are all computed from it. Frozen
    /// with the document once the revision is released.
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub uses: Vec<Use>,
    /// Every review this revision has been through, oldest first: the last
    /// one is the current round ([`Review`]). A revision is reviewed where it
    /// lives, so a review costs what its part costs to write.
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub reviews: Vec<Review>,
    /// The discussion on this revision, oldest first; a reply names the
    /// comment it answers. Anyone who can read the part reads it.
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub comments: Vec<Comment>,
    /// Files that belong to THIS revision — its drawing, its test report.
    /// Frozen with the revision at release, like its document; a new revision
    /// starts with the same references (the files are shared, not copied).
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub attachments: Vec<Attachment>,
    /// A picture of the document, made and uploaded by a CAD client or the
    /// bake worker (the server never renders). It describes the document
    /// whose hash it carries: once the document changes it is stale, and
    /// [`crate::thumbnail::current`] no longer serves it.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub thumbnail: Option<Thumbnail>,
    /// An instructions revision's assembly: derived from the document's
    /// `instructions.target.sourceKey` on every write, never set by a
    /// request. `None` for every other class, and for instructions whose
    /// target is not a PLM revision (a local file).
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub instructs: Option<Instructs>,
    /// An instructions revision's player data ([`crate::player`]), keyed by
    /// the document it was made from like a thumbnail.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub player: Option<PlayerArtifact>,
    /// Customer links to a released instructions revision's player.
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub shares: Vec<Share>,
}

/// The interactive player's data for one instructions revision: a blob of
/// the attachment store, current while the revision's document is the one
/// it was made from.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct PlayerArtifact {
    pub sha256: String,
    pub size: u64,
    /// The revision's `content_hash` when the data was made.
    pub content_hash: String,
    pub uploaded_by: String,
    pub uploaded_at: Timestamp,
}

/// A customer link (`/s/<token>`) to a released revision's player. The
/// token is shown once; only its SHA-256 is kept.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct Share {
    /// What the list shows and a revoke names: the first 12 hex of the hash.
    pub id: String,
    #[serde(skip_serializing_if = "String::is_empty", default)]
    pub token_sha256: String,
    #[serde(default)]
    pub label: String,
    pub created_by: String,
    pub created_at: Timestamp,
}

/// The assembly revision an instructions revision instructs.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct Instructs {
    pub part: String,
    pub revision: String,
}

/// A revision's thumbnail: a PNG blob beside the attachments, keyed by the
/// document it pictures.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct Thumbnail {
    /// Lower-case hex SHA-256 of the PNG, and the blob's name.
    pub sha256: String,
    pub size: u64,
    pub width: u32,
    pub height: u32,
    /// The revision's `content_hash` when the picture was made.
    pub content_hash: String,
    /// The producer's renderer version (`brep-thumb/1`).
    pub renderer: String,
    pub uploaded_by: String,
    pub uploaded_at: Timestamp,
}

/// A file attached to a part or to one of its revisions.
///
/// The bytes live once under `<data>/blobs/`, named by their SHA-256, so two
/// references to the same file share them; this record is the reference.
/// A blob goes when the last reference to it does.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct Attachment {
    pub id: String,
    /// The file name shown and offered on download.
    pub name: String,
    /// What the uploader said it is (`application/pdf`). Only a short list of
    /// safe types is ever shown inline; everything else downloads.
    pub media_type: String,
    pub size: u64,
    /// Lower-case hex SHA-256 of the bytes, and the blob's name.
    pub sha256: String,
    /// `datasheet`, `drawing`, `spec`, `image` or `other`.
    #[serde(default = "other_kind")]
    pub kind: String,
    #[serde(default, skip_serializing_if = "String::is_empty")]
    pub note: String,
    pub uploaded_by: String,
    pub uploaded_at: Timestamp,
}

fn other_kind() -> String {
    "other".to_string()
}

/// One line of an assembly revision's "uses" list: which part, which of its
/// revisions, and how many.
///
/// Every occurrence of the same part and revision in the CAD document folds
/// into ONE line with their count as `quantity`; two lines for the same child
/// differ in revision or find number.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct Use {
    /// The child [`Part`], by id.
    pub part: String,
    /// The child revision, by id — PINNED. Empty is a FLOATING reference:
    /// whatever revision of the child is currently released (else its newest),
    /// resolved each time the BOM is read.
    #[serde(default)]
    pub revision: String,
    /// How many per ONE of the parent. Greater than zero; fractional for
    /// things bought by length or weight.
    #[serde(default = "one")]
    pub quantity: f64,
    /// What `quantity` counts: `each` unless it says otherwise (`mm`, `g`).
    #[serde(default = "each")]
    pub unit: String,
    /// The balloon number on the drawing, when there is one.
    #[serde(default, skip_serializing_if = "String::is_empty")]
    pub find_number: String,
    /// Reference designators (`R1, R2`) or occurrence names, free text.
    #[serde(default, skip_serializing_if = "String::is_empty")]
    pub reference: String,
    #[serde(default, skip_serializing_if = "String::is_empty")]
    pub notes: String,
}

fn one() -> f64 {
    1.0
}

fn each() -> String {
    "each".to_string()
}

impl Revision {
    /// A new, empty draft.
    pub fn draft(id: String, label: String, created_by: String, stamp: Timestamp) -> Self {
        Revision {
            has_geometry: false,
            geometry_content_hash: String::new(),
            attributes: BTreeMap::new(),
            attributes_initialized: false,
            occurrences: Vec::new(),
            id,
            label,
            lifecycle: Lifecycle::Draft,
            origin: Origin::default(),
            content_hash: String::new(),
            size: 0,
            lock: None,
            created_by,
            created_at: stamp,
            modified_at: stamp,
            released_by: None,
            released_at: None,
            family: None,
            template: None,
            bake: None,
            uses: Vec::new(),
            reviews: Vec::new(),
            comments: Vec::new(),
            attachments: Vec::new(),
            thumbnail: None,
            instructs: None,
            player: None,
            shares: Vec::new(),
        }
    }

    /// The current review round: the last one, whatever its status.
    pub fn review(&self) -> Option<&Review> {
        self.reviews.last()
    }

    /// The current review round, if it is still undecided or approved —
    /// the one a decision or a release acts on.
    pub fn live_review_mut(&mut self) -> Option<&mut Review> {
        self.reviews.last_mut().filter(|r| r.is_live())
    }

    /// The document still waits for a bake — queued, taken, or failed. A
    /// revision in this state cannot release: once released its bytes are
    /// frozen and the bake could never be written.
    pub fn needs_bake(&self) -> bool {
        self.bake.as_ref().is_some_and(|bake| bake.status != BakeStatus::Done)
    }

    /// The store key this revision's document lives at — the key the CAD app's
    /// `sourceKey` becomes. `part/<part id>/rev/<revision id>`.
    pub fn document_key(&self, part_id: &str) -> String {
        crate::identity::document_key(part_id, &self.id)
    }
}

/// A part: permanent identity, the number, and its revisions oldest-first.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Part {
    /// The part number itself, stored directly as the database primary key.
    pub id: String,
    /// The number (`CPART000000001`, or whatever the type's mode accepted).
    /// Unique across the server, compared without regard to ASCII case.
    pub number: String,
    /// Which [`PartType`] minted or accepted the number.
    pub part_type: String,
    /// The counter value the number was spelled from, kept so a renumbering or
    /// an audit does not have to re-parse the string. `0` for a number that
    /// did not come from a counter.
    pub sequence: u64,
    /// What the part's document is for. Fixed at creation.
    #[serde(default)]
    pub document_class: DocumentClass,
    pub name: String,
    #[serde(default)]
    pub description: String,
    /// The part's ONE primary [`Category`], by id. Empty is uncategorized.
    /// A value that names no category — text written before the catalog
    /// existed — loads as-is and reads as uncategorized; it is replaced the
    /// next time someone sets the category.
    #[serde(default)]
    pub category: String,
    #[serde(default)]
    pub tags: Vec<String>,
    /// The upstream library identity for an imported part (`"Timer:NE555D"`),
    /// which is what makes a re-import idempotent. Inert today.
    #[serde(default)]
    pub external_ref: String,
    /// Catalog attribute values, keyed by [`AttributeDef::key`]. Checked
    /// against the category's schema when written; COMPLETE (every required
    /// key present) only at release, because a draft may be unfinished. A
    /// value whose key the current category does not define is kept — moving
    /// a part between categories never throws data away — and is inert.
    #[serde(default)]
    pub attributes: BTreeMap<String, serde_json::Value>,
    /// Who makes this part and who sells it: the approved manufacturer parts,
    /// each with its supplier offers. Part-level, like `attributes`, and NOT
    /// frozen by [`Settings::lock_released_attributes`] — sourcing changes
    /// with the market, not with the design.
    #[serde(default)]
    pub sourcing: Vec<ManufacturerPart>,
    /// For a family or a template: the part type its generated parts are
    /// created in — a family's new members, a template's spin-outs. Empty
    /// means the family's or template's own type. Inert on a normal part.
    #[serde(default)]
    pub member_part_type: String,
    /// Files that belong to the part whatever its revision — a datasheet, a
    /// supplier drawing. Not frozen by a release: like sourcing, they follow
    /// the part, not one design of it. See [`Attachment`].
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub attachments: Vec<Attachment>,
    pub created_by: String,
    pub created_at: Timestamp,
    #[serde(default)]
    pub revisions: Vec<Revision>,
}

impl Part {
    pub fn revision(&self, revision_id: &str) -> Option<&Revision> {
        self.revisions.iter().find(|r| r.id == revision_id)
    }

    pub fn revision_mut(&mut self, revision_id: &str) -> Option<&mut Revision> {
        self.revisions.iter_mut().find(|r| r.id == revision_id)
    }

    /// The newest revision, whatever its state. "Newest" is creation order —
    /// the list's order — because a free-text label says nothing about age.
    pub fn latest(&self) -> Option<&Revision> {
        self.revisions.last()
    }

    /// The revision labelled `label`, compared without regard to ASCII case —
    /// `a` and `A` would be two revisions nobody could tell apart aloud.
    pub fn revision_by_label(&self, label: &str) -> Option<&Revision> {
        let label = label.trim();
        self.revisions
            .iter()
            .find(|r| r.label.eq_ignore_ascii_case(label))
    }

    /// The first revision still in work (Draft or InReview), if any. Whether a
    /// second may be started beside it is the administrator's
    /// [`Settings::allow_multiple_open_drafts`].
    pub fn open_draft(&self) -> Option<&Revision> {
        self.revisions.iter().find(|r| r.lifecycle.is_editable())
    }

    /// The revision currently Released. A release supersedes every other, so
    /// there is at most one.
    pub fn current_release(&self) -> Option<&Revision> {
        self.revisions.iter().rev().find(|r| r.lifecycle == Lifecycle::Released)
    }

    /// Whether [`Settings::lock_released_attributes`] would freeze this part's
    /// catalog values: some revision has been released (whatever it is now),
    /// and none is in work to carry a change.
    pub fn catalog_frozen(&self) -> bool {
        self.revisions.iter().any(|r| r.released_at.is_some()) && self.open_draft().is_none()
    }
}

// ===========================================================================
// Families, templates and the bake queue
// ===========================================================================

/// What a family's Generate records on a member revision. The document
/// carries the CAD app's own `familySource` stamp; this is the server's
/// copy, which is what the family view and the next Generate read without
/// opening every member document.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct FamilyStamp {
    /// The family part's id.
    pub family_part: String,
    /// The family revision whose document the member was generated from.
    pub family_revision: String,
    /// The row's values, expression source keyed by expression name.
    #[serde(default)]
    pub values: BTreeMap<String, String>,
    #[serde(default)]
    pub description: String,
    /// The CAD app's `row_hash` of the row: part number, description, values.
    pub values_hash: String,
    /// The CAD app's `history_hash` of the family: its model less the table.
    pub history_hash: String,
    /// [`Revision::content_hash`] of the document as Generate (or its bake)
    /// wrote it. A member whose hash has moved on since was edited by hand.
    pub generated_hash: String,
    pub generated_by: String,
    pub generated_at: Timestamp,
}

/// What a template spin-out records on the new part's first revision.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct TemplateStamp {
    pub template_part: String,
    pub template_revision: String,
    /// The input values the copy was made with.
    #[serde(default)]
    pub values: BTreeMap<String, String>,
    /// The CAD app's `template_hash` of the template document at the time.
    pub content_hash: String,
    pub created_at: Timestamp,
}

/// Where a bake is. The server never links the kernel, so a document it
/// assembled (a family member, a template copy) is checked and rebuilt by a
/// headless CAD worker, which takes the job from the queue.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum BakeStatus {
    /// Waiting for a worker.
    Pending,
    /// A worker has it (see [`Bake::claimed_at`] for the lease).
    Claimed,
    /// The worker reported a problem; a person retries it.
    Failed,
    /// Baked, or superseded by a document someone saved.
    Done,
}

impl BakeStatus {
    pub fn as_str(self) -> &'static str {
        match self {
            BakeStatus::Pending => "pending",
            BakeStatus::Claimed => "claimed",
            BakeStatus::Failed => "failed",
            BakeStatus::Done => "done",
        }
    }
}

/// One revision's bake job. The job's id is the revision's id: a revision has
/// at most one.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct Bake {
    /// A Force resave job rebuilds this exact saved document. Older generated
    /// jobs have no snapshot hash and retain their existing bake behavior.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub resave_hash: Option<String>,
    pub status: BakeStatus,
    /// Why it was queued ("generated from family CPART000000012 row M6x20").
    #[serde(default)]
    pub reason: String,
    pub requested_by: String,
    pub requested_at: Timestamp,
    #[serde(default)]
    pub claimed_by: Option<String>,
    #[serde(default)]
    pub claimed_at: Option<Timestamp>,
    /// How many times a worker has taken it.
    #[serde(default)]
    pub attempts: u32,
    /// The worker's report of the last failure.
    #[serde(default)]
    pub error: String,
    #[serde(default)]
    pub finished_by: Option<String>,
    #[serde(default)]
    pub finished_at: Option<Timestamp>,
}

// ===========================================================================
// Sourcing — manufacturers, suppliers, and what each offers for a part
// ===========================================================================

/// A manufacturer or a supplier. The two are separate lists (round 1)
/// even though they share a shape: a manufacturer part names a
/// manufacturer, an offer names a supplier, and a company that is both —
/// one that sells what it makes — appears in each list.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Company {
    /// Generated; never shown to a person.
    pub id: String,
    /// Unique within its list, compared without regard to ASCII case.
    pub name: String,
    #[serde(default)]
    pub website: String,
    #[serde(default)]
    pub notes: String,
    #[serde(default)]
    pub created_at: Timestamp,
}

/// Where a manufacturer part stands in its maker's catalogue.
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum SourcingStatus {
    #[default]
    Active,
    /// Not recommended for new designs.
    Nrnd,
    Obsolete,
}

impl SourcingStatus {
    pub fn as_str(self) -> &'static str {
        match self {
            SourcingStatus::Active => "active",
            SourcingStatus::Nrnd => "nrnd",
            SourcingStatus::Obsolete => "obsolete",
        }
    }
}

/// One manufacturer's part that satisfies one of our parts: the MPN.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ManufacturerPart {
    pub id: String,
    /// The [`Company`] id in the manufacturers list.
    pub manufacturer: String,
    /// The manufacturer's part number. Unique per manufacturer within one
    /// part, compared without regard to ASCII case.
    pub mpn: String,
    #[serde(default)]
    pub status: SourcingStatus,
    /// The one to buy first. At most one per part.
    #[serde(default)]
    pub preferred: bool,
    #[serde(default)]
    pub datasheet: String,
    #[serde(default)]
    pub notes: String,
    /// Who sells this MPN, and on what terms.
    #[serde(default)]
    pub offers: Vec<SupplierOffer>,
    #[serde(default)]
    pub created_at: Timestamp,
}

/// One supplier's offer for one manufacturer part.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SupplierOffer {
    pub id: String,
    /// The [`Company`] id in the suppliers list.
    pub supplier: String,
    /// The supplier's own part number (SPN).
    #[serde(default)]
    pub spn: String,
    #[serde(default)]
    pub url: String,
    /// A currency code as the supplier quotes it (`USD`). Text, not a list:
    /// nothing here converts between currencies.
    #[serde(default)]
    pub currency: String,
    /// Unit price by quantity, ascending by `qty`.
    #[serde(default)]
    pub price_breaks: Vec<PriceBreak>,
    #[serde(default)]
    pub lead_time_days: Option<u32>,
    /// Minimum order quantity.
    #[serde(default)]
    pub moq: Option<u64>,
    /// Stock on hand when last checked, if known.
    #[serde(default)]
    pub stock: Option<u64>,
    #[serde(default)]
    pub notes: String,
    /// When the offer was last written — how stale the price is.
    #[serde(default)]
    pub updated_at: Timestamp,
}

/// Buy at least `qty` and each costs `unit_price`.
#[derive(Debug, Clone, Copy, PartialEq, Serialize, Deserialize)]
pub struct PriceBreak {
    pub qty: u64,
    pub unit_price: f64,
}

// ===========================================================================
// The audit log — who changed what, and when
// ===========================================================================

/// Who made a change.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct Actor {
    /// The user's id; empty for the server itself.
    #[serde(default)]
    pub user_id: String,
    /// The username at the time, kept so the log reads right after a rename
    /// or a deletion.
    #[serde(default)]
    pub username: String,
    /// How the change arrived: `session` (a browser), `token` (an API token),
    /// or `system` (the server itself, or a caller with no signed-in user —
    /// start-up, a test, a sign-in before it succeeds).
    #[serde(default)]
    pub via: String,
    /// The client's address, when the request carried one.
    #[serde(default, skip_serializing_if = "String::is_empty")]
    pub ip: String,
}

impl Actor {
    /// The server itself.
    pub fn system() -> Self {
        Actor { user_id: String::new(), username: String::new(), via: "system".into(), ip: String::new() }
    }
}

/// What a change was made to.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct EntityRef {
    /// `part`, `revision`, `user`, `token`, `part-type`, `category`,
    /// `manufacturer`, `supplier`, `settings`, `script` — or, for a
    /// collection a later slice adds, its name in the store.
    pub kind: String,
    pub id: String,
    /// What a person calls it: a part number, `CPART000000001 rev B`, a
    /// username, a category name.
    #[serde(default)]
    pub label: String,
    /// For a revision, its part's id, so a part's history includes its
    /// revisions' events.
    #[serde(default, skip_serializing_if = "String::is_empty")]
    pub part_id: String,
}

/// One field's value before and after. `null` on a side where the field did
/// not exist.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct FieldChange {
    pub before: serde_json::Value,
    pub after: serde_json::Value,
}

/// One entry of the audit log. Append-only: nothing edits or deletes one.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct AuditEvent {
    /// 1, 2, 3 … in the order events were written.
    pub id: u64,
    pub at: Timestamp,
    pub actor: Actor,
    /// What happened: `create`, `update`, `delete`, `release`, `checkout`,
    /// `checkin`, `break-lock`, `document`, `uses`, `sourcing`, `sign-in`,
    /// `sign-in-failed`, `session-ended`, `script-write`, … See
    /// `crate::audit` for the full list.
    pub action: String,
    pub entity: EntityRef,
    /// The fields that changed. Secrets (password verifiers, token hashes)
    /// appear as `"(changed)"`, never as their value.
    #[serde(default, skip_serializing_if = "std::collections::BTreeMap::is_empty")]
    pub changes: BTreeMap<String, FieldChange>,
    /// A sentence, for events a field diff does not explain.
    #[serde(default, skip_serializing_if = "String::is_empty")]
    pub detail: String,
    /// The store's sequence number after the change; 0 for an event that
    /// changed nothing in the store (a failed sign-in, a script file).
    #[serde(default)]
    pub seq: u64,
}

// ===========================================================================
// Review and approval
// ===========================================================================

/// Where a review round stands.
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum ReviewStatus {
    /// Waiting for approvals.
    #[default]
    Open,
    /// Its rule is met: enough approvals, no rejection. It may still be
    /// rejected until the subject is released.
    Approved,
    /// A reviewer rejected it; the subject went back to Draft. Final.
    Rejected,
    /// The subject was pulled back to Draft by its author. Final.
    Withdrawn,
}

impl ReviewStatus {
    pub fn as_str(self) -> &'static str {
        match self {
            ReviewStatus::Open => "open",
            ReviewStatus::Approved => "approved",
            ReviewStatus::Rejected => "rejected",
            ReviewStatus::Withdrawn => "withdrawn",
        }
    }
}

/// Who may review: one user, or everyone in a group.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct ReviewerRef {
    /// `user` or `group`.
    pub kind: ReviewerKind,
    /// A user's id, or a group's name.
    pub id: String,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum ReviewerKind {
    User,
    Group,
}

/// A reviewer's verdict.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum Verdict {
    Approve,
    Reject,
}

impl Verdict {
    pub fn as_str(self) -> &'static str {
        match self {
            Verdict::Approve => "approve",
            Verdict::Reject => "reject",
        }
    }
}

/// One reviewer's decision. A rejection always carries a comment.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct Decision {
    pub user_id: String,
    pub verdict: Verdict,
    #[serde(default, skip_serializing_if = "String::is_empty")]
    pub comment: String,
    pub at: Timestamp,
    /// The document hash approved or rejected by this decision.
    pub content_hash: String,
}

impl Decision {
    /// Whether this decision was given on the subject as it is now.
    pub fn is_current(&self, current: &str) -> bool {
        self.content_hash == current
    }
}

/// One round of review of a revision, opened when its
/// subject moves to review.
///
/// The rule it is judged by is COPIED in when it opens (`required_approvals`,
/// `reviewers`, `allow_self_approval`): an administrator changing the
/// settings afterwards changes the next round, not one already under way.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct Review {
    pub id: String,
    #[serde(default)]
    pub status: ReviewStatus,
    /// Who submitted the subject for review.
    pub opened_by: String,
    pub opened_at: Timestamp,
    /// Who may approve or reject. Empty: anyone in the check-in group.
    #[serde(default)]
    pub reviewers: Vec<ReviewerRef>,
    /// Approvals needed before the subject may release. 0 means none.
    #[serde(default)]
    pub required_approvals: u32,
    /// Whether the person who submitted it may approve it themselves.
    #[serde(default)]
    pub allow_self_approval: bool,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub due: Option<Timestamp>,
    /// What decided the rule: `settings`, `part-type:<id>`,
    /// `category:<id>`, and `+script` when the reviewers hook changed it.
    #[serde(default)]
    pub rule_source: String,
    #[serde(default)]
    pub decisions: Vec<Decision>,
    /// Set when the round ends: a rejection, a withdrawal, or the release it
    /// allowed.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub closed_by: Option<String>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub closed_at: Option<Timestamp>,
}

impl Review {
    /// Still deciding, or approved and not yet used: a decision or a
    /// release may act on it.
    pub fn is_live(&self) -> bool {
        matches!(self.status, ReviewStatus::Open | ReviewStatus::Approved) && self.closed_at.is_none()
    }

    /// Each user's LATEST decision, if it is an approval that the rule lets
    /// count (never the submitter's unless the rule allows it) — whatever
    /// version it was given on.
    fn standing_approvals(&self) -> Vec<&Decision> {
        let mut latest: Vec<&Decision> = Vec::new();
        for decision in &self.decisions {
            latest.retain(|d| d.user_id != decision.user_id);
            latest.push(decision);
        }
        latest
            .into_iter()
            .filter(|d| d.verdict == Verdict::Approve && (self.allow_self_approval || d.user_id != self.opened_by))
            .collect()
    }

    /// The users whose approval counts: a standing approval given on the
    /// subject as it is now, `current` being its content hash.
    pub fn approvers(&self, current: &str) -> Vec<&str> {
        self.standing_approvals().into_iter().filter(|d| d.is_current(current)).map(|d| d.user_id.as_str()).collect()
    }

    /// Standing approvals given on an older version of the subject: they need renewing to count.
    pub fn stale_approvers(&self, current: &str) -> Vec<&str> {
        self.standing_approvals().into_iter().filter(|d| !d.is_current(current)).map(|d| d.user_id.as_str()).collect()
    }

    /// Whether the rule is met on the subject as it is now: enough current
    /// approvals, and no rejection.
    pub fn is_met(&self, current: &str) -> bool {
        !self.decisions.iter().any(|d| d.verdict == Verdict::Reject)
            && self.approvers(current).len() as u32 >= self.required_approvals
    }
}

/// A comment on a revision.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct Comment {
    pub id: String,
    pub author: String,
    pub at: Timestamp,
    pub body: String,
    /// The comment this one answers; empty for a new thread.
    #[serde(default, skip_serializing_if = "String::is_empty")]
    pub parent: String,
}

/// The rule a review round is opened with.
#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
#[serde(default)]
pub struct ReviewRule {
    /// Approvals needed before release. 0 — the default — means a release
    /// needs no review at all, as before reviews existed.
    pub required_approvals: u32,
    /// Who may decide. Empty: anyone in the check-in group.
    pub reviewers: Vec<ReviewerRef>,
    /// Days from submission to the due date; 0 for none.
    pub due_days: u32,
    /// Whether the submitter's own approval counts.
    pub allow_self_approval: bool,
}

/// A rule for the parts of one part type, or of one category and every
/// category under it. A category's beats a part type's, and the nearest
/// category beats its ancestors.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct ReviewOverride {
    /// `part_type` or `category`.
    pub scope: String,
    pub id: String,
    pub rule: ReviewRule,
}

// ===========================================================================
// Server settings — the administrator's policy choices
// ===========================================================================

/// Configurable names and availability of workflow status options.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct StatusOption {
    pub state: Lifecycle,
    pub name: String,
    pub enabled: bool,
}

pub fn default_status_options() -> Vec<StatusOption> {
    [(Lifecycle::Draft, "none"), (Lifecycle::InReview, "review"),
     (Lifecycle::Released, "released"), (Lifecycle::Obsolete, "obsolete"),
     (Lifecycle::Superseded, "superseded")].into_iter()
        .map(|(state, name)| StatusOption { state, name: name.into(), enabled: true }).collect()
}

/// Policy the administrator decides, not code. Each
/// defaults to the PERMISSIVE choice, and a file written before settings
/// existed loads with those defaults.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(default)]
pub struct Settings {
    pub setup_completed: bool,
    pub status_options: Vec<StatusOption>,
    /// Refuse changes to a part's category and attribute values while the
    /// part has no revision in work and at least one released one — so a
    /// released part's catalog values change only by starting a new revision.
    /// Off by default: attributes stay editable after release.
    pub lock_released_attributes: bool,
    /// Let a part have more than one revision in work (Draft or InReview) at
    /// once. On by default, which is what lets a family row create the
    /// revision it names while another is open.
    pub allow_multiple_open_drafts: bool,
    /// Refuse to release an assembly revision while anything in its "uses"
    /// list is not Released — the classic release gate. Off by default: the
    /// release goes ahead and says which children are unreleased.
    pub require_released_children: bool,
    /// A browser session ends after this many minutes without a request.
    /// 14 days by default, which is what sessions lasted before this setting.
    pub session_idle_minutes: u64,
    /// A browser session ends this many hours after sign-in however busy it
    /// is. 30 days by default; 0 means no absolute limit.
    pub session_max_hours: u64,
    /// Let administrators edit and test-run scripts in the browser. On by
    /// default. Scripts are fully trusted, so an admin
    /// login with the editor on is a shell on the server; turning it off
    /// leaves the scripts directory live and editable only on the host. The
    /// server's `--lock-script-editor` flag forces it off regardless.
    pub script_editor_enabled: bool,
    /// The review rule for a revision whose part no override covers. The
    /// default needs 0 approvals, so a release works as it did before
    /// reviews existed until an administrator sets one.
    pub review_rule: ReviewRule,
    /// Rules for particular part types and categories ([`ReviewOverride`]).
    pub review_overrides: Vec<ReviewOverride>,
    /// Let a signed-in user browse (read only) other users' workspaces
    /// ([`WorkspaceEntry`]). On by default, the permissive choice of rounds
    /// 6-7: a workspace holds links to parts everyone may already read, and
    /// files a colleague often needs to see (a customer's photo of a
    /// failure). Off, a workspace is its owner's alone; an administrator can
    /// still read any, to support a user.
    pub workspaces_browsable: bool,
}

impl Default for Settings {
    fn default() -> Self {
        Self {
            setup_completed: false,
            status_options: default_status_options(),
            lock_released_attributes: false,
            allow_multiple_open_drafts: true,
            require_released_children: false,
            session_idle_minutes: 14 * 24 * 60,
            session_max_hours: 30 * 24,
            script_editor_enabled: true,
            review_rule: ReviewRule::default(),
            review_overrides: Vec::new(),
            workspaces_browsable: true,
        }
    }
}

// ===========================================================================
// Workspaces — each user's folders of links and files (plm-cad-integration D12)
// ===========================================================================

/// One workspace entry: a folder, a link (to a part or a folder), or a file.
///
/// Stored flat. A folder is stored once and sits nowhere (`parent` empty);
/// every place it appears is a link to it. A link or a file sits in a folder
/// (`parent`) or at the top of a user's Home (`parent` empty, `owner` the
/// user). A link is never a copy; deleting one never touches what it points
/// at. A file's bytes are blobs in the attachment store, and every replace
/// keeps the version before it ([`FileVersion`]).
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct WorkspaceEntry {
    pub id: String,
    /// Who owns it. A folder's owner renames it and changes what is in it.
    /// For an entry at the top of a Home, that Home's user; else whoever
    /// placed it.
    pub owner: String,
    /// The folder it is in; empty at the top of a Home, and always for a
    /// folder.
    #[serde(default)]
    pub parent: String,
    /// A part link's or file's name is unique among those of its folder,
    /// ignoring ASCII case. A link to a folder carries the folder's name.
    pub name: String,
    pub kind: WorkspaceKind,
    /// For a link: the part (and the revision it is pinned to), or the folder.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub link: Option<WorkspaceLink>,
    /// For a file: every version, oldest first; the last is current.
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub versions: Vec<FileVersion>,
    pub created_at: Timestamp,
    #[serde(default)]
    pub modified_at: Timestamp,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum WorkspaceKind {
    Folder,
    Link,
    File,
}

impl WorkspaceKind {
    pub fn as_str(self) -> &'static str {
        match self {
            WorkspaceKind::Folder => "folder",
            WorkspaceKind::Link => "link",
            WorkspaceKind::File => "file",
        }
    }
}

/// What a link points at: a part, a folder, a file, or a part's (or
/// revision's) attachment. Held by id, so a part renamed or renumbered is
/// still found.
#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize)]
pub struct WorkspaceLink {
    /// The part; for a link to an attachment, the part it is on. Empty for a
    /// link to a folder or a file.
    #[serde(default)]
    pub part_id: String,
    /// For a link to a folder: the folder, which is one folder however many
    /// Homes link it (its name, owner and contents are the same in each).
    #[serde(default, skip_serializing_if = "String::is_empty")]
    pub folder_id: String,
    /// For a link to a file: the file, stored once like a folder.
    #[serde(default, skip_serializing_if = "String::is_empty")]
    pub file_id: String,
    /// For a link to an attachment: which, on the part `part_id` (and the
    /// revision `revision_id`, when it is a revision's).
    #[serde(default, skip_serializing_if = "String::is_empty")]
    pub attachment_id: String,
    /// Empty: the link follows the part's newest revision, drafts included
    /// (D13). Else the revision it is pinned to.
    #[serde(default)]
    pub revision_id: String,
}

/// One version of a workspace file.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct FileVersion {
    /// 1, 2, 3 … in upload order; never reused.
    pub version: u32,
    pub sha256: String,
    pub size: u64,
    pub media_type: String,
    pub uploaded_by: String,
    pub uploaded_at: Timestamp,
    /// The version this one restored, when it was made by a restore.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub restored_from: Option<u32>,
}

// ===========================================================================
// The catalog — categories and their typed attribute schemas
// ===========================================================================

/// A node of the catalog tree. A part has ONE primary category plus free tags
///. A child category INHERITS every attribute of
/// its ancestors and may add its own, but may not redefine one — so
/// "Fasteners" can require a material and "Fasteners/Screws" add a thread
/// without ever disagreeing about what a material is.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Category {
    /// Stable key, stored on parts (`"screws"`). Never changes.
    pub id: String,
    /// What the UI shows (`"Screws"`).
    pub name: String,
    /// The parent's id; empty for a top-level category.
    #[serde(default)]
    pub parent: String,
    /// The attributes THIS category adds. Its effective schema is these plus
    /// every ancestor's.
    #[serde(default)]
    pub attributes: Vec<AttributeDef>,
    #[serde(default)]
    pub created_at: Timestamp,
}

/// One typed attribute a category defines.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct AttributeDef {
    /// The key values are stored under on a part (`"thread"`).
    pub key: String,
    /// What the UI shows (`"Thread size"`).
    pub name: String,
    #[serde(flatten)]
    pub kind: AttributeKind,
    /// Must have a value before a revision of the part can release. A draft
    /// may leave it empty.
    #[serde(default)]
    pub required: bool,
}

/// What values an attribute takes. Serialized under `type`, beside the
/// attribute's other fields: `{"key":"length","name":"Length","type":"number","unit":"mm"}`.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(tag = "type", rename_all = "lowercase")]
pub enum AttributeKind {
    Text,
    /// A number. `unit` is a label the UI shows, not a conversion.
    Number {
        #[serde(default)]
        unit: String,
    },
    Bool,
    /// One of a fixed list of values.
    Enum { values: Vec<String> },
}

impl AttributeKind {
    pub fn as_str(&self) -> &'static str {
        match self {
            AttributeKind::Text => "text",
            AttributeKind::Number { .. } => "number",
            AttributeKind::Bool => "bool",
            AttributeKind::Enum { .. } => "enum",
        }
    }
}