tnuctipun 0.2.0

The Tnuctipun of Ringworld — ancient, subversive, ingenious — or a type-safe MongoDB builder library
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
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
use bson;
use num_traits::Num;
use std::collections::HashMap;

use crate::expr::Expr;
use crate::field_witnesses::{FieldName, HasField};
use crate::path::Path;

pub struct UpdateBuilder<T> {
    pub prefix: Vec<String>,
    clauses: HashMap<UpdateOperation, Vec<(String, bson::Bson)>>,
    _marker: std::marker::PhantomData<T>,
}

impl<T> Default for UpdateBuilder<T> {
    fn default() -> Self {
        Self::new()
    }
}

impl<T> UpdateBuilder<T> {
    /// Creates a new `UpdateBuilder` instance.
    ///
    /// The builder starts with an empty prefix and no update clauses.
    /// This is the foundation for constructing MongoDB update documents
    /// using the fluent API pattern.
    ///
    /// # Returns
    ///
    /// A new `UpdateBuilder` instance ready for method chaining.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use tnuctipun::updates::UpdateBuilder;
    ///
    /// struct User {
    ///     pub name: String,
    ///     pub age: i32,
    /// }
    ///
    /// let builder = UpdateBuilder::<User>::new();
    /// ```
    pub fn new() -> Self {
        UpdateBuilder {
            prefix: Vec::new(),
            clauses: HashMap::new(),
            _marker: std::marker::PhantomData,
        }
    }

    /// Returns a fully qualified field path for the given field name marker type.
    ///
    /// This method constructs the complete dot-notation path for a field by combining
    /// any existing prefix (for nested document updates) with the field name.
    ///
    /// # Type Parameters
    ///
    /// * `F` - A field name marker type that implements `FieldName`
    ///
    /// # Returns
    ///
    /// A `String` containing the fully qualified field path.
    ///
    /// # Examples
    ///
    /// - Without prefix: `"field_name"`
    /// - With prefix `["parent"]`: `"parent.field_name"`
    /// - With nested prefix `["root", "child"]`: `"root.child.field_name"`
    fn field_path<F: FieldName>(&self) -> String {
        if self.prefix.is_empty() {
            F::field_name().to_string()
        } else {
            format!("{}.{}", self.prefix.join("."), F::field_name())
        }
    }

    /// Adds a new clause to the update document for the specified operation.
    ///
    /// This method organizes update clauses by operation type, allowing multiple
    /// fields to be updated with the same operation (e.g., multiple `$set` operations
    /// are combined into a single `$set` document).
    ///
    /// # Parameters
    ///
    /// * `op` - The update operation type (e.g., Set, Unset, Inc)
    /// * `path` - The field path to update
    /// * `clause` - The BSON value for the update
    fn push_clause(&mut self, op: UpdateOperation, path: String, clause: bson::Bson) {
        // If nothing exists for key `op` in clauses, create a new vector
        self.clauses.entry(op).or_default().push((path, clause));
    }

    /// Sets the value of a field in the document.
    ///
    /// This method corresponds to MongoDB's `$set` operator, which sets the value of a field.
    /// If the field does not exist, `$set` will add a new field with the specified value.
    /// If the field does exist, `$set` will replace the existing value.
    ///
    /// # Type Parameters
    ///
    /// * `F` - A field name marker type that implements `FieldName`
    /// * `V` - A value type that can be converted into `bson::Bson`
    ///
    /// # Parameters
    ///
    /// * `value` - The value to set for the field
    ///
    /// # Returns
    ///
    /// Returns `&mut Self` to allow method chaining.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use tnuctipun::{FieldName, FieldWitnesses, updates::empty};
    /// use serde::{Serialize, Deserialize};
    /// use bson::doc;
    ///
    /// #[derive(FieldWitnesses, Serialize, Deserialize)]
    /// struct User {
    ///     pub name: String,
    ///     pub age: i32,
    /// }
    ///
    /// let update_doc = empty::<User>()
    ///     .set::<user_fields::Name, _>("Jane Doe".to_string())
    ///     .set::<user_fields::Age, _>(25)
    ///     .build();
    /// // Results in: { "$set": { "name": "Jane Doe", "age": 25 } }
    /// ```
    pub fn set<F: FieldName, V: Into<bson::Bson>>(&mut self, value: V) -> &mut Self
    where
        T: HasField<F>,
    {
        let path = self.field_path::<F>();

        self.push_clause(UpdateOperation::Set, path, value.into());

        self
    }

    /// Sets the value of a field using a typed MongoDB expression.
    ///
    /// While [`set`](Self::set) can also accept an expression (because `Expr<T, V>`
    /// converts into `bson::Bson`), this method is often safer and clearer at call sites:
    /// it explicitly requires `Expr<T, V>`, ensuring the expression root type matches
    /// this `UpdateBuilder<T>`.
    pub fn set_expr<F: FieldName, V>(&mut self, expr: Expr<T, V>) -> &mut Self
    where
        T: HasField<F>,
    {
        self.set::<F, _>(expr)
    }

    /// Removes a field from the document.
    ///
    /// This method corresponds to MongoDB's `$unset` operator, which deletes a particular field.
    /// The operation removes the field completely from the document. If the field does not exist,
    /// the operation has no effect but is not considered an error.
    ///
    /// # Type Parameters
    ///
    /// * `F` - A field name marker type that implements `FieldName`
    ///
    /// # Returns
    ///
    /// Returns `&mut Self` to allow method chaining.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use tnuctipun::{FieldName, FieldWitnesses, updates::empty};
    /// use serde::{Serialize, Deserialize};
    ///
    /// #[derive(FieldWitnesses, Serialize, Deserialize)]
    /// struct TempData {
    ///     pub temp_data: String,
    ///     pub obsolete_field: i32,
    /// }
    ///
    /// let update_doc = empty::<TempData>()
    ///     .unset::<tempdata_fields::TempData>()
    ///     .unset::<tempdata_fields::ObsoleteField>()
    ///     .build();
    /// // Results in: { "$unset": { "temp_data": "", "obsolete_field": "" } }
    /// ```
    pub fn unset<F: FieldName>(&mut self) -> &mut Self
    where
        T: HasField<F>,
    {
        let path = self.field_path::<F>();

        self.push_clause(UpdateOperation::Unset, path, bson::Bson::Null);

        self
    }

    /// Increments the value of a numeric field by the specified amount.
    ///
    /// This method corresponds to MongoDB's `$inc` operator, which increments a field by a specified value.
    /// The field must contain a numeric value (integer, long, double, or decimal). If the field does not exist,
    /// it is created with the increment value. If the field exists but is not numeric, the operation will fail.
    ///
    /// # Type Parameters
    ///
    /// * `F` - A field name marker type that implements `FieldName`
    /// * `N` - A numeric type that implements `Num` and can be converted into `bson::Bson`
    ///
    /// # Parameters
    ///
    /// * `value` - The amount to increment the field by (can be negative for decrementing)
    ///
    /// # Returns
    ///
    /// Returns `&mut Self` to allow method chaining.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use tnuctipun::{FieldName, FieldWitnesses, updates::empty};
    /// use serde::{Serialize, Deserialize};
    ///
    /// #[derive(FieldWitnesses, Serialize, Deserialize)]
    /// struct Stats {
    ///     pub count: i32,
    ///     pub score: i32,
    /// }
    ///
    /// let update_doc = empty::<Stats>()
    ///     .inc::<stats_fields::Count, _>(1)
    ///     .inc::<stats_fields::Score, _>(-5)
    ///     .build();
    /// // Results in: { "$inc": { "count": 1, "score": -5 } }
    /// ```
    pub fn inc<F: FieldName, N: Num + Into<bson::Bson>>(&mut self, value: N) -> &mut Self
    where
        T: HasField<F>,
    {
        let path = self.field_path::<F>();

        self.push_clause(UpdateOperation::Inc, path, value.into());

        self
    }

    /// Updates a field only if the specified value is greater than the existing field value.
    ///
    /// This method corresponds to MongoDB's `$max` operator, which only updates the field if the specified value
    /// is greater than the existing field value. If the field does not exist, it sets the field to the specified value.
    /// The field must contain a value that can be compared to the specified value, typically numeric types or dates.
    ///
    /// # Type Parameters
    ///
    /// * `F` - A field name marker type that implements `FieldName`
    /// * `N` - A numeric type that implements `Num` and can be converted into `bson::Bson`
    ///
    /// # Parameters
    ///
    /// * `value` - The value to compare against the existing field value
    ///
    /// # Returns
    ///
    /// Returns `&mut Self` to allow method chaining.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use tnuctipun::{FieldName, FieldWitnesses, updates::empty};
    /// use serde::{Serialize, Deserialize};
    ///
    /// #[derive(FieldWitnesses, Serialize, Deserialize)]
    /// struct HighScore {
    ///     pub best_score: i32,
    ///     pub max_level: i32,
    /// }
    ///
    /// let update_doc = empty::<HighScore>()
    ///     .max::<highscore_fields::BestScore, _>(1500)     // Only update if 1500 > current score
    ///     .max::<highscore_fields::MaxLevel, _>(10)        // Only update if 10 > current max level
    ///     .build();
    /// // Results in: { "$max": { "best_score": 1500, "max_level": 10 } }
    /// ```
    pub fn max<F: FieldName, N: Num + Into<bson::Bson>>(&mut self, value: N) -> &mut Self
    where
        T: HasField<F>,
    {
        let path = self.field_path::<F>();

        self.push_clause(UpdateOperation::Max, path, value.into());

        self
    }

    /// Updates a field only if the specified value is less than the existing field value.
    ///
    /// This method corresponds to MongoDB's `$min` operator, which only updates the field if the specified value
    /// is less than the existing field value. If the field does not exist, it sets the field to the specified value.
    /// The field must contain a value that can be compared to the specified value, typically numeric types or dates.
    ///
    /// # Type Parameters
    ///
    /// * `F` - A field name marker type that implements `FieldName`
    /// * `N` - A numeric type that implements `Num` and can be converted into `bson::Bson`
    ///
    /// # Parameters
    ///
    /// * `value` - The value to compare against the existing field value
    ///
    /// # Returns
    ///
    /// Returns `&mut Self` to allow method chaining.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use tnuctipun::{FieldName, FieldWitnesses, updates::empty};
    /// use serde::{Serialize, Deserialize};
    ///
    /// #[derive(FieldWitnesses, Serialize, Deserialize)]
    /// struct Pricing {
    ///     pub lowest_price: f64,
    ///     pub min_quantity: i32,
    /// }
    ///
    /// let update_doc = empty::<Pricing>()
    ///     .min::<pricing_fields::LowestPrice, _>(29.99)    // Only update if 29.99 < current price
    ///     .min::<pricing_fields::MinQuantity, _>(5)        // Only update if 5 < current min quantity
    ///     .build();
    /// // Results in: { "$min": { "lowest_price": 29.99, "min_quantity": 5 } }
    /// ```
    pub fn min<F: FieldName, N: Num + Into<bson::Bson>>(&mut self, value: N) -> &mut Self
    where
        T: HasField<F>,
    {
        let path = self.field_path::<F>();

        self.push_clause(UpdateOperation::Min, path, value.into());

        self
    }

    /// Multiplies the value of a numeric field by the specified amount.
    ///
    /// This method corresponds to MongoDB's `$mul` operator, which multiplies the value of a field by a number.
    /// The field must contain a numeric value (integer, long, double, or decimal). If the field does not exist,
    /// it is created with a value of zero (0 * multiplier = 0). If the field exists but is not numeric, the operation will fail.
    ///
    /// # Type Parameters
    ///
    /// * `F` - A field name marker type that implements `FieldName`
    /// * `N` - A numeric type that implements `Num` and can be converted into `bson::Bson`
    ///
    /// # Parameters
    ///
    /// * `value` - The multiplier to apply to the field value
    ///
    /// # Returns
    ///
    /// Returns `&mut Self` to allow method chaining.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use tnuctipun::{FieldName, FieldWitnesses, updates::empty};
    /// use serde::{Serialize, Deserialize};
    ///
    /// #[derive(FieldWitnesses, Serialize, Deserialize)]
    /// struct Product {
    ///     pub price: f64,
    ///     pub quantity: i32,
    /// }
    ///
    /// let update_doc = empty::<Product>()
    ///     .mul::<product_fields::Price, _>(1.1)      // Increase price by 10%
    ///     .mul::<product_fields::Quantity, _>(2)     // Double the quantity
    ///     .build();
    /// // Results in: { "$mul": { "price": 1.1, "quantity": 2 } }
    /// ```
    pub fn mul<F: FieldName, N: Num + Into<bson::Bson>>(&mut self, value: N) -> &mut Self
    where
        T: HasField<F>,
    {
        let path = self.field_path::<F>();

        self.push_clause(UpdateOperation::Mul, path, value.into());

        self
    }

    /// Renames a field in the document.
    ///
    /// This method corresponds to MongoDB's `$rename` operator, which renames a field.
    /// The new field name must differ from the existing field name. If the target field name
    /// already exists, its value will be overwritten. If the source field does not exist,
    /// the operation has no effect.
    ///
    /// # Type Parameters
    ///
    /// * `F` - A field name marker type that implements `FieldName`
    ///
    /// # Parameters
    ///
    /// * `new_name` - The new name for the field
    ///
    /// # Returns
    ///
    /// Returns `&mut Self` to allow method chaining.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use tnuctipun::{FieldName, FieldWitnesses, updates::empty};
    /// use serde::{Serialize, Deserialize};
    ///
    /// #[derive(FieldWitnesses, Serialize, Deserialize)]
    /// struct Document {
    ///     pub old_name: String,
    ///     pub legacy_field: String,
    /// }
    ///
    /// let update_doc = empty::<Document>()
    ///     .rename::<document_fields::OldName>("new_name")
    ///     .rename::<document_fields::LegacyField>("modern_field")
    ///     .build();
    /// // Results in: { "$rename": { "old_name": "new_name", "legacy_field": "modern_field" } }
    /// ```
    pub fn rename<F: FieldName>(&mut self, new_name: &str) -> &mut Self
    where
        T: HasField<F>,
    {
        let path = self.field_path::<F>();

        self.push_clause(
            UpdateOperation::Rename,
            path,
            bson::Bson::String(new_name.to_string()),
        );

        self
    }

    /// Sets the value of a field to the current date.
    ///
    /// This method corresponds to MongoDB's `$currentDate` operator, which sets the value of a field
    /// to the current date, either as a BSON Date or a BSON Timestamp. The default type is Date.
    /// This is useful for tracking when documents were last modified.
    ///
    /// # Type Parameters
    ///
    /// * `F` - A field name marker type that implements `FieldName`
    ///
    /// # Parameters
    ///
    /// * `date_type` - The type of date value to set (Date or Timestamp)
    ///
    /// # Returns
    ///
    /// Returns `&mut Self` to allow method chaining.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use tnuctipun::{FieldName, FieldWitnesses, updates::{empty, CurrentDateType}};
    /// use serde::{Serialize, Deserialize};
    ///
    /// #[derive(FieldWitnesses, Serialize, Deserialize)]
    /// struct Timestamps {
    ///     pub last_modified: String, // In practice, this would be a proper date type
    ///     pub updated_at: String,    // In practice, this would be a proper date type
    /// }
    ///
    /// let update_doc = empty::<Timestamps>()
    ///     .current_date::<timestamps_fields::LastModified>(CurrentDateType::Date)
    ///     .current_date::<timestamps_fields::UpdatedAt>(CurrentDateType::Timestamp)
    ///     .build();
    /// // Results in: { "$currentDate": { "last_modified": "date", "updated_at": "timestamp" } }
    /// ```
    pub fn current_date<F: FieldName>(&mut self, date_type: CurrentDateType) -> &mut Self
    where
        T: HasField<F>,
    {
        let path = self.field_path::<F>();

        self.push_clause(
            UpdateOperation::CurrentDate,
            path,
            bson::Bson::String(date_type.to_string()),
        );

        self
    }

    /// Adds a value to an array field only if it does not already exist.
    ///
    /// This method corresponds to MongoDB's `$addToSet` operator, which adds a value to an array
    /// unless the value is already present, in which case it does nothing to that array.
    /// This ensures array uniqueness without duplicates. If the field is not an array,
    /// the operation will fail. If the field does not exist, it creates a new array with the value.
    ///
    /// # Type Parameters
    ///
    /// * `F` - A field name marker type that implements `FieldName`
    /// * `V` - A value type that can be converted into `bson::Bson`
    ///
    /// # Parameters
    ///
    /// * `value` - The value to add to the array if it's not already present
    ///
    /// # Returns
    ///
    /// Returns `&mut Self` to allow method chaining.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use tnuctipun::{FieldName, FieldWitnesses, updates::empty};
    /// use serde::{Serialize, Deserialize};
    /// use bson::doc;
    ///
    /// #[derive(FieldWitnesses, Serialize, Deserialize)]
    /// struct Article {
    ///     pub tags: Vec<String>,
    ///     pub categories: Vec<String>,
    /// }
    ///
    /// let update_doc = empty::<Article>()
    ///     .add_to_set::<article_fields::Tags, _>("rust".to_string())
    ///     .add_to_set::<article_fields::Categories, _>("programming".to_string())
    ///     .build();
    /// // Results in: { "$addToSet": { "tags": "rust", "categories": "programming" } }
    /// ```
    pub fn add_to_set<F: FieldName, V: Into<bson::Bson>>(&mut self, value: V) -> &mut Self
    where
        T: HasField<F>,
        T::Value: IntoIterator<Item = V>,
    {
        let path = self.field_path::<F>();

        self.push_clause(UpdateOperation::AddToSet, path, value.into());

        self
    }

    /// Adds multiple values to an array field only if they do not already exist.
    ///
    /// This method corresponds to MongoDB's `$addToSet` operator with the `$each` modifier,
    /// which adds multiple values to an array unless the values are already present.
    /// This ensures array uniqueness without duplicates for all provided values.
    /// If the field is not an array, the operation will fail. If the field does not exist,
    /// it creates a new array with the unique values.
    ///
    /// # Type Parameters
    ///
    /// * `F` - A field name marker type that implements `FieldName`
    /// * `I` - An iterable type that yields items of type `V`
    /// * `V` - A value type that can be converted into `bson::Bson`
    ///
    /// # Parameters
    ///
    /// * `values` - An iterable collection of values to add to the array if they're not already present
    ///
    /// # Returns
    ///
    /// Returns `&mut Self` to allow method chaining.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use tnuctipun::{FieldName, FieldWitnesses, updates::empty};
    /// use serde::{Serialize, Deserialize};
    /// use bson::doc;
    ///
    /// #[derive(FieldWitnesses, Serialize, Deserialize)]
    /// struct Article {
    ///     pub tags: Vec<String>,
    ///     pub categories: Vec<String>,
    /// }
    ///
    /// let new_tags = vec!["rust".to_string(), "mongodb".to_string(), "database".to_string()];
    /// let new_categories = vec!["programming".to_string(), "tutorial".to_string()];
    ///
    /// let update_doc = empty::<Article>()
    ///     .add_to_set_each::<article_fields::Tags, _, _>(new_tags)
    ///     .add_to_set_each::<article_fields::Categories, _, _>(new_categories)
    ///     .build();
    /// // Results in: {
    /// //   "$addToSet": {
    /// //     "tags": { "$each": ["rust", "mongodb", "database"] },
    /// //     "categories": { "$each": ["programming", "tutorial"] }
    /// //   }
    /// // }
    /// ```
    ///
    /// # Difference from `add_to_set`
    ///
    /// Unlike `add_to_set` which adds a single value, `add_to_set_each` can efficiently
    /// add multiple values in a single operation:
    ///
    /// ```rust
    /// use tnuctipun::{FieldName, FieldWitnesses, updates::empty};
    /// use serde::{Serialize, Deserialize};
    ///
    /// #[derive(FieldWitnesses, Serialize, Deserialize)]
    /// struct Collection {
    ///     pub items: Vec<String>,
    /// }
    ///
    /// // Single value addition
    /// let single_update = empty::<Collection>()
    ///     .add_to_set::<collection_fields::Items, _>("item1".to_string())
    ///     .build();
    /// // Results in: { "$addToSet": { "items": "item1" } }
    ///
    /// // Multiple value addition
    /// let multiple_update = empty::<Collection>()
    ///     .add_to_set_each::<collection_fields::Items, _, _>(vec!["item1".to_string(), "item2".to_string()])
    ///     .build();
    /// // Results in: { "$addToSet": { "items": { "$each": ["item1", "item2"] } } }
    /// ```
    pub fn add_to_set_each<F: FieldName, I: IntoIterator<Item = V>, V: Into<bson::Bson>>(
        &mut self,
        values: I,
    ) -> &mut Self
    where
        T: HasField<F>,
        T::Value: IntoIterator<Item = V>,
    {
        let path = self.field_path::<F>();
        let values_vec: Vec<bson::Bson> = values.into_iter().map(|v| v.into()).collect();

        self.push_clause(
            UpdateOperation::AddToSet,
            path,
            bson::doc! { "$each": values_vec }.into(),
        );

        self
    }

    /// Removes the first or last element from an array field.
    ///
    /// This method corresponds to MongoDB's `$pop` operator, which removes either the first
    /// or the last element from an array. The operation fails if the field is not an array.
    /// If the array is empty, the operation has no effect.
    ///
    /// # Type Parameters
    ///
    /// * `F` - A field name marker type that implements `FieldName`
    ///
    /// # Parameters
    ///
    /// * `strategy` - The strategy for which element to remove (`PopStrategy::First` or `PopStrategy::Last`)
    ///
    /// # Returns
    ///
    /// Returns `&mut Self` to allow method chaining.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use tnuctipun::{FieldWitnesses, updates::{empty, PopStrategy}};
    /// use serde::{Serialize, Deserialize};
    ///
    /// #[derive(FieldWitnesses, Serialize, Deserialize)]
    /// struct Collections {
    ///     pub queue: Vec<String>,
    ///     pub stack: Vec<String>,
    /// }
    ///
    /// let update_doc = empty::<Collections>()
    ///     .pop::<collections_fields::Queue>(PopStrategy::First)    // Remove first element (FIFO)
    ///     .pop::<collections_fields::Stack>(PopStrategy::Last)     // Remove last element (LIFO)
    ///     .build();
    /// // Results in: { "$pop": { "queue": -1, "stack": 1 } }
    /// ```
    pub fn pop<F: FieldName>(&mut self, strategy: PopStrategy) -> &mut Self
    where
        T: HasField<F>,
        T::Value: IntoIterator,
    {
        let path = self.field_path::<F>();

        self.push_clause(UpdateOperation::Pop, path, strategy.into());

        self
    }

    /// Removes array elements that match a conditional BSON expression.
    ///
    /// This method corresponds to MongoDB's `$pull` operator with a conditional expression,
    /// which removes from an existing array all instances of values that match the specified condition.
    /// This is useful for complex matching scenarios where you need to remove elements based on
    /// embedded document fields or complex criteria.
    ///
    /// # Type Parameters
    ///
    /// * `F` - A field name marker type that implements `FieldName`
    ///
    /// # Parameters
    ///
    /// * `cond` - A BSON expression that defines the condition for elements to be removed
    ///
    /// # Returns
    ///
    /// Returns `&mut Self` to allow method chaining.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use tnuctipun::{FieldName, FieldWitnesses, updates::empty};
    /// use serde::{Serialize, Deserialize};
    /// use bson::{doc, Bson};
    ///
    /// #[derive(FieldWitnesses, Serialize, Deserialize)]
    /// struct Inventory {
    ///     pub items: Vec<bson::Document>,
    /// }
    ///
    /// // Remove all items where quantity is less than 5
    /// let condition = doc! { "quantity": { "$lt": 5 } };
    ///
    /// let update_doc = empty::<Inventory>()
    ///     .pull_expr::<inventory_fields::Items>(Bson::Document(condition))
    ///     .build();
    /// // Results in: { "$pull": { "items": { "quantity": { "$lt": 5 } } } }
    /// ```
    pub fn pull_expr<F: FieldName>(&mut self, cond: bson::Bson) -> &mut Self
    where
        T: HasField<F>,
        T::Value: IntoIterator,
    {
        let path = self.field_path::<F>();

        self.push_clause(UpdateOperation::Pull, path, cond);

        self
    }

    /// Removes all array elements that match a specific value.
    ///
    /// This method corresponds to MongoDB's `$pull` operator with a simple value match,
    /// which removes from an existing array all instances of the specified value.
    /// This is the simpler version of `pull_expr` for exact value matching.
    ///
    /// # Type Parameters
    ///
    /// * `F` - A field name marker type that implements `FieldName`
    /// * `V` - A value type that can be converted into `bson::Bson`
    ///
    /// # Parameters
    ///
    /// * `value` - The exact value to remove from the array
    ///
    /// # Returns
    ///
    /// Returns `&mut Self` to allow method chaining.
    ///
    /// Returns `&mut Self` to allow method chaining.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use tnuctipun::{FieldName, FieldWitnesses, updates::empty};
    /// use serde::{Serialize, Deserialize};
    ///
    /// #[derive(FieldWitnesses, Serialize, Deserialize)]
    /// struct Content {
    ///     pub tags: Vec<String>,
    ///     pub scores: Vec<i32>,
    /// }
    ///
    /// let update_doc = empty::<Content>()
    ///     .pull::<content_fields::Tags, _>("deprecated".to_string())
    ///     .pull::<content_fields::Scores, _>(0)
    ///     .build();
    /// // Results in: { "$pull": { "tags": "deprecated", "scores": 0 } }
    /// ```
    pub fn pull<F: FieldName, V: Into<bson::Bson>>(&mut self, value: V) -> &mut Self
    where
        T: HasField<F>,
        T::Value: IntoIterator<Item = V>,
    {
        let path = self.field_path::<F>();

        self.push_clause(UpdateOperation::Pull, path, value.into());

        self
    }

    /// Removes all instances of specified values from an array.
    ///
    /// This method corresponds to MongoDB's `$pullAll` operator, which removes all instances
    /// of the specified values from an existing array. Unlike `$pull`, which removes elements
    /// by specifying a condition, `$pullAll` removes elements that match any of the listed values.
    /// The field must be an array, or the operation will fail.
    ///
    /// # Type Parameters
    ///
    /// * `F` - A field name marker type that implements `FieldName`
    /// * `I` - An iterable type that can be converted into `bson::Bson`
    ///
    /// # Parameters
    ///
    /// * `values` - An iterable collection of values to remove from the array
    ///
    /// # Returns
    ///
    /// Returns `&mut Self` to allow method chaining.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use tnuctipun::{FieldName, FieldWitnesses, updates::empty};
    /// use serde::{Serialize, Deserialize};
    /// use bson::Bson;
    ///
    /// #[derive(FieldWitnesses, Serialize, Deserialize)]
    /// struct Lists {
    ///     pub tags: Vec<String>,
    ///     pub numbers: Vec<i32>,
    /// }
    ///
    /// let tags_to_remove = vec!["old".to_string(), "deprecated".to_string(), "unused".to_string()];
    /// let numbers_to_remove = vec![1i32, 3i32, 5i32, 7i32];
    ///
    /// empty::<Lists>()
    ///     .pull_all::<lists_fields::Tags, _>(tags_to_remove)
    ///     .pull_all::<lists_fields::Numbers, _>(numbers_to_remove)
    ///     .build();
    /// // Results in: { "$pullAll": { "tags": ["old", "deprecated", "unused"], "numbers": [1, 3, 5, 7] } }
    /// ```
    pub fn pull_all<F: FieldName, I>(&mut self, values: I) -> &mut Self
    where
        T: HasField<F>,
        I: Into<bson::Bson> + IntoIterator,
        T::Value: IntoIterator<Item = I::Item>,
    {
        let path = self.field_path::<F>();

        self.push_clause(UpdateOperation::PullAll, path, values.into());

        self
    }

    /// Appends a value to an array field.
    ///
    /// This method corresponds to MongoDB's `$push` operator, which appends a specified value
    /// to an array. If the field is absent, it creates a new array field with the value as its element.
    /// If the field exists but is not an array, the operation will fail. Unlike `$addToSet`,
    /// `$push` allows duplicate values to be added to the array.
    ///
    /// # Type Parameters
    ///
    /// * `F` - A field name marker type that implements `FieldName`
    /// * `V` - A value type that can be converted into `bson::Bson`
    ///
    /// # Parameters
    ///
    /// * `value` - The value to append to the array
    ///
    /// # Returns
    ///
    /// Returns `&mut Self` to allow method chaining.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use tnuctipun::{FieldName, FieldWitnesses, updates::empty};
    /// use serde::{Serialize, Deserialize};
    ///
    /// #[derive(FieldWitnesses, Serialize, Deserialize)]
    /// struct Logging {
    ///     pub logs: Vec<String>,
    ///     pub history: Vec<String>,
    /// }
    ///
    /// let update_doc = empty::<Logging>()
    ///     .push::<logging_fields::Logs, _>("User logged in".to_string())
    ///     .push::<logging_fields::History, _>("Action performed".to_string())
    ///     .build();
    /// // Results in: { "$push": { "logs": "User logged in", "history": "Action performed" } }
    /// ```
    pub fn push<F: FieldName, V: Into<bson::Bson>>(&mut self, value: V) -> &mut Self
    where
        T: HasField<F>,
        T::Value: IntoIterator<Item = V>,
    {
        let path = self.field_path::<F>();

        self.push_clause(UpdateOperation::Push, path, value.into());

        self
    }

    /// Appends multiple values to an array field with advanced options.
    ///
    /// This method corresponds to MongoDB's `$push` operator with the `$each` modifier,
    /// along with optional modifiers like `$slice`, `$sort`, and `$position`. It provides
    /// more control over how values are added to arrays compared to the basic `push` method.
    ///
    /// The method accepts a `PushEach` clause that can be configured with:
    /// - `$each`: The array of values to append (always included)
    /// - `$slice`: Limits the array to a specified number of elements
    /// - `$sort`: Sorts the array elements after adding new values
    /// - `$position`: Specifies where in the array to insert the new elements
    ///
    /// # Type Parameters
    ///
    /// * `F` - A field name marker type that implements `FieldName`
    /// * `I` - An iterable type that yields items of type `V`
    /// * `V` - A value type that can be converted into `bson::Bson`
    /// * `Clause` - A type that can be converted into `PushEach<I, V>`
    ///
    /// # Parameters
    ///
    /// * `clause` - A `PushEach` clause or any type that converts to it, containing the values and options
    ///
    /// # Returns
    ///
    /// Returns `&mut Self` to allow method chaining.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use tnuctipun::{FieldName, FieldWitnesses, updates::empty};
    /// use serde::{Serialize, Deserialize};
    ///
    /// #[derive(FieldWitnesses, Serialize, Deserialize)]
    /// struct GameData {
    ///     pub scores: Vec<i32>,
    ///     pub tags: Vec<String>,
    /// }
    ///
    /// // Basic usage - just append values
    /// let values = vec![100, 200, 300];
    /// let update_doc = empty::<GameData>()
    ///     .push_each::<gamedata_fields::Scores, _, _, _>(values)
    ///     .build();
    /// // Results in: { "$push": { "scores": { "$each": [100, 200, 300] } } }
    /// ```
    ///
    /// # Advanced Usage with PushEach Options
    ///
    /// For more advanced scenarios, you can create a `PushEach` instance with specific options:
    ///
    /// ```rust
    /// use tnuctipun::updates::{PushEach, PushEachSlice, PushEachSort};
    ///
    /// // This would be used with appropriate field setup
    /// // let push_clause = PushEach::from(vec![1, 2, 3])
    /// //     .with_slice(PushEachSlice::PushFirstSlice(5))
    /// //     .with_sort(PushEachSort::PushSortDescending);
    /// ```
    ///
    /// # Difference from `push`
    ///
    /// Unlike `push` which adds a single value, `push_each` efficiently adds multiple
    /// values in a single operation and supports advanced array manipulation options.
    pub fn push_each<
        F: FieldName,
        I: IntoIterator<Item = V>,
        V: Into<bson::Bson>,
        Clause: Into<PushEach<I, V>>,
    >(
        &mut self,
        clause: Clause,
    ) -> &mut Self
    where
        T: HasField<F>,
        T::Value: IntoIterator<Item = V>,
    {
        let path = self.field_path::<F>();
        let c: PushEach<I, V> = clause.into();

        self.push_clause(UpdateOperation::Push, path, c.into());

        self
    }

    /// Conditionally applies update operations based on an optional value.
    ///
    /// This method provides a convenient way to conditionally add update operations
    /// to the builder when dealing with optional values. If the provided `Option<A>`
    /// contains a value (`Some(A)`), the closure is executed with the unwrapped value
    /// and the current builder. If the option is `None`, the operation is skipped
    /// and the builder remains unchanged.
    ///
    /// This is particularly useful when building update documents dynamically where
    /// some fields should only be updated if certain values are provided, avoiding
    /// the need for explicit `if let Some(value) = option { ... }` patterns.
    ///
    /// # Type Parameters
    ///
    /// * `A` - The type of the value contained in the `Option`
    /// * `F` - The closure type that takes a mutable reference to the builder and the unwrapped value
    ///
    /// # Parameters
    ///
    /// * `value` - An `Option<A>` that may contain a value to use in the update operation
    /// * `f` - A closure that takes `&mut Self` and `A` and returns `&mut Self`, defining the update operations to apply
    ///
    /// # Returns
    ///
    /// Returns `&mut Self` to allow method chaining, regardless of whether the option contained a value.
    ///
    /// # Examples
    ///
    /// ## Basic Usage
    ///
    /// ```rust
    /// use tnuctipun::{FieldWitnesses, updates::empty};
    /// use serde::{Serialize, Deserialize};
    ///
    /// #[derive(FieldWitnesses, Serialize, Deserialize)]
    /// struct User {
    ///     pub name: String,
    ///     pub age: i32,
    ///     pub email: Option<String>,
    /// }
    ///
    /// let maybe_email = Some("user@example.com".to_string());
    /// let no_email: Option<String> = None;
    ///
    /// // When the option contains a value, the update is applied
    /// let update_with_email = empty::<User>()
    ///     .set::<user_fields::Name, _>("John Doe".to_string())
    ///     .if_some(maybe_email, |builder, email| {
    ///         builder.set::<user_fields::Email, _>(email)
    ///     })
    ///     .build();
    /// // Results in: { "$set": { "name": "John Doe", "email": "user@example.com" } }
    ///
    /// // When the option is None, the update is skipped
    /// let update_without_email = empty::<User>()
    ///     .set::<user_fields::Name, _>("Jane Doe".to_string())
    ///     .if_some(no_email, |builder, email| {
    ///         builder.set::<user_fields::Email, _>(email)
    ///     })
    ///     .build();
    /// // Results in: { "$set": { "name": "Jane Doe" } }
    /// ```
    ///
    /// ## Multiple Conditional Operations
    ///
    /// ```rust
    /// use tnuctipun::{FieldWitnesses, updates::empty};
    /// use serde::{Serialize, Deserialize};
    ///
    /// #[derive(FieldWitnesses, Serialize, Deserialize)]
    /// struct Profile {
    ///     pub name: String,
    ///     pub bio: Option<String>,
    ///     pub website: Option<String>,
    ///     pub score: i32,
    /// }
    ///
    /// let maybe_bio = Some("Software developer".to_string());
    /// let maybe_website: Option<String> = None;
    /// let maybe_score_increase = Some(10);
    ///
    /// let update_doc = empty::<Profile>()
    ///     .set::<profile_fields::Name, _>("Alice".to_string())
    ///     .if_some(maybe_bio, |builder, bio| {
    ///         builder.set::<profile_fields::Bio, _>(bio)
    ///     })
    ///     .if_some(maybe_website, |builder, website| {
    ///         builder.set::<profile_fields::Website, _>(website)
    ///     })
    ///     .if_some(maybe_score_increase, |builder, increase| {
    ///         builder.inc::<profile_fields::Score, _>(increase)
    ///     })
    ///     .build();
    /// // Results in: {
    /// //   "$set": { "name": "Alice", "bio": "Software developer" },
    /// //   "$inc": { "score": 10 }
    /// // }
    /// ```
    ///
    /// ## Complex Update Operations
    ///
    /// ```rust
    /// use tnuctipun::{FieldWitnesses, updates::empty};
    /// use serde::{Serialize, Deserialize};
    ///
    /// #[derive(FieldWitnesses, Serialize, Deserialize)]
    /// struct Product {
    ///     pub name: String,
    ///     pub tags: Vec<String>,
    ///     pub discount: Option<f64>,
    /// }
    ///
    /// let maybe_discount = Some(0.15); // 15% discount
    ///
    /// let update_doc = empty::<Product>()
    ///     .set::<product_fields::Name, _>("Special Item".to_string())
    ///     .push::<product_fields::Tags, _>("on-sale".to_string())
    ///     .if_some(maybe_discount, |builder, discount| {
    ///         builder
    ///             .set::<product_fields::Discount, _>(discount)
    ///             .push::<product_fields::Tags, _>("discounted".to_string())
    ///     })
    ///     .build();
    /// // Results in: {
    /// //   "$set": { "name": "Special Item", "discount": 0.15 },
    /// //   "$push": { "tags": { "$each": ["on-sale", "discounted"] } }
    /// // }
    /// ```
    pub fn if_some<A, F>(&mut self, value: Option<A>, f: F) -> &mut Self
    where
        F: FnOnce(&mut Self, A) -> &mut Self,
    {
        if let Some(v) = value {
            f(self, v);
        }

        self
    }

    /// Performs a raw update operation with a custom BSON expression.
    ///
    /// This method provides an escape hatch for advanced MongoDB update operations
    /// that are not directly supported by the type-safe methods. It allows you to
    /// specify any MongoDB update operator along with a custom BSON expression.
    ///
    /// **Warning**: This method bypasses type safety and should be used with caution.
    /// Prefer using the type-safe methods when possible. This is intended for cases
    /// where you need to use MongoDB features that aren't yet supported by the
    /// typed API, or for complex expressions that require manual BSON construction.
    ///
    /// # Type Parameters
    ///
    /// * `F` - A field name marker type that implements `FieldName`
    ///
    /// # Parameters
    ///
    /// * `op` - The MongoDB update operation to perform
    /// * `expr` - A raw BSON expression for the operation
    ///
    /// # Returns
    ///
    /// Returns `&mut Self` to allow method chaining.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use tnuctipun::{FieldName, FieldWitnesses, updates::{empty, UpdateOperation}};
    /// use serde::{Serialize, Deserialize};
    /// use bson::{doc, Bson};
    ///
    /// #[derive(FieldWitnesses, Serialize, Deserialize)]
    /// struct Document {
    ///     pub custom_field: String,
    ///     pub array_field: Vec<i32>,
    /// }
    ///
    /// // Example: Using a complex $push operation with multiple modifiers
    /// let complex_push = doc! {
    ///     "$each": [1, 2, 3],
    ///     "$slice": -10,
    ///     "$sort": -1
    /// };
    ///
    /// let update_doc = empty::<Document>()
    ///     .untyped::<document_fields::ArrayField>(
    ///         UpdateOperation::Push,
    ///         Bson::Document(complex_push)
    ///     )
    ///     .build();
    /// // Results in: {
    /// //   "$push": {
    /// //     "array_field": {
    /// //       "$each": [1, 2, 3],
    /// //       "$slice": -10,
    /// //       "$sort": -1
    /// //     }
    /// //   }
    /// // }
    /// ```
    ///
    /// # Use Cases
    ///
    /// - Complex array operations with multiple modifiers
    /// - New MongoDB operators not yet supported by the typed API
    /// - Custom aggregation expressions in update operations
    /// - Conditional updates using MongoDB expressions
    ///
    /// # Evolution Note
    ///
    /// This method is marked for potential removal if a comprehensive safe expression
    /// builder is implemented in the future. The goal is to eventually provide type-safe
    /// alternatives for all MongoDB update operations.
    pub fn untyped<F: FieldName>(&mut self, op: UpdateOperation, expr: bson::Bson) -> &mut Self
    where
        T: HasField<F>,
    {
        // EVOLUTION: Remove if a safe expression builder is provided
        let path = self.field_path::<F>();

        self.push_clause(op, path, expr);

        self
    }

    /// Performs nested field updates using a path lookup function.
    ///
    /// This method enables type-safe updates on nested document structures by providing
    /// a lookup function that navigates to the target nested field, and a configuration
    /// function that defines the update operations to apply to that nested context.
    ///
    /// # Type Parameters
    ///
    /// * `F` - The field name marker type for the starting field that implements `FieldName`
    /// * `L` - The lookup function type that navigates from the starting field to the target field
    /// * `G` - The field name marker type for the target nested field that implements `FieldName`
    /// * `U` - The target struct type that contains the nested field and implements `HasField<G>`
    /// * `N` - The configuration function type that defines update operations on the nested field
    ///
    /// # Parameters
    ///
    /// * `lookup` - A function that takes a path to field `F` and returns a path to the target field `G`
    /// * `f` - A function that takes an `UpdateBuilder<U>` for the nested context and returns it configured with update operations
    ///
    /// # Returns
    ///
    /// Returns `&mut Self` to allow method chaining.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use tnuctipun::{FieldWitnesses, updates::empty};
    /// use serde::{Serialize, Deserialize};
    ///
    /// #[derive(FieldWitnesses, Serialize, Deserialize)]
    /// struct Address {
    ///     pub street: String,
    ///     pub city: String,
    ///     pub zip_code: String,
    /// }
    ///
    /// #[derive(FieldWitnesses, Serialize, Deserialize)]
    /// struct User {
    ///     pub name: String,
    ///     pub home_address: Address,
    ///     pub work_address: Address,
    /// }
    ///
    /// // Update nested field in home address
    /// let update_doc = empty::<User>()
    ///     .with_lookup::<user_fields::HomeAddress, _, address_fields::City, Address, _>(
    ///         |path| path.field::<address_fields::City>(),
    ///         |nested| {
    ///             nested.set::<address_fields::City, _>("San Francisco".to_string());
    ///         }
    ///     )
    ///     .build();
    /// // Results in: { "$set": { "home_address.city": "San Francisco" } }
    /// ```
    ///
    /// # Usage with Multiple Nested Operations
    ///
    /// ```rust
    /// use tnuctipun::{FieldWitnesses, updates::empty};
    /// use serde::{Serialize, Deserialize};
    ///
    /// #[derive(FieldWitnesses, Serialize, Deserialize)]
    /// struct Address {
    ///     pub street: String,
    ///     pub city: String,
    ///     pub zip_code: String,
    /// }
    ///
    /// #[derive(FieldWitnesses, Serialize, Deserialize)]
    /// struct User {
    ///     pub name: String,
    ///     pub home_address: Address,
    ///     pub work_address: Address,
    /// }
    ///
    /// let update_doc = empty::<User>()
    ///     .with_lookup::<user_fields::HomeAddress, _, address_fields::City, Address, _>(
    ///         |path| path.field::<address_fields::City>(),
    ///         |nested| {
    ///             nested
    ///                 .set::<address_fields::City, _>("Boston".to_string())
    ///                 .set::<address_fields::ZipCode, _>("02101".to_string());
    ///         }
    ///     )
    ///     .with_lookup::<user_fields::WorkAddress, _, address_fields::Street, Address, _>(
    ///         |path| path.field::<address_fields::Street>(),
    ///         |nested| {
    ///             nested.set::<address_fields::Street, _>("123 Business Ave".to_string());
    ///         }
    ///     )
    ///     .build();
    /// // Results in: {
    /// //   "$set": {
    /// //     "home_address.city": "Boston",
    /// //     "home_address.zip_code": "02101",
    /// //     "work_address.street": "123 Business Ave"
    /// //   }
    /// // }
    /// ```
    pub fn with_lookup<F: FieldName, L, G: FieldName, U: HasField<G>, N>(
        &mut self,
        lookup: L,
        f: N,
    ) -> &mut Self
    where
        T: HasField<F>,
        L: FnOnce(&Path<F, T, T>) -> Path<G, U, T>,
        N: FnOnce(&mut UpdateBuilder<U>),
    {
        // Create a base field path for the lookup
        let base_field: Path<F, T, T> = Path {
            prefix: self.prefix.clone(),
            _marker: std::marker::PhantomData,
        };

        // Resolve the field path using the provided lookup function
        let resolved_field = lookup(&base_field);

        // Create a new UpdateBuilder for the nested field
        let mut nested_builder = UpdateBuilder::<U> {
            prefix: resolved_field.prefix.clone(),
            clauses: HashMap::new(),
            _marker: std::marker::PhantomData,
        };

        f(&mut nested_builder);

        // Merge the nested clauses properly into the main builder
        for (operation, clauses_vec) in nested_builder.clauses {
            self.clauses
                .entry(operation)
                .or_default()
                .extend(clauses_vec);
        }

        self
    }

    /// Convenience method for updating fields using identity lookup.
    ///
    /// This method provides a simpler interface for field updates when you don't need
    /// to navigate to nested fields. It's equivalent to calling `with_lookup` with an
    /// identity function that returns the same field path unchanged.
    ///
    /// This is particularly useful for applying update operations within the current
    /// document context without having to deal with path navigation.
    ///
    /// # Type Parameters
    ///
    /// * `F` - The field name marker type that implements `FieldName`
    /// * `N` - The configuration function type that defines update operations
    ///
    /// # Parameters
    ///
    /// * `f` - A function that takes an `UpdateBuilder<T>` and returns it configured with update operations
    ///
    /// # Returns
    ///
    /// Returns `&mut Self` to allow method chaining.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use tnuctipun::{FieldWitnesses, updates::empty};
    /// use serde::{Serialize, Deserialize};
    ///
    /// #[derive(FieldWitnesses, Serialize, Deserialize)]
    /// struct User {
    ///     pub name: String,
    ///     pub age: i32,
    ///     pub active: bool,
    /// }
    ///
    /// // Apply multiple operations in the same context
    /// let update_doc = empty::<User>()
    ///     .with_field::<user_fields::Name, _>(|nested| {
    ///         nested
    ///             .set::<user_fields::Name, _>("John Doe".to_string())
    ///             .inc::<user_fields::Age, _>(1)
    ///             .set::<user_fields::Active, _>(true);
    ///     })
    ///     .build();
    /// // Results in: {
    /// //   "$set": {
    /// //     "name": "John Doe",
    /// //     "active": true
    /// //   },
    /// //   "$inc": { "age": 1 }
    /// // }
    /// ```
    ///
    /// # Comparison with Direct Method Calls
    ///
    /// The following two approaches are equivalent:
    ///
    /// ```rust
    /// use tnuctipun::{FieldWitnesses, updates::empty};
    /// use serde::{Serialize, Deserialize};
    ///
    /// #[derive(FieldWitnesses, Serialize, Deserialize)]
    /// struct Product {
    ///     pub name: String,
    ///     pub price: f64,
    /// }
    ///
    /// // Using with_field
    /// let update_doc1 = empty::<Product>()
    ///     .with_field::<product_fields::Name, _>(|nested| {
    ///         nested.set::<product_fields::Name, _>("Widget".to_string());
    ///     })
    ///     .build();
    ///
    /// // Using direct method calls
    /// let update_doc2 = empty::<Product>()
    ///     .set::<product_fields::Name, _>("Widget".to_string())
    ///     .build();
    ///
    /// // Both produce the same result: { "$set": { "name": "Widget" } }
    /// assert_eq!(update_doc1, update_doc2);
    /// ```
    pub fn with_field<F: FieldName, N>(&mut self, f: N) -> &mut Self
    where
        T: HasField<F>,
        N: FnOnce(&mut UpdateBuilder<T>),
    {
        self.with_lookup::<F, _, F, T, _>(|path| path.clone(), f)
    }

    /// Builds the final MongoDB update document.
    ///
    /// This method consumes the accumulated update operations and produces a
    /// `bson::Document` that can be used directly with MongoDB update queries.
    /// All update clauses are organized by operation type (e.g., `$set`, `$inc`)
    /// and combined into their respective operation documents.
    ///
    /// # Returns
    ///
    /// A `bson::Document` containing all the update operations that were added
    /// to this builder, organized by MongoDB update operator.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use tnuctipun::{FieldWitnesses, updates::empty};
    /// use serde::{Serialize, Deserialize};
    ///
    /// #[derive(FieldWitnesses, Serialize, Deserialize)]
    /// struct User {
    ///     pub name: String,
    ///     pub age: i32,
    /// }
    ///
    /// let update_doc = empty::<User>()
    ///     .set::<user_fields::Name, _>("John".to_string())
    ///     .inc::<user_fields::Age, _>(1)
    ///     .build();
    /// // Results in: {
    /// //   "$set": { "name": "John" },
    /// //   "$inc": { "age": 1 }
    /// // }
    /// ```
    ///
    /// # Behavior with Multiple Operations
    ///
    /// Operations of the same type are combined into a single operation document:
    ///
    /// ```rust
    /// use tnuctipun::{FieldWitnesses, updates::empty};
    /// use serde::{Serialize, Deserialize};
    ///
    /// #[derive(FieldWitnesses, Serialize, Deserialize)]
    /// struct Stats {
    ///     pub views: i32,
    ///     pub likes: i32,
    /// }
    ///
    /// let update_doc = empty::<Stats>()
    ///     .inc::<stats_fields::Views, _>(1)
    ///     .inc::<stats_fields::Likes, _>(1)
    ///     .build();
    /// // Results in: {
    /// //   "$inc": {
    /// //     "views": 1,
    /// //     "likes": 1
    /// //   }
    /// // }
    /// ```
    pub fn build(&mut self) -> bson::Document {
        let mut doc = bson::Document::new();

        for (op, op_clauses) in &self.clauses {
            let operation = op.as_str();
            let mut operation_doc = bson::Document::new();

            for (field, clause) in op_clauses {
                operation_doc.insert(field.clone(), clause.clone());
            }

            // Insert the operation document into the main document
            doc.insert(operation, operation_doc);
        }

        doc
    }
}

// ---

/// MongoDB update operations that can be performed on documents.
///
/// This enum represents the various update operators available in MongoDB,
/// each corresponding to a specific type of modification that can be applied
/// to document fields during update operations.
///
/// # Examples
///
/// ```rust
/// use tnuctipun::updates::UpdateOperation;
///
/// let set_op = UpdateOperation::Set;
///
/// assert_eq!(format!("{}", set_op), "$set");
///
/// let inc_op = UpdateOperation::Inc;
///
/// assert_eq!(format!("{}", inc_op), "$inc");
/// ```
#[derive(Eq, Hash, PartialEq)]
pub enum UpdateOperation {
    /// Sets the value of a field in a document.
    ///
    /// Corresponds to MongoDB's `$set` operator, which sets the value of a field.
    /// If the field does not exist, `$set` will add a new field with the specified value.
    Set,

    /// Removes the specified field from a document.
    ///
    /// Corresponds to MongoDB's `$unset` operator, which deletes a particular field.
    /// The specified value in the `$unset` expression does not impact the operation.
    Unset,

    /// Increments the value of a field by the specified amount.
    ///
    /// Corresponds to MongoDB's `$inc` operator, which increments a field by a specified value.
    /// The field must contain a numeric value. If the field does not exist, it is created with the increment value.
    Inc,

    /// Updates a field only if the specified value is greater than the existing field value.
    ///
    /// Corresponds to MongoDB's `$max` operator, which only updates the field if the specified value
    /// is greater than the existing field value. If the field does not exist, it sets the field to the specified value.
    Max,

    /// Updates a field only if the specified value is less than the existing field value.
    ///
    /// Corresponds to MongoDB's `$min` operator, which only updates the field if the specified value
    /// is less than the existing field value. If the field does not exist, it sets the field to the specified value.
    Min,

    /// Multiplies the value of a field by the specified amount.
    ///
    /// Corresponds to MongoDB's `$mul` operator, which multiplies the value of a field by a number.
    /// The field must contain a numeric value. If the field does not exist, it is created with a value of zero.
    Mul,

    /// Renames a field.
    ///
    /// Corresponds to MongoDB's `$rename` operator, which renames a field.
    /// The new field name must differ from the existing field name.
    Rename,

    /// Sets the value of a field to the current date.
    ///
    /// Corresponds to MongoDB's `$currentDate` operator, which sets the value of a field to the current date,
    /// either as a Date or a timestamp. The default type is Date.
    CurrentDate,

    /// Adds elements to an array only if they do not already exist in the set.
    ///
    /// Corresponds to MongoDB's `$addToSet` operator, which adds a value to an array unless the value
    /// is already present, in which case it does nothing to that array.
    AddToSet,

    /// Removes the first or last item of an array.
    ///
    /// Corresponds to MongoDB's `$pop` operator, which removes the first or last element of an array.
    /// Pass -1 to remove the first element, or 1 to remove the last element.
    Pop,

    /// Removes all array elements that match a specified query.
    ///
    /// Corresponds to MongoDB's `$pull` operator, which removes from an existing array all instances
    /// of a value or values that match a specified condition.
    Pull,

    /// Removes all matching values from an array.
    ///
    /// Corresponds to MongoDB's `$pullAll` operator, which removes all instances of the specified
    /// values from an existing array. Unlike `$pull`, which removes elements by specifying a condition,
    /// `$pullAll` removes elements that match any of the listed values.
    PullAll,

    /// Appends a specified value to an array.
    ///
    /// Corresponds to MongoDB's `$push` operator, which appends a specified value to an array.
    /// If the field is absent, it creates a new array field with the value as its element.
    Push,
}

impl UpdateOperation {
    /// Returns the MongoDB operator string for this update operation.
    pub const fn as_str(&self) -> &'static str {
        match self {
            UpdateOperation::Set => "$set",
            UpdateOperation::Unset => "$unset",
            UpdateOperation::Inc => "$inc",
            UpdateOperation::Max => "$max",
            UpdateOperation::Min => "$min",
            UpdateOperation::Mul => "$mul",
            UpdateOperation::Rename => "$rename",
            UpdateOperation::CurrentDate => "$currentDate",
            UpdateOperation::AddToSet => "$addToSet",
            UpdateOperation::Pop => "$pop",
            UpdateOperation::Pull => "$pull",
            UpdateOperation::PullAll => "$pullAll",
            UpdateOperation::Push => "$push",
        }
    }
}

/// Controls how many elements to keep in an array after a `$push` operation with `$each`.
///
/// This enum corresponds to MongoDB's `$slice` modifier for the `$push` operator,
/// which limits the array to a specified number of elements after adding new values.
/// The slice operation happens after the `$push` and `$sort` operations.
///
/// # Examples
///
/// ```rust
/// use tnuctipun::updates::PushEachSlice;
/// use bson::Bson;
///
/// let keep_first_10 = PushEachSlice::PushFirstSlice(10);
/// let bson_val: Bson = keep_first_10.into(); // Results in Bson::Int32(10)
///
/// let keep_last_5 = PushEachSlice::PushLastSlice(5);
/// let bson_val: Bson = keep_last_5.into(); // Results in Bson::Int32(-5)
/// ```
pub enum PushEachSlice {
    /// Empties the array (keeps 0 elements).
    ///
    /// Corresponds to `$slice: 0` in MongoDB, which removes all elements from the array.
    PushEmptySlice,

    /// Keeps only the last n elements of the array.
    ///
    /// Corresponds to `$slice: -n` in MongoDB. This is useful for implementing
    /// a "recent items" or "last n entries" pattern.
    PushLastSlice(usize),

    /// Keeps only the first n elements of the array.
    ///
    /// Corresponds to `$slice: n` in MongoDB. This is useful for implementing
    /// a "top n" or "first n entries" pattern.
    PushFirstSlice(usize),
}

impl From<PushEachSlice> for bson::Bson {
    fn from(slice: PushEachSlice) -> Self {
        match slice {
            PushEachSlice::PushEmptySlice => bson::Bson::Int32(0),
            PushEachSlice::PushLastSlice(n) => bson::Bson::Int32(-(n as i32)),
            PushEachSlice::PushFirstSlice(n) => bson::Bson::Int32(n as i32),
        }
    }
}

/// Specifies how to sort array elements after a `$push` operation with `$each`.
///
/// This enum corresponds to MongoDB's `$sort` modifier for the `$push` operator,
/// which sorts the array elements after adding new values but before applying
/// any `$slice` operation.
///
/// # Examples
///
/// ```rust
/// use tnuctipun::updates::PushEachSort;
/// use bson::{Bson, doc};
///
/// let ascending = PushEachSort::PushSortAscending;
/// let bson_val: Bson = ascending.into(); // Results in Bson::Int32(1)
///
/// let descending = PushEachSort::PushSortDescending;
/// let bson_val: Bson = descending.into(); // Results in Bson::Int32(-1)
///
/// // For complex sorting on embedded documents
/// let sort_expr = doc! { "score": -1, "timestamp": 1 };
/// let complex_sort = PushEachSort::PushSortExpression(sort_expr);
/// ```
pub enum PushEachSort {
    /// Sort array elements in ascending order.
    ///
    /// Corresponds to `$sort: 1` in MongoDB. For numeric values, this sorts
    /// from smallest to largest. For strings, this sorts alphabetically.
    PushSortAscending,

    /// Sort array elements in descending order.
    ///
    /// Corresponds to `$sort: -1` in MongoDB. For numeric values, this sorts
    /// from largest to smallest. For strings, this sorts in reverse alphabetical order.
    PushSortDescending,

    /// Sort using a custom expression document.
    ///
    /// Corresponds to `$sort: { field: 1, other_field: -1 }` in MongoDB.
    /// This is useful when working with arrays of embedded documents where
    /// you need to sort by specific fields within those documents.
    ///
    /// # TODO
    /// In the future, this will be replaced with a type-safe expression builder
    /// to avoid manual BSON document construction.
    PushSortExpression(bson::Document),
}

impl From<PushEachSort> for bson::Bson {
    fn from(sort: PushEachSort) -> Self {
        match sort {
            PushEachSort::PushSortAscending => bson::Bson::Int32(1),
            PushEachSort::PushSortDescending => bson::Bson::Int32(-1),
            PushEachSort::PushSortExpression(expr) => expr.into(),
        }
    }
}

/// Specifies where to insert new elements in an array during a `$push` operation with `$each`.
///
/// This enum corresponds to MongoDB's `$position` modifier for the `$push` operator,
/// which determines where in the existing array the new elements should be inserted.
/// By default, new elements are appended to the end of the array.
///
/// # Examples
///
/// ```rust
/// use tnuctipun::updates::PushEachPosition;
/// use bson::Bson;
///
/// let insert_at_start = PushEachPosition::PushTakeFirst(0);
/// let bson_val: Bson = insert_at_start.into(); // Results in Bson::Int32(0)
///
/// let insert_at_position_3 = PushEachPosition::PushTakeFirst(3);
/// let bson_val: Bson = insert_at_position_3.into(); // Results in Bson::Int32(3)
/// ```
pub enum PushEachPosition {
    /// Insert elements starting at the specified position from the beginning.
    ///
    /// Corresponds to `$position: n` in MongoDB, where n is a non-negative integer.
    /// Position 0 means insert at the beginning of the array, position 1 means
    /// insert after the first element, etc.
    PushTakeFirst(usize),

    /// Insert elements at the specified position from the end.
    ///
    /// Corresponds to `$position: -n` in MongoDB, where n is a positive integer.
    /// Position -1 means insert before the last element, -2 means insert before
    /// the second-to-last element, etc.
    PushTakeLast(usize),
}

impl From<PushEachPosition> for bson::Bson {
    fn from(position: PushEachPosition) -> Self {
        match position {
            PushEachPosition::PushTakeFirst(n) => bson::Bson::Int32(n as i32),
            PushEachPosition::PushTakeLast(n) => bson::Bson::Int32(-(n as i32)),
        }
    }
}

/// Configuration for MongoDB's `$push` operation with `$each` and optional modifiers.
///
/// This struct represents a MongoDB `$push` operation with the `$each` modifier,
/// along with optional `$slice`, `$sort`, and `$position` modifiers. It provides
/// fine-grained control over how elements are added to arrays.
///
/// The operation sequence in MongoDB is:
/// 1. Insert elements at the specified position (`$position`)
/// 2. Sort the entire array (`$sort`)
/// 3. Trim the array to the specified size (`$slice`)
///
/// # Type Parameters
///
/// * `Values` - An iterable type containing the values to push
/// * `V` - The value type that can be converted to BSON
///
/// # Examples
///
/// ```rust
/// use tnuctipun::updates::{PushEach, PushEachSlice, PushEachSort, PushEachPosition};
///
/// // Basic usage with just values
/// let values = vec![1, 2, 3];
/// let push_clause: PushEach<_, _> = values.into();
///
/// // Advanced usage with modifiers (this is conceptual - actual builder methods would be needed)
/// // let advanced_push = PushEach::from(vec![1, 2, 3])
/// //     .with_position(PushEachPosition::PushTakeFirst(0))  // Insert at beginning
/// //     .with_sort(PushEachSort::PushSortAscending)         // Sort ascending
/// //     .with_slice(PushEachSlice::PushFirstSlice(10));     // Keep first 10 elements
/// ```
///
/// # MongoDB Equivalent
///
/// A `PushEach` with all modifiers set would generate MongoDB syntax like:
/// ```javascript
/// {
///   "$push": {
///     "field": {
///       "$each": [1, 2, 3],
///       "$position": 0,
///       "$sort": 1,
///       "$slice": 10
///     }
///   }
/// }
/// ```
pub struct PushEach<Values: IntoIterator<Item = V>, V: Into<bson::Bson>> {
    /// The values to be added to the array.
    ///
    /// This can be any type that implements `IntoIterator<Item = V>` where `V: Into<bson::Bson>`,
    /// such as `Vec<String>`, `Vec<i32>`, or any collection of values that can be converted to BSON.
    pub values: Values,

    /// Optional slice modifier to limit the array size after the push operation.
    ///
    /// Controls how many elements to keep in the array after adding new values.
    /// See [`PushEachSlice`] for available options.
    pub slice: std::option::Option<PushEachSlice>,

    /// Optional sort modifier to sort the array after the push operation.
    ///
    /// Defines how the array should be sorted after adding new values.
    /// See [`PushEachSort`] for available options.
    pub sort: std::option::Option<PushEachSort>,

    /// Optional position modifier to specify where in the array to insert new values.
    ///
    /// Controls the insertion point for new values in the array.
    /// See [`PushEachPosition`] for available options.
    pub position: std::option::Option<PushEachPosition>,
}

/// Allows to create a `PushEach` instance from a simple iterable of values.
impl<Values: IntoIterator> From<Values> for PushEach<Values, <Values as IntoIterator>::Item>
where
    <Values as IntoIterator>::Item: Into<bson::Bson>,
{
    fn from(values: Values) -> Self {
        PushEach {
            values,
            slice: None,
            sort: None,
            position: None,
        }
    }
}

impl<Values: IntoIterator<Item = V>, V: Into<bson::Bson>> PushEach<Values, V> {
    /// Creates a new `PushEach` instance from an iterable of values.
    ///
    /// This is equivalent to using the `From` trait implementation.
    pub fn new(values: Values) -> Self {
        Self {
            values,
            slice: None,
            sort: None,
            position: None,
        }
    }

    /// Sets the slice modifier to control array length after the push operation.
    ///
    /// The slice operation happens after the push and sort operations, limiting
    /// the array to the specified number of elements.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use tnuctipun::updates::{PushEach, PushEachSlice};
    ///
    /// let push_clause = PushEach::new(vec![1, 2, 3])
    ///     .with_slice(PushEachSlice::PushFirstSlice(10)); // Keep first 10 elements
    /// ```
    pub fn with_slice(mut self, slice: PushEachSlice) -> Self {
        self.slice = Some(slice);
        self
    }

    /// Sets the sort modifier to control array ordering after the push operation.
    ///
    /// The sort operation happens after the push but before any slice operation.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use tnuctipun::updates::{PushEach, PushEachSort};
    /// use bson::doc;
    ///
    /// // Simple ascending sort
    /// let push_clause = PushEach::new(vec![3, 1, 2])
    ///     .with_sort(PushEachSort::PushSortAscending);
    ///
    /// // Complex sort for embedded documents
    /// let sort_expr = doc! { "score": -1, "name": 1 };
    /// let docs = vec![doc! { "score": 100, "name": "Alice" }];
    /// let complex_push = PushEach::new(docs)
    ///     .with_sort(PushEachSort::PushSortExpression(sort_expr));
    /// ```
    pub fn with_sort(mut self, sort: PushEachSort) -> Self {
        self.sort = Some(sort);
        self
    }

    /// Sets the position modifier to control where new elements are inserted.
    ///
    /// By default, elements are appended to the end of the array. This modifier
    /// allows insertion at a specific position.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use tnuctipun::updates::{PushEach, PushEachPosition};
    ///
    /// // Insert at the beginning of the array
    /// let push_clause = PushEach::new(vec![1, 2, 3])
    ///     .with_position(PushEachPosition::PushTakeFirst(0));
    ///
    /// // Insert at position 5
    /// let push_clause = PushEach::new(vec![1, 2, 3])
    ///     .with_position(PushEachPosition::PushTakeFirst(5));
    /// ```
    pub fn with_position(mut self, position: PushEachPosition) -> Self {
        self.position = Some(position);
        self
    }
}

impl<Values: IntoIterator<Item = V>, V: Into<bson::Bson>> From<PushEach<Values, V>> for bson::Bson {
    fn from(push_each: PushEach<Values, V>) -> Self {
        let mut expr = bson::Document::new();

        // Convert the values to BSON
        let values: Vec<bson::Bson> = push_each.values.into_iter().map(|v| v.into()).collect();

        expr.insert("$each", bson::Bson::Array(values));

        // Add optional fields if they are set
        if let Some(slice) = push_each.slice {
            expr.insert("$slice", bson::Bson::from(slice));
        }

        if let Some(sort) = push_each.sort {
            expr.insert("$sort", bson::Bson::from(sort));
        }

        if let Some(position) = push_each.position {
            expr.insert("$position", bson::Bson::from(position));
        }

        expr.into()
    }
}

/// Converts `UpdateOperation` variants to their corresponding MongoDB operator strings.
///
/// This implementation allows `UpdateOperation` enum variants to be converted to the
/// string representations expected by MongoDB update operations.
impl std::fmt::Display for UpdateOperation {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "{}", self.as_str())
    }
}

// ---

/// Type of date value to use with MongoDB's `$currentDate` operator.
///
/// This enum specifies whether to set a field to the current date as a BSON Date
/// or as a BSON Timestamp. The choice affects both the storage format and the
/// precision of the date value in MongoDB.
///
/// # Examples
///
/// ```rust
/// use tnuctipun::updates::CurrentDateType;
///
/// let date_type = CurrentDateType::Date;
///
/// assert_eq!(format!("{}", date_type), "date");
///
/// let timestamp_type = CurrentDateType::Timestamp;
///
/// assert_eq!(format!("{}", timestamp_type), "timestamp");
/// ```
///
/// # Usage with UpdateBuilder
///
/// ```rust
/// use tnuctipun::updates::{UpdateBuilder, CurrentDateType};
/// // Assuming you have appropriate field witnesses set up
///
/// let mut builder = UpdateBuilder::<()>::new();
/// // This would set a field to the current date
/// // builder.current_date::<SomeField>(CurrentDateType::Date);
/// ```
pub enum CurrentDateType {
    /// Sets the field to the current date as a BSON Date.
    ///
    /// This is the default behavior and stores the date with millisecond precision.
    /// BSON Date values are stored as 64-bit integers representing milliseconds
    /// since the Unix epoch (January 1, 1970, 00:00:00 UTC).
    Date,

    /// Sets the field to the current date as a BSON Timestamp.
    ///
    /// BSON Timestamps are MongoDB's internal timestamp type, primarily used
    /// for internal MongoDB operations. They consist of a 32-bit timestamp
    /// (seconds since epoch) and a 32-bit incrementing ordinal for operations
    /// within a given second.
    ///
    /// Note: BSON Timestamps are different from BSON Date and are mainly
    /// intended for internal MongoDB use cases.
    Timestamp,
}

/// Converts `CurrentDateType` variants to their string representations expected by MongoDB.
///
/// This implementation allows `CurrentDateType` enum variants to be converted to the
/// string values that MongoDB expects in `$currentDate` operations.
///
/// # Examples
///
/// ```rust
/// use tnuctipun::updates::CurrentDateType;
///
/// assert_eq!(format!("{}", CurrentDateType::Date), "date");
/// assert_eq!(format!("{}", CurrentDateType::Timestamp), "timestamp");
/// ```
impl std::fmt::Display for CurrentDateType {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        let s = match self {
            CurrentDateType::Date => "date",
            CurrentDateType::Timestamp => "timestamp",
        };

        write!(f, "{s}")
    }
}

// ---

/// Strategy for removing elements from an array using MongoDB's `$pop` operator.
///
/// This enum specifies which end of an array to remove an element from when using
/// the `$pop` update operation. MongoDB's `$pop` operator removes either the first
/// or the last element from an array field.
///
/// # Examples
///
/// ```rust
/// use tnuctipun::updates::PopStrategy;
/// use bson::Bson;
///
/// let first_strategy = PopStrategy::First;
/// let first_bson: Bson = first_strategy.into();
/// // This converts to Bson::Int32(-1)
///
/// let last_strategy = PopStrategy::Last;
/// let last_bson: Bson = last_strategy.into();
/// // This converts to Bson::Int32(1)
/// ```
///
/// # Usage with UpdateBuilder
///
/// ```rust
/// use tnuctipun::updates::{UpdateBuilder, PopStrategy};
/// // Assuming you have appropriate field witnesses set up
///
/// let mut builder = UpdateBuilder::<()>::new();
/// // This would remove the first element from an array field
/// // builder.pop::<SomeArrayField>(PopStrategy::First);
/// ```
pub enum PopStrategy {
    /// Remove the first element from the array.
    ///
    /// Corresponds to MongoDB's `$pop` with value `-1`, which removes the first
    /// (leftmost) element from an array. This is equivalent to a "shift" operation
    /// in many programming languages.
    First,

    /// Remove the last element from the array.
    ///
    /// Corresponds to MongoDB's `$pop` with value `1`, which removes the last
    /// (rightmost) element from an array. This is equivalent to a "pop" operation
    /// in many programming languages.
    Last,
}

/// Converts `PopStrategy` variants to their corresponding BSON values expected by MongoDB.
///
/// This implementation allows `PopStrategy` enum variants to be converted to the
/// BSON integer values that MongoDB expects for the `$pop` operation:
/// - `PopStrategy::First` converts to `Bson::Int32(-1)`
/// - `PopStrategy::Last` converts to `Bson::Int32(1)`
///
/// # Examples
///
/// ```rust
/// use tnuctipun::updates::PopStrategy;
/// use bson::Bson;
///
/// let strategy = PopStrategy::First;
/// let bson_value: Bson = strategy.into();
///
/// assert_eq!(bson_value, Bson::Int32(-1));
///
/// let strategy = PopStrategy::Last;
/// let bson_value: Bson = strategy.into();
///
/// assert_eq!(bson_value, Bson::Int32(1));
/// ```
impl From<PopStrategy> for bson::Bson {
    fn from(strategy: PopStrategy) -> Self {
        match strategy {
            PopStrategy::First => bson::Bson::Int32(-1),
            PopStrategy::Last => bson::Bson::Int32(1),
        }
    }
}

/// Creates a new empty `UpdateBuilder` instance.
///
/// This function provides a convenient way to start a fluent chain of update operations
/// without needing to explicitly call `UpdateBuilder::new()` and assign it to a mutable variable.
///
/// # Type Parameters
///
/// * `T` - The target struct type that implements the necessary field witness traits
///
/// # Examples
///
/// ```rust
/// use tnuctipun::{FieldWitnesses, updates::empty};
/// use serde::{Serialize, Deserialize};
///
/// #[derive(FieldWitnesses, Serialize, Deserialize)]
/// struct User {
///     pub id: String,
///     pub name: String,
///     pub email: String,
///     pub age: i32,
/// }
///
/// // Use method chaining (recommended)
/// let update_doc = empty::<User>()
///     .set::<user_fields::Name, _>("John Doe".to_string())
///     .inc::<user_fields::Age, _>(1)
///     .unset::<user_fields::Email>()
///     .build();
/// // Results in: {
/// //   "$set": { "name": "John Doe" },
/// //   "$inc": { "age": 1 },
/// //   "$unset": { "email": null }
/// // }
/// ```
pub fn empty<T>() -> UpdateBuilder<T> {
    UpdateBuilder::new()
}

// ---

#[cfg(test)]
mod tests {
    use super::*;
    use crate::field_witnesses::FieldName;

    // Mock field name marker types for testing
    struct TestFieldName;
    impl FieldName for TestFieldName {
        fn field_name() -> &'static str {
            "test_field"
        }
    }

    struct AnotherFieldName;
    impl FieldName for AnotherFieldName {
        fn field_name() -> &'static str {
            "another_field"
        }
    }

    struct NestedFieldName;
    impl FieldName for NestedFieldName {
        fn field_name() -> &'static str {
            "nested.field"
        }
    }

    #[test]
    fn test_field_path_empty_prefix() {
        let builder = UpdateBuilder::<()>::new();
        let path = builder.field_path::<TestFieldName>();

        assert_eq!(path, "test_field");
    }

    #[test]
    fn test_field_path_single_prefix() {
        let mut builder = UpdateBuilder::<()>::new();

        builder.prefix.push("parent".to_string());

        let path = builder.field_path::<TestFieldName>();

        assert_eq!(path, "parent.test_field");
    }

    #[test]
    fn test_field_path_multiple_prefix() {
        let mut builder = UpdateBuilder::<()>::new();

        builder.prefix.push("root".to_string());
        builder.prefix.push("parent".to_string());
        builder.prefix.push("child".to_string());

        let path = builder.field_path::<TestFieldName>();

        assert_eq!(path, "root.parent.child.test_field");
    }

    #[test]
    fn test_field_path_different_field_types() {
        let mut builder = UpdateBuilder::<()>::new();

        builder.prefix.push("prefix".to_string());

        let path1 = builder.field_path::<TestFieldName>();

        assert_eq!(path1, "prefix.test_field");

        let path2 = builder.field_path::<AnotherFieldName>();

        assert_eq!(path2, "prefix.another_field");
    }

    #[test]
    fn test_field_path_nested_field_name() {
        let mut builder = UpdateBuilder::<()>::new();

        builder.prefix.push("outer".to_string());

        let path = builder.field_path::<NestedFieldName>();

        assert_eq!(path, "outer.nested.field");
    }

    #[test]
    fn test_field_path_empty_string_prefix() {
        let mut builder = UpdateBuilder::<()>::new();

        builder.prefix.push("".to_string());

        let path = builder.field_path::<TestFieldName>();

        assert_eq!(path, ".test_field");
    }

    #[test]
    fn test_field_path_consistency_across_multiple_calls() {
        let mut builder = UpdateBuilder::<()>::new();

        builder.prefix.push("consistent".to_string());

        let path1 = builder.field_path::<TestFieldName>();
        let path2 = builder.field_path::<TestFieldName>();

        assert_eq!(path1, path2);
        assert_eq!(path1, "consistent.test_field");
    }

    #[test]
    fn test_field_path_deeply_nested_prefix() {
        let mut builder = UpdateBuilder::<()>::new();

        // Simulate a deeply nested structure
        for i in 0..10 {
            builder.prefix.push(format!("level{i}"));
        }

        let path = builder.field_path::<TestFieldName>();

        assert_eq!(
            path,
            "level0.level1.level2.level3.level4.level5.level6.level7.level8.level9.test_field"
        );
    }

    #[test]
    fn test_field_path_special_characters_in_prefix() {
        let mut builder = UpdateBuilder::<()>::new();

        builder.prefix.push("with-dash".to_string());
        builder.prefix.push("with_underscore".to_string());
        builder.prefix.push("with123numbers".to_string());

        let path = builder.field_path::<TestFieldName>();

        assert_eq!(path, "with-dash.with_underscore.with123numbers.test_field");
    }

    #[test]
    fn test_field_path_with_numeric_string_prefix() {
        let mut builder = UpdateBuilder::<()>::new();

        builder.prefix.push("0".to_string());
        builder.prefix.push("123".to_string());

        let path = builder.field_path::<TestFieldName>();

        assert_eq!(path, "0.123.test_field");
    }
}