worker-matcher 0.3.0

Worker matcher for healthcare information exchange: deterministic and probabilistic matching with multinational national identifiers (UK NHS / FR NIR / ES TSI / IE IHI / UK NI H&C / US SSN), E.164 phone normalisation, address parsing, nickname dictionary, email scoring, and explainable per-field breakdowns.
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
//! Data models for worker demographics and identifiers.
//!
//! This module is intentionally **logic-free**: it defines the types that
//! flow through the matching engine but contains no matching code itself.
//! See [`crate::matcher`] for the engine and [`crate::normalizer`] for the
//! text transformations that the matcher applies to these fields.
//!
//! All public types here are `Serialize + Deserialize` so they round-trip
//! through JSON, MessagePack, or any other `serde` format.
//!
//! ## Building a worker
//!
//! Prefer [`Worker::builder`] over constructing the struct literal — the
//! builder accepts `impl Into<String>` so call-sites can pass `&str`,
//! `String`, or owned values interchangeably.
//!
//! ```
//! use worker_matcher::{Gender, Worker};
//! use chrono::NaiveDate;
//!
//! let p = Worker::builder()
//!     .uk_nhs_number("9434765919")
//!     .given_name("Dafydd")
//!     .family_name("Jones")
//!     .date_of_birth(NaiveDate::from_ymd_opt(1980, 5, 15).unwrap())
//!     .gender(Gender::Male)
//!     .build();
//!
//! assert_eq!(p.given_name.as_deref(), Some("Dafydd"));
//! assert_eq!(p.gender, Some(Gender::Male));
//! ```

use chrono::NaiveDate;
use serde::{Deserialize, Serialize};

/// Gender/sex classification used to compare two [`Worker`] records.
///
/// The four-arm enumeration mirrors common healthcare data dictionaries
/// (HL7 FHIR `AdministrativeGender`, NHS Data Dictionary `Worker Gender`).
/// `Other` and `Unknown` are deliberately distinct: `Other` represents a
/// recorded non-binary value, whereas `Unknown` represents missing data.
///
/// # Example
///
/// ```
/// use worker_matcher::Gender;
///
/// let g = Gender::Female;
/// assert_eq!(g, Gender::Female);
/// assert_ne!(g, Gender::Male);
/// ```
///
/// `Gender` is `Copy`, so it is cheap to pass by value.
///
/// ```
/// # use worker_matcher::Gender;
/// fn describe(g: Gender) -> &'static str {
///     match g {
///         Gender::Male    => "male",
///         Gender::Female  => "female",
///         Gender::Other   => "other",
///         Gender::Unknown => "unknown",
///     }
/// }
/// assert_eq!(describe(Gender::Male), "male");
/// ```
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum Gender {
    /// Administrative gender recorded as male.
    Male,
    /// Administrative gender recorded as female.
    Female,
    /// Recorded non-binary or otherwise specified value.
    Other,
    /// No gender recorded, or gender intentionally withheld.
    Unknown,
}

/// ABO + RhD blood type used as supporting evidence in worker matcher.
///
/// Blood type is a **weak positive** signal and a **strong negative**
/// signal:
///
/// - Many people share a blood type (≈38% of the US population is O+),
///   so agreement alone is not strong evidence of a match.
/// - Two records with disagreeing blood types almost certainly refer
///   to **different** people — blood type does not change over a
///   lifetime (modulo bone-marrow transplant edge cases).
///
/// The matcher therefore weights blood type at the same low level as
/// gender by default (`MatchConfig::blood_type_weight = 0.05`) but the
/// per-field score in `MatchBreakdown::blood_type_score` is surfaced
/// for downstream consumers that want to flag disagreement explicitly.
///
/// Blood type is **not** an identifying field for `Worker::validate`,
/// and it is **not** consulted by `deterministic_match` — disagreement
/// is a soft signal, not a binary disqualifier.
///
/// # JSON
///
/// Variants serialise as their canonical short form (`"A+"`, `"O-"`,
/// `"AB+"`, etc.) via `#[serde(rename = …)]`.
///
/// ```
/// use worker_matcher::BloodType;
/// assert_eq!(serde_json::to_string(&BloodType::APositive).unwrap(), "\"A+\"");
/// let back: BloodType = serde_json::from_str("\"AB-\"").unwrap();
/// assert_eq!(back, BloodType::ABNegative);
/// ```
///
/// # Parsing
///
/// [`BloodType::parse`] accepts the canonical short forms plus the
/// most common textual layouts found in real EMR data:
///
/// ```
/// use worker_matcher::BloodType;
/// assert_eq!(BloodType::parse("A+"),         Some(BloodType::APositive));
/// assert_eq!(BloodType::parse("a positive"), Some(BloodType::APositive));
/// assert_eq!(BloodType::parse("AB neg"),     Some(BloodType::ABNegative));
/// assert_eq!(BloodType::parse("O-"),         Some(BloodType::ONegative));
/// assert_eq!(BloodType::parse("0+"),         Some(BloodType::OPositive));  // zero/O confusion
/// assert_eq!(BloodType::parse(""),           None);
/// assert_eq!(BloodType::parse("Z+"),         None);
/// ```
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub enum BloodType {
    /// A positive (A+).
    #[serde(rename = "A+")]
    APositive,
    /// A negative (A−).
    #[serde(rename = "A-")]
    ANegative,
    /// B positive (B+).
    #[serde(rename = "B+")]
    BPositive,
    /// B negative (B−).
    #[serde(rename = "B-")]
    BNegative,
    /// AB positive (AB+). Universal red-cell recipient.
    #[serde(rename = "AB+")]
    ABPositive,
    /// AB negative (AB−). Rare; universal-plasma donor.
    #[serde(rename = "AB-")]
    ABNegative,
    /// O positive (O+). Most common worldwide.
    #[serde(rename = "O+")]
    OPositive,
    /// O negative (O−). Universal red-cell donor.
    #[serde(rename = "O-")]
    ONegative,
}

impl BloodType {
    /// Canonical short form: `"A+"`, `"A-"`, `"B+"`, `"B-"`, `"AB+"`,
    /// `"AB-"`, `"O+"`, `"O-"`.
    ///
    /// ```
    /// use worker_matcher::BloodType;
    /// assert_eq!(BloodType::APositive.as_str(),  "A+");
    /// assert_eq!(BloodType::ABNegative.as_str(), "AB-");
    /// ```
    pub fn as_str(&self) -> &'static str {
        match self {
            BloodType::APositive => "A+",
            BloodType::ANegative => "A-",
            BloodType::BPositive => "B+",
            BloodType::BNegative => "B-",
            BloodType::ABPositive => "AB+",
            BloodType::ABNegative => "AB-",
            BloodType::OPositive => "O+",
            BloodType::ONegative => "O-",
        }
    }

    /// Parse a blood-type string, accepting canonical short forms as
    /// well as the common textual layouts seen in EMR / HL7 data.
    /// Returns `None` for unparseable, empty, or rare-phenotype input;
    /// consumers that need to preserve a rare phenotype should store
    /// the raw string elsewhere.
    ///
    /// Accepted shapes (case-insensitive, whitespace tolerated):
    ///
    /// - Canonical: `A+`, `A-`, `B+`, `B-`, `AB+`, `AB-`, `O+`, `O-`.
    /// - Word forms: `A positive`, `A pos`, `A negative`, `A neg`.
    /// - With sign-separator: `A_pos`, `A-neg`, `AB +`.
    /// - With zero/O confusion: `0+` is read as `O+`.
    ///
    /// ```
    /// use worker_matcher::BloodType;
    /// assert_eq!(BloodType::parse("O Negative"), Some(BloodType::ONegative));
    /// assert_eq!(BloodType::parse("ab+"),        Some(BloodType::ABPositive));
    /// assert_eq!(BloodType::parse("Bombay"),     None); // rare phenotype, not supported
    /// ```
    pub fn parse(s: &str) -> Option<BloodType> {
        let upper: String = s
            .trim()
            .to_uppercase()
            .chars()
            .map(|c| if c == '0' { 'O' } else { c })
            .collect();
        if upper.is_empty() {
            return None;
        }
        let (group, rest): (&str, &str) = if let Some(r) = upper.strip_prefix("AB") {
            ("AB", r)
        } else if let Some(r) = upper.strip_prefix('A') {
            ("A", r)
        } else if let Some(r) = upper.strip_prefix('B') {
            ("B", r)
        } else if let Some(r) = upper.strip_prefix('O') {
            ("O", r)
        } else {
            return None;
        };
        let positive = parse_rh_sign(rest)?;
        Some(match (group, positive) {
            ("A", true) => BloodType::APositive,
            ("A", false) => BloodType::ANegative,
            ("B", true) => BloodType::BPositive,
            ("B", false) => BloodType::BNegative,
            ("AB", true) => BloodType::ABPositive,
            ("AB", false) => BloodType::ABNegative,
            ("O", true) => BloodType::OPositive,
            ("O", false) => BloodType::ONegative,
            _ => return None,
        })
    }
}

impl std::fmt::Display for BloodType {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.write_str(self.as_str())
    }
}

/// Parse the Rhesus-sign portion of a blood-type string after the ABO
/// group prefix has been stripped. Returns `Some(true)` for positive,
/// `Some(false)` for negative, `None` for unparseable input.
fn parse_rh_sign(s: &str) -> Option<bool> {
    let trimmed = s.trim_start_matches([' ', '\t', '_', '/']).trim();
    if trimmed.is_empty() {
        return None;
    }
    // Word-form check first: "A POS", "A-NEG", "A_POSITIVE" all reach
    // here with `trimmed` containing the word (possibly prefixed by a
    // separator like `-` or `+`). We tolerate one leading sign
    // character as a separator, since the word itself disambiguates.
    let word_candidate = trimmed.trim_start_matches(['-', '+']).trim();
    if word_candidate.starts_with("POSITIVE")
        || word_candidate.starts_with("POS")
        || word_candidate == "P"
    {
        return Some(true);
    }
    if word_candidate.starts_with("NEGATIVE")
        || word_candidate.starts_with("NEG")
        || word_candidate == "N"
    {
        return Some(false);
    }
    // Single-character sign forms (with optional `VE` suffix).
    if let Some(after) = trimmed.strip_prefix('+') {
        let tail = after.trim().trim_start_matches("VE");
        if tail.trim().is_empty() {
            return Some(true);
        }
        return None;
    }
    if let Some(after) = trimmed.strip_prefix('-') {
        let tail = after.trim().trim_start_matches("VE");
        if tail.trim().is_empty() {
            return Some(false);
        }
        return None;
    }
    None
}

/// Physical address used as supporting evidence in worker matcher.
///
/// All fields are `Option<String>` so partial addresses are first-class —
/// a record with only a postcode is still useful for matching.
///
/// The matcher does **not** weight every component equally; see
/// [`crate::matcher::MatchingEngine`] for the weighted comparison rules.
///
/// # Example
///
/// ```
/// use worker_matcher::Address;
///
/// let mut addr = Address::new();
/// addr.line1    = Some("10 Downing Street".into());
/// addr.city     = Some("London".into());
/// addr.postcode = Some("SW1A 2AA".into());
///
/// assert_eq!(addr.postcode.as_deref(), Some("SW1A 2AA"));
/// assert!(addr.country.is_none());
/// ```
///
/// `Address` is JSON round-trippable.
///
/// ```
/// # use worker_matcher::Address;
/// let mut a = Address::new();
/// a.postcode = Some("CF10 1AA".into());
///
/// let json = serde_json::to_string(&a).unwrap();
/// let back: Address = serde_json::from_str(&json).unwrap();
/// assert_eq!(a, back);
/// ```
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[non_exhaustive]
pub struct Address {
    /// First line — typically house number and street, e.g. `"10 Downing Street"`.
    pub line1: Option<String>,
    /// Second line — typically flat, locality, or care-of details.
    pub line2: Option<String>,
    /// Town or city, e.g. `"Cardiff"`.
    pub city: Option<String>,
    /// County or administrative region, e.g. `"South Glamorgan"`.
    pub county: Option<String>,
    /// Postal code, e.g. `"CF10 1AA"`. Compared after whitespace normalisation.
    pub postcode: Option<String>,
    /// Country, e.g. `"Wales"` or `"United Kingdom"`.
    pub country: Option<String>,
}

impl Address {
    /// Construct an empty address with every field set to `None`.
    ///
    /// # Example
    ///
    /// ```
    /// use worker_matcher::Address;
    ///
    /// let a = Address::new();
    /// assert!(a.line1.is_none());
    /// assert!(a.postcode.is_none());
    /// ```
    pub fn new() -> Self {
        Self {
            line1: None,
            line2: None,
            city: None,
            county: None,
            postcode: None,
            country: None,
        }
    }

    /// Fluent setter for `line1`.
    ///
    /// ```
    /// use worker_matcher::Address;
    /// let a = Address::new().with_line1("10 Downing Street");
    /// assert_eq!(a.line1.as_deref(), Some("10 Downing Street"));
    /// ```
    pub fn with_line1(mut self, value: impl Into<String>) -> Self {
        self.line1 = Some(value.into());
        self
    }

    /// Fluent setter for `line2`.
    pub fn with_line2(mut self, value: impl Into<String>) -> Self {
        self.line2 = Some(value.into());
        self
    }

    /// Fluent setter for `city`.
    pub fn with_city(mut self, value: impl Into<String>) -> Self {
        self.city = Some(value.into());
        self
    }

    /// Fluent setter for `county`.
    pub fn with_county(mut self, value: impl Into<String>) -> Self {
        self.county = Some(value.into());
        self
    }

    /// Fluent setter for `postcode`.
    ///
    /// ```
    /// use worker_matcher::Address;
    /// let a = Address::new().with_postcode("CF10 1AA");
    /// assert_eq!(a.postcode.as_deref(), Some("CF10 1AA"));
    /// ```
    pub fn with_postcode(mut self, value: impl Into<String>) -> Self {
        self.postcode = Some(value.into());
        self
    }

    /// Fluent setter for `country`.
    pub fn with_country(mut self, value: impl Into<String>) -> Self {
        self.country = Some(value.into());
        self
    }
}

impl Default for Address {
    /// Identical to [`Address::new`].
    fn default() -> Self {
        Self::new()
    }
}

/// A passport book — country of issue, book number, and optional
/// effective date range.
///
/// Passport data has three properties that make it a poor fit for the
/// crate's per-scheme `Option<String>` national-identifier pattern,
/// and which this type captures explicitly:
///
/// 1. **Scheme-local provenance.** A passport book number is only
///    meaningful alongside its issuing country. The book number
///    `"AB123456"` issued by the United Kingdom is a different
///    identifier from `"AB123456"` issued by the United States; the
///    matcher MUST NOT cross-match them. Provenance lives on the
///    [`PassportBook::country`] field, not on the field name.
/// 2. **Multi-country.** A single worker may hold passports from
///    multiple countries simultaneously (dual / multiple citizenship).
///    A `Vec<PassportBook>` lets a [`crate::Worker`] carry one entry
///    per book without privileging any particular jurisdiction.
/// 3. **Time-varying.** When a passport is renewed, the new book has
///    a different number; the old book number is no longer current
///    but the worker is unchanged. Worker records may carry the
///    current book, prior books, or both. Matching treats any shared
///    `(country, number)` pair across the two records as evidence
///    that the records refer to the same worker, regardless of issue
///    date.
///
/// Construction via [`PassportBook::new`] canonicalises both the
/// country (trimmed, uppercased; must be exactly 2 ASCII letters) and
/// the number (whitespace stripped, letters uppercased) so two records
/// carrying different textual layouts of the same book canonicalise to
/// the same `(country, number)` key. Date fields are optional metadata
/// and are **not** used in matching — they exist for downstream
/// display and audit. Per-country structural validation is
/// intentionally not performed; passport formats vary widely and a
/// case+whitespace canonical form is sufficient for matching.
///
/// # Example
///
/// ```
/// use worker_matcher::PassportBook;
/// use chrono::NaiveDate;
///
/// let book = PassportBook::new("gb", " 123 456 789 ")
///     .expect("valid book")
///     .with_issued(NaiveDate::from_ymd_opt(2020, 1, 1).unwrap())
///     .with_expires(NaiveDate::from_ymd_opt(2030, 1, 1).unwrap());
///
/// assert_eq!(book.country, "GB");
/// assert_eq!(book.number,  "123456789");
/// assert!(book.issued.is_some());
///
/// // Rejection: country must be exactly 2 ASCII letters.
/// assert!(PassportBook::new("GBR", "123").is_none());
/// assert!(PassportBook::new("1A",  "123").is_none());
/// // Rejection: number must canonicalise to a non-empty string.
/// assert!(PassportBook::new("GB",  "   ").is_none());
/// ```
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct PassportBook {
    /// ISO 3166-1 alpha-2 country code of issuance, uppercased.
    pub country: String,
    /// Passport book number, whitespace-stripped and uppercased.
    pub number: String,
    /// Optional issue date (not used in matching).
    #[serde(default)]
    pub issued: Option<NaiveDate>,
    /// Optional expiry date (not used in matching).
    #[serde(default)]
    pub expires: Option<NaiveDate>,
}

impl PassportBook {
    /// Construct a passport book, validating and canonicalising the
    /// country code (trimmed + uppercased; must be exactly 2 ASCII
    /// letters) and the book number (whitespace stripped + uppercased;
    /// must be non-empty after stripping).
    ///
    /// Returns `None` for an invalid country code or an empty
    /// canonical number.
    ///
    /// ```
    /// use worker_matcher::PassportBook;
    /// let b = PassportBook::new("us", "abc 123 456").unwrap();
    /// assert_eq!(b.country, "US");
    /// assert_eq!(b.number,  "ABC123456");
    /// ```
    pub fn new(country: impl AsRef<str>, number: impl AsRef<str>) -> Option<Self> {
        let country = country.as_ref().trim().to_ascii_uppercase();
        if country.len() != 2 || !country.chars().all(|c| c.is_ascii_alphabetic()) {
            return None;
        }
        // Strip common data-entry separators (whitespace, ASCII
        // hyphens, periods, slashes) and uppercase. This matches the
        // canonicalisation used by `parse_es_tsi` / `parse_ie_ihi` so
        // textual variants of the same book canonicalise identically.
        let number: String = number
            .as_ref()
            .chars()
            .filter(|c| !c.is_whitespace() && !matches!(*c, '-' | '.' | '/'))
            .collect::<String>()
            .to_uppercase();
        if number.is_empty() {
            return None;
        }
        Some(Self {
            country,
            number,
            issued: None,
            expires: None,
        })
    }

    /// Attach an issue date. The date is metadata only — it is NOT
    /// used in matching.
    ///
    /// ```
    /// use worker_matcher::PassportBook;
    /// use chrono::NaiveDate;
    /// let b = PassportBook::new("GB", "123456789").unwrap()
    ///     .with_issued(NaiveDate::from_ymd_opt(2020, 1, 1).unwrap());
    /// assert!(b.issued.is_some());
    /// ```
    pub fn with_issued(mut self, date: NaiveDate) -> Self {
        self.issued = Some(date);
        self
    }

    /// Attach an expiry date. The date is metadata only — it is NOT
    /// used in matching.
    ///
    /// ```
    /// use worker_matcher::PassportBook;
    /// use chrono::NaiveDate;
    /// let b = PassportBook::new("GB", "123456789").unwrap()
    ///     .with_expires(NaiveDate::from_ymd_opt(2030, 1, 1).unwrap());
    /// assert!(b.expires.is_some());
    /// ```
    pub fn with_expires(mut self, date: NaiveDate) -> Self {
        self.expires = Some(date);
        self
    }
}

/// Core worker demographic data structure.
///
/// Every field is optional. The matcher tolerates missing data field-by-field
/// — a `None` value never penalises a worker. See
/// [`crate::matcher::MatchingEngine::match_workers`] for how missing fields
/// affect the weighted score.
///
/// Construct via [`Worker::builder`] rather than struct literal syntax so
/// the call-site stays compact and forward-compatible if fields are added.
///
/// # Example
///
/// ```
/// use worker_matcher::{Gender, Worker};
/// use chrono::NaiveDate;
///
/// let p = Worker::builder()
///     .given_name("Siân")
///     .family_name("Evans")
///     .date_of_birth(NaiveDate::from_ymd_opt(1990, 3, 10).unwrap())
///     .gender(Gender::Female)
///     .build();
///
/// assert_eq!(p.given_name.as_deref(), Some("Siân"));
/// assert!(p.uk_nhs_number.is_none());
/// ```
///
/// `Worker` round-trips through `serde`.
///
/// ```
/// # use worker_matcher::Worker;
/// let p = Worker::builder().given_name("Test").family_name("Worker").build();
/// let json = serde_json::to_string(&p).unwrap();
/// let back: Worker = serde_json::from_str(&json).unwrap();
/// assert_eq!(p, back);
/// ```
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[non_exhaustive]
pub struct Worker {
    /// United Kingdom NHS Number (England, Wales, Isle of Man) — a 10-digit
    /// Modulus-11 identifier parsed via
    /// [`crate::identifiers::parse_uk_nhs_number`]. Whitespace tolerated in
    /// the spaced `"XXX XXX XXXX"` layout.
    #[serde(default)]
    pub uk_nhs_number: Option<String>,

    /// France NIR (*Numéro d'Inscription au Répertoire*) — the 15-character
    /// national identifier with a Modulus-97 check key. Parsed via
    /// [`crate::identifiers::parse_fr_nir`].
    #[serde(default)]
    pub fr_nir: Option<String>,

    /// España (Spain) TSI (*Tarjeta Sanitaria Individual*) / CIP-SNS — the
    /// national healthcare identifier with regionally-varying format. Parsed
    /// via [`crate::identifiers::parse_es_tsi`].
    #[serde(default)]
    pub es_tsi: Option<String>,

    /// Éire (Ireland) IHI (Individual Health Identifier) — the 7-digit
    /// healthcare identifier issued under the Health Identifiers Act 2014.
    /// Parsed via [`crate::identifiers::parse_ie_ihi`].
    #[serde(default)]
    pub ie_ihi: Option<String>,

    /// United Kingdom Northern Ireland H&C Number (Health and Care Number)
    /// — a 10-digit Modulus-11 identifier issued by HSC. Shares the NHS
    /// Number algorithm. Parsed via
    /// [`crate::identifiers::parse_uk_hc_number`].
    #[serde(default)]
    pub uk_hc_number: Option<String>,

    /// United States Social Security Number (SSN) — a 9-digit identifier
    /// issued by the Social Security Administration. Parsed via
    /// [`crate::identifiers::parse_us_ssn`].
    #[serde(default)]
    pub us_ssn: Option<String>,

    /// Australia IHI (Individual Healthcare Identifier) — 16-digit
    /// identifier issued by the Healthcare Identifiers Service. Parsed
    /// via [`crate::identifiers::parse_au_ihi`].
    #[serde(default)]
    pub au_ihi: Option<String>,

    /// Germany KVNR (*Krankenversichertennummer*) — 10-character
    /// (letter + 9 digits) lifelong health-insurance number. Parsed via
    /// [`crate::identifiers::parse_de_kvnr`].
    #[serde(default)]
    pub de_kvnr: Option<String>,

    /// Italy *Codice Fiscale* (CF) — 16-character alphanumeric
    /// identifier issued by the tax authority. Parsed via
    /// [`crate::identifiers::parse_it_cf`].
    #[serde(default)]
    pub it_cf: Option<String>,

    /// Netherlands BSN (*Burgerservicenummer*) — 9-digit citizen-service
    /// number used by Dutch authorities and healthcare providers. Parsed
    /// via [`crate::identifiers::parse_nl_bsn`].
    #[serde(default)]
    pub nl_bsn: Option<String>,

    /// Sweden *Workernummer* — 10- or 12-digit workeral identity number
    /// (`YYMMDDNNNC` or `YYYYMMDDNNNC` with optional `-` / `+`
    /// separator). Parsed via
    /// [`crate::identifiers::parse_se_workernummer`].
    #[serde(default)]
    pub se_workernummer: Option<String>,

    /// United Kingdom (Scotland) CHI Number (Community Health Index) —
    /// 10-digit identifier used by NHS Scotland. Shares the Mod-11
    /// algorithm of the NHS Number but is scheme-local. Parsed via
    /// [`crate::identifiers::parse_uk_chi_number`].
    #[serde(default)]
    pub uk_chi_number: Option<String>,

    /// Belgium National Number (*Rijksregisternummer*). 11 digits, Mod-97.
    /// Parsed via [`crate::identifiers::parse_be_nn`].
    #[serde(default)]
    pub be_nn: Option<String>,

    /// Bulgaria EGN (*Edinen grazhdanski nomer*). 10 digits, weighted Mod-11.
    /// Parsed via [`crate::identifiers::parse_bg_egn`].
    #[serde(default)]
    pub bg_egn: Option<String>,

    /// Czech Republic *Rodné číslo*. 9 or 10 digits (10-digit divisible by 11).
    /// Parsed via [`crate::identifiers::parse_cz_rc`].
    #[serde(default)]
    pub cz_rc: Option<String>,

    /// Denmark CPR (*Centrale Workerregister*). 10 digits, format only.
    /// Parsed via [`crate::identifiers::parse_dk_cpr`].
    #[serde(default)]
    pub dk_cpr: Option<String>,

    /// Estonia *Isikukood* (Workeral Identification Code). 11 digits, cascading Mod-11.
    /// Parsed via [`crate::identifiers::parse_ee_ik`].
    #[serde(default)]
    pub ee_ik: Option<String>,

    /// Spain DNI / NIE. 8 digits + Mod-23 control letter (NIE prefixed X/Y/Z).
    /// Parsed via [`crate::identifiers::parse_es_dni`].
    #[serde(default)]
    pub es_dni: Option<String>,

    /// Finland HETU (*Henkilötunnus*). 11 chars with century sign + Mod-31 check.
    /// Parsed via [`crate::identifiers::parse_fi_hetu`].
    #[serde(default)]
    pub fi_hetu: Option<String>,

    /// Croatia OIB (*Osobni identifikacijski broj*). 11 digits, ISO 7064 MOD 11,10.
    /// Parsed via [`crate::identifiers::parse_hr_oib`].
    #[serde(default)]
    pub hr_oib: Option<String>,

    /// Iceland *Kennitala*. 10 digits, weighted Mod-11.
    /// Parsed via [`crate::identifiers::parse_is_kt`].
    #[serde(default)]
    pub is_kt: Option<String>,

    /// Lithuania *Asmens kodas*. 11 digits, cascading Mod-11 (same algorithm as Estonia).
    /// Parsed via [`crate::identifiers::parse_lt_ak`].
    #[serde(default)]
    pub lt_ak: Option<String>,

    /// Latvia *Workeras kods*. 11 digits, weighted Mod-11.
    /// Parsed via [`crate::identifiers::parse_lv_pk`].
    #[serde(default)]
    pub lv_pk: Option<String>,

    /// Malta National ID. 7 digits + letter in `{M, G, A, P, L, H, B, Z}`.
    /// Parsed via [`crate::identifiers::parse_mt_id`].
    #[serde(default)]
    pub mt_id: Option<String>,

    /// Norway *Fødselsnummer*. 11 digits, dual Mod-11.
    /// Parsed via [`crate::identifiers::parse_no_fnr`].
    #[serde(default)]
    pub no_fnr: Option<String>,

    /// Poland PESEL. 11 digits, weighted Mod-10.
    /// Parsed via [`crate::identifiers::parse_pl_pesel`].
    #[serde(default)]
    pub pl_pesel: Option<String>,

    /// Romania CNP (*Cod Numeric Workeral*). 13 digits, weighted Mod-11.
    /// Parsed via [`crate::identifiers::parse_ro_cnp`].
    #[serde(default)]
    pub ro_cnp: Option<String>,

    /// Slovenia EMŠO (*Enotna Matična Številka Občana*). 13 digits, weighted Mod-11.
    /// Parsed via [`crate::identifiers::parse_si_emso`].
    #[serde(default)]
    pub si_emso: Option<String>,

    /// Slovakia *Rodné číslo*. 9 or 10 digits (same algorithm as Czech RČ).
    /// Parsed via [`crate::identifiers::parse_sk_rc`].
    #[serde(default)]
    pub sk_rc: Option<String>,

    /// United Kingdom National Insurance Number (NINO). Format `AA999999A`
    /// with banned prefixes and `{A,B,C,D}` suffix.
    /// Parsed via [`crate::identifiers::parse_uk_nino`].
    #[serde(default)]
    pub uk_nino: Option<String>,

    /// Greece DSS (Dematerialised Securities System) investor share code.
    /// 10-digit identifier issued by the Hellenic Central Securities
    /// Depository (ATHEXCSD). Parsed via
    /// [`crate::identifiers::parse_gr_dss`].
    #[serde(default)]
    pub gr_dss: Option<String>,

    /// Liechtenstein National Identity Card Number. 2 letters + 8 digits
    /// (per the spec) or 2 letters + 9 digits (per the spec's example).
    /// Note: the LI ID card number is **regenerated on each renewal**, so
    /// consumers that need stable cross-renewal matching should prefer
    /// [`PassportBook`] with `country = "LI"`. Parsed via
    /// [`crate::identifiers::parse_li_id`].
    #[serde(default)]
    pub li_id: Option<String>,

    /// Netherlands National Identity Card Number. 9 characters: positions
    /// 1–2 are uppercase letters except `O`; positions 3–8 are
    /// alphanumeric except `O`; position 9 is a digit. Distinct from the
    /// BSN (citizen-service number), which is permanent — this ID-card
    /// number changes with each renewed card.
    /// Parsed via [`crate::identifiers::parse_nl_id`].
    #[serde(default)]
    pub nl_id: Option<String>,

    /// Poland NIP (*Numer Identyfikacji Podatkowej*) tax identification
    /// number. 10 digits, weighted Mod-11 check. Parsed via
    /// [`crate::identifiers::parse_pl_nip`].
    #[serde(default)]
    pub pl_nip: Option<String>,

    /// Portugal NIF (*Número de Identificação Fiscal*) tax identification
    /// number. 9 digits, weighted Mod-11 check. Parsed via
    /// [`crate::identifiers::parse_pt_nif`].
    #[serde(default)]
    pub pt_nif: Option<String>,

    /// Brazil CPF (*Cadastro de Pessoas Físicas*). 11-digit national tax /
    /// identification number with two Mod-11 check digits. Parsed at match
    /// time via [`crate::identifiers::parse_br_cpf`].
    #[serde(default)]
    pub br_cpf: Option<String>,

    /// China Resident Identity Card number (*居民身份证*) — 18-character
    /// 1999 reform format (17 digits + check character). Parsed at match
    /// time via [`crate::identifiers::parse_cn_rrn`].
    #[serde(default)]
    pub cn_rrn: Option<String>,

    /// India Aadhaar number. 12 digits with Verhoeff check digit. Parsed
    /// at match time via [`crate::identifiers::parse_in_aadhaar`].
    #[serde(default)]
    pub in_aadhaar: Option<String>,

    /// Japan My Number (*個人番号*). 12-digit workeral identification
    /// number with weighted Mod-11 check digit. Parsed at match time via
    /// [`crate::identifiers::parse_jp_my_number`].
    #[serde(default)]
    pub jp_my_number: Option<String>,

    /// Mexico CURP (*Clave Única de Registro de Población*). 18-character
    /// alphanumeric identifier encoding name initials, date of birth,
    /// sex, state, and a check digit. Parsed at match time via
    /// [`crate::identifiers::parse_mx_curp`].
    #[serde(default)]
    pub mx_curp: Option<String>,

    /// New Zealand NHI (National Health Index) number. Original 7-character
    /// format (3 letters + 4 digits, Mod-11 check digit). The 2019
    /// alphanumeric NHI revision is not supported by the parser. Parsed at
    /// match time via [`crate::identifiers::parse_nz_nhi`].
    #[serde(default)]
    pub nz_nhi: Option<String>,

    /// South Africa ID Number. 13 digits encoding date of birth, sequence,
    /// citizenship, and a Luhn check digit. Parsed at match time via
    /// [`crate::identifiers::parse_za_id`].
    #[serde(default)]
    pub za_id: Option<String>,

    /// Given name (sometimes called "first name" or "forename").
    pub given_name: Option<String>,

    /// Middle name(s). Currently unused in scoring — see spec OQ-1.
    pub middle_name: Option<String>,

    /// Family name (sometimes called "surname" or "last name").
    pub family_name: Option<String>,

    /// Date of birth. Compared by exact equality.
    pub date_of_birth: Option<NaiveDate>,

    /// Date of death (FHIR `Patient.deceasedDateTime`). Compared using
    /// the same DOB transposition heuristic as
    /// [`Worker::date_of_birth`] — DD/MM ↔ MM/DD data-entry bugs are
    /// just as common in death records as in birth records.
    #[serde(default)]
    pub death_date: Option<NaiveDate>,

    /// Administrative gender. See [`Gender`].
    pub gender: Option<Gender>,

    /// ABO+RhD blood type. Stable for life, so disagreement is strong
    /// evidence against a match; agreement is a weak positive signal
    /// (many people share a blood type). See [`BloodType`] for the
    /// scoring contract.
    #[serde(default)]
    pub blood_type: Option<BloodType>,

    /// FHIR `Patient.multipleBirth` — birth order in a multiple-birth
    /// set (twin / triplet / etc.). Convention:
    ///
    /// - `None` — unknown, not recorded, or singleton (the matcher
    ///   treats `None` as "no signal" and skips the field).
    /// - `Some(n)` with `n >= 1` — the `n`-th birth in a multiple-birth
    ///   set (1-indexed).
    ///
    /// Used to distinguish identical twins who otherwise share name,
    /// DOB, address, and demographic data. Disagreement (e.g. `Some(1)`
    /// vs `Some(2)`) is reliable evidence the records refer to
    /// different people in the same multiple-birth set.
    #[serde(default)]
    pub multiple_birth: Option<u8>,

    /// Current residential address.
    pub address: Option<Address>,

    /// Place of birth. Modelled as an [`Address`] for FHIR
    /// (`Patient.birthPlace`) parity — typically only `city` and
    /// `country` are populated in practice. Stable over a lifetime
    /// (modulo refugee / adoption edge cases), so disagreement on a
    /// populated value is informative. Scored independently from the
    /// current `address` field.
    #[serde(default)]
    pub birth_place: Option<Address>,

    /// Place of death. Modelled as an [`Address`] (parallel to
    /// [`Worker::birth_place`]) — typically only `city` and `country`
    /// are populated. Useful for disambiguating records of deceased
    /// workers (e.g. distinguishing two people with the same name and
    /// DOB by where they died). Scored independently from `address`
    /// and `birth_place`.
    #[serde(default)]
    pub death_place: Option<Address>,

    /// Previous residential addresses. Used by the address sub-score
    /// (best-of cartesian product across `address ∪ previous_addresses`
    /// on both sides; see spec §12.4.2).
    pub previous_addresses: Vec<Address>,

    /// Passport books held by the worker. A single worker may hold
    /// passports from multiple countries simultaneously and may
    /// accumulate historical book numbers as old passports are
    /// renewed; this `Vec` carries every book ever recorded on the
    /// worker (current and historical) without privileging any
    /// particular jurisdiction. Matching treats any shared
    /// `(country, number)` pair across the two workers' lists as
    /// evidence that the records refer to the same worker, regardless
    /// of issue date. See [`PassportBook`].
    #[serde(default)]
    pub passport_books: Vec<PassportBook>,

    /// Primary phone number. Falls back to [`Self::mobile`] in scoring if absent.
    pub phone: Option<String>,

    /// Mobile phone number. Used as the fallback for [`Self::phone`].
    pub mobile: Option<String>,

    /// Email address. Not currently used in scoring (see spec task T-11).
    pub email: Option<String>,

    /// Local hospital or practice identifier. Not normalised — different
    /// organisations may issue colliding values.
    pub local_id: Option<String>,
}

impl Worker {
    /// Begin constructing a [`Worker`] with the [`WorkerBuilder`].
    ///
    /// All fields default to `None` / empty until a setter is called.
    ///
    /// # Example
    ///
    /// ```
    /// use worker_matcher::Worker;
    ///
    /// let p = Worker::builder()
    ///     .given_name("John")
    ///     .family_name("Smith")
    ///     .build();
    ///
    /// assert_eq!(p.family_name.as_deref(), Some("Smith"));
    /// ```
    pub fn builder() -> WorkerBuilder {
        WorkerBuilder::default()
    }

    /// Validate that the worker carries at least one identifying field.
    ///
    /// Returns `Ok(())` if any of the following is set: a name (`given_name`
    /// or `family_name`), or any national identifier (`uk_nhs_number`,
    /// `fr_nir`, `es_tsi`, `ie_ihi`, `uk_hc_number`). Otherwise returns
    /// [`crate::MatchingError::MissingField`].
    ///
    /// This is **not** invoked automatically by the matcher — call it at the
    /// system boundary when you ingest data, not on every comparison.
    ///
    /// # Example
    ///
    /// ```
    /// use worker_matcher::Worker;
    ///
    /// assert!(Worker::builder().given_name("Ada").build().validate().is_ok());
    /// assert!(Worker::builder().uk_nhs_number("9434765919").build().validate().is_ok());
    /// assert!(Worker::builder().ie_ihi("1234567").build().validate().is_ok());
    /// assert!(Worker::builder().us_ssn("123-45-6789").build().validate().is_ok());
    /// assert!(Worker::builder().de_kvnr("A123456780").build().validate().is_ok());
    /// assert!(
    ///     Worker::builder()
    ///         .add_passport_book(worker_matcher::PassportBook::new("GB", "123456789").unwrap())
    ///         .build()
    ///         .validate()
    ///         .is_ok()
    /// );
    /// assert!(Worker::builder().build().validate().is_err());
    /// ```
    pub fn validate(&self) -> crate::Result<()> {
        let has_name = self.given_name.is_some() || self.family_name.is_some();
        let has_identifier = self.uk_nhs_number.is_some()
            || self.fr_nir.is_some()
            || self.es_tsi.is_some()
            || self.ie_ihi.is_some()
            || self.uk_hc_number.is_some()
            || self.us_ssn.is_some()
            || self.au_ihi.is_some()
            || self.de_kvnr.is_some()
            || self.it_cf.is_some()
            || self.nl_bsn.is_some()
            || self.se_workernummer.is_some()
            || self.uk_chi_number.is_some()
            || self.be_nn.is_some()
            || self.bg_egn.is_some()
            || self.cz_rc.is_some()
            || self.dk_cpr.is_some()
            || self.ee_ik.is_some()
            || self.es_dni.is_some()
            || self.fi_hetu.is_some()
            || self.hr_oib.is_some()
            || self.is_kt.is_some()
            || self.lt_ak.is_some()
            || self.lv_pk.is_some()
            || self.mt_id.is_some()
            || self.no_fnr.is_some()
            || self.pl_pesel.is_some()
            || self.ro_cnp.is_some()
            || self.si_emso.is_some()
            || self.sk_rc.is_some()
            || self.uk_nino.is_some()
            || self.gr_dss.is_some()
            || self.li_id.is_some()
            || self.nl_id.is_some()
            || self.pl_nip.is_some()
            || self.pt_nif.is_some()
            || self.br_cpf.is_some()
            || self.cn_rrn.is_some()
            || self.in_aadhaar.is_some()
            || self.jp_my_number.is_some()
            || self.mx_curp.is_some()
            || self.nz_nhi.is_some()
            || self.za_id.is_some()
            || !self.passport_books.is_empty();
        if !has_name && !has_identifier {
            return Err(crate::MatchingError::MissingField(
                "At least one of: a name, a national identifier (any of 30 supported schemes), or at least one passport book is required"
                    .to_string(),
            ));
        }
        Ok(())
    }
}

/// Fluent builder for [`Worker`].
///
/// All setters accept `impl Into<String>` so call-sites may pass `&str`,
/// `String`, or `&String` interchangeably without explicit conversion.
///
/// # Example
///
/// ```
/// use worker_matcher::{Gender, Worker, WorkerBuilder};
/// use chrono::NaiveDate;
///
/// let p: Worker = WorkerBuilder::default()
///     .uk_nhs_number("9434765919")
///     .given_name(String::from("Owen"))   // owned String
///     .family_name("Williams")            // &str
///     .date_of_birth(NaiveDate::from_ymd_opt(1972, 11, 4).unwrap())
///     .gender(Gender::Male)
///     .build();
///
/// assert_eq!(p.uk_nhs_number.as_deref(), Some("9434765919"));
/// ```
#[derive(Default)]
pub struct WorkerBuilder {
    uk_nhs_number: Option<String>,
    fr_nir: Option<String>,
    es_tsi: Option<String>,
    ie_ihi: Option<String>,
    uk_hc_number: Option<String>,
    us_ssn: Option<String>,
    au_ihi: Option<String>,
    de_kvnr: Option<String>,
    it_cf: Option<String>,
    nl_bsn: Option<String>,
    se_workernummer: Option<String>,
    uk_chi_number: Option<String>,
    be_nn: Option<String>,
    bg_egn: Option<String>,
    cz_rc: Option<String>,
    dk_cpr: Option<String>,
    ee_ik: Option<String>,
    es_dni: Option<String>,
    fi_hetu: Option<String>,
    hr_oib: Option<String>,
    is_kt: Option<String>,
    lt_ak: Option<String>,
    lv_pk: Option<String>,
    mt_id: Option<String>,
    no_fnr: Option<String>,
    pl_pesel: Option<String>,
    ro_cnp: Option<String>,
    si_emso: Option<String>,
    sk_rc: Option<String>,
    uk_nino: Option<String>,
    gr_dss: Option<String>,
    li_id: Option<String>,
    nl_id: Option<String>,
    pl_nip: Option<String>,
    pt_nif: Option<String>,
    br_cpf: Option<String>,
    cn_rrn: Option<String>,
    in_aadhaar: Option<String>,
    jp_my_number: Option<String>,
    mx_curp: Option<String>,
    nz_nhi: Option<String>,
    za_id: Option<String>,
    given_name: Option<String>,
    middle_name: Option<String>,
    family_name: Option<String>,
    date_of_birth: Option<NaiveDate>,
    death_date: Option<NaiveDate>,
    gender: Option<Gender>,
    blood_type: Option<BloodType>,
    multiple_birth: Option<u8>,
    address: Option<Address>,
    birth_place: Option<Address>,
    death_place: Option<Address>,
    previous_addresses: Vec<Address>,
    passport_books: Vec<PassportBook>,
    phone: Option<String>,
    mobile: Option<String>,
    email: Option<String>,
    local_id: Option<String>,
}

impl WorkerBuilder {
    /// Set the United Kingdom NHS Number (England, Wales, Isle of Man).
    ///
    /// The string is stored verbatim; normalisation and validation happen at
    /// match time via [`crate::identifiers::parse_uk_nhs_number`]. Whitespace
    /// in the canonical `"XXX XXX XXXX"` layout is permitted.
    ///
    /// ```
    /// # use worker_matcher::Worker;
    /// let p = Worker::builder().uk_nhs_number("943 476 5919").build();
    /// assert_eq!(p.uk_nhs_number.as_deref(), Some("943 476 5919"));
    /// ```
    pub fn uk_nhs_number<S: Into<String>>(mut self, value: S) -> Self {
        self.uk_nhs_number = Some(value.into());
        self
    }

    /// Set the France NIR (*Numéro d'Inscription au Répertoire*).
    ///
    /// The 15-character national identifier. Stored verbatim; parsing happens
    /// at match time via [`crate::identifiers::parse_fr_nir`].
    ///
    /// ```
    /// # use worker_matcher::Worker;
    /// let p = Worker::builder().fr_nir("180127512345642").build();
    /// assert_eq!(p.fr_nir.as_deref(), Some("180127512345642"));
    /// ```
    pub fn fr_nir<S: Into<String>>(mut self, value: S) -> Self {
        self.fr_nir = Some(value.into());
        self
    }

    /// Set the España (Spain) TSI (*Tarjeta Sanitaria Individual*) / CIP-SNS
    /// identifier.
    ///
    /// Stored verbatim; parsing happens at match time via
    /// [`crate::identifiers::parse_es_tsi`].
    ///
    /// ```
    /// # use worker_matcher::Worker;
    /// let p = Worker::builder().es_tsi("ABCD123456XY1234").build();
    /// assert_eq!(p.es_tsi.as_deref(), Some("ABCD123456XY1234"));
    /// ```
    pub fn es_tsi<S: Into<String>>(mut self, value: S) -> Self {
        self.es_tsi = Some(value.into());
        self
    }

    /// Set the Éire (Ireland) IHI (Individual Health Identifier).
    ///
    /// The 7-digit identifier. Stored verbatim; parsing happens at match time
    /// via [`crate::identifiers::parse_ie_ihi`].
    ///
    /// ```
    /// # use worker_matcher::Worker;
    /// let p = Worker::builder().ie_ihi("1234567").build();
    /// assert_eq!(p.ie_ihi.as_deref(), Some("1234567"));
    /// ```
    pub fn ie_ihi<S: Into<String>>(mut self, value: S) -> Self {
        self.ie_ihi = Some(value.into());
        self
    }

    /// Set the United Kingdom Northern Ireland H&C (Health and Care) Number.
    ///
    /// A 10-digit Modulus-11 identifier sharing the NHS Number algorithm.
    /// Stored verbatim; parsing happens at match time via
    /// [`crate::identifiers::parse_uk_hc_number`].
    ///
    /// ```
    /// # use worker_matcher::Worker;
    /// let p = Worker::builder().uk_hc_number("9434765919").build();
    /// assert_eq!(p.uk_hc_number.as_deref(), Some("9434765919"));
    /// ```
    pub fn uk_hc_number<S: Into<String>>(mut self, value: S) -> Self {
        self.uk_hc_number = Some(value.into());
        self
    }

    /// Set the United States Social Security Number (SSN).
    ///
    /// A 9-digit identifier issued by the Social Security Administration.
    /// Stored verbatim; parsing happens at match time via
    /// [`crate::identifiers::parse_us_ssn`]. The canonical
    /// `"AAA-GG-SSSS"` layout and the compact `"AAAGGSSSS"` layout are
    /// equivalent under parsing.
    ///
    /// ```
    /// # use worker_matcher::Worker;
    /// let p = Worker::builder().us_ssn("123-45-6789").build();
    /// assert_eq!(p.us_ssn.as_deref(), Some("123-45-6789"));
    /// ```
    pub fn us_ssn<S: Into<String>>(mut self, value: S) -> Self {
        self.us_ssn = Some(value.into());
        self
    }

    /// Set the Australia IHI (Individual Healthcare Identifier).
    ///
    /// 16-digit identifier with a Luhn check, conforming to ISO/IEC
    /// 7812-1. Stored verbatim; parsing happens at match time via
    /// [`crate::identifiers::parse_au_ihi`].
    ///
    /// ```
    /// # use worker_matcher::Worker;
    /// let p = Worker::builder().au_ihi("8003601234567894").build();
    /// assert_eq!(p.au_ihi.as_deref(), Some("8003601234567894"));
    /// ```
    pub fn au_ihi<S: Into<String>>(mut self, value: S) -> Self {
        self.au_ihi = Some(value.into());
        self
    }

    /// Set the Germany KVNR (*Krankenversichertennummer*).
    ///
    /// 10-character (1 letter + 9 digits) lifelong health-insurance
    /// number with a Mod-10 check. Stored verbatim; parsing happens at
    /// match time via [`crate::identifiers::parse_de_kvnr`].
    ///
    /// ```
    /// # use worker_matcher::Worker;
    /// let p = Worker::builder().de_kvnr("A123456780").build();
    /// assert_eq!(p.de_kvnr.as_deref(), Some("A123456780"));
    /// ```
    pub fn de_kvnr<S: Into<String>>(mut self, value: S) -> Self {
        self.de_kvnr = Some(value.into());
        self
    }

    /// Set the Italy *Codice Fiscale* (CF).
    ///
    /// 16-character alphanumeric tax identifier with a Mod-26 check
    /// character. Stored verbatim; parsing happens at match time via
    /// [`crate::identifiers::parse_it_cf`].
    ///
    /// ```
    /// # use worker_matcher::Worker;
    /// let p = Worker::builder().it_cf("RSSMRA85T10A562S").build();
    /// assert_eq!(p.it_cf.as_deref(), Some("RSSMRA85T10A562S"));
    /// ```
    pub fn it_cf<S: Into<String>>(mut self, value: S) -> Self {
        self.it_cf = Some(value.into());
        self
    }

    /// Set the Netherlands BSN (*Burgerservicenummer*).
    ///
    /// 9-digit citizen-service number with the "11-test" check rule.
    /// Stored verbatim; parsing happens at match time via
    /// [`crate::identifiers::parse_nl_bsn`].
    ///
    /// ```
    /// # use worker_matcher::Worker;
    /// let p = Worker::builder().nl_bsn("111222333").build();
    /// assert_eq!(p.nl_bsn.as_deref(), Some("111222333"));
    /// ```
    pub fn nl_bsn<S: Into<String>>(mut self, value: S) -> Self {
        self.nl_bsn = Some(value.into());
        self
    }

    /// Set the Sweden *Workernummer*.
    ///
    /// 10- or 12-digit workeral identity number with a Luhn check
    /// computed over the 10-digit form. Stored verbatim; parsing happens
    /// at match time via [`crate::identifiers::parse_se_workernummer`].
    ///
    /// ```
    /// # use worker_matcher::Worker;
    /// let p = Worker::builder().se_workernummer("19460324-3850").build();
    /// assert_eq!(p.se_workernummer.as_deref(), Some("19460324-3850"));
    /// ```
    pub fn se_workernummer<S: Into<String>>(mut self, value: S) -> Self {
        self.se_workernummer = Some(value.into());
        self
    }

    /// Set the United Kingdom (Scotland) CHI Number (Community Health Index).
    ///
    /// 10-digit identifier issued by NHS Scotland, sharing the Mod-11
    /// algorithm of the NHS Number but scheme-local. Stored verbatim;
    /// parsing happens at match time via
    /// [`crate::identifiers::parse_uk_chi_number`].
    ///
    /// ```
    /// # use worker_matcher::Worker;
    /// let p = Worker::builder().uk_chi_number("0101701233").build();
    /// assert_eq!(p.uk_chi_number.as_deref(), Some("0101701233"));
    /// ```
    pub fn uk_chi_number<S: Into<String>>(mut self, value: S) -> Self {
        self.uk_chi_number = Some(value.into());
        self
    }

    /// Set the Belgium National Number (*Rijksregisternummer*). 11 digits, Mod-97.
    pub fn be_nn<S: Into<String>>(mut self, value: S) -> Self {
        self.be_nn = Some(value.into());
        self
    }

    /// Set the Bulgaria EGN (*Edinen grazhdanski nomer*). 10 digits, weighted Mod-11.
    pub fn bg_egn<S: Into<String>>(mut self, value: S) -> Self {
        self.bg_egn = Some(value.into());
        self
    }

    /// Set the Czech Republic *Rodné číslo*. 9 or 10 digits.
    pub fn cz_rc<S: Into<String>>(mut self, value: S) -> Self {
        self.cz_rc = Some(value.into());
        self
    }

    /// Set the Denmark CPR (*Centrale Workerregister*). 10 digits.
    pub fn dk_cpr<S: Into<String>>(mut self, value: S) -> Self {
        self.dk_cpr = Some(value.into());
        self
    }

    /// Set the Estonia *Isikukood* (Workeral Identification Code). 11 digits.
    pub fn ee_ik<S: Into<String>>(mut self, value: S) -> Self {
        self.ee_ik = Some(value.into());
        self
    }

    /// Set the Spain DNI / NIE. 8 digits + Mod-23 letter.
    pub fn es_dni<S: Into<String>>(mut self, value: S) -> Self {
        self.es_dni = Some(value.into());
        self
    }

    /// Set the Finland HETU (*Henkilötunnus*). 11 chars with century sign.
    pub fn fi_hetu<S: Into<String>>(mut self, value: S) -> Self {
        self.fi_hetu = Some(value.into());
        self
    }

    /// Set the Croatia OIB (*Osobni identifikacijski broj*). 11 digits.
    pub fn hr_oib<S: Into<String>>(mut self, value: S) -> Self {
        self.hr_oib = Some(value.into());
        self
    }

    /// Set the Iceland *Kennitala*. 10 digits.
    pub fn is_kt<S: Into<String>>(mut self, value: S) -> Self {
        self.is_kt = Some(value.into());
        self
    }

    /// Set the Lithuania *Asmens kodas*. 11 digits.
    pub fn lt_ak<S: Into<String>>(mut self, value: S) -> Self {
        self.lt_ak = Some(value.into());
        self
    }

    /// Set the Latvia *Workeras kods*. 11 digits.
    pub fn lv_pk<S: Into<String>>(mut self, value: S) -> Self {
        self.lv_pk = Some(value.into());
        self
    }

    /// Set the Malta National ID. 7 digits + letter.
    pub fn mt_id<S: Into<String>>(mut self, value: S) -> Self {
        self.mt_id = Some(value.into());
        self
    }

    /// Set the Norway *Fødselsnummer*. 11 digits, dual Mod-11.
    pub fn no_fnr<S: Into<String>>(mut self, value: S) -> Self {
        self.no_fnr = Some(value.into());
        self
    }

    /// Set the Poland PESEL. 11 digits, weighted Mod-10.
    pub fn pl_pesel<S: Into<String>>(mut self, value: S) -> Self {
        self.pl_pesel = Some(value.into());
        self
    }

    /// Set the Romania CNP (*Cod Numeric Workeral*). 13 digits.
    pub fn ro_cnp<S: Into<String>>(mut self, value: S) -> Self {
        self.ro_cnp = Some(value.into());
        self
    }

    /// Set the Slovenia EMŠO (*Enotna Matična Številka Občana*). 13 digits.
    pub fn si_emso<S: Into<String>>(mut self, value: S) -> Self {
        self.si_emso = Some(value.into());
        self
    }

    /// Set the Slovakia *Rodné číslo*. 9 or 10 digits.
    pub fn sk_rc<S: Into<String>>(mut self, value: S) -> Self {
        self.sk_rc = Some(value.into());
        self
    }

    /// Set the United Kingdom National Insurance Number (NINO).
    pub fn uk_nino<S: Into<String>>(mut self, value: S) -> Self {
        self.uk_nino = Some(value.into());
        self
    }

    /// Set the Greece DSS investor share code. 10 digits.
    pub fn gr_dss<S: Into<String>>(mut self, value: S) -> Self {
        self.gr_dss = Some(value.into());
        self
    }

    /// Set the Liechtenstein National Identity Card Number. 2 letters + 8 digits.
    pub fn li_id<S: Into<String>>(mut self, value: S) -> Self {
        self.li_id = Some(value.into());
        self
    }

    /// Set the Netherlands National Identity Card Number. 9 chars per spec.
    pub fn nl_id<S: Into<String>>(mut self, value: S) -> Self {
        self.nl_id = Some(value.into());
        self
    }

    /// Set the Poland NIP (*Numer Identyfikacji Podatkowej*). 10 digits, weighted Mod-11.
    pub fn pl_nip<S: Into<String>>(mut self, value: S) -> Self {
        self.pl_nip = Some(value.into());
        self
    }

    /// Set the Portugal NIF (*Número de Identificação Fiscal*). 9 digits, weighted Mod-11.
    pub fn pt_nif<S: Into<String>>(mut self, value: S) -> Self {
        self.pt_nif = Some(value.into());
        self
    }

    /// Set the Brazil CPF (*Cadastro de Pessoas Físicas*). 11 digits, two Mod-11 check digits.
    pub fn br_cpf<S: Into<String>>(mut self, value: S) -> Self {
        self.br_cpf = Some(value.into());
        self
    }

    /// Set the China Resident Identity Card number (*居民身份证*). 18 chars, weighted Mod-11 + date substring.
    pub fn cn_rrn<S: Into<String>>(mut self, value: S) -> Self {
        self.cn_rrn = Some(value.into());
        self
    }

    /// Set the India Aadhaar number. 12 digits, Verhoeff check digit.
    pub fn in_aadhaar<S: Into<String>>(mut self, value: S) -> Self {
        self.in_aadhaar = Some(value.into());
        self
    }

    /// Set the Japan My Number (*個人番号*). 12 digits, weighted Mod-11 check digit.
    pub fn jp_my_number<S: Into<String>>(mut self, value: S) -> Self {
        self.jp_my_number = Some(value.into());
        self
    }

    /// Set the Mexico CURP. 18 alphanumeric chars, structural + Mod-10 check digit.
    pub fn mx_curp<S: Into<String>>(mut self, value: S) -> Self {
        self.mx_curp = Some(value.into());
        self
    }

    /// Set the New Zealand NHI Number. Original 7-char format (3 letters + 4 digits).
    pub fn nz_nhi<S: Into<String>>(mut self, value: S) -> Self {
        self.nz_nhi = Some(value.into());
        self
    }

    /// Set the South Africa ID Number. 13 digits, Luhn + date substring.
    pub fn za_id<S: Into<String>>(mut self, value: S) -> Self {
        self.za_id = Some(value.into());
        self
    }

    /// Set the given name (forename).
    ///
    /// ```
    /// # use worker_matcher::Worker;
    /// let p = Worker::builder().given_name("Carys").build();
    /// assert_eq!(p.given_name.as_deref(), Some("Carys"));
    /// ```
    pub fn given_name<S: Into<String>>(mut self, value: S) -> Self {
        self.given_name = Some(value.into());
        self
    }

    /// Set the middle name(s).
    ///
    /// Stored on the worker but not currently used in matching scoring
    /// (see spec OQ-1).
    ///
    /// ```
    /// # use worker_matcher::Worker;
    /// let p = Worker::builder().middle_name("Eleri").build();
    /// assert_eq!(p.middle_name.as_deref(), Some("Eleri"));
    /// ```
    pub fn middle_name<S: Into<String>>(mut self, value: S) -> Self {
        self.middle_name = Some(value.into());
        self
    }

    /// Set the family name (surname).
    ///
    /// ```
    /// # use worker_matcher::Worker;
    /// let p = Worker::builder().family_name("Pritchard").build();
    /// assert_eq!(p.family_name.as_deref(), Some("Pritchard"));
    /// ```
    pub fn family_name<S: Into<String>>(mut self, value: S) -> Self {
        self.family_name = Some(value.into());
        self
    }

    /// Set the date of birth.
    ///
    /// ```
    /// # use worker_matcher::Worker;
    /// use chrono::NaiveDate;
    /// let dob = NaiveDate::from_ymd_opt(1990, 1, 1).unwrap();
    /// let p = Worker::builder().date_of_birth(dob).build();
    /// assert_eq!(p.date_of_birth, Some(dob));
    /// ```
    pub fn date_of_birth(mut self, value: NaiveDate) -> Self {
        self.date_of_birth = Some(value);
        self
    }

    /// Set the date of death (FHIR `Patient.deceasedDateTime`).
    ///
    /// ```
    /// # use worker_matcher::Worker;
    /// use chrono::NaiveDate;
    /// let dod = NaiveDate::from_ymd_opt(2024, 6, 30).unwrap();
    /// let p = Worker::builder().death_date(dod).build();
    /// assert_eq!(p.death_date, Some(dod));
    /// ```
    pub fn death_date(mut self, value: NaiveDate) -> Self {
        self.death_date = Some(value);
        self
    }

    /// Set the recorded gender.
    ///
    /// ```
    /// # use worker_matcher::{Gender, Worker};
    /// let p = Worker::builder().gender(Gender::Female).build();
    /// assert_eq!(p.gender, Some(Gender::Female));
    /// ```
    pub fn gender(mut self, value: Gender) -> Self {
        self.gender = Some(value);
        self
    }

    /// Set the recorded ABO+RhD blood type.
    ///
    /// ```
    /// # use worker_matcher::{BloodType, Worker};
    /// let p = Worker::builder().blood_type(BloodType::OPositive).build();
    /// assert_eq!(p.blood_type, Some(BloodType::OPositive));
    /// ```
    pub fn blood_type(mut self, value: BloodType) -> Self {
        self.blood_type = Some(value);
        self
    }

    /// Set the multiple-birth indicator (FHIR `Patient.multipleBirth`).
    ///
    /// The value is the 1-indexed birth order within a multiple-birth
    /// set: `1` for the first born, `2` for the second, and so on.
    /// `0` is conventionally not used; consumers should pass `None`
    /// (do not call this setter) for singletons or unknown values.
    ///
    /// ```
    /// # use worker_matcher::Worker;
    /// // First of identical twins.
    /// let p = Worker::builder().multiple_birth(1).build();
    /// assert_eq!(p.multiple_birth, Some(1));
    /// ```
    pub fn multiple_birth(mut self, value: u8) -> Self {
        self.multiple_birth = Some(value);
        self
    }

    /// Set the current residential address.
    ///
    /// ```
    /// # use worker_matcher::{Address, Worker};
    /// let mut a = Address::new();
    /// a.postcode = Some("CF10 1AA".into());
    /// let p = Worker::builder().address(a).build();
    /// assert_eq!(p.address.unwrap().postcode.as_deref(), Some("CF10 1AA"));
    /// ```
    pub fn address(mut self, value: Address) -> Self {
        self.address = Some(value);
        self
    }

    /// Set the place of birth (FHIR `Patient.birthPlace`).
    ///
    /// Typically only [`Address::city`] and [`Address::country`] are
    /// populated for a birth place.
    ///
    /// ```
    /// # use worker_matcher::{Address, Worker};
    /// let p = Worker::builder()
    ///     .birth_place(Address::new().with_city("Cardiff").with_country("Wales"))
    ///     .build();
    /// assert_eq!(p.birth_place.as_ref().unwrap().city.as_deref(), Some("Cardiff"));
    /// ```
    pub fn birth_place(mut self, value: Address) -> Self {
        self.birth_place = Some(value);
        self
    }

    /// Set the place of death.
    ///
    /// Modelled as an [`Address`] for parity with [`Self::birth_place`]
    /// — typically only [`Address::city`] and [`Address::country`] are
    /// populated.
    ///
    /// ```
    /// # use worker_matcher::{Address, Worker};
    /// let p = Worker::builder()
    ///     .death_place(Address::new().with_city("Glasgow").with_country("Scotland"))
    ///     .build();
    /// assert_eq!(p.death_place.as_ref().unwrap().city.as_deref(), Some("Glasgow"));
    /// ```
    pub fn death_place(mut self, value: Address) -> Self {
        self.death_place = Some(value);
        self
    }

    /// Set the list of previous addresses. Used by the address
    /// sub-score (best-of cartesian product, see spec §12.4.2).
    ///
    /// ```
    /// # use worker_matcher::{Address, Worker};
    /// let p = Worker::builder()
    ///     .previous_addresses(vec![Address::new(), Address::new()])
    ///     .build();
    /// assert_eq!(p.previous_addresses.len(), 2);
    /// ```
    pub fn previous_addresses(mut self, value: Vec<Address>) -> Self {
        self.previous_addresses = value;
        self
    }

    /// Append a single passport book to the worker's list. Chainable;
    /// call multiple times to record multi-country or historical
    /// books.
    ///
    /// ```
    /// # use worker_matcher::{PassportBook, Worker};
    /// let p = Worker::builder()
    ///     .add_passport_book(PassportBook::new("GB", "123456789").unwrap())
    ///     .add_passport_book(PassportBook::new("US", "AB1234567").unwrap())
    ///     .build();
    /// assert_eq!(p.passport_books.len(), 2);
    /// ```
    pub fn add_passport_book(mut self, book: PassportBook) -> Self {
        self.passport_books.push(book);
        self
    }

    /// Replace the entire passport-book list.
    ///
    /// ```
    /// # use worker_matcher::{PassportBook, Worker};
    /// let books = vec![PassportBook::new("GB", "123456789").unwrap()];
    /// let p = Worker::builder().passport_books(books).build();
    /// assert_eq!(p.passport_books.len(), 1);
    /// ```
    pub fn passport_books(mut self, value: Vec<PassportBook>) -> Self {
        self.passport_books = value;
        self
    }

    /// Set the primary phone number.
    ///
    /// ```
    /// # use worker_matcher::Worker;
    /// let p = Worker::builder().phone("029 2034 5678").build();
    /// assert_eq!(p.phone.as_deref(), Some("029 2034 5678"));
    /// ```
    pub fn phone<S: Into<String>>(mut self, value: S) -> Self {
        self.phone = Some(value.into());
        self
    }

    /// Set the mobile phone number. Used as a fallback when `phone` is absent.
    ///
    /// ```
    /// # use worker_matcher::Worker;
    /// let p = Worker::builder().mobile("07700 900123").build();
    /// assert_eq!(p.mobile.as_deref(), Some("07700 900123"));
    /// ```
    pub fn mobile<S: Into<String>>(mut self, value: S) -> Self {
        self.mobile = Some(value.into());
        self
    }

    /// Set the email address. Not currently used in scoring.
    ///
    /// ```
    /// # use worker_matcher::Worker;
    /// let p = Worker::builder().email("alice@example.org").build();
    /// assert_eq!(p.email.as_deref(), Some("alice@example.org"));
    /// ```
    pub fn email<S: Into<String>>(mut self, value: S) -> Self {
        self.email = Some(value.into());
        self
    }

    /// Set the local hospital or practice identifier.
    ///
    /// ```
    /// # use worker_matcher::Worker;
    /// let p = Worker::builder().local_id("MRN-12345").build();
    /// assert_eq!(p.local_id.as_deref(), Some("MRN-12345"));
    /// ```
    pub fn local_id<S: Into<String>>(mut self, value: S) -> Self {
        self.local_id = Some(value.into());
        self
    }

    /// Consume the builder and produce the [`Worker`].
    ///
    /// ```
    /// # use worker_matcher::Worker;
    /// let p = Worker::builder().given_name("Eira").build();
    /// assert!(p.family_name.is_none());
    /// ```
    pub fn build(self) -> Worker {
        Worker {
            uk_nhs_number: self.uk_nhs_number,
            fr_nir: self.fr_nir,
            es_tsi: self.es_tsi,
            ie_ihi: self.ie_ihi,
            uk_hc_number: self.uk_hc_number,
            us_ssn: self.us_ssn,
            au_ihi: self.au_ihi,
            de_kvnr: self.de_kvnr,
            it_cf: self.it_cf,
            nl_bsn: self.nl_bsn,
            se_workernummer: self.se_workernummer,
            uk_chi_number: self.uk_chi_number,
            be_nn: self.be_nn,
            bg_egn: self.bg_egn,
            cz_rc: self.cz_rc,
            dk_cpr: self.dk_cpr,
            ee_ik: self.ee_ik,
            es_dni: self.es_dni,
            fi_hetu: self.fi_hetu,
            hr_oib: self.hr_oib,
            is_kt: self.is_kt,
            lt_ak: self.lt_ak,
            lv_pk: self.lv_pk,
            mt_id: self.mt_id,
            no_fnr: self.no_fnr,
            pl_pesel: self.pl_pesel,
            ro_cnp: self.ro_cnp,
            si_emso: self.si_emso,
            sk_rc: self.sk_rc,
            uk_nino: self.uk_nino,
            gr_dss: self.gr_dss,
            li_id: self.li_id,
            nl_id: self.nl_id,
            pl_nip: self.pl_nip,
            pt_nif: self.pt_nif,
            br_cpf: self.br_cpf,
            cn_rrn: self.cn_rrn,
            in_aadhaar: self.in_aadhaar,
            jp_my_number: self.jp_my_number,
            mx_curp: self.mx_curp,
            nz_nhi: self.nz_nhi,
            za_id: self.za_id,
            given_name: self.given_name,
            middle_name: self.middle_name,
            family_name: self.family_name,
            date_of_birth: self.date_of_birth,
            death_date: self.death_date,
            gender: self.gender,
            blood_type: self.blood_type,
            multiple_birth: self.multiple_birth,
            address: self.address,
            birth_place: self.birth_place,
            death_place: self.death_place,
            previous_addresses: self.previous_addresses,
            passport_books: self.passport_books,
            phone: self.phone,
            mobile: self.mobile,
            email: self.email,
            local_id: self.local_id,
        }
    }
}

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

    #[test]
    fn address_new_is_all_none() {
        let a = Address::new();
        assert!(a.line1.is_none());
        assert!(a.line2.is_none());
        assert!(a.city.is_none());
        assert!(a.county.is_none());
        assert!(a.postcode.is_none());
        assert!(a.country.is_none());
    }

    #[test]
    fn address_default_matches_new() {
        assert_eq!(Address::default(), Address::new());
    }

    #[test]
    fn address_fluent_builders_chain() {
        let a = Address::new()
            .with_line1("10 Downing Street")
            .with_city("London")
            .with_postcode("SW1A 2AA")
            .with_country("United Kingdom");
        assert_eq!(a.line1.as_deref(), Some("10 Downing Street"));
        assert_eq!(a.city.as_deref(), Some("London"));
        assert_eq!(a.postcode.as_deref(), Some("SW1A 2AA"));
        assert_eq!(a.country.as_deref(), Some("United Kingdom"));
        assert!(a.line2.is_none());
        assert!(a.county.is_none());
    }

    #[test]
    fn address_round_trips_through_serde() {
        let mut a = Address::new();
        a.line1 = Some("123 High Street".into());
        a.postcode = Some("CF10 1AA".into());
        let json = serde_json::to_string(&a).expect("serialise");
        let back: Address = serde_json::from_str(&json).expect("deserialise");
        assert_eq!(a, back);
    }

    #[test]
    fn worker_builder_starts_empty() {
        let p = Worker::builder().build();
        assert!(p.uk_nhs_number.is_none());
        assert!(p.fr_nir.is_none());
        assert!(p.es_tsi.is_none());
        assert!(p.ie_ihi.is_none());
        assert!(p.uk_hc_number.is_none());
        assert!(p.us_ssn.is_none());
        assert!(p.au_ihi.is_none());
        assert!(p.de_kvnr.is_none());
        assert!(p.it_cf.is_none());
        assert!(p.nl_bsn.is_none());
        assert!(p.se_workernummer.is_none());
        assert!(p.uk_chi_number.is_none());
        assert!(p.given_name.is_none());
        assert!(p.family_name.is_none());
        assert!(p.date_of_birth.is_none());
        assert!(p.gender.is_none());
        assert!(p.address.is_none());
        assert!(p.previous_addresses.is_empty());
        assert!(p.passport_books.is_empty());
        assert!(p.phone.is_none());
        assert!(p.mobile.is_none());
        assert!(p.email.is_none());
        assert!(p.local_id.is_none());
    }

    #[test]
    fn worker_builder_carries_all_national_identifiers() {
        let p = Worker::builder()
            .uk_nhs_number("9434765919")
            .fr_nir("180127512345642")
            .es_tsi("ABCD123456XY1234")
            .ie_ihi("1234567")
            .uk_hc_number("9434765919")
            .us_ssn("123-45-6789")
            .au_ihi("8003601234567894")
            .de_kvnr("A123456780")
            .it_cf("RSSMRA85T10A562S")
            .nl_bsn("111222333")
            .se_workernummer("4603243850")
            .uk_chi_number("0101701233")
            .build();
        assert_eq!(p.uk_nhs_number.as_deref(), Some("9434765919"));
        assert_eq!(p.fr_nir.as_deref(), Some("180127512345642"));
        assert_eq!(p.es_tsi.as_deref(), Some("ABCD123456XY1234"));
        assert_eq!(p.ie_ihi.as_deref(), Some("1234567"));
        assert_eq!(p.uk_hc_number.as_deref(), Some("9434765919"));
        assert_eq!(p.us_ssn.as_deref(), Some("123-45-6789"));
        assert_eq!(p.au_ihi.as_deref(), Some("8003601234567894"));
        assert_eq!(p.de_kvnr.as_deref(), Some("A123456780"));
        assert_eq!(p.it_cf.as_deref(), Some("RSSMRA85T10A562S"));
        assert_eq!(p.nl_bsn.as_deref(), Some("111222333"));
        assert_eq!(p.se_workernummer.as_deref(), Some("4603243850"));
        assert_eq!(p.uk_chi_number.as_deref(), Some("0101701233"));
    }

    #[test]
    fn worker_builder_accepts_str_and_string() {
        let p = Worker::builder()
            .given_name("Owen") // &str
            .family_name(String::from("Jones")) // String
            .build();
        assert_eq!(p.given_name.as_deref(), Some("Owen"));
        assert_eq!(p.family_name.as_deref(), Some("Jones"));
    }

    #[test]
    fn worker_validate_requires_one_of_three_fields() {
        assert!(Worker::builder().given_name("a").build().validate().is_ok());
        assert!(
            Worker::builder()
                .family_name("a")
                .build()
                .validate()
                .is_ok()
        );
        assert!(
            Worker::builder()
                .uk_nhs_number("9434765919")
                .build()
                .validate()
                .is_ok()
        );
        let err = Worker::builder()
            .build()
            .validate()
            .expect_err("should be missing");
        assert!(matches!(err, crate::MatchingError::MissingField(_)));
    }

    #[test]
    fn worker_round_trips_through_serde() {
        let p = Worker::builder()
            .uk_nhs_number("9434765919")
            .given_name("Carys")
            .family_name("Pritchard")
            .date_of_birth(chrono::NaiveDate::from_ymd_opt(1990, 6, 1).unwrap())
            .gender(Gender::Female)
            .build();
        let json = serde_json::to_string(&p).expect("serialise");
        let back: Worker = serde_json::from_str(&json).expect("deserialise");
        assert_eq!(p, back);
    }

    // ---------- BloodType ----------

    #[test]
    fn blood_type_parses_canonical_short_forms() {
        for (s, want) in [
            ("A+", BloodType::APositive),
            ("A-", BloodType::ANegative),
            ("B+", BloodType::BPositive),
            ("B-", BloodType::BNegative),
            ("AB+", BloodType::ABPositive),
            ("AB-", BloodType::ABNegative),
            ("O+", BloodType::OPositive),
            ("O-", BloodType::ONegative),
        ] {
            assert_eq!(BloodType::parse(s), Some(want), "parse {s:?}");
        }
    }

    #[test]
    fn blood_type_parses_lowercase_and_whitespace() {
        assert_eq!(BloodType::parse("  a+ "), Some(BloodType::APositive));
        assert_eq!(BloodType::parse("ab-"), Some(BloodType::ABNegative));
    }

    #[test]
    fn blood_type_parses_word_forms() {
        assert_eq!(BloodType::parse("A positive"), Some(BloodType::APositive));
        assert_eq!(BloodType::parse("A pos"), Some(BloodType::APositive));
        assert_eq!(BloodType::parse("A POS"), Some(BloodType::APositive));
        assert_eq!(BloodType::parse("A negative"), Some(BloodType::ANegative));
        assert_eq!(BloodType::parse("ab neg"), Some(BloodType::ABNegative));
        assert_eq!(BloodType::parse("o NEG"), Some(BloodType::ONegative));
    }

    #[test]
    fn blood_type_parses_zero_as_o() {
        assert_eq!(BloodType::parse("0+"), Some(BloodType::OPositive));
        assert_eq!(BloodType::parse("0-"), Some(BloodType::ONegative));
    }

    #[test]
    fn blood_type_parses_with_separator() {
        assert_eq!(BloodType::parse("A_pos"), Some(BloodType::APositive));
        assert_eq!(BloodType::parse("A-neg"), Some(BloodType::ANegative));
        assert_eq!(BloodType::parse("AB +"), Some(BloodType::ABPositive));
    }

    #[test]
    fn blood_type_parses_ve_suffix() {
        assert_eq!(BloodType::parse("A+VE"), Some(BloodType::APositive));
        assert_eq!(BloodType::parse("a-ve"), Some(BloodType::ANegative));
    }

    #[test]
    fn blood_type_rejects_unparseable() {
        assert_eq!(BloodType::parse(""), None);
        assert_eq!(BloodType::parse("   "), None);
        assert_eq!(BloodType::parse("Z+"), None);
        assert_eq!(BloodType::parse("A"), None); // no sign
        assert_eq!(BloodType::parse("Bombay"), None);
        assert_eq!(BloodType::parse("A++"), None);
    }

    #[test]
    fn blood_type_as_str_and_display_round_trip() {
        for bt in [
            BloodType::APositive,
            BloodType::ANegative,
            BloodType::BPositive,
            BloodType::BNegative,
            BloodType::ABPositive,
            BloodType::ABNegative,
            BloodType::OPositive,
            BloodType::ONegative,
        ] {
            let s = bt.as_str();
            assert_eq!(format!("{bt}"), s);
            assert_eq!(BloodType::parse(s), Some(bt));
        }
    }

    #[test]
    fn blood_type_serde_uses_short_form() {
        for (bt, json) in [
            (BloodType::APositive, "\"A+\""),
            (BloodType::ABNegative, "\"AB-\""),
            (BloodType::ONegative, "\"O-\""),
        ] {
            assert_eq!(serde_json::to_string(&bt).unwrap(), json);
            let back: BloodType = serde_json::from_str(json).unwrap();
            assert_eq!(back, bt);
        }
    }

    #[test]
    fn worker_builder_sets_blood_type() {
        let p = Worker::builder().blood_type(BloodType::OPositive).build();
        assert_eq!(p.blood_type, Some(BloodType::OPositive));
    }

    #[test]
    fn worker_default_has_no_blood_type() {
        let p = Worker::builder().build();
        assert!(p.blood_type.is_none());
    }

    #[test]
    fn gender_is_copy_and_eq() {
        let g = Gender::Female;
        let h = g; // Copy
        assert_eq!(g, h);
        assert_ne!(g, Gender::Male);
    }

    // ---------- PassportBook ----------

    #[test]
    fn passport_book_new_canonicalises_country_and_number() {
        let b = PassportBook::new("  gb  ", " 123 ABC 789 ").unwrap();
        assert_eq!(b.country, "GB");
        assert_eq!(b.number, "123ABC789");
    }

    #[test]
    fn passport_book_new_strips_common_separators() {
        // Hyphens, periods, slashes and whitespace all stripped.
        let b = PassportBook::new("GB", "ABC-123/456.789").unwrap();
        assert_eq!(b.number, "ABC123456789");
        let c = PassportBook::new("US", "AB-12-34-567").unwrap();
        assert_eq!(c.number, "AB1234567");
    }

    #[test]
    fn passport_book_new_rejects_bad_country() {
        assert!(PassportBook::new("GBR", "123").is_none()); // 3 letters
        assert!(PassportBook::new("G", "123").is_none()); // 1 letter
        assert!(PassportBook::new("1A", "123").is_none()); // not alphabetic
        assert!(PassportBook::new("", "123").is_none()); // empty
    }

    #[test]
    fn passport_book_new_rejects_empty_number() {
        assert!(PassportBook::new("GB", "").is_none());
        assert!(PassportBook::new("GB", "   ").is_none());
        assert!(PassportBook::new("GB", "\t\n").is_none());
    }

    #[test]
    fn passport_book_with_dates_sets_metadata() {
        let b = PassportBook::new("GB", "123")
            .unwrap()
            .with_issued(chrono::NaiveDate::from_ymd_opt(2020, 1, 1).unwrap())
            .with_expires(chrono::NaiveDate::from_ymd_opt(2030, 1, 1).unwrap());
        assert!(b.issued.is_some());
        assert!(b.expires.is_some());
    }

    #[test]
    fn passport_book_round_trips_through_serde() {
        let b = PassportBook::new("US", "AB1234567")
            .unwrap()
            .with_issued(chrono::NaiveDate::from_ymd_opt(2024, 6, 1).unwrap());
        let json = serde_json::to_string(&b).unwrap();
        let back: PassportBook = serde_json::from_str(&json).unwrap();
        assert_eq!(b, back);
    }

    #[test]
    fn passport_book_serde_default_dates() {
        // Legacy payloads lacking the optional date fields must
        // deserialise cleanly.
        let legacy = r#"{"country": "GB", "number": "123"}"#;
        let b: PassportBook = serde_json::from_str(legacy).unwrap();
        assert_eq!(b.country, "GB");
        assert_eq!(b.number, "123");
        assert!(b.issued.is_none());
        assert!(b.expires.is_none());
    }

    #[test]
    fn worker_builder_carries_passport_books() {
        let p = Worker::builder()
            .add_passport_book(PassportBook::new("GB", "111").unwrap())
            .add_passport_book(PassportBook::new("US", "222").unwrap())
            .build();
        assert_eq!(p.passport_books.len(), 2);
        assert_eq!(p.passport_books[0].country, "GB");
        assert_eq!(p.passport_books[1].country, "US");
    }

    #[test]
    fn worker_validate_accepts_solo_passport_book() {
        let p = Worker::builder()
            .add_passport_book(PassportBook::new("GB", "123456789").unwrap())
            .build();
        assert!(p.validate().is_ok());
    }

    #[test]
    fn previous_addresses_setter_replaces_vec() {
        let mut a = Address::new();
        a.postcode = Some("CF10 1AA".into());
        let p = Worker::builder()
            .previous_addresses(vec![a.clone()])
            .build();
        assert_eq!(p.previous_addresses, vec![a]);
    }
}