behaviorsim-rs 0.7.0

Domain-agnostic specification for modeling individual psychology and social dynamics
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
//! Microsystem implementation for immediate face-to-face environments.
//!
//! Microsystems are the innermost layer of Bronfenbrenner's ecological model,
//! representing settings where the individual has direct, face-to-face
//! interactions with others.
//!
//! # Value Type
//!
//! Microsystem values are static f64, NOT StateValue. Unlike mood/needs which
//! have base+delta+decay, context values represent environmental conditions
//! that change via explicit events, not gradual decay.

use crate::enums::{
    EducationPath, FamilyPath, HealthcarePath, MicrosystemPath, NeighborhoodPath, ReligiousPath,
    SocialPath, WorkPath,
};
use crate::types::EntityId;

/// Type of microsystem environment.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum MicrosystemType {
    /// Work environment.
    Work,
    /// Family environment.
    Family,
    /// Social/friendship environment.
    Social,
    /// Educational environment.
    Education,
    /// Healthcare environment.
    Healthcare,
    /// Religious community environment.
    Religious,
    /// Neighborhood environment.
    Neighborhood,
}

/// Role within a family context.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
pub enum FamilyRole {
    /// Child role in family.
    Child,
    /// Parent role in family.
    Parent,
    /// Spouse/partner role.
    Spouse,
    /// Sibling role.
    Sibling,
    /// Extended family member.
    Extended,
    /// No family role (default).
    #[default]
    None,
}

impl FamilyRole {
    /// Returns the effect multiplier for this role.
    ///
    /// Per spec: Parents amplify caregiving events 1.5x,
    /// children absorb parent stress 1.3x, etc.
    #[must_use]
    pub const fn effect_multiplier(&self) -> f64 {
        match self {
            FamilyRole::Parent => 1.5,
            FamilyRole::Child => 1.3,
            FamilyRole::Spouse => 1.2,
            FamilyRole::Sibling => 1.1,
            FamilyRole::Extended => 1.0,
            FamilyRole::None => 1.0,
        }
    }
}

/// Interaction profile for a microsystem.
///
/// Tracks the frequency and complexity of interactions, which determine
/// whether proximal processes can occur.
#[derive(Debug, Clone, PartialEq, Default)]
pub struct InteractionProfile {
    /// Interaction frequency (0-1, normalized).
    pub interaction_frequency: f64,

    /// Progressive complexity of shared tasks (0-1).
    pub interaction_complexity: f64,

    /// Mutual engagement balance (0-1).
    pub reciprocity_balance: f64,

    /// Primary dyadic relationships in this context.
    pub primary_dyadic_relationships: Vec<EntityId>,
}

impl InteractionProfile {
    /// Creates a new InteractionProfile with default values.
    #[must_use]
    pub fn new() -> Self {
        InteractionProfile {
            interaction_frequency: 0.5,
            interaction_complexity: 0.5,
            reciprocity_balance: 0.6,
            primary_dyadic_relationships: Vec::new(),
        }
    }

    /// Creates an interaction profile with specified frequency and complexity.
    #[must_use]
    pub fn with_values(frequency: f64, complexity: f64) -> Self {
        InteractionProfile {
            interaction_frequency: frequency.clamp(0.0, 1.0),
            interaction_complexity: complexity.clamp(0.0, 1.0),
            reciprocity_balance: 0.6,
            primary_dyadic_relationships: Vec::new(),
        }
    }

    /// Returns the reciprocity score for proximal process gating.
    #[must_use]
    pub fn reciprocity_score(&self) -> f64 {
        let average =
            (self.interaction_frequency + self.interaction_complexity + self.reciprocity_balance)
                / 3.0;
        average.clamp(0.0, 1.0)
    }
}

/// Work context dimensions.
#[derive(Debug, Clone, PartialEq)]
pub struct WorkContext {
    /// Current work pressure (0-1).
    pub workload_stress: f64,

    /// Satisfaction with job role (0-1).
    pub role_satisfaction: f64,

    /// Emotional supportiveness of workplace (0-1).
    pub warmth: f64,

    /// Degree of antagonism/conflict (0-1).
    pub hostility: f64,

    /// Clear expectations and responsibilities (0-1).
    pub role_clarity: f64,

    /// Routine and consistent environment (0-1).
    pub predictability: f64,

    /// Mental engagement and challenge (0-1).
    pub cognitive_stimulation: f64,

    /// Security and constancy of position (0-1).
    pub stability: f64,

    /// Interaction profile for this context.
    pub interaction_profile: InteractionProfile,

    /// Supervisor entity (if any).
    pub supervisor_id: Option<EntityId>,

    /// Coworker entities.
    pub peer_ids: Vec<EntityId>,
}

impl Default for WorkContext {
    fn default() -> Self {
        WorkContext {
            workload_stress: 0.3,
            role_satisfaction: 0.5,
            warmth: 0.5,
            hostility: 0.2,
            role_clarity: 0.6,
            predictability: 0.5,
            cognitive_stimulation: 0.5,
            stability: 0.6,
            interaction_profile: InteractionProfile::new(),
            supervisor_id: None,
            peer_ids: Vec::new(),
        }
    }
}

impl WorkContext {
    /// Gets a value by path.
    #[must_use]
    pub fn get_value(&self, path: WorkPath) -> f64 {
        match path {
            WorkPath::WorkloadStress => self.workload_stress,
            WorkPath::RoleSatisfaction => self.role_satisfaction,
            WorkPath::Warmth => self.warmth,
            WorkPath::Hostility => self.hostility,
            WorkPath::RoleClarity => self.role_clarity,
            WorkPath::Predictability => self.predictability,
            WorkPath::CognitiveStimulation => self.cognitive_stimulation,
            WorkPath::Stability => self.stability,
            WorkPath::InteractionFrequency => self.interaction_profile.interaction_frequency,
            WorkPath::InteractionComplexity => self.interaction_profile.interaction_complexity,
        }
    }

    /// Sets a value by path.
    pub fn set_value(&mut self, path: WorkPath, value: f64) {
        let clamped = value.clamp(0.0, 1.0);
        match path {
            WorkPath::WorkloadStress => self.workload_stress = clamped,
            WorkPath::RoleSatisfaction => self.role_satisfaction = clamped,
            WorkPath::Warmth => self.warmth = clamped,
            WorkPath::Hostility => self.hostility = clamped,
            WorkPath::RoleClarity => self.role_clarity = clamped,
            WorkPath::Predictability => self.predictability = clamped,
            WorkPath::CognitiveStimulation => self.cognitive_stimulation = clamped,
            WorkPath::Stability => self.stability = clamped,
            WorkPath::InteractionFrequency => {
                self.interaction_profile.interaction_frequency = clamped
            }
            WorkPath::InteractionComplexity => {
                self.interaction_profile.interaction_complexity = clamped
            }
        }
    }
}

/// Family context dimensions.
#[derive(Debug, Clone, PartialEq)]
pub struct FamilyContext {
    /// Overall family satisfaction (0-1).
    pub family_satisfaction: f64,

    /// Caregiving burden (0-1).
    pub caregiving_burden: f64,

    /// Emotional warmth (0-1).
    pub warmth: f64,

    /// Active hostility (0-1).
    pub hostility: f64,

    /// Clear family role expectations (0-1).
    pub role_clarity: f64,

    /// Consistent family routines (0-1).
    pub predictability: f64,

    /// Family stability (0-1).
    pub stability: f64,

    /// Sense of family cohesion and unity (0-1).
    pub cohesion: f64,

    /// Role within family.
    pub family_role: FamilyRole,

    /// Interaction profile for this context.
    pub interaction_profile: InteractionProfile,

    /// Family member entities.
    pub family_unit: Vec<EntityId>,
}

impl Default for FamilyContext {
    fn default() -> Self {
        FamilyContext {
            family_satisfaction: 0.6,
            caregiving_burden: 0.2,
            warmth: 0.6,
            hostility: 0.1,
            role_clarity: 0.5,
            predictability: 0.5,
            stability: 0.6,
            cohesion: 0.6,
            family_role: FamilyRole::None,
            interaction_profile: InteractionProfile::new(),
            family_unit: Vec::new(),
        }
    }
}

impl FamilyContext {
    /// Gets a value by path.
    #[must_use]
    pub fn get_value(&self, path: FamilyPath) -> f64 {
        match path {
            FamilyPath::FamilySatisfaction => self.family_satisfaction,
            FamilyPath::CaregivingBurden => self.caregiving_burden,
            FamilyPath::Warmth => self.warmth,
            FamilyPath::Hostility => self.hostility,
            FamilyPath::RoleClarity => self.role_clarity,
            FamilyPath::Predictability => self.predictability,
            FamilyPath::Stability => self.stability,
            FamilyPath::Cohesion => self.cohesion,
            FamilyPath::InteractionFrequency => self.interaction_profile.interaction_frequency,
            FamilyPath::InteractionComplexity => self.interaction_profile.interaction_complexity,
        }
    }

    /// Sets a value by path.
    pub fn set_value(&mut self, path: FamilyPath, value: f64) {
        let clamped = value.clamp(0.0, 1.0);
        match path {
            FamilyPath::FamilySatisfaction => self.family_satisfaction = clamped,
            FamilyPath::CaregivingBurden => self.caregiving_burden = clamped,
            FamilyPath::Warmth => self.warmth = clamped,
            FamilyPath::Hostility => self.hostility = clamped,
            FamilyPath::RoleClarity => self.role_clarity = clamped,
            FamilyPath::Predictability => self.predictability = clamped,
            FamilyPath::Stability => self.stability = clamped,
            FamilyPath::Cohesion => self.cohesion = clamped,
            FamilyPath::InteractionFrequency => {
                self.interaction_profile.interaction_frequency = clamped
            }
            FamilyPath::InteractionComplexity => {
                self.interaction_profile.interaction_complexity = clamped
            }
        }
    }
}

/// Social context dimensions.
#[derive(Debug, Clone, PartialEq)]
pub struct SocialContext {
    /// Standing in social group (0-1).
    pub group_standing: f64,

    /// Supportiveness of social circle (0-1).
    pub warmth: f64,

    /// Gossip, exclusion, rivalry (0-1).
    pub hostility: f64,

    /// Reliability of social connections (0-1).
    pub predictability: f64,

    /// Interaction profile for this context.
    pub interaction_profile: InteractionProfile,

    /// Close friend entities.
    pub close_friends: Vec<EntityId>,
}

impl Default for SocialContext {
    fn default() -> Self {
        SocialContext {
            group_standing: 0.5,
            warmth: 0.5,
            hostility: 0.2,
            predictability: 0.5,
            interaction_profile: InteractionProfile::new(),
            close_friends: Vec::new(),
        }
    }
}

impl SocialContext {
    /// Gets a value by path.
    #[must_use]
    pub fn get_value(&self, path: SocialPath) -> f64 {
        match path {
            SocialPath::GroupStanding => self.group_standing,
            SocialPath::Warmth => self.warmth,
            SocialPath::Hostility => self.hostility,
            SocialPath::Predictability => self.predictability,
            SocialPath::InteractionFrequency => self.interaction_profile.interaction_frequency,
            SocialPath::InteractionComplexity => self.interaction_profile.interaction_complexity,
        }
    }

    /// Sets a value by path.
    pub fn set_value(&mut self, path: SocialPath, value: f64) {
        let clamped = value.clamp(0.0, 1.0);
        match path {
            SocialPath::GroupStanding => self.group_standing = clamped,
            SocialPath::Warmth => self.warmth = clamped,
            SocialPath::Hostility => self.hostility = clamped,
            SocialPath::Predictability => self.predictability = clamped,
            SocialPath::InteractionFrequency => {
                self.interaction_profile.interaction_frequency = clamped
            }
            SocialPath::InteractionComplexity => {
                self.interaction_profile.interaction_complexity = clamped
            }
        }
    }
}

/// Education context dimensions.
#[derive(Debug, Clone, PartialEq)]
pub struct EducationContext {
    /// Intellectual challenge level (0-1).
    pub cognitive_demand: f64,

    /// Tutoring, mentorship availability (0-1).
    pub competence_support: f64,

    /// Emotional support (0-1).
    pub warmth: f64,

    /// Active antagonism (0-1).
    pub hostility: f64,

    /// Interaction profile for this context.
    pub interaction_profile: InteractionProfile,

    /// Instructor entities.
    pub instructors: Vec<EntityId>,

    /// Peer student entities.
    pub peer_ids: Vec<EntityId>,
}

impl Default for EducationContext {
    fn default() -> Self {
        EducationContext {
            cognitive_demand: 0.5,
            competence_support: 0.5,
            warmth: 0.5,
            hostility: 0.2,
            interaction_profile: InteractionProfile::new(),
            instructors: Vec::new(),
            peer_ids: Vec::new(),
        }
    }
}

impl EducationContext {
    /// Gets a value by path.
    #[must_use]
    pub fn get_value(&self, path: EducationPath) -> f64 {
        match path {
            EducationPath::CognitiveDemand => self.cognitive_demand,
            EducationPath::CompetenceSupport => self.competence_support,
            EducationPath::Warmth => self.warmth,
            EducationPath::Hostility => self.hostility,
            EducationPath::InteractionFrequency => self.interaction_profile.interaction_frequency,
            EducationPath::InteractionComplexity => self.interaction_profile.interaction_complexity,
        }
    }

    /// Sets a value by path.
    pub fn set_value(&mut self, path: EducationPath, value: f64) {
        let clamped = value.clamp(0.0, 1.0);
        match path {
            EducationPath::CognitiveDemand => self.cognitive_demand = clamped,
            EducationPath::CompetenceSupport => self.competence_support = clamped,
            EducationPath::Warmth => self.warmth = clamped,
            EducationPath::Hostility => self.hostility = clamped,
            EducationPath::InteractionFrequency => {
                self.interaction_profile.interaction_frequency = clamped
            }
            EducationPath::InteractionComplexity => {
                self.interaction_profile.interaction_complexity = clamped
            }
        }
    }
}

/// Healthcare context dimensions.
#[derive(Debug, Clone, PartialEq)]
pub struct HealthcareContext {
    /// Healthcare access frequency (0-1).
    pub access_frequency: f64,

    /// Provider responsiveness (0-1).
    pub responsiveness: f64,

    /// Provider warmth (0-1).
    pub warmth: f64,

    /// Provider hostility (0-1).
    pub hostility: f64,

    /// Interaction profile for this context.
    pub interaction_profile: InteractionProfile,

    /// Primary provider entity (if any).
    pub primary_provider_id: Option<EntityId>,
}

impl Default for HealthcareContext {
    fn default() -> Self {
        HealthcareContext {
            access_frequency: 0.3,
            responsiveness: 0.5,
            warmth: 0.5,
            hostility: 0.1,
            interaction_profile: InteractionProfile::new(),
            primary_provider_id: None,
        }
    }
}

impl HealthcareContext {
    /// Gets a value by path.
    #[must_use]
    pub fn get_value(&self, path: HealthcarePath) -> f64 {
        match path {
            HealthcarePath::AccessFrequency => self.access_frequency,
            HealthcarePath::Responsiveness => self.responsiveness,
            HealthcarePath::Warmth => self.warmth,
            HealthcarePath::Hostility => self.hostility,
            HealthcarePath::InteractionFrequency => self.interaction_profile.interaction_frequency,
            HealthcarePath::InteractionComplexity => {
                self.interaction_profile.interaction_complexity
            }
        }
    }

    /// Sets a value by path.
    pub fn set_value(&mut self, path: HealthcarePath, value: f64) {
        let clamped = value.clamp(0.0, 1.0);
        match path {
            HealthcarePath::AccessFrequency => self.access_frequency = clamped,
            HealthcarePath::Responsiveness => self.responsiveness = clamped,
            HealthcarePath::Warmth => self.warmth = clamped,
            HealthcarePath::Hostility => self.hostility = clamped,
            HealthcarePath::InteractionFrequency => {
                self.interaction_profile.interaction_frequency = clamped
            }
            HealthcarePath::InteractionComplexity => {
                self.interaction_profile.interaction_complexity = clamped
            }
        }
    }
}

/// Religious context dimensions.
#[derive(Debug, Clone, PartialEq)]
pub struct ReligiousContext {
    /// Ritual participation frequency (0-1).
    pub ritual_frequency: f64,

    /// Community warmth (0-1).
    pub warmth: f64,

    /// Community hostility (0-1).
    pub hostility: f64,

    /// Interaction profile for this context.
    pub interaction_profile: InteractionProfile,

    /// Community leader entity (if any).
    pub leader_id: Option<EntityId>,
}

impl Default for ReligiousContext {
    fn default() -> Self {
        ReligiousContext {
            ritual_frequency: 0.3,
            warmth: 0.5,
            hostility: 0.1,
            interaction_profile: InteractionProfile::new(),
            leader_id: None,
        }
    }
}

impl ReligiousContext {
    /// Gets a value by path.
    #[must_use]
    pub fn get_value(&self, path: ReligiousPath) -> f64 {
        match path {
            ReligiousPath::RitualFrequency => self.ritual_frequency,
            ReligiousPath::Warmth => self.warmth,
            ReligiousPath::Hostility => self.hostility,
            ReligiousPath::InteractionFrequency => self.interaction_profile.interaction_frequency,
            ReligiousPath::InteractionComplexity => self.interaction_profile.interaction_complexity,
        }
    }

    /// Sets a value by path.
    pub fn set_value(&mut self, path: ReligiousPath, value: f64) {
        let clamped = value.clamp(0.0, 1.0);
        match path {
            ReligiousPath::RitualFrequency => self.ritual_frequency = clamped,
            ReligiousPath::Warmth => self.warmth = clamped,
            ReligiousPath::Hostility => self.hostility = clamped,
            ReligiousPath::InteractionFrequency => {
                self.interaction_profile.interaction_frequency = clamped
            }
            ReligiousPath::InteractionComplexity => {
                self.interaction_profile.interaction_complexity = clamped
            }
        }
    }
}

/// Neighborhood context dimensions.
#[derive(Debug, Clone, PartialEq)]
pub struct NeighborhoodContext {
    /// Neighborhood safety (0-1).
    pub safety: f64,

    /// Community cohesion (0-1).
    pub cohesion: f64,

    /// Neighbor warmth (0-1).
    pub warmth: f64,

    /// Neighbor hostility (0-1).
    pub hostility: f64,

    /// Interaction profile for this context.
    pub interaction_profile: InteractionProfile,

    /// Proximity network entities.
    pub proximity_network: Vec<EntityId>,
}

impl Default for NeighborhoodContext {
    fn default() -> Self {
        NeighborhoodContext {
            safety: 0.6,
            cohesion: 0.5,
            warmth: 0.5,
            hostility: 0.2,
            interaction_profile: InteractionProfile::new(),
            proximity_network: Vec::new(),
        }
    }
}

impl NeighborhoodContext {
    /// Gets a value by path.
    #[must_use]
    pub fn get_value(&self, path: NeighborhoodPath) -> f64 {
        match path {
            NeighborhoodPath::Safety => self.safety,
            NeighborhoodPath::Cohesion => self.cohesion,
            NeighborhoodPath::Warmth => self.warmth,
            NeighborhoodPath::Hostility => self.hostility,
            NeighborhoodPath::InteractionFrequency => {
                self.interaction_profile.interaction_frequency
            }
            NeighborhoodPath::InteractionComplexity => {
                self.interaction_profile.interaction_complexity
            }
        }
    }

    /// Sets a value by path.
    pub fn set_value(&mut self, path: NeighborhoodPath, value: f64) {
        let clamped = value.clamp(0.0, 1.0);
        match path {
            NeighborhoodPath::Safety => self.safety = clamped,
            NeighborhoodPath::Cohesion => self.cohesion = clamped,
            NeighborhoodPath::Warmth => self.warmth = clamped,
            NeighborhoodPath::Hostility => self.hostility = clamped,
            NeighborhoodPath::InteractionFrequency => {
                self.interaction_profile.interaction_frequency = clamped
            }
            NeighborhoodPath::InteractionComplexity => {
                self.interaction_profile.interaction_complexity = clamped
            }
        }
    }
}

/// A microsystem instance containing one of the context types.
#[derive(Debug, Clone, PartialEq)]
pub enum Microsystem {
    /// Work environment context.
    Work(WorkContext),
    /// Family environment context.
    Family(FamilyContext),
    /// Social environment context.
    Social(SocialContext),
    /// Education environment context.
    Education(EducationContext),
    /// Healthcare environment context.
    Healthcare(HealthcareContext),
    /// Religious environment context.
    Religious(ReligiousContext),
    /// Neighborhood environment context.
    Neighborhood(NeighborhoodContext),
}

impl Microsystem {
    /// Creates a work microsystem from a WorkContext.
    #[must_use]
    pub fn new_work(context: WorkContext) -> Self {
        Microsystem::Work(context)
    }

    /// Creates a family microsystem from a FamilyContext.
    #[must_use]
    pub fn new_family(context: FamilyContext) -> Self {
        Microsystem::Family(context)
    }

    /// Creates a social microsystem from a SocialContext.
    #[must_use]
    pub fn new_social(context: SocialContext) -> Self {
        Microsystem::Social(context)
    }

    /// Creates an education microsystem from an EducationContext.
    #[must_use]
    pub fn new_education(context: EducationContext) -> Self {
        Microsystem::Education(context)
    }

    /// Creates a healthcare microsystem from a HealthcareContext.
    #[must_use]
    pub fn new_healthcare(context: HealthcareContext) -> Self {
        Microsystem::Healthcare(context)
    }

    /// Creates a religious microsystem from a ReligiousContext.
    #[must_use]
    pub fn new_religious(context: ReligiousContext) -> Self {
        Microsystem::Religious(context)
    }

    /// Creates a neighborhood microsystem from a NeighborhoodContext.
    #[must_use]
    pub fn new_neighborhood(context: NeighborhoodContext) -> Self {
        Microsystem::Neighborhood(context)
    }

    /// Returns the type of this microsystem.
    #[must_use]
    pub fn microsystem_type(&self) -> MicrosystemType {
        match self {
            Microsystem::Work(_) => MicrosystemType::Work,
            Microsystem::Family(_) => MicrosystemType::Family,
            Microsystem::Social(_) => MicrosystemType::Social,
            Microsystem::Education(_) => MicrosystemType::Education,
            Microsystem::Healthcare(_) => MicrosystemType::Healthcare,
            Microsystem::Religious(_) => MicrosystemType::Religious,
            Microsystem::Neighborhood(_) => MicrosystemType::Neighborhood,
        }
    }

    /// Gets a value by microsystem path.
    #[must_use]
    pub fn get_value(&self, path: &MicrosystemPath) -> f64 {
        match (self, path) {
            (Microsystem::Work(w), MicrosystemPath::Work(p)) => w.get_value(*p),
            (Microsystem::Family(f), MicrosystemPath::Family(p)) => f.get_value(*p),
            (Microsystem::Social(s), MicrosystemPath::Social(p)) => s.get_value(*p),
            (Microsystem::Education(e), MicrosystemPath::Education(p)) => e.get_value(*p),
            (Microsystem::Healthcare(h), MicrosystemPath::Healthcare(p)) => h.get_value(*p),
            (Microsystem::Religious(r), MicrosystemPath::Religious(p)) => r.get_value(*p),
            (Microsystem::Neighborhood(n), MicrosystemPath::Neighborhood(p)) => n.get_value(*p),
            // Type mismatch - return 0.0 as a safe default
            _ => 0.0,
        }
    }

    /// Sets a value by microsystem path.
    ///
    /// Returns true if the path matches the microsystem type, false otherwise.
    pub fn set_value(&mut self, path: &MicrosystemPath, value: f64) -> bool {
        match (self, path) {
            (Microsystem::Work(w), MicrosystemPath::Work(p)) => {
                w.set_value(*p, value);
                true
            }
            (Microsystem::Family(f), MicrosystemPath::Family(p)) => {
                f.set_value(*p, value);
                true
            }
            (Microsystem::Social(s), MicrosystemPath::Social(p)) => {
                s.set_value(*p, value);
                true
            }
            (Microsystem::Education(e), MicrosystemPath::Education(p)) => {
                e.set_value(*p, value);
                true
            }
            (Microsystem::Healthcare(h), MicrosystemPath::Healthcare(p)) => {
                h.set_value(*p, value);
                true
            }
            (Microsystem::Religious(r), MicrosystemPath::Religious(p)) => {
                r.set_value(*p, value);
                true
            }
            (Microsystem::Neighborhood(n), MicrosystemPath::Neighborhood(p)) => {
                n.set_value(*p, value);
                true
            }
            _ => false,
        }
    }

    /// Returns a reference to the work context, if this is a work microsystem.
    #[must_use]
    pub fn work(&self) -> Option<&WorkContext> {
        match self {
            Microsystem::Work(w) => Some(w),
            _ => None,
        }
    }

    /// Returns a mutable reference to the work context.
    pub fn work_mut(&mut self) -> Option<&mut WorkContext> {
        match self {
            Microsystem::Work(w) => Some(w),
            _ => None,
        }
    }

    /// Returns a reference to the family context, if this is a family microsystem.
    #[must_use]
    pub fn family(&self) -> Option<&FamilyContext> {
        match self {
            Microsystem::Family(f) => Some(f),
            _ => None,
        }
    }

    /// Returns a mutable reference to the family context.
    pub fn family_mut(&mut self) -> Option<&mut FamilyContext> {
        match self {
            Microsystem::Family(f) => Some(f),
            _ => None,
        }
    }

    /// Returns a reference to the social context, if this is a social microsystem.
    #[must_use]
    pub fn social(&self) -> Option<&SocialContext> {
        match self {
            Microsystem::Social(s) => Some(s),
            _ => None,
        }
    }

    /// Returns a mutable reference to the social context.
    pub fn social_mut(&mut self) -> Option<&mut SocialContext> {
        match self {
            Microsystem::Social(s) => Some(s),
            _ => None,
        }
    }

    /// Returns a reference to the education context, if this is an education microsystem.
    #[must_use]
    pub fn education(&self) -> Option<&EducationContext> {
        match self {
            Microsystem::Education(e) => Some(e),
            _ => None,
        }
    }

    /// Returns a mutable reference to the education context.
    pub fn education_mut(&mut self) -> Option<&mut EducationContext> {
        match self {
            Microsystem::Education(e) => Some(e),
            _ => None,
        }
    }

    /// Returns a reference to the healthcare context, if this is a healthcare microsystem.
    #[must_use]
    pub fn healthcare(&self) -> Option<&HealthcareContext> {
        match self {
            Microsystem::Healthcare(h) => Some(h),
            _ => None,
        }
    }

    /// Returns a mutable reference to the healthcare context.
    pub fn healthcare_mut(&mut self) -> Option<&mut HealthcareContext> {
        match self {
            Microsystem::Healthcare(h) => Some(h),
            _ => None,
        }
    }

    /// Returns a reference to the religious context, if this is a religious microsystem.
    #[must_use]
    pub fn religious(&self) -> Option<&ReligiousContext> {
        match self {
            Microsystem::Religious(r) => Some(r),
            _ => None,
        }
    }

    /// Returns a mutable reference to the religious context.
    pub fn religious_mut(&mut self) -> Option<&mut ReligiousContext> {
        match self {
            Microsystem::Religious(r) => Some(r),
            _ => None,
        }
    }

    /// Returns a reference to the neighborhood context, if this is a neighborhood microsystem.
    #[must_use]
    pub fn neighborhood(&self) -> Option<&NeighborhoodContext> {
        match self {
            Microsystem::Neighborhood(n) => Some(n),
            _ => None,
        }
    }

    /// Returns a mutable reference to the neighborhood context.
    pub fn neighborhood_mut(&mut self) -> Option<&mut NeighborhoodContext> {
        match self {
            Microsystem::Neighborhood(n) => Some(n),
            _ => None,
        }
    }

    /// Returns the warmth value for this microsystem.
    #[must_use]
    pub fn warmth(&self) -> f64 {
        match self {
            Microsystem::Work(w) => w.warmth,
            Microsystem::Family(f) => f.warmth,
            Microsystem::Social(s) => s.warmth,
            Microsystem::Education(e) => e.warmth,
            Microsystem::Healthcare(h) => h.warmth,
            Microsystem::Religious(r) => r.warmth,
            Microsystem::Neighborhood(n) => n.warmth,
        }
    }

    /// Returns the hostility value for this microsystem.
    #[must_use]
    pub fn hostility(&self) -> f64 {
        match self {
            Microsystem::Work(w) => w.hostility,
            Microsystem::Family(f) => f.hostility,
            Microsystem::Social(s) => s.hostility,
            Microsystem::Education(e) => e.hostility,
            Microsystem::Healthcare(h) => h.hostility,
            Microsystem::Religious(r) => r.hostility,
            Microsystem::Neighborhood(n) => n.hostility,
        }
    }

    /// Returns the interaction frequency for this microsystem.
    #[must_use]
    pub fn interaction_frequency(&self) -> f64 {
        match self {
            Microsystem::Work(w) => w.interaction_profile.interaction_frequency,
            Microsystem::Family(f) => f.interaction_profile.interaction_frequency,
            Microsystem::Social(s) => s.interaction_profile.interaction_frequency,
            Microsystem::Education(e) => e.interaction_profile.interaction_frequency,
            Microsystem::Healthcare(h) => h.interaction_profile.interaction_frequency,
            Microsystem::Religious(r) => r.interaction_profile.interaction_frequency,
            Microsystem::Neighborhood(n) => n.interaction_profile.interaction_frequency,
        }
    }

    /// Returns the interaction complexity for this microsystem.
    #[must_use]
    pub fn interaction_complexity(&self) -> f64 {
        match self {
            Microsystem::Work(w) => w.interaction_profile.interaction_complexity,
            Microsystem::Family(f) => f.interaction_profile.interaction_complexity,
            Microsystem::Social(s) => s.interaction_profile.interaction_complexity,
            Microsystem::Education(e) => e.interaction_profile.interaction_complexity,
            Microsystem::Healthcare(h) => h.interaction_profile.interaction_complexity,
            Microsystem::Religious(r) => r.interaction_profile.interaction_complexity,
            Microsystem::Neighborhood(n) => n.interaction_profile.interaction_complexity,
        }
    }

    /// Returns the reciprocity balance for this microsystem.
    #[must_use]
    pub fn interaction_reciprocity(&self) -> f64 {
        match self {
            Microsystem::Work(w) => w.interaction_profile.reciprocity_balance,
            Microsystem::Family(f) => f.interaction_profile.reciprocity_balance,
            Microsystem::Social(s) => s.interaction_profile.reciprocity_balance,
            Microsystem::Education(e) => e.interaction_profile.reciprocity_balance,
            Microsystem::Healthcare(h) => h.interaction_profile.reciprocity_balance,
            Microsystem::Religious(r) => r.interaction_profile.reciprocity_balance,
            Microsystem::Neighborhood(n) => n.interaction_profile.reciprocity_balance,
        }
    }

    /// Returns the stress level for this microsystem.
    ///
    /// Returns a computed stress value based on the microsystem type.
    #[must_use]
    pub fn stress_level(&self) -> f64 {
        match self {
            Microsystem::Work(w) => w.workload_stress,
            Microsystem::Family(f) => f.caregiving_burden * 0.7 + f.hostility * 0.3,
            Microsystem::Social(s) => s.hostility * 0.6 + (1.0 - s.group_standing) * 0.4,
            Microsystem::Education(e) => e.cognitive_demand * 0.5 + e.hostility * 0.5,
            Microsystem::Healthcare(h) => h.hostility * 0.5 + (1.0 - h.responsiveness) * 0.5,
            Microsystem::Religious(r) => r.hostility,
            Microsystem::Neighborhood(n) => (1.0 - n.safety) * 0.6 + n.hostility * 0.4,
        }
    }
}

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

    // --- MicrosystemType tests ---

    #[test]
    fn microsystem_type_all_variants() {
        let _ = MicrosystemType::Work;
        let _ = MicrosystemType::Family;
        let _ = MicrosystemType::Social;
        let _ = MicrosystemType::Education;
        let _ = MicrosystemType::Healthcare;
        let _ = MicrosystemType::Religious;
        let _ = MicrosystemType::Neighborhood;
    }

    // --- FamilyRole tests ---

    #[test]
    fn family_role_effect_multipliers() {
        assert!((FamilyRole::Parent.effect_multiplier() - 1.5).abs() < f64::EPSILON);
        assert!((FamilyRole::Child.effect_multiplier() - 1.3).abs() < f64::EPSILON);
        assert!((FamilyRole::Spouse.effect_multiplier() - 1.2).abs() < f64::EPSILON);
        assert!((FamilyRole::Sibling.effect_multiplier() - 1.1).abs() < f64::EPSILON);
        assert!((FamilyRole::Extended.effect_multiplier() - 1.0).abs() < f64::EPSILON);
        assert!((FamilyRole::None.effect_multiplier() - 1.0).abs() < f64::EPSILON);
    }

    #[test]
    fn family_role_default() {
        assert_eq!(FamilyRole::default(), FamilyRole::None);
    }

    // --- InteractionProfile tests ---

    #[test]
    fn interaction_profile_new() {
        let profile = InteractionProfile::new();
        assert!((profile.interaction_frequency - 0.5).abs() < f64::EPSILON);
        assert!((profile.interaction_complexity - 0.5).abs() < f64::EPSILON);
        assert!((profile.reciprocity_balance - 0.6).abs() < f64::EPSILON);
        assert!(profile.primary_dyadic_relationships.is_empty());
    }

    #[test]
    fn interaction_profile_with_values() {
        let profile = InteractionProfile::with_values(0.8, 0.6);
        assert!((profile.interaction_frequency - 0.8).abs() < f64::EPSILON);
        assert!((profile.interaction_complexity - 0.6).abs() < f64::EPSILON);
        assert!((profile.reciprocity_balance - 0.6).abs() < f64::EPSILON);
    }

    #[test]
    fn interaction_profile_with_values_clamped() {
        let profile = InteractionProfile::with_values(1.5, -0.5);
        assert!((profile.interaction_frequency - 1.0).abs() < f64::EPSILON);
        assert!((profile.interaction_complexity - 0.0).abs() < f64::EPSILON);
    }

    #[test]
    fn interaction_profile_reciprocity_score() {
        let mut profile = InteractionProfile::with_values(0.8, 0.6);
        profile.reciprocity_balance = 0.4;
        let score = profile.reciprocity_score();
        assert!((score - 0.6).abs() < 0.01);
    }

    // --- WorkContext tests ---

    #[test]
    fn microsystem_work_context_creation() {
        let work = WorkContext::default();
        assert!(work.workload_stress >= 0.0 && work.workload_stress <= 1.0);
        assert!(work.warmth >= 0.0 && work.warmth <= 1.0);
    }

    #[test]
    fn work_context_get_value() {
        let work = WorkContext::default();
        assert!((work.get_value(WorkPath::WorkloadStress) - 0.3).abs() < f64::EPSILON);
        assert!((work.get_value(WorkPath::RoleSatisfaction) - 0.5).abs() < f64::EPSILON);
    }

    #[test]
    fn work_context_set_value() {
        let mut work = WorkContext::default();
        work.set_value(WorkPath::WorkloadStress, 0.8);
        assert!((work.workload_stress - 0.8).abs() < f64::EPSILON);
    }

    #[test]
    fn work_context_set_value_clamped() {
        let mut work = WorkContext::default();
        work.set_value(WorkPath::Warmth, 1.5);
        assert!((work.warmth - 1.0).abs() < f64::EPSILON);
    }

    #[test]
    fn work_context_all_paths() {
        let work = WorkContext::default();
        for path in WorkPath::all() {
            let value = work.get_value(path);
            assert!(value >= 0.0 && value <= 1.0);
        }
    }

    // --- FamilyContext tests ---

    #[test]
    fn microsystem_family_context_creation() {
        let family = FamilyContext::default();
        assert!(family.warmth >= 0.0 && family.warmth <= 1.0);
        assert_eq!(family.family_role, FamilyRole::None);
    }

    #[test]
    fn family_context_get_value() {
        let family = FamilyContext::default();
        assert!((family.get_value(FamilyPath::FamilySatisfaction) - 0.6).abs() < f64::EPSILON);
    }

    #[test]
    fn family_context_set_value() {
        let mut family = FamilyContext::default();
        family.set_value(FamilyPath::CaregivingBurden, 0.7);
        assert!((family.caregiving_burden - 0.7).abs() < f64::EPSILON);
    }

    #[test]
    fn family_context_all_paths() {
        let family = FamilyContext::default();
        for path in FamilyPath::all() {
            let value = family.get_value(path);
            assert!(value >= 0.0 && value <= 1.0);
        }
    }

    // --- SocialContext tests ---

    #[test]
    fn microsystem_social_context_creation() {
        let social = SocialContext::default();
        assert!(social.warmth >= 0.0 && social.warmth <= 1.0);
    }

    #[test]
    fn social_context_get_value() {
        let social = SocialContext::default();
        assert!((social.get_value(SocialPath::GroupStanding) - 0.5).abs() < f64::EPSILON);
    }

    #[test]
    fn social_context_set_value() {
        let mut social = SocialContext::default();
        social.set_value(SocialPath::Warmth, 0.9);
        assert!((social.warmth - 0.9).abs() < f64::EPSILON);
    }

    #[test]
    fn social_context_all_paths() {
        let social = SocialContext::default();
        for path in SocialPath::all() {
            let value = social.get_value(path);
            assert!(value >= 0.0 && value <= 1.0);
        }
    }

    // --- EducationContext tests ---

    #[test]
    fn education_context_default() {
        let edu = EducationContext::default();
        assert!((edu.cognitive_demand - 0.5).abs() < f64::EPSILON);
    }

    #[test]
    fn education_context_all_paths() {
        let edu = EducationContext::default();
        for path in EducationPath::all() {
            let value = edu.get_value(path);
            assert!(value >= 0.0 && value <= 1.0);
        }
    }

    #[test]
    fn education_context_set_value() {
        let mut edu = EducationContext::default();
        edu.set_value(EducationPath::CognitiveDemand, 0.8);
        assert!((edu.cognitive_demand - 0.8).abs() < f64::EPSILON);
    }

    // --- HealthcareContext tests ---

    #[test]
    fn healthcare_context_default() {
        let hc = HealthcareContext::default();
        assert!((hc.access_frequency - 0.3).abs() < f64::EPSILON);
    }

    #[test]
    fn healthcare_context_all_paths() {
        let hc = HealthcareContext::default();
        for path in HealthcarePath::all() {
            let value = hc.get_value(path);
            assert!(value >= 0.0 && value <= 1.0);
        }
    }

    #[test]
    fn healthcare_context_set_value() {
        let mut hc = HealthcareContext::default();
        hc.set_value(HealthcarePath::Responsiveness, 0.9);
        assert!((hc.responsiveness - 0.9).abs() < f64::EPSILON);
    }

    // --- ReligiousContext tests ---

    #[test]
    fn religious_context_default() {
        let rel = ReligiousContext::default();
        assert!((rel.ritual_frequency - 0.3).abs() < f64::EPSILON);
    }

    #[test]
    fn religious_context_all_paths() {
        let rel = ReligiousContext::default();
        for path in ReligiousPath::all() {
            let value = rel.get_value(path);
            assert!(value >= 0.0 && value <= 1.0);
        }
    }

    #[test]
    fn religious_context_set_value() {
        let mut rel = ReligiousContext::default();
        rel.set_value(ReligiousPath::Warmth, 0.8);
        assert!((rel.warmth - 0.8).abs() < f64::EPSILON);
    }

    // --- NeighborhoodContext tests ---

    #[test]
    fn neighborhood_context_default() {
        let nb = NeighborhoodContext::default();
        assert!((nb.safety - 0.6).abs() < f64::EPSILON);
    }

    #[test]
    fn neighborhood_context_all_paths() {
        let nb = NeighborhoodContext::default();
        for path in NeighborhoodPath::all() {
            let value = nb.get_value(path);
            assert!(value >= 0.0 && value <= 1.0);
        }
    }

    #[test]
    fn neighborhood_context_set_value() {
        let mut nb = NeighborhoodContext::default();
        nb.set_value(NeighborhoodPath::Safety, 0.9);
        assert!((nb.safety - 0.9).abs() < f64::EPSILON);
    }

    // --- Microsystem tests ---

    #[test]
    fn microsystem_work_creation() {
        let m = Microsystem::new_work(WorkContext::default());
        assert_eq!(m.microsystem_type(), MicrosystemType::Work);
    }

    #[test]
    fn microsystem_family_creation() {
        let m = Microsystem::new_family(FamilyContext::default());
        assert_eq!(m.microsystem_type(), MicrosystemType::Family);
    }

    #[test]
    fn microsystem_social_creation() {
        let m = Microsystem::new_social(SocialContext::default());
        assert_eq!(m.microsystem_type(), MicrosystemType::Social);
    }

    #[test]
    fn microsystem_education_creation() {
        let m = Microsystem::new_education(EducationContext::default());
        assert_eq!(m.microsystem_type(), MicrosystemType::Education);
    }

    #[test]
    fn microsystem_healthcare_creation() {
        let m = Microsystem::new_healthcare(HealthcareContext::default());
        assert_eq!(m.microsystem_type(), MicrosystemType::Healthcare);
    }

    #[test]
    fn microsystem_religious_creation() {
        let m = Microsystem::new_religious(ReligiousContext::default());
        assert_eq!(m.microsystem_type(), MicrosystemType::Religious);
    }

    #[test]
    fn microsystem_neighborhood_creation() {
        let m = Microsystem::new_neighborhood(NeighborhoodContext::default());
        assert_eq!(m.microsystem_type(), MicrosystemType::Neighborhood);
    }

    #[test]
    fn microsystem_get_value_matching_type() {
        let m = Microsystem::new_work(WorkContext::default());
        let value = m.get_value(&MicrosystemPath::Work(WorkPath::Warmth));
        assert!(value >= 0.0 && value <= 1.0);
    }

    #[test]
    fn microsystem_get_value_mismatched_type() {
        let m = Microsystem::new_work(WorkContext::default());
        let value = m.get_value(&MicrosystemPath::Family(FamilyPath::Warmth));
        assert!((value - 0.0).abs() < f64::EPSILON);
    }

    #[test]
    fn microsystem_set_value_matching_type() {
        let mut m = Microsystem::new_work(WorkContext::default());
        let result = m.set_value(&MicrosystemPath::Work(WorkPath::Warmth), 0.9);
        assert!(result);
        assert!((m.warmth() - 0.9).abs() < f64::EPSILON);
    }

    #[test]
    fn microsystem_set_value_mismatched_type() {
        let mut m = Microsystem::new_work(WorkContext::default());
        let result = m.set_value(&MicrosystemPath::Family(FamilyPath::Warmth), 0.9);
        assert!(!result);
    }

    #[test]
    fn microsystem_typed_accessors() {
        let m = Microsystem::new_work(WorkContext::default());
        assert!(m.work().is_some());
        assert!(m.family().is_none());

        let m = Microsystem::new_family(FamilyContext::default());
        assert!(m.family().is_some());
        assert!(m.work().is_none());

        let m = Microsystem::new_social(SocialContext::default());
        assert!(m.social().is_some());
        assert!(m.work().is_none());

        let m = Microsystem::new_education(EducationContext::default());
        assert!(m.education().is_some());

        let m = Microsystem::new_healthcare(HealthcareContext::default());
        assert!(m.healthcare().is_some());

        let m = Microsystem::new_religious(ReligiousContext::default());
        assert!(m.religious().is_some());

        let m = Microsystem::new_neighborhood(NeighborhoodContext::default());
        assert!(m.neighborhood().is_some());
    }

    #[test]
    fn microsystem_typed_mut_accessors() {
        let mut m = Microsystem::new_work(WorkContext::default());
        assert!(m.work_mut().is_some());
        assert!(m.family_mut().is_none());

        let mut m = Microsystem::new_family(FamilyContext::default());
        assert!(m.family_mut().is_some());

        let mut m = Microsystem::new_social(SocialContext::default());
        assert!(m.social_mut().is_some());

        let mut m = Microsystem::new_education(EducationContext::default());
        assert!(m.education_mut().is_some());

        let mut m = Microsystem::new_healthcare(HealthcareContext::default());
        assert!(m.healthcare_mut().is_some());

        let mut m = Microsystem::new_religious(ReligiousContext::default());
        assert!(m.religious_mut().is_some());

        let mut m = Microsystem::new_neighborhood(NeighborhoodContext::default());
        assert!(m.neighborhood_mut().is_some());
    }

    #[test]
    fn microsystem_warmth() {
        let m = Microsystem::new_work(WorkContext::default());
        let warmth = m.warmth();
        assert!(warmth >= 0.0 && warmth <= 1.0);
    }

    #[test]
    fn microsystem_hostility() {
        let m = Microsystem::new_family(FamilyContext::default());
        let hostility = m.hostility();
        assert!(hostility >= 0.0 && hostility <= 1.0);
    }

    #[test]
    fn microsystem_interaction_frequency() {
        let m = Microsystem::new_work(WorkContext::default());
        let freq = m.interaction_frequency();
        assert!(freq >= 0.0 && freq <= 1.0);
    }

    #[test]
    fn microsystem_interaction_complexity() {
        let m = Microsystem::new_social(SocialContext::default());
        let complexity = m.interaction_complexity();
        assert!(complexity >= 0.0 && complexity <= 1.0);
    }

    #[test]
    fn microsystem_stress_level() {
        let m = Microsystem::new_work(WorkContext::default());
        let stress = m.stress_level();
        assert!(stress >= 0.0 && stress <= 1.0);
    }

    #[test]
    fn microsystem_stress_level_all_types() {
        let types = [
            Microsystem::new_work(WorkContext::default()),
            Microsystem::new_family(FamilyContext::default()),
            Microsystem::new_social(SocialContext::default()),
            Microsystem::new_education(EducationContext::default()),
            Microsystem::new_healthcare(HealthcareContext::default()),
            Microsystem::new_religious(ReligiousContext::default()),
            Microsystem::new_neighborhood(NeighborhoodContext::default()),
        ];

        for m in types {
            let stress = m.stress_level();
            assert!(stress >= 0.0 && stress <= 1.0);
        }
    }

    #[test]
    fn microsystem_clone_and_eq() {
        let m1 = Microsystem::new_work(WorkContext::default());
        let m2 = m1.clone();
        assert_eq!(m1, m2);
    }

    #[test]
    fn microsystem_debug() {
        let m = Microsystem::new_work(WorkContext::default());
        let debug = format!("{:?}", m);
        assert!(debug.contains("Work"));
    }

    // --- Additional coverage tests for set_value paths ---

    #[test]
    fn work_context_set_value_all_paths() {
        let mut work = WorkContext::default();
        for path in WorkPath::all() {
            work.set_value(path, 0.7);
            assert!((work.get_value(path) - 0.7).abs() < f64::EPSILON);
        }
    }

    #[test]
    fn family_context_set_value_all_paths() {
        let mut family = FamilyContext::default();
        for path in FamilyPath::all() {
            family.set_value(path, 0.7);
            assert!((family.get_value(path) - 0.7).abs() < f64::EPSILON);
        }
    }

    #[test]
    fn social_context_set_value_all_paths() {
        let mut social = SocialContext::default();
        for path in SocialPath::all() {
            social.set_value(path, 0.7);
            assert!((social.get_value(path) - 0.7).abs() < f64::EPSILON);
        }
    }

    #[test]
    fn education_context_set_value_all_paths() {
        let mut edu = EducationContext::default();
        for path in EducationPath::all() {
            edu.set_value(path, 0.7);
            assert!((edu.get_value(path) - 0.7).abs() < f64::EPSILON);
        }
    }

    #[test]
    fn healthcare_context_set_value_all_paths() {
        let mut hc = HealthcareContext::default();
        for path in HealthcarePath::all() {
            hc.set_value(path, 0.7);
            assert!((hc.get_value(path) - 0.7).abs() < f64::EPSILON);
        }
    }

    #[test]
    fn religious_context_set_value_all_paths() {
        let mut rel = ReligiousContext::default();
        for path in ReligiousPath::all() {
            rel.set_value(path, 0.7);
            assert!((rel.get_value(path) - 0.7).abs() < f64::EPSILON);
        }
    }

    #[test]
    fn neighborhood_context_set_value_all_paths() {
        let mut nb = NeighborhoodContext::default();
        for path in NeighborhoodPath::all() {
            nb.set_value(path, 0.7);
            assert!((nb.get_value(path) - 0.7).abs() < f64::EPSILON);
        }
    }

    // --- Tests for Microsystem get_value/set_value for all types ---

    #[test]
    fn microsystem_get_value_family() {
        let m = Microsystem::new_family(FamilyContext::default());
        let value = m.get_value(&MicrosystemPath::Family(FamilyPath::Warmth));
        assert!(value >= 0.0 && value <= 1.0);
    }

    #[test]
    fn microsystem_get_value_social() {
        let m = Microsystem::new_social(SocialContext::default());
        let value = m.get_value(&MicrosystemPath::Social(SocialPath::Warmth));
        assert!(value >= 0.0 && value <= 1.0);
    }

    #[test]
    fn microsystem_get_value_education() {
        let m = Microsystem::new_education(EducationContext::default());
        let value = m.get_value(&MicrosystemPath::Education(EducationPath::Warmth));
        assert!(value >= 0.0 && value <= 1.0);
    }

    #[test]
    fn microsystem_get_value_healthcare() {
        let m = Microsystem::new_healthcare(HealthcareContext::default());
        let value = m.get_value(&MicrosystemPath::Healthcare(HealthcarePath::Warmth));
        assert!(value >= 0.0 && value <= 1.0);
    }

    #[test]
    fn microsystem_get_value_religious() {
        let m = Microsystem::new_religious(ReligiousContext::default());
        let value = m.get_value(&MicrosystemPath::Religious(ReligiousPath::Warmth));
        assert!(value >= 0.0 && value <= 1.0);
    }

    #[test]
    fn microsystem_get_value_neighborhood() {
        let m = Microsystem::new_neighborhood(NeighborhoodContext::default());
        let value = m.get_value(&MicrosystemPath::Neighborhood(NeighborhoodPath::Warmth));
        assert!(value >= 0.0 && value <= 1.0);
    }

    #[test]
    fn microsystem_set_value_family() {
        let mut m = Microsystem::new_family(FamilyContext::default());
        let result = m.set_value(&MicrosystemPath::Family(FamilyPath::Warmth), 0.9);
        assert!(result);
        assert!((m.warmth() - 0.9).abs() < f64::EPSILON);
    }

    #[test]
    fn microsystem_set_value_social() {
        let mut m = Microsystem::new_social(SocialContext::default());
        let result = m.set_value(&MicrosystemPath::Social(SocialPath::Warmth), 0.9);
        assert!(result);
        assert!((m.warmth() - 0.9).abs() < f64::EPSILON);
    }

    #[test]
    fn microsystem_set_value_education() {
        let mut m = Microsystem::new_education(EducationContext::default());
        let result = m.set_value(&MicrosystemPath::Education(EducationPath::Warmth), 0.9);
        assert!(result);
        assert!((m.warmth() - 0.9).abs() < f64::EPSILON);
    }

    #[test]
    fn microsystem_set_value_healthcare() {
        let mut m = Microsystem::new_healthcare(HealthcareContext::default());
        let result = m.set_value(&MicrosystemPath::Healthcare(HealthcarePath::Warmth), 0.9);
        assert!(result);
        assert!((m.warmth() - 0.9).abs() < f64::EPSILON);
    }

    #[test]
    fn microsystem_set_value_religious() {
        let mut m = Microsystem::new_religious(ReligiousContext::default());
        let result = m.set_value(&MicrosystemPath::Religious(ReligiousPath::Warmth), 0.9);
        assert!(result);
        assert!((m.warmth() - 0.9).abs() < f64::EPSILON);
    }

    #[test]
    fn microsystem_set_value_neighborhood() {
        let mut m = Microsystem::new_neighborhood(NeighborhoodContext::default());
        let result = m.set_value(
            &MicrosystemPath::Neighborhood(NeighborhoodPath::Warmth),
            0.9,
        );
        assert!(result);
        assert!((m.warmth() - 0.9).abs() < f64::EPSILON);
    }

    // --- Tests for warmth/hostility/interaction_frequency/complexity for all types ---

    #[test]
    fn microsystem_warmth_all_types() {
        let types = [
            Microsystem::new_work(WorkContext::default()),
            Microsystem::new_family(FamilyContext::default()),
            Microsystem::new_social(SocialContext::default()),
            Microsystem::new_education(EducationContext::default()),
            Microsystem::new_healthcare(HealthcareContext::default()),
            Microsystem::new_religious(ReligiousContext::default()),
            Microsystem::new_neighborhood(NeighborhoodContext::default()),
        ];
        for m in types {
            let warmth = m.warmth();
            assert!(warmth >= 0.0 && warmth <= 1.0);
        }
    }

    #[test]
    fn microsystem_hostility_all_types() {
        let types = [
            Microsystem::new_work(WorkContext::default()),
            Microsystem::new_family(FamilyContext::default()),
            Microsystem::new_social(SocialContext::default()),
            Microsystem::new_education(EducationContext::default()),
            Microsystem::new_healthcare(HealthcareContext::default()),
            Microsystem::new_religious(ReligiousContext::default()),
            Microsystem::new_neighborhood(NeighborhoodContext::default()),
        ];
        for m in types {
            let hostility = m.hostility();
            assert!(hostility >= 0.0 && hostility <= 1.0);
        }
    }

    #[test]
    fn microsystem_interaction_frequency_all_types() {
        let types = [
            Microsystem::new_work(WorkContext::default()),
            Microsystem::new_family(FamilyContext::default()),
            Microsystem::new_social(SocialContext::default()),
            Microsystem::new_education(EducationContext::default()),
            Microsystem::new_healthcare(HealthcareContext::default()),
            Microsystem::new_religious(ReligiousContext::default()),
            Microsystem::new_neighborhood(NeighborhoodContext::default()),
        ];
        for m in types {
            let freq = m.interaction_frequency();
            assert!(freq >= 0.0 && freq <= 1.0);
        }
    }

    #[test]
    fn microsystem_interaction_complexity_all_types() {
        let types = [
            Microsystem::new_work(WorkContext::default()),
            Microsystem::new_family(FamilyContext::default()),
            Microsystem::new_social(SocialContext::default()),
            Microsystem::new_education(EducationContext::default()),
            Microsystem::new_healthcare(HealthcareContext::default()),
            Microsystem::new_religious(ReligiousContext::default()),
            Microsystem::new_neighborhood(NeighborhoodContext::default()),
        ];
        for m in types {
            let complexity = m.interaction_complexity();
            assert!(complexity >= 0.0 && complexity <= 1.0);
        }
    }

    // --- Tests for mut accessor returning None ---

    #[test]
    fn microsystem_typed_mut_accessors_none_variants() {
        let mut m = Microsystem::new_work(WorkContext::default());
        assert!(m.social_mut().is_none());
        assert!(m.education_mut().is_none());
        assert!(m.healthcare_mut().is_none());
        assert!(m.religious_mut().is_none());
        assert!(m.neighborhood_mut().is_none());

        let mut m = Microsystem::new_family(FamilyContext::default());
        assert!(m.work_mut().is_none());
        assert!(m.social_mut().is_none());
        assert!(m.education_mut().is_none());
        assert!(m.healthcare_mut().is_none());
        assert!(m.religious_mut().is_none());
        assert!(m.neighborhood_mut().is_none());

        let mut m = Microsystem::new_social(SocialContext::default());
        assert!(m.family_mut().is_none());

        let mut m = Microsystem::new_education(EducationContext::default());
        assert!(m.work_mut().is_none());

        let mut m = Microsystem::new_healthcare(HealthcareContext::default());
        assert!(m.work_mut().is_none());

        let mut m = Microsystem::new_religious(ReligiousContext::default());
        assert!(m.work_mut().is_none());

        let mut m = Microsystem::new_neighborhood(NeighborhoodContext::default());
        assert!(m.work_mut().is_none());
    }

    // --- Tests for read accessor returning None ---

    #[test]
    fn microsystem_typed_accessors_none_variants() {
        // Test that immutable accessors return None for non-matching types
        let m = Microsystem::new_work(WorkContext::default());
        assert!(m.social().is_none());
        assert!(m.education().is_none());
        assert!(m.healthcare().is_none());
        assert!(m.religious().is_none());
        assert!(m.neighborhood().is_none());

        let m = Microsystem::new_family(FamilyContext::default());
        assert!(m.social().is_none());
        assert!(m.education().is_none());
        assert!(m.healthcare().is_none());
        assert!(m.religious().is_none());
        assert!(m.neighborhood().is_none());

        let m = Microsystem::new_social(SocialContext::default());
        assert!(m.education().is_none());
        assert!(m.healthcare().is_none());
        assert!(m.religious().is_none());
        assert!(m.neighborhood().is_none());

        let m = Microsystem::new_education(EducationContext::default());
        assert!(m.social().is_none());
        assert!(m.healthcare().is_none());
        assert!(m.religious().is_none());
        assert!(m.neighborhood().is_none());

        let m = Microsystem::new_healthcare(HealthcareContext::default());
        assert!(m.social().is_none());
        assert!(m.education().is_none());
        assert!(m.religious().is_none());
        assert!(m.neighborhood().is_none());

        let m = Microsystem::new_religious(ReligiousContext::default());
        assert!(m.social().is_none());
        assert!(m.education().is_none());
        assert!(m.healthcare().is_none());
        assert!(m.neighborhood().is_none());

        let m = Microsystem::new_neighborhood(NeighborhoodContext::default());
        assert!(m.social().is_none());
        assert!(m.education().is_none());
        assert!(m.healthcare().is_none());
        assert!(m.religious().is_none());
    }


    #[test]
    fn microsystem_family_support_modifies_valence() {
        // High family support (warmth) increases valence
        // This tests that family warmth is a measurable dimension
        // that can influence entity state through microsystem effects

        // Create high-support family
        let mut high_support = FamilyContext::default();
        high_support.warmth = 0.9;
        high_support.hostility = 0.1;
        high_support.family_satisfaction = 0.8;

        // Create low-support family
        let mut low_support = FamilyContext::default();
        low_support.warmth = 0.2;
        low_support.hostility = 0.7;
        low_support.family_satisfaction = 0.3;

        let high_family = Microsystem::new_family(high_support);
        let low_family = Microsystem::new_family(low_support);

        // High support family has higher warmth
        assert!(high_family.warmth() > low_family.warmth());
        assert!((high_family.warmth() - 0.9).abs() < f64::EPSILON);
        assert!((low_family.warmth() - 0.2).abs() < f64::EPSILON);

        // High support family has lower stress level
        let high_stress = high_family.stress_level();
        let low_stress = low_family.stress_level();
        assert!(high_stress < low_stress);
    }

    #[test]
    fn microsystem_social_isolation_increases_loneliness() {
        // Low social support raises loneliness
        // Tests that low social interaction and warmth correlate with isolation

        // Create socially connected context
        let mut connected = SocialContext::default();
        connected.warmth = 0.8;
        connected.group_standing = 0.7;
        connected.hostility = 0.1;
        connected.interaction_profile = InteractionProfile::with_values(0.8, 0.7);

        // Create isolated context
        let mut isolated = SocialContext::default();
        isolated.warmth = 0.2;
        isolated.group_standing = 0.2;
        isolated.hostility = 0.6;
        isolated.interaction_profile = InteractionProfile::with_values(0.1, 0.1);

        let connected_micro = Microsystem::new_social(connected);
        let isolated_micro = Microsystem::new_social(isolated);

        // Isolated context has lower warmth
        assert!(isolated_micro.warmth() < connected_micro.warmth());

        // Isolated context has higher hostility
        assert!(isolated_micro.hostility() > connected_micro.hostility());

        // Isolated context has lower interaction frequency
        assert!(isolated_micro.interaction_frequency() < connected_micro.interaction_frequency());

        // Isolated context has higher stress level
        assert!(isolated_micro.stress_level() > connected_micro.stress_level());

        // Social microsystem stress is influenced by hostility and low standing
        // stress = hostility * 0.6 + (1.0 - group_standing) * 0.4
        let isolated_expected = 0.6 * 0.6 + (1.0 - 0.2) * 0.4; // 0.36 + 0.32 = 0.68
        assert!((isolated_micro.stress_level() - isolated_expected).abs() < 0.01);
    }

    #[test]
    fn microsystem_set_value_mismatched_type_returns_false() {
        // Test that setting a work path on a family microsystem returns false
        let mut family_micro = Microsystem::new_family(FamilyContext::default());
        let work_path = MicrosystemPath::Work(WorkPath::WorkloadStress);
        let result = family_micro.set_value(&work_path, 0.5);
        assert!(!result);

        // Test setting a family path on a work microsystem returns false
        let mut work_micro = Microsystem::new_work(WorkContext::default());
        let family_path = MicrosystemPath::Family(FamilyPath::Warmth);
        let result = work_micro.set_value(&family_path, 0.5);
        assert!(!result);
    }

    #[test]
    fn microsystem_get_value_mismatched_type_returns_zero() {
        // Test that getting a work path from a family microsystem returns 0.0
        let family_micro = Microsystem::new_family(FamilyContext::default());
        let work_path = MicrosystemPath::Work(WorkPath::WorkloadStress);
        let value = family_micro.get_value(&work_path);
        assert!((value - 0.0).abs() < f64::EPSILON);

        // Test getting a family path from a work microsystem returns 0.0
        let work_micro = Microsystem::new_work(WorkContext::default());
        let family_path = MicrosystemPath::Family(FamilyPath::Warmth);
        let value = work_micro.get_value(&family_path);
        assert!((value - 0.0).abs() < f64::EPSILON);
    }
}