alembic-adapter-netbox 0.2.0

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

const CUSTOM_OBJECT_FEATURE: &str = "custom-object";
const CUSTOM_OBJECT_APP_LABEL: &str = "netbox_custom_objects";
const ALEMBIC_CUSTOM_OBJECT_PREFIX: &str = "alembic custom object for ";

#[async_trait]
impl Adapter for NetBoxAdapter {
    async fn read(
        &self,
        schema: &Schema,
        types: &[TypeName],
        state_store: &alembic_engine::StateStore,
    ) -> Result<ObservedState> {
        let registry: ObjectTypeRegistry = build_registry_for_schema(self, schema).await?;
        let mut state = ObservedState::default();
        let mappings = state_mappings(state_store);

        let requested: BTreeSet<TypeName> = if types.is_empty() {
            registry.type_names().into_iter().collect()
        } else {
            types.iter().cloned().collect()
        };

        for type_name in requested {
            let info = registry
                .info_for(&type_name)
                .ok_or_else(|| anyhow!("unsupported type {}", type_name))?;
            let type_schema = schema
                .types
                .get(type_name.as_str())
                .ok_or_else(|| anyhow!("missing schema for {}", type_name))?;
            let resource: Resource<Value> = self.client.resource(info.endpoint.clone());
            let objects = match self.client.list_all(&resource, None).await {
                Ok(objects) => objects,
                Err(err)
                    if is_404_anyhow(&err) && info.features.contains(CUSTOM_OBJECT_FEATURE) =>
                {
                    continue;
                }
                Err(err) => return Err(err),
            };
            for object in objects {
                let (backend_id, mut attrs) = extract_attrs(object)?;
                normalize_attrs(&mut attrs, type_schema, schema, &registry, &mappings);
                let key = build_key_from_schema(type_schema, &attrs)
                    .with_context(|| format!("build key for {}", type_name))?;
                state.insert(ObservedObject {
                    type_name: type_name.clone(),
                    key,
                    attrs,
                    backend_id: Some(BackendId::Int(backend_id)),
                });
            }
        }

        Ok(state)
    }

    async fn write(
        &self,
        schema: &Schema,
        ops: &[Op],
        state: &alembic_engine::StateStore,
    ) -> Result<ApplyReport> {
        let registry: ObjectTypeRegistry = build_registry_for_schema(self, schema).await?;
        let custom_fields_by_type = self.client.fetch_custom_fields().await?;
        let mut applied = Vec::new();
        let mut resolved = resolved_from_state(state);

        for op in ops {
            if let Op::Create { uid, .. } = op {
                resolved.remove(uid);
            }
        }

        let tag_names = collect_tag_names(ops, &registry)?;
        if !tag_names.is_empty() {
            let mut existing = self.client.fetch_tags().await?;
            let missing: Vec<String> = tag_names.difference(&existing).cloned().collect();
            if !missing.is_empty() {
                self.create_tags(&missing).await?;
                for tag in missing {
                    existing.insert(tag);
                }
            }
        }

        let mut creates_updates = Vec::new();
        let mut deletes = Vec::new();
        for op in ops {
            match op {
                Op::Delete { .. } => deletes.push(op.clone()),
                _ => creates_updates.push(op.clone()),
            }
        }

        struct ApplyDriver<'a> {
            adapter: &'a NetBoxAdapter,
            resolved: &'a mut BTreeMap<Uid, u64>,
            registry: &'a ObjectTypeRegistry,
            schema: &'a Schema,
            custom_fields_by_type: &'a BTreeMap<String, BTreeSet<String>>,
        }

        #[async_trait]
        impl RetryApplyDriver for ApplyDriver<'_> {
            async fn apply_non_delete(&mut self, op: &Op) -> Result<AppliedOp> {
                match op {
                    Op::Create { .. } => self
                        .adapter
                        .apply_create(
                            op,
                            self.resolved,
                            self.registry,
                            self.schema,
                            self.custom_fields_by_type,
                        )
                        .await
                        .map(|backend_id| AppliedOp {
                            uid: op.uid(),
                            type_name: op.type_name().clone(),
                            backend_id: Some(BackendId::Int(backend_id)),
                        }),
                    Op::Update { .. } => self
                        .adapter
                        .apply_update(
                            op,
                            self.resolved,
                            self.registry,
                            self.schema,
                            self.custom_fields_by_type,
                        )
                        .await
                        .map(|backend_id| AppliedOp {
                            uid: op.uid(),
                            type_name: op.type_name().clone(),
                            backend_id: Some(BackendId::Int(backend_id)),
                        }),
                    Op::Delete { .. } => unreachable!("delete ops filtered before retry"),
                }
            }

            fn is_retryable(&self, err: &anyhow::Error) -> bool {
                is_missing_ref_error(err)
            }
        }

        let mut driver = ApplyDriver {
            adapter: self,
            resolved: &mut resolved,
            registry: &registry,
            schema,
            custom_fields_by_type: &custom_fields_by_type,
        };
        let retry_result = apply_non_delete_with_retries(&creates_updates, &mut driver).await?;

        if !retry_result.pending.is_empty() {
            let missing = describe_missing_refs(&retry_result.pending, &resolved);
            return Err(anyhow!("unresolved references: {missing}"));
        }

        for applied_op in retry_result.applied {
            if let Some(BackendId::Int(backend_id)) = &applied_op.backend_id {
                resolved.insert(applied_op.uid, *backend_id);
            }
            applied.push(applied_op);
        }

        for op in deletes {
            if let Op::Delete {
                uid,
                type_name,
                key,
                backend_id,
            } = op
            {
                let id = if let Some(BackendId::Int(id)) = backend_id {
                    id
                } else if let Some(id) = resolved.get(&uid).copied() {
                    id
                } else {
                    let info = registry
                        .info_for(&type_name)
                        .ok_or_else(|| anyhow!("unsupported type {}", type_name))?;
                    let type_schema = schema
                        .types
                        .get(type_name.as_str())
                        .ok_or_else(|| anyhow!("missing schema for {}", type_name))?;
                    self.lookup_backend_id(&type_name, &info, type_schema, &key, &resolved)
                        .await
                        .with_context(|| {
                            format!("resolve backend id for delete: {}", key_string(&key))
                        })?
                };
                let info = registry
                    .info_for(&type_name)
                    .ok_or_else(|| anyhow!("unsupported type {}", type_name))?;
                let resource: Resource<Value> = self.client.resource(info.endpoint.clone());
                let batch = [BulkDelete::new(id)];
                match resource.bulk_delete(&batch).await {
                    Ok(_) => {}
                    Err(err) if is_404_error(&err) => {
                        tracing::warn!(type_name = %type_name, "object already deleted");
                    }
                    Err(err) => return Err(err.into()),
                }
                applied.push(AppliedOp {
                    uid,
                    type_name: type_name.clone(),
                    backend_id: None,
                });
            }
        }

        Ok(ApplyReport {
            applied,
            ..Default::default()
        })
    }

    async fn ensure_schema(&self, schema: &Schema) -> Result<ProvisionReport> {
        let mut registry: ObjectTypeRegistry = self.client.fetch_object_types().await?;
        let custom_fields_by_type = self.client.fetch_custom_fields().await?;
        let custom_object_types = self.client.fetch_custom_object_types().await?;
        let custom_object_fields = self.client.fetch_custom_object_type_fields().await?;
        let custom_objects_available = custom_object_types.is_some();

        let mut created_fields = Vec::new();
        let created_tags = Vec::new();
        let mut created_object_types = Vec::new();
        let mut created_object_fields = Vec::new();
        let mut deleted_object_types = Vec::new();
        let mut deleted_object_fields = Vec::new();

        let mut custom_types_by_name: BTreeMap<String, CustomObjectType> = BTreeMap::new();
        if let Some(types) = custom_object_types {
            for item in types {
                custom_types_by_name.insert(item.name.clone(), item);
            }
        }

        let mut custom_fields_by_type_id: BTreeMap<u64, BTreeMap<String, u64>> = BTreeMap::new();
        if let Some(fields) = custom_object_fields {
            for field in fields {
                custom_fields_by_type_id
                    .entry(field.custom_object_type)
                    .or_default()
                    .insert(field.name, field.id);
            }
        }

        let mut custom_schema_types: Vec<(TypeName, &TypeSchema)> = Vec::new();
        let mut custom_schema_type_names: BTreeSet<String> = BTreeSet::new();
        for (type_name, type_schema) in &schema.types {
            let type_name = TypeName::new(type_name);
            if registry.contains_type(&type_name) {
                let info = registry
                    .info_for(&type_name)
                    .ok_or_else(|| anyhow!("unsupported type {}", type_name))?;
                if !supports_feature(&info.features, &["custom-fields"]) {
                    continue;
                }

                let native_fields = native_fields_for_type(self, &info, type_schema).await?;
                let existing = custom_fields_by_type
                    .get(type_name.as_str())
                    .cloned()
                    .unwrap_or_default();

                for (field_name, field_schema) in &type_schema.fields {
                    if matches!(
                        field_schema.r#type,
                        FieldType::Ref { .. } | FieldType::ListRef { .. }
                    ) {
                        continue;
                    }
                    if native_fields.contains(field_name) || existing.contains(field_name) {
                        continue;
                    }
                    if self
                        .create_custom_field(&type_name, field_name, field_schema)
                        .await?
                    {
                        created_fields.push(format!("{}.{}", type_name, field_name));
                    }
                }
                continue;
            }

            custom_schema_type_names.insert(type_name.as_str().to_string());
            custom_schema_types.push((type_name, type_schema));
        }

        let mut desired_fields_by_type_id: BTreeMap<u64, BTreeSet<String>> = BTreeMap::new();

        if !custom_schema_types.is_empty() {
            if !custom_objects_available {
                let list = custom_schema_types
                    .iter()
                    .map(|(name, _)| name.as_str())
                    .collect::<Vec<_>>()
                    .join(", ");
                return Err(anyhow!(
                    "schema includes custom type(s) but netbox custom objects are not available: {list}"
                ));
            }

            let mut custom_type_ids: BTreeMap<String, u64> = BTreeMap::new();
            for (type_name, _) in &custom_schema_types {
                let custom_name = custom_object_type_name(type_name);
                let type_id = if let Some(existing) = custom_types_by_name.get(&custom_name) {
                    let (app_label, model) =
                        custom_object_type_parts(existing).unwrap_or_else(|| {
                            (CUSTOM_OBJECT_APP_LABEL.to_string(), custom_name.clone())
                        });
                    registry.insert_custom_object_type(
                        type_name.clone(),
                        custom_object_endpoint(&custom_name),
                        custom_object_features(),
                        app_label,
                        model,
                    );
                    existing.id
                } else {
                    let payload = Map::from_iter([
                        ("name".to_string(), Value::String(custom_name.clone())),
                        ("slug".to_string(), Value::String(custom_name.clone())),
                        (
                            "description".to_string(),
                            Value::String(format!(
                                "alembic custom object for {}",
                                type_name.as_str()
                            )),
                        ),
                        (
                            "verbose_name_plural".to_string(),
                            Value::String(custom_object_verbose_name_plural(type_name)),
                        ),
                    ]);
                    let resource: Resource<Value> = self
                        .client
                        .resource("plugins/custom-objects/custom-object-types/");
                    match resource.create(&Value::Object(payload)).await {
                        Ok(created) => {
                            let created_type = super::client::parse_custom_object_type(created)?;
                            let id = created_type.id;
                            let (app_label, model) = custom_object_type_parts(&created_type)
                                .unwrap_or_else(|| {
                                    (CUSTOM_OBJECT_APP_LABEL.to_string(), custom_name.clone())
                                });
                            registry.insert_custom_object_type(
                                type_name.clone(),
                                custom_object_endpoint(&custom_name),
                                custom_object_features(),
                                app_label,
                                model,
                            );
                            custom_types_by_name.insert(custom_name.clone(), created_type);
                            created_object_types.push(type_name.to_string());
                            id
                        }
                        Err(err) => {
                            if let Some(types) = self.client.fetch_custom_object_types().await? {
                                if let Some(existing) =
                                    types.into_iter().find(|item| item.name == custom_name)
                                {
                                    let (app_label, model) = custom_object_type_parts(&existing)
                                        .unwrap_or_else(|| {
                                            (
                                                CUSTOM_OBJECT_APP_LABEL.to_string(),
                                                custom_name.clone(),
                                            )
                                        });
                                    registry.insert_custom_object_type(
                                        type_name.clone(),
                                        custom_object_endpoint(&custom_name),
                                        custom_object_features(),
                                        app_label,
                                        model,
                                    );
                                    let id = existing.id;
                                    custom_types_by_name.insert(custom_name.clone(), existing);
                                    id
                                } else {
                                    return Err(err.into());
                                }
                            } else {
                                return Err(err.into());
                            }
                        }
                    }
                };
                custom_type_ids.insert(type_name.as_str().to_string(), type_id);
            }

            for (type_name, type_schema) in custom_schema_types {
                let Some(type_id) = custom_type_ids.get(type_name.as_str()).copied() else {
                    return Err(anyhow!("custom object type id missing for {}", type_name));
                };
                let existing_fields = custom_fields_by_type_id.entry(type_id).or_default();
                let mut provisioner = CustomObjectFieldProvisioner {
                    adapter: self,
                    registry: &registry,
                    custom_object_type_id: type_id,
                    existing_fields,
                    created_object_fields: &mut created_object_fields,
                    type_name: &type_name,
                };
                let mut desired_fields = BTreeSet::new();
                for (field_name, field_schema) in &type_schema.key {
                    if desired_fields.insert(field_name.clone()) {
                        provisioner.ensure(field_name, field_schema, true).await?;
                    }
                }
                for (field_name, field_schema) in &type_schema.fields {
                    if desired_fields.insert(field_name.clone()) {
                        provisioner.ensure(field_name, field_schema, false).await?;
                    }
                }
                desired_fields_by_type_id.insert(type_id, desired_fields);
            }
        }

        if custom_objects_available {
            let resource_fields: Resource<Value> = self
                .client
                .resource("plugins/custom-objects/custom-object-type-fields/");
            let resource_types: Resource<Value> = self
                .client
                .resource("plugins/custom-objects/custom-object-types/");

            for custom_type in custom_types_by_name.values() {
                let Some(type_name) = alembic_custom_object_name(custom_type) else {
                    continue;
                };
                let is_desired = custom_schema_type_names.contains(type_name.as_str());
                if is_desired {
                    let Some(existing_fields) = custom_fields_by_type_id.get(&custom_type.id)
                    else {
                        continue;
                    };
                    let desired_fields = desired_fields_by_type_id.get(&custom_type.id);
                    for (field_name, field_id) in existing_fields {
                        if is_reserved_custom_object_field(field_name) {
                            continue;
                        }
                        if desired_fields.is_some_and(|fields| fields.contains(field_name)) {
                            continue;
                        }
                        match resource_fields.delete(*field_id).await {
                            Ok(_) => {}
                            Err(err) if is_404_error(&err) => {
                                tracing::warn!(
                                    type_name = %type_name,
                                    field = %field_name,
                                    "custom object field already deleted"
                                );
                            }
                            Err(err) => return Err(err.into()),
                        }
                        deleted_object_fields.push(format!("{}.{}", type_name, field_name));
                    }
                } else {
                    if let Some(existing_fields) = custom_fields_by_type_id.get(&custom_type.id) {
                        for (field_name, field_id) in existing_fields {
                            if is_reserved_custom_object_field(field_name) {
                                continue;
                            }
                            match resource_fields.delete(*field_id).await {
                                Ok(_) => {}
                                Err(err) if is_404_error(&err) => {
                                    tracing::warn!(
                                        type_name = %type_name,
                                        field = %field_name,
                                        "custom object field already deleted"
                                    );
                                }
                                Err(err) => return Err(err.into()),
                            }
                            deleted_object_fields.push(format!("{}.{}", type_name, field_name));
                        }
                    }
                    match resource_types.delete(custom_type.id).await {
                        Ok(_) => {}
                        Err(err) if is_404_error(&err) => {
                            tracing::warn!(
                                type_name = %type_name,
                                "custom object type already deleted"
                            );
                        }
                        Err(err) => return Err(err.into()),
                    }
                    deleted_object_types.push(type_name);
                }
            }
        }

        Ok(ProvisionReport {
            created_fields,
            created_tags,
            created_object_types,
            created_object_fields,
            deprecated_object_types: Vec::new(),
            deprecated_object_fields: Vec::new(),
            deleted_object_types,
            deleted_object_fields,
        })
    }
}

impl NetBoxAdapter {
    async fn apply_create(
        &self,
        op: &Op,
        resolved: &mut BTreeMap<Uid, u64>,
        registry: &ObjectTypeRegistry,
        schema: &Schema,
        custom_fields_by_type: &BTreeMap<String, BTreeSet<String>>,
    ) -> Result<u64> {
        let (uid, type_name, desired) = match op {
            Op::Create {
                uid,
                type_name,
                desired,
            } => (*uid, type_name, desired),
            _ => return Err(anyhow!("expected create operation")),
        };
        let info = registry
            .info_for(type_name)
            .ok_or_else(|| anyhow!("unsupported type {}", type_name))?;
        let type_schema = schema
            .types
            .get(type_name.as_str())
            .ok_or_else(|| anyhow!("missing schema for {}", type_name))?;
        let resource: Resource<Value> = self.client.resource(info.endpoint.clone());
        let custom_fields = custom_fields_by_type
            .get(info.type_name.as_str())
            .cloned()
            .unwrap_or_default();
        let body = build_request_body(
            type_name,
            type_schema,
            &desired.attrs,
            resolved,
            &custom_fields,
            &info.features,
        )?;
        let response: Value = match resource.create(&body).await {
            Ok(response) => response,
            Err(err) if is_conflict_error(&err) => {
                if let Ok(existing) = self
                    .lookup_backend_id(type_name, &info, type_schema, &desired.key, resolved)
                    .await
                {
                    tracing::warn!(
                        type_name = %type_name,
                        key = %key_string(&desired.key),
                        "create already exists; using existing object"
                    );
                    resolved.insert(uid, existing);
                    return Ok(existing);
                }
                return Err(err.into());
            }
            Err(err) => return Err(err.into()),
        };
        let backend_id = response
            .get("id")
            .and_then(Value::as_u64)
            .ok_or_else(|| anyhow!("create {} returned no id", type_name))?;
        resolved.insert(uid, backend_id);
        Ok(backend_id)
    }

    async fn apply_update(
        &self,
        op: &Op,
        resolved: &BTreeMap<Uid, u64>,
        registry: &ObjectTypeRegistry,
        schema: &Schema,
        custom_fields_by_type: &BTreeMap<String, BTreeSet<String>>,
    ) -> Result<u64> {
        let (uid, type_name, desired, backend_id) = match op {
            Op::Update {
                uid,
                type_name,
                desired,
                backend_id,
                ..
            } => {
                let id = match backend_id {
                    Some(BackendId::Int(id)) => Some(*id),
                    Some(_) => return Err(anyhow!("netbox requires integer backend id")),
                    None => None,
                };
                (*uid, type_name, desired, id)
            }
            _ => return Err(anyhow!("expected update operation")),
        };
        let info = registry
            .info_for(type_name)
            .ok_or_else(|| anyhow!("unsupported type {}", type_name))?;
        let type_schema = schema
            .types
            .get(type_name.as_str())
            .ok_or_else(|| anyhow!("missing schema for {}", type_name))?;
        let id = if let Some(id) = backend_id {
            id
        } else if let Some(id) = resolved.get(&uid).copied() {
            id
        } else {
            self.lookup_backend_id(type_name, &info, type_schema, &desired.key, resolved)
                .await
                .with_context(|| format!("resolve backend id for {}", type_name))?
        };
        let resource: Resource<Value> = self.client.resource(info.endpoint.clone());
        let custom_fields = custom_fields_by_type
            .get(info.type_name.as_str())
            .cloned()
            .unwrap_or_default();
        let body = build_request_body(
            type_name,
            type_schema,
            &desired.attrs,
            resolved,
            &custom_fields,
            &info.features,
        )?;
        let _response = resource.patch(id, &body).await?;
        Ok(id)
    }

    async fn lookup_backend_id(
        &self,
        type_name: &TypeName,
        info: &super::registry::ObjectTypeInfo,
        type_schema: &TypeSchema,
        key: &Key,
        resolved: &BTreeMap<Uid, u64>,
    ) -> Result<u64> {
        let query = query_from_key(type_schema, key, resolved)?;
        let resource: Resource<Value> = self.client.resource(info.endpoint.clone());
        let page = resource.list(Some(query)).await?;
        let item = page
            .results
            .into_iter()
            .next()
            .ok_or_else(|| anyhow!("{} not found for key {}", type_name, key_string(key)))?;
        item.get("id")
            .and_then(Value::as_u64)
            .ok_or_else(|| anyhow!("{} lookup missing id", type_name))
    }

    async fn create_tags(&self, tags: &[String]) -> Result<()> {
        let resource = self.client.extras().tags();
        for tag in tags {
            let payload = serde_json::json!({
                "name": tag,
                "slug": slugify(tag),
            });
            if let Err(err) = resource.create(&payload).await {
                let existing = self.client.fetch_tags().await?;
                if existing.contains(tag) {
                    tracing::warn!(tag = %tag, "tag already exists");
                    continue;
                }
                return Err(err.into());
            }
        }
        Ok(())
    }

    async fn create_custom_field(
        &self,
        type_name: &TypeName,
        field_name: &str,
        field_schema: &FieldSchema,
    ) -> Result<bool> {
        let field_type = custom_field_type_for_schema(field_schema);
        let mut payload = Map::new();
        payload.insert("name".to_string(), Value::String(field_name.to_string()));
        payload.insert("label".to_string(), Value::String(field_name.to_string()));
        payload.insert("type".to_string(), Value::String(field_type));
        payload.insert(
            "object_types".to_string(),
            Value::Array(vec![Value::String(type_name.as_str().to_string())]),
        );
        if field_schema.required {
            payload.insert("required".to_string(), Value::Bool(true));
        }
        if let Some(description) = &field_schema.description {
            payload.insert(
                "description".to_string(),
                Value::String(description.clone()),
            );
        }
        let resource = self.client.extras().custom_fields();
        match resource.create(&Value::Object(payload)).await {
            Ok(_) => Ok(true),
            Err(err) => {
                let existing = self.client.fetch_custom_fields().await?;
                if existing
                    .get(type_name.as_str())
                    .is_some_and(|fields| fields.contains(field_name))
                {
                    tracing::warn!(
                        type_name = %type_name,
                        field = %field_name,
                        "custom field already exists"
                    );
                    Ok(false)
                } else {
                    Err(err.into())
                }
            }
        }
    }
}

fn extract_attrs(value: Value) -> Result<(u64, JsonMap)> {
    let Value::Object(mut map) = value else {
        return Err(anyhow!("expected object payload"));
    };
    let backend_id = map
        .get("id")
        .and_then(Value::as_u64)
        .ok_or_else(|| anyhow!("missing id in payload"))?;
    let custom_fields = map.remove("custom_fields");
    let tags = map.remove("tags");
    map.remove("id");
    map.remove("url");
    map.remove("display");
    map.remove("custom_object_type");
    let mut attrs: JsonMap = map.into_iter().collect::<BTreeMap<_, _>>().into();
    if let Some(Value::Object(fields)) = custom_fields {
        for (key, value) in fields {
            attrs.entry(key).or_insert(value);
        }
    }
    if let Some(tags_value) = tags {
        let tags = tags_from_value(&tags_value)?;
        attrs.insert(
            "tags".to_string(),
            Value::Array(tags.into_iter().map(Value::String).collect()),
        );
    }
    Ok((backend_id, attrs))
}

fn normalize_attrs(
    attrs: &mut JsonMap,
    type_schema: &TypeSchema,
    schema: &Schema,
    registry: &ObjectTypeRegistry,
    mappings: &super::state::StateMappings,
) {
    let keys: Vec<String> = attrs.keys().cloned().collect();
    for key in keys {
        if let Some(value) = attrs.get(&key).cloned() {
            // Look up the field's target type from the schema
            let target_hint = type_schema
                .fields
                .get(&key)
                .map(|fs| &fs.r#type)
                .and_then(|ft| match ft {
                    FieldType::Ref { target } => Some(target.as_str()),
                    FieldType::ListRef { target } => Some(target.as_str()),
                    _ => None,
                });
            let normalized = normalize_value(value, target_hint, schema, registry, mappings);
            attrs.insert(key, normalized);
        }
    }
    if attrs.contains_key("type") && !attrs.contains_key("if_type") {
        if let Some(value) = attrs.remove("type") {
            attrs.insert("if_type".to_string(), value);
        }
    }
    if let (Some(Value::String(kind)), Some(id_value)) = (
        attrs.remove("assigned_object_type"),
        attrs.remove("assigned_object_id"),
    ) {
        if kind == "dcim.interface" {
            if let Some(id) = as_u64(&id_value) {
                if let Some(uid) = mappings.uid_for("dcim.interface", id) {
                    attrs.insert(
                        "assigned_interface".to_string(),
                        Value::String(uid.to_string()),
                    );
                }
            }
        }
    }
    if let (Some(Value::String(scope)), Some(id_value)) =
        (attrs.remove("scope_type"), attrs.remove("scope_id"))
    {
        if scope == "dcim.site" {
            if let Some(id) = as_u64(&id_value) {
                if let Some(uid) = mappings.uid_for("dcim.site", id) {
                    attrs.insert("site".to_string(), Value::String(uid.to_string()));
                }
            }
        }
    }
}

fn normalize_value(
    value: Value,
    target_hint: Option<&str>,
    schema: &Schema,
    registry: &ObjectTypeRegistry,
    mappings: &super::state::StateMappings,
) -> Value {
    match value {
        Value::Array(items) => Value::Array(
            items
                .into_iter()
                .map(|item| normalize_value(item, target_hint, schema, registry, mappings))
                .collect(),
        ),
        Value::Object(map) => {
            if let Some(id) = map.get("id").and_then(as_u64) {
                // First, try existing approach: lookup via URL + mappings
                if let Some(uid) = uid_for_nested_object(&map, registry, mappings) {
                    return Value::String(uid.to_string());
                }
                // If we know the target type from schema, try to generate UID from key fields
                if let Some(target) = target_hint {
                    if let Some(uid) = uid_from_key_fields(&map, target, schema, registry, mappings)
                    {
                        return Value::String(uid.to_string());
                    }
                }
                // If it looks like a resource summary but isn't managed by us,
                // fall back to the ID integer to match desired state integers.
                if map.contains_key("url") || map.contains_key("display") {
                    return Value::Number(id.into());
                }
            }
            if let Some(value) = map.get("value").and_then(Value::as_str) {
                let label_only = map.keys().all(|key| key == "value" || key == "label");
                if label_only {
                    return Value::String(value.to_string());
                }
            }
            // Recurse into nested objects without a target hint
            let mut normalized = Map::new();
            for (key, value) in map {
                normalized.insert(
                    key,
                    normalize_value(value, None, schema, registry, mappings),
                );
            }
            Value::Object(normalized)
        }
        other => other,
    }
}

fn as_u64(value: &Value) -> Option<u64> {
    match value {
        Value::Number(num) => num.as_u64(),
        Value::String(raw) => raw.parse().ok(),
        _ => None,
    }
}

fn uid_for_nested_object(
    map: &Map<String, Value>,
    registry: &ObjectTypeRegistry,
    mappings: &super::state::StateMappings,
) -> Option<Uid> {
    let id = map.get("id")?.as_u64()?;
    let endpoint = map
        .get("url")
        .and_then(Value::as_str)
        .and_then(|url| registry.type_name_for_endpoint(url))?;
    mappings.uid_for(endpoint, id)
}

/// Generate a UID from key fields when we know the target type but the object isn't in mappings.
/// This handles the case where nested objects don't have URLs but we know the target type from schema.
fn uid_from_key_fields(
    map: &Map<String, Value>,
    target: &str,
    schema: &Schema,
    registry: &ObjectTypeRegistry,
    mappings: &super::state::StateMappings,
) -> Option<Uid> {
    // First, try to determine type from URL if available and use mappings
    if let Some(type_from_url) = map
        .get("url")
        .and_then(Value::as_str)
        .and_then(|url| registry.type_name_for_endpoint(url))
    {
        if let Some(id) = map.get("id").and_then(as_u64) {
            if let Some(uid) = mappings.uid_for(type_from_url, id) {
                return Some(uid);
            }
        }
    }

    // Get the target type's schema to find its key fields
    let target_schema = schema.types.get(target)?;

    // Build a key from available fields
    let mut key_map = BTreeMap::new();
    for key_field in target_schema.key.keys() {
        let value = map.get(key_field)?;
        key_map.insert(key_field.clone(), value.clone());
    }

    // Generate deterministic UID from type name and key
    let key = Key::from(key_map);
    Some(uid_v5(target, &key_string(&key)))
}

fn build_request_body(
    type_name: &TypeName,
    type_schema: &TypeSchema,
    attrs: &JsonMap,
    resolved: &BTreeMap<Uid, u64>,
    custom_fields: &BTreeSet<String>,
    features: &BTreeSet<String>,
) -> Result<Value> {
    let mut body = Map::new();
    let mut custom = Map::new();

    for (key, value) in attrs.iter() {
        let api_key = if type_name.as_str() == "dcim.interface" && key == "if_type" {
            "type"
        } else {
            key.as_str()
        };
        if key == "tags" {
            if !supports_feature(features, &["tags"]) {
                return Err(anyhow!("{} does not support tags", type_name));
            }
            let tags = tags_from_value(value)?;
            let tag_inputs = build_tag_inputs(&tags);
            body.insert(api_key.to_string(), serde_json::to_value(tag_inputs)?);
            continue;
        }

        let field_schema = type_schema
            .fields
            .get(key)
            .ok_or_else(|| anyhow!("missing schema for field {key}"))?;
        let encoded = resolve_value_for_type(&field_schema.r#type, value.clone(), resolved)?;

        if custom_fields.contains(key) {
            if !supports_feature(features, &["custom-fields"]) {
                return Err(anyhow!("{} does not support custom fields", type_name));
            }
            custom.insert(key.clone(), encoded);
        } else {
            body.insert(api_key.to_string(), encoded);
        }
    }

    if !custom.is_empty() {
        body.insert("custom_fields".to_string(), Value::Object(custom));
    }

    Ok(Value::Object(body))
}

fn resolve_value_for_type(
    field_type: &alembic_core::FieldType,
    value: Value,
    resolved: &BTreeMap<Uid, u64>,
) -> Result<Value> {
    alembic_engine::resolve_value_for_type(field_type, value, resolved, |id| {
        Value::Number((*id).into())
    })
}

fn query_from_key(
    type_schema: &TypeSchema,
    key: &Key,
    resolved: &BTreeMap<Uid, u64>,
) -> Result<QueryBuilder> {
    let mut query = QueryBuilder::new();
    for (field, value) in query_filters_from_key(type_schema, key, resolved)? {
        query = query.filter(field, value);
    }
    Ok(query)
}

fn collect_tag_names(ops: &[Op], registry: &ObjectTypeRegistry) -> Result<BTreeSet<String>> {
    let mut tags = BTreeSet::new();
    for op in ops {
        let (type_name, desired) = match op {
            Op::Create {
                type_name, desired, ..
            } => (type_name, desired),
            Op::Update {
                type_name, desired, ..
            } => (type_name, desired),
            Op::Delete { .. } => continue,
        };
        if let Some(tag_value) = desired.attrs.get("tags") {
            let info = registry
                .info_for(type_name)
                .ok_or_else(|| anyhow!("unsupported type {}", type_name))?;
            if !supports_feature(&info.features, &["tags"]) {
                return Err(anyhow!("{} does not support tags", type_name));
            }
            for tag in tags_from_value(tag_value)? {
                tags.insert(tag);
            }
        }
    }
    Ok(tags)
}

async fn build_registry_for_schema(
    adapter: &NetBoxAdapter,
    schema: &Schema,
) -> Result<ObjectTypeRegistry> {
    let mut registry = adapter.client.fetch_object_types().await?;
    let mut missing = Vec::new();
    for type_name in schema.types.keys() {
        let type_name = TypeName::new(type_name);
        if !registry.contains_type(&type_name) {
            missing.push(type_name);
        }
    }
    if missing.is_empty() {
        return Ok(registry);
    }

    let custom_object_types = adapter.client.fetch_custom_object_types().await?;
    if custom_object_types.is_none() {
        let list = missing
            .iter()
            .map(|t| t.as_str())
            .collect::<Vec<_>>()
            .join(", ");
        return Err(anyhow!(
            "schema includes custom types but netbox custom objects are not available: {list}"
        ));
    }
    let custom_object_types = custom_object_types.unwrap_or_default();
    let mut custom_by_name: BTreeMap<String, CustomObjectType> = BTreeMap::new();
    for custom_type in custom_object_types {
        custom_by_name.insert(custom_type.name.clone(), custom_type);
    }

    for type_name in missing {
        let custom_name = custom_object_type_name(&type_name);
        let endpoint = custom_object_endpoint(&custom_name);
        if let Some(custom_type) = custom_by_name.get(&custom_name) {
            if let Some((app_label, model)) = custom_object_type_parts(custom_type) {
                registry.insert_custom_object_type(
                    type_name,
                    endpoint,
                    custom_object_features(),
                    app_label,
                    model,
                );
                continue;
            }
        }
        registry.insert_custom_object_type(
            type_name,
            endpoint,
            custom_object_features(),
            CUSTOM_OBJECT_APP_LABEL.to_string(),
            custom_name,
        );
    }

    Ok(registry)
}

struct CustomObjectFieldProvisioner<'a> {
    adapter: &'a NetBoxAdapter,
    registry: &'a ObjectTypeRegistry,
    custom_object_type_id: u64,
    existing_fields: &'a mut BTreeMap<String, u64>,
    created_object_fields: &'a mut Vec<String>,
    type_name: &'a TypeName,
}

impl<'a> CustomObjectFieldProvisioner<'a> {
    async fn ensure(
        &mut self,
        field_name: &str,
        field_schema: &FieldSchema,
        is_key: bool,
    ) -> Result<()> {
        if is_reserved_custom_object_field(field_name) {
            return Ok(());
        }
        if self.existing_fields.contains_key(field_name) {
            return Ok(());
        }
        validate_custom_object_field_name(field_name)?;
        let payload = custom_object_field_payload(
            self.registry,
            self.custom_object_type_id,
            field_name,
            field_schema,
            is_key,
        )?;
        let resource: Resource<Value> = self
            .adapter
            .client
            .resource("plugins/custom-objects/custom-object-type-fields/");
        match resource.create(&payload).await {
            Ok(created) => {
                if let Some(field_id) = custom_object_field_id(&created) {
                    self.existing_fields
                        .insert(field_name.to_string(), field_id);
                }
                self.created_object_fields
                    .push(format!("{}.{}", self.type_name, field_name));
            }
            Err(err) => {
                let Some(fields) = self
                    .adapter
                    .client
                    .fetch_custom_object_type_fields()
                    .await?
                else {
                    return Err(err.into());
                };
                if fields.iter().any(|field| {
                    field.custom_object_type == self.custom_object_type_id
                        && field.name == field_name
                }) {
                    tracing::warn!(
                        type_name = %self.type_name,
                        field = %field_name,
                        "custom object field already exists"
                    );
                    if let Some(existing) = fields.iter().find(|field| {
                        field.custom_object_type == self.custom_object_type_id
                            && field.name == field_name
                    }) {
                        self.existing_fields
                            .insert(field_name.to_string(), existing.id);
                    }
                } else {
                    return Err(err.into());
                }
            }
        }
        Ok(())
    }
}

async fn native_fields_for_type(
    adapter: &NetBoxAdapter,
    info: &super::registry::ObjectTypeInfo,
    type_schema: &TypeSchema,
) -> Result<BTreeSet<String>> {
    let mut native: BTreeSet<String> = type_schema.key.keys().cloned().collect();
    for field in [
        "name",
        "slug",
        "description",
        "status",
        "role",
        "type",
        "site",
        "tenant",
        "device",
        "tags",
        "custom_fields",
        "local_context_data",
        "created",
        "last_updated",
    ] {
        native.insert(field.to_string());
    }

    let resource: Resource<Value> = adapter.client.resource(info.endpoint.clone());
    let page = resource
        .list(Some(QueryBuilder::default().limit(1)))
        .await?;
    if let Some(Value::Object(map)) = page.results.into_iter().next() {
        for key in map.keys() {
            native.insert(key.clone());
        }
    }
    if info.type_name.as_str() == "dcim.interface" {
        native.insert("if_type".to_string());
    }

    Ok(native)
}

fn custom_object_type_name(type_name: &TypeName) -> String {
    let mut out = String::new();
    let mut last_underscore = false;
    for ch in type_name.as_str().chars() {
        let lower = ch.to_ascii_lowercase();
        if lower.is_ascii_alphanumeric() {
            out.push(lower);
            last_underscore = false;
        } else if !last_underscore {
            out.push('_');
            last_underscore = true;
        }
    }
    while out.ends_with('_') {
        out.pop();
    }
    out
}

fn custom_object_features() -> BTreeSet<String> {
    [CUSTOM_OBJECT_FEATURE.to_string(), "tags".to_string()]
        .into_iter()
        .collect()
}

fn custom_object_type_parts(custom_type: &CustomObjectType) -> Option<(String, String)> {
    if let Some(parts) = custom_type.object_type_parts() {
        return Some(parts);
    }
    custom_type
        .table_model_name
        .as_deref()
        .map(|name| (CUSTOM_OBJECT_APP_LABEL.to_string(), name.to_lowercase()))
}

fn alembic_custom_object_name(custom_type: &CustomObjectType) -> Option<String> {
    custom_type
        .description
        .as_deref()
        .and_then(|desc| desc.strip_prefix(ALEMBIC_CUSTOM_OBJECT_PREFIX))
        .map(|name| name.to_string())
}

fn custom_object_endpoint(custom_name: &str) -> String {
    format!("plugins/custom-objects/{custom_name}/")
}

fn custom_object_verbose_name_plural(type_name: &TypeName) -> String {
    let base = type_name
        .as_str()
        .split('.')
        .next_back()
        .unwrap_or_else(|| type_name.as_str());
    let label = title_case(base);
    if label.ends_with('s') {
        label
    } else {
        format!("{label}s")
    }
}

fn custom_object_field_id(value: &Value) -> Option<u64> {
    match value {
        Value::Object(map) => map.get("id").and_then(as_u64),
        _ => None,
    }
}

fn title_case(value: &str) -> String {
    value
        .split(|ch: char| !ch.is_ascii_alphanumeric())
        .filter(|segment| !segment.is_empty())
        .map(|segment| {
            let mut chars = segment.chars();
            let Some(first) = chars.next() else {
                return String::new();
            };
            let mut out = String::new();
            out.push(first.to_ascii_uppercase());
            out.push_str(&chars.as_str().to_ascii_lowercase());
            out
        })
        .collect::<Vec<_>>()
        .join(" ")
}

fn is_reserved_custom_object_field(name: &str) -> bool {
    matches!(
        name,
        "id" | "url" | "display" | "custom_object_type" | "created" | "last_updated" | "tags"
    )
}

fn validate_custom_object_field_name(name: &str) -> Result<()> {
    if name.is_empty()
        || !name
            .chars()
            .all(|ch| ch.is_ascii_alphanumeric() || ch == '_')
    {
        return Err(anyhow!(
            "invalid custom object field name '{}': only letters, digits, and underscores are allowed",
            name
        ));
    }
    Ok(())
}

fn custom_object_field_payload(
    registry: &ObjectTypeRegistry,
    custom_object_type_id: u64,
    field_name: &str,
    field_schema: &FieldSchema,
    is_key: bool,
) -> Result<Value> {
    let mut payload = Map::new();
    payload.insert(
        "custom_object_type".to_string(),
        Value::Number(custom_object_type_id.into()),
    );
    payload.insert("name".to_string(), Value::String(field_name.to_string()));
    payload.insert("label".to_string(), Value::String(title_case(field_name)));
    payload.insert(
        "type".to_string(),
        Value::String(custom_object_field_type(&field_schema.r#type).to_string()),
    );
    if is_key || field_schema.required {
        payload.insert("required".to_string(), Value::Bool(true));
    }
    if let Some(pattern) = &field_schema.pattern {
        payload.insert(
            "validation_regex".to_string(),
            Value::String(pattern.clone()),
        );
    }

    match &field_schema.r#type {
        FieldType::Ref { target } | FieldType::ListRef { target } => {
            let target_type = TypeName::new(target);
            if registry.contains_type(&target_type) {
                let info = registry
                    .info_for(&target_type)
                    .ok_or_else(|| anyhow!("invalid target type {}", target))?;
                payload.insert("app_label".to_string(), Value::String(info.app_label));
                payload.insert("model".to_string(), Value::String(info.model));
            } else {
                let custom_name = custom_object_type_name(&target_type);
                payload.insert(
                    "app_label".to_string(),
                    Value::String(CUSTOM_OBJECT_APP_LABEL.to_string()),
                );
                payload.insert("model".to_string(), Value::String(custom_name));
            }
        }
        _ => {}
    }

    Ok(Value::Object(payload))
}

fn custom_object_field_type(field_type: &FieldType) -> &'static str {
    match field_type {
        FieldType::Text => "longtext",
        FieldType::Int => "integer",
        FieldType::Float => "decimal",
        FieldType::Bool => "boolean",
        FieldType::Date => "date",
        FieldType::Datetime => "datetime",
        FieldType::Json | FieldType::List { .. } | FieldType::Map { .. } => "json",
        FieldType::Ref { .. } => "object",
        FieldType::ListRef { .. } => "multiobject",
        _ => "text",
    }
}

fn supports_feature(features: &BTreeSet<String>, candidates: &[&str]) -> bool {
    candidates.iter().any(|name| features.contains(*name))
}

fn is_missing_ref_error(err: &anyhow::Error) -> bool {
    err.downcast_ref::<AdapterApplyError>()
        .is_some_and(|e| matches!(e, AdapterApplyError::MissingRef { .. }))
}

fn is_404_error(err: &netbox::Error) -> bool {
    err.to_string().contains("status 404")
}

fn is_404_anyhow(err: &anyhow::Error) -> bool {
    err.downcast_ref::<netbox::Error>()
        .is_some_and(|e| matches!(e, netbox::Error::ApiError { status: 404, .. }))
}

fn is_conflict_error(err: &netbox::Error) -> bool {
    match err {
        netbox::Error::ApiError {
            status,
            message,
            body,
        } => {
            if !matches!(status, 400 | 409) {
                return false;
            }
            let message = message.to_lowercase();
            let body = body.to_lowercase();
            message.contains("already exists")
                || message.contains("unique")
                || body.contains("already exists")
                || body.contains("unique")
        }
        _ => false,
    }
}

fn describe_missing_refs(ops: &[Op], resolved: &BTreeMap<Uid, u64>) -> String {
    let mut missing = BTreeSet::new();
    for op in ops {
        if let Op::Create { desired, .. } | Op::Update { desired, .. } = op {
            for value in desired.attrs.values() {
                collect_missing_refs(value, resolved, &mut missing);
            }
        }
    }
    missing
        .into_iter()
        .map(|uid| uid.to_string())
        .collect::<Vec<_>>()
        .join(", ")
}

fn collect_missing_refs(value: &Value, resolved: &BTreeMap<Uid, u64>, missing: &mut BTreeSet<Uid>) {
    match value {
        Value::String(raw) => {
            if let Ok(uid) = Uid::parse_str(raw) {
                if !resolved.contains_key(&uid) {
                    missing.insert(uid);
                }
            }
        }
        Value::Array(items) => {
            for item in items {
                collect_missing_refs(item, resolved, missing);
            }
        }
        Value::Object(map) => {
            for value in map.values() {
                collect_missing_refs(value, resolved, missing);
            }
        }
        _ => {}
    }
}

#[cfg(test)]
mod tests {
    use super::is_conflict_error;

    #[test]
    fn conflict_error_detects_unique_message() {
        let err = netbox::Error::ApiError {
            status: 400,
            message: "slug: This field must be unique.".to_string(),
            body: String::new(),
        };
        assert!(is_conflict_error(&err));
    }

    #[test]
    fn conflict_error_rejects_other_status() {
        let err = netbox::Error::ApiError {
            status: 404,
            message: "Not found".to_string(),
            body: String::new(),
        };
        assert!(!is_conflict_error(&err));
    }
}

#[cfg(test)]
mod test_normalization {
    use super::*;
    use alembic_core::FieldSchema;
    use serde_json::json;

    #[test]
    fn test_normalize_value_netbox() {
        let registry = ObjectTypeRegistry::default();
        let mappings = super::super::state::StateMappings::default();
        let schema = Schema {
            types: BTreeMap::new(),
        };

        // Test summary object to integer ID normalization
        let summary = json!({
            "id": 5,
            "url": "http://localhost/api/dcim/sites/5/",
            "display": "FRA1"
        });
        let normalized = normalize_value(summary, None, &schema, &registry, &mappings);
        assert_eq!(normalized, json!(5));

        // Test value/label normalization
        let status = json!({
            "value": "active",
            "label": "Active"
        });
        let normalized = normalize_value(status, None, &schema, &registry, &mappings);
        assert_eq!(normalized, json!("active"));
    }

    #[test]
    fn test_normalize_attrs_netbox() {
        let registry = ObjectTypeRegistry::default();
        let mappings = super::super::state::StateMappings::default();
        let type_schema = TypeSchema {
            key: BTreeMap::new(),
            fields: BTreeMap::new(),
        };
        let schema = Schema {
            types: BTreeMap::new(),
        };
        let mut attrs = JsonMap::default();
        attrs.insert("type".to_string(), json!("1000base-t"));

        normalize_attrs(&mut attrs, &type_schema, &schema, &registry, &mappings);
        assert_eq!(attrs.get("if_type").unwrap(), &json!("1000base-t"));
    }

    #[test]
    fn test_uid_from_key_fields() {
        let registry = ObjectTypeRegistry::default();
        let mappings = super::super::state::StateMappings::default();

        // Build a schema with a type that has "name" as the key field
        let mut schema = Schema {
            types: BTreeMap::new(),
        };
        let mut type_schema = TypeSchema {
            key: BTreeMap::new(),
            fields: BTreeMap::new(),
        };
        type_schema.key.insert(
            "name".to_string(),
            FieldSchema {
                r#type: FieldType::String,
                required: true,
                nullable: false,
                description: None,
                format: None,
                pattern: None,
            },
        );
        schema.types.insert("dcim.device".to_string(), type_schema);

        // Nested object without URL but with key field
        let nested = serde_json::Map::from_iter([
            ("id".to_string(), json!(123)),
            ("name".to_string(), json!("router-01")),
        ]);

        let uid = uid_from_key_fields(&nested, "dcim.device", &schema, &registry, &mappings);
        assert!(uid.is_some());

        // The UID should be deterministic: same inputs = same output
        let uid2 = uid_from_key_fields(&nested, "dcim.device", &schema, &registry, &mappings);
        assert_eq!(uid, uid2);

        // Different key value should produce different UID
        let nested2 = serde_json::Map::from_iter([
            ("id".to_string(), json!(456)),
            ("name".to_string(), json!("router-02")),
        ]);
        let uid3 = uid_from_key_fields(&nested2, "dcim.device", &schema, &registry, &mappings);
        assert!(uid3.is_some());
        assert_ne!(uid, uid3);
    }

    #[test]
    fn test_build_request_body() {
        let mut fields = BTreeMap::new();
        fields.insert(
            "site".to_string(),
            FieldSchema {
                r#type: alembic_core::FieldType::Ref {
                    target: "dcim.site".to_string(),
                },
                required: true,
                nullable: false,
                description: None,
                format: None,
                pattern: None,
            },
        );
        let type_schema = TypeSchema {
            key: BTreeMap::new(),
            fields,
        };
        let mut attrs = JsonMap::default();
        let site_uid = Uid::from_u128(1);
        attrs.insert("site".to_string(), json!(site_uid.to_string()));

        let mut resolved = BTreeMap::new();
        resolved.insert(site_uid, 5);

        let body = build_request_body(
            &TypeName::new("dcim.device"),
            &type_schema,
            &attrs,
            &resolved,
            &BTreeSet::new(),
            &BTreeSet::new(),
        )
        .unwrap();
        assert_eq!(body.get("site").unwrap(), &json!(5));
    }

    #[test]
    fn test_resolve_value_for_type() {
        let resolved = BTreeMap::from([(Uid::from_u128(1), 5u64)]);

        // Ref
        let val = resolve_value_for_type(
            &alembic_core::FieldType::Ref {
                target: "t".to_string(),
            },
            json!(Uid::from_u128(1).to_string()),
            &resolved,
        )
        .unwrap();
        assert_eq!(val, json!(5));

        // ListRef
        let val = resolve_value_for_type(
            &alembic_core::FieldType::ListRef {
                target: "t".to_string(),
            },
            json!([Uid::from_u128(1).to_string()]),
            &resolved,
        )
        .unwrap();
        assert_eq!(val, json!([5]));
    }

    #[test]
    fn test_supports_feature() {
        let mut features = BTreeSet::new();
        features.insert("tags".to_string());
        assert!(supports_feature(&features, &["tags"]));
        assert!(!supports_feature(&features, &["custom-fields"]));
    }

    #[test]
    fn test_query_from_key() {
        let mut key_fields = BTreeMap::new();
        key_fields.insert(
            "name".to_string(),
            FieldSchema {
                r#type: alembic_core::FieldType::String,
                required: true,
                nullable: false,
                description: None,
                format: None,
                pattern: None,
            },
        );
        key_fields.insert(
            "site".to_string(),
            FieldSchema {
                r#type: alembic_core::FieldType::Ref {
                    target: "dcim.site".to_string(),
                },
                required: true,
                nullable: false,
                description: None,
                format: None,
                pattern: None,
            },
        );
        let type_schema = TypeSchema {
            key: key_fields,
            fields: BTreeMap::new(),
        };

        let site_uid = Uid::from_u128(1);
        let mut key_map = BTreeMap::new();
        key_map.insert("name".to_string(), json!("leaf01"));
        key_map.insert("site".to_string(), json!(site_uid.to_string()));
        let key = Key::from(key_map);

        let mut resolved = BTreeMap::new();
        resolved.insert(site_uid, 5u64);

        let query = query_from_key(&type_schema, &key, &resolved).unwrap();
        let json = serde_json::to_value(&query).unwrap();
        let pairs = json.as_array().unwrap();
        assert_eq!(pairs.len(), 2);
        assert!(pairs.iter().any(|p| p == &json!(["name", "leaf01"])));
        assert!(pairs.iter().any(|p| p == &json!(["site", "5"])));
    }
}