oxirs-embed 0.4.1

Knowledge graph embeddings with TransE, ComplEx, and custom models
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
//! Research Publication Networks - Academic Knowledge Graph Embeddings
//!
//! This module provides specialized embeddings and analysis for research publication networks,
//! including author embeddings, citation analysis, collaboration networks, and impact prediction.

use crate::Vector;
use anyhow::Result;
use chrono::{DateTime, Utc};
use scirs2_core::random::{Random, RngExt};
use serde::{Deserialize, Serialize};
use std::collections::{HashMap, HashSet};
use std::sync::{Arc, RwLock};
use tokio::task::JoinHandle;
use tracing::{debug, info};

/// Research publication network analyzer and embedding generator
pub struct ResearchNetworkAnalyzer {
    /// Author embeddings cache
    author_embeddings: Arc<RwLock<HashMap<String, AuthorEmbedding>>>,
    /// Publication embeddings cache
    publication_embeddings: Arc<RwLock<HashMap<String, PublicationEmbedding>>>,
    /// Citation network graph
    citation_network: Arc<RwLock<CitationNetwork>>,
    /// Collaboration network
    collaboration_network: Arc<RwLock<CollaborationNetwork>>,
    /// Topic models
    topic_models: Arc<RwLock<HashMap<String, TopicModel>>>,
    /// Author profile records (name, affiliations) registered via
    /// [`register_author_profile`](Self::register_author_profile). This is
    /// the only source of author identity metadata: there is no external
    /// author database wired into this analyzer, so
    /// [`generate_author_embedding`](Self::generate_author_embedding) reads
    /// from here rather than fabricating a name/affiliation.
    author_profiles: Arc<RwLock<HashMap<String, AuthorProfile>>>,
    /// Publication metadata records registered via
    /// [`register_publication_metadata`](Self::register_publication_metadata).
    /// The only source of publication bibliographic metadata: there is no
    /// external publication database wired into this analyzer, so
    /// [`generate_publication_embedding`](Self::generate_publication_embedding)
    /// reads from here rather than fabricating a title/venue/year.
    publication_metadata: Arc<RwLock<HashMap<String, PublicationMetadataInput>>>,
    /// Configuration
    config: ResearchNetworkConfig,
    /// Background analysis tasks
    analysis_tasks: Vec<JoinHandle<()>>,
}

/// Author identity metadata supplied by the caller (there is no external
/// author database wired into [`ResearchNetworkAnalyzer`]).
#[derive(Debug, Clone)]
pub struct AuthorProfile {
    /// Author's display name.
    pub name: String,
    /// Author's institutional affiliation(s).
    pub affiliations: Vec<String>,
}

/// Publication bibliographic metadata supplied by the caller (there is no
/// external publication database wired into [`ResearchNetworkAnalyzer`]).
#[derive(Debug, Clone)]
pub struct PublicationMetadataInput {
    /// Publication title.
    pub title: String,
    /// Publication abstract, if available.
    pub abstract_text: String,
    /// Author identifiers (matching the `author_id` used elsewhere in this
    /// analyzer), in author order.
    pub authors: Vec<String>,
    /// Venue (journal/conference) name.
    pub venue: String,
    /// Publication year.
    pub year: u32,
    /// DOI or other persistent identifier, if known.
    pub doi: Option<String>,
}

/// Configuration for research network analysis
#[derive(Debug, Clone)]
pub struct ResearchNetworkConfig {
    /// Maximum number of authors to track
    pub max_authors: usize,
    /// Maximum number of publications to track
    pub max_publications: usize,
    /// Citation network update interval (hours)
    pub citation_update_interval_hours: u64,
    /// Collaboration analysis interval (hours)
    pub collaboration_analysis_interval_hours: u64,
    /// Impact prediction model refresh interval (hours)
    pub impact_prediction_refresh_hours: u64,
    /// Enable real-time citation tracking
    pub enable_real_time_citation_tracking: bool,
    /// Minimum citation count for impact analysis
    pub min_citation_threshold: u32,
    /// Topic modeling configuration
    pub topic_config: TopicModelingConfig,
    /// Embedding dimension
    pub embedding_dimension: usize,
}

impl Default for ResearchNetworkConfig {
    fn default() -> Self {
        Self {
            max_authors: 100_000,
            max_publications: 1_000_000,
            citation_update_interval_hours: 24,
            collaboration_analysis_interval_hours: 12,
            impact_prediction_refresh_hours: 48,
            enable_real_time_citation_tracking: true,
            min_citation_threshold: 5,
            topic_config: TopicModelingConfig::default(),
            embedding_dimension: 512,
        }
    }
}

/// Topic modeling configuration
#[derive(Debug, Clone)]
pub struct TopicModelingConfig {
    /// Number of topics to extract
    pub num_topics: usize,
    /// Minimum word frequency
    pub min_word_freq: u32,
    /// Maximum document frequency ratio
    pub max_doc_freq_ratio: f64,
    /// LDA iterations
    pub lda_iterations: u32,
    /// Topic coherence threshold
    pub coherence_threshold: f64,
}

impl Default for TopicModelingConfig {
    fn default() -> Self {
        Self {
            num_topics: 50,
            min_word_freq: 5,
            max_doc_freq_ratio: 0.8,
            lda_iterations: 1000,
            coherence_threshold: 0.4,
        }
    }
}

/// Author information and embeddings
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct AuthorEmbedding {
    /// Author unique identifier
    pub author_id: String,
    /// Author name
    pub name: String,
    /// Author affiliations
    pub affiliations: Vec<String>,
    /// Research interests/topics
    pub research_topics: Vec<String>,
    /// H-index
    pub h_index: f64,
    /// Total citation count
    pub citation_count: u64,
    /// Publication count
    pub publication_count: u64,
    /// Author embedding vector
    pub embedding: Vector,
    /// Collaboration score
    pub collaboration_score: f64,
    /// Impact score
    pub impact_score: f64,
    /// Career stage
    pub career_stage: CareerStage,
    /// Last updated
    pub last_updated: DateTime<Utc>,
}

/// Publication information and embeddings
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PublicationEmbedding {
    /// Publication unique identifier
    pub publication_id: String,
    /// Title
    pub title: String,
    /// Abstract
    pub abstract_text: String,
    /// Authors
    pub authors: Vec<String>,
    /// Venue (journal/conference)
    pub venue: String,
    /// Publication year
    pub year: u32,
    /// Citation count
    pub citation_count: u64,
    /// Topic distribution
    pub topic_distribution: Vec<f64>,
    /// Publication embedding vector
    pub embedding: Vector,
    /// Impact prediction score
    pub predicted_impact: f64,
    /// Publication type
    pub publication_type: PublicationType,
    /// DOI or other identifier
    pub doi: Option<String>,
    /// Last updated
    pub last_updated: DateTime<Utc>,
}

/// Career stage classification
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum CareerStage {
    EarlyCareer,
    MidCareer,
    SeniorCareer,
    Emeritus,
    Unknown,
}

/// Publication type classification
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum PublicationType {
    JournalArticle,
    ConferencePaper,
    BookChapter,
    Book,
    Preprint,
    Thesis,
    TechnicalReport,
    Other,
}

/// Citation network representation
#[derive(Debug, Clone)]
pub struct CitationNetwork {
    /// Citation edges: (citing_paper, cited_paper, citation_context)
    pub citations: HashMap<String, Vec<Citation>>,
    /// Co-citation relationships
    pub co_citations: HashMap<String, Vec<CoCitation>>,
    /// Bibliographic coupling
    pub bibliographic_coupling: HashMap<String, Vec<BibliographicCoupling>>,
    /// Citation patterns over time
    pub temporal_patterns: HashMap<String, Vec<TemporalCitation>>,
}

/// Citation information
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Citation {
    /// Citing paper ID
    pub citing_paper: String,
    /// Cited paper ID
    pub cited_paper: String,
    /// Citation context/sentence
    pub context: String,
    /// Citation type (supportive, contrasting, neutral)
    pub citation_type: CitationType,
    /// Position in the paper (intro, methods, results, discussion)
    pub section: PaperSection,
    /// Timestamp of citation
    pub timestamp: DateTime<Utc>,
}

/// Citation type classification
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum CitationType {
    Supportive,
    Contrasting,
    Neutral,
    Background,
    Methodological,
}

/// Paper section where citation occurs
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum PaperSection {
    Introduction,
    RelatedWork,
    Methods,
    Results,
    Discussion,
    Conclusion,
    Other,
}

/// Co-citation relationship
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CoCitation {
    /// First paper
    pub paper1: String,
    /// Second paper
    pub paper2: String,
    /// Number of papers citing both
    pub co_citation_count: u32,
    /// Similarity score
    pub similarity_score: f64,
}

/// Bibliographic coupling
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct BibliographicCoupling {
    /// First paper
    pub paper1: String,
    /// Second paper
    pub paper2: String,
    /// Number of shared references
    pub shared_references: u32,
    /// Coupling strength
    pub coupling_strength: f64,
}

/// Temporal citation pattern
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TemporalCitation {
    /// Paper ID
    pub paper_id: String,
    /// Citation timestamp
    pub timestamp: DateTime<Utc>,
    /// Citations at this time
    pub citation_count: u64,
    /// Velocity (citations per time unit)
    pub citation_velocity: f64,
}

/// Collaboration network
#[derive(Debug, Clone)]
pub struct CollaborationNetwork {
    /// Author collaborations: (author1, author2, collaboration_strength)
    pub collaborations: HashMap<String, Vec<Collaboration>>,
    /// Research groups/communities
    pub research_communities: Vec<ResearchCommunity>,
    /// Collaboration patterns over time
    pub temporal_collaborations: HashMap<String, Vec<TemporalCollaboration>>,
}

/// Collaboration between authors
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Collaboration {
    /// First author
    pub author1: String,
    /// Second author
    pub author2: String,
    /// Number of joint publications
    pub joint_publications: u32,
    /// Collaboration strength score
    pub strength: f64,
    /// Shared research topics
    pub shared_topics: Vec<String>,
    /// First collaboration date
    pub first_collaboration: DateTime<Utc>,
    /// Last collaboration date
    pub last_collaboration: DateTime<Utc>,
}

/// Research community/cluster
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ResearchCommunity {
    /// Community ID
    pub community_id: String,
    /// Community members (author IDs)
    pub members: Vec<String>,
    /// Community topics
    pub topics: Vec<String>,
    /// Central/influential members
    pub central_members: Vec<String>,
    /// Community coherence score
    pub coherence_score: f64,
    /// Community size
    pub size: usize,
}

/// Temporal collaboration pattern
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TemporalCollaboration {
    /// Author ID
    pub author_id: String,
    /// Time period
    pub timestamp: DateTime<Utc>,
    /// Active collaborations in this period
    pub active_collaborations: u32,
    /// New collaborations formed
    pub new_collaborations: u32,
}

/// Topic model for research areas
#[derive(Debug, Clone)]
pub struct TopicModel {
    /// Topic ID
    pub topic_id: String,
    /// Topic name/label
    pub topic_name: String,
    /// Topic words with probabilities
    pub topic_words: Vec<(String, f64)>,
    /// Document-topic distribution
    pub document_topics: HashMap<String, f64>,
    /// Topic coherence score
    pub coherence_score: f64,
    /// Topic trend over time
    pub temporal_trend: Vec<TopicTrend>,
}

/// Topic trend over time
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TopicTrend {
    /// Time period
    pub timestamp: DateTime<Utc>,
    /// Topic popularity/frequency
    pub popularity: f64,
    /// Number of publications in this topic
    pub publication_count: u64,
    /// Topic growth rate
    pub growth_rate: f64,
}

/// Impact prediction model
#[derive(Debug, Clone)]
pub struct ImpactPredictor {
    /// Feature weights for impact prediction
    pub feature_weights: HashMap<String, f64>,
    /// Model performance metrics
    pub performance_metrics: PredictionMetrics,
    /// Last model update
    pub last_update: DateTime<Utc>,
}

/// Prediction performance metrics
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PredictionMetrics {
    /// Mean absolute error
    pub mae: f64,
    /// Root mean square error
    pub rmse: f64,
    /// R-squared score
    pub r2_score: f64,
    /// Precision at different thresholds
    pub precision_at_k: HashMap<u32, f64>,
}

impl ResearchNetworkAnalyzer {
    /// Create new research network analyzer
    pub fn new(config: ResearchNetworkConfig) -> Self {
        Self {
            author_embeddings: Arc::new(RwLock::new(HashMap::new())),
            publication_embeddings: Arc::new(RwLock::new(HashMap::new())),
            citation_network: Arc::new(RwLock::new(CitationNetwork {
                citations: HashMap::new(),
                co_citations: HashMap::new(),
                bibliographic_coupling: HashMap::new(),
                temporal_patterns: HashMap::new(),
            })),
            collaboration_network: Arc::new(RwLock::new(CollaborationNetwork {
                collaborations: HashMap::new(),
                research_communities: Vec::new(),
                temporal_collaborations: HashMap::new(),
            })),
            topic_models: Arc::new(RwLock::new(HashMap::new())),
            author_profiles: Arc::new(RwLock::new(HashMap::new())),
            publication_metadata: Arc::new(RwLock::new(HashMap::new())),
            config,
            analysis_tasks: Vec::new(),
        }
    }

    /// Register (or replace) an author's identity metadata.
    ///
    /// [`generate_author_embedding`](Self::generate_author_embedding) reads
    /// the author's name/affiliations from this registry — there is no
    /// external author database to resolve them from otherwise, so a
    /// profile must be registered before an embedding can be generated.
    pub fn register_author_profile(&self, author_id: impl Into<String>, profile: AuthorProfile) {
        self.author_profiles
            .write()
            .expect("rwlock should not be poisoned")
            .insert(author_id.into(), profile);
    }

    /// Register (or replace) a publication's bibliographic metadata.
    ///
    /// [`generate_publication_embedding`](Self::generate_publication_embedding)
    /// reads title/abstract/authors/venue/year/DOI from this registry —
    /// there is no external publication database to resolve them from
    /// otherwise, so metadata must be registered before an embedding can be
    /// generated. Registering also makes the publication discoverable via
    /// `get_author_publications` for every
    /// author listed in `metadata.authors`.
    pub fn register_publication_metadata(
        &self,
        publication_id: impl Into<String>,
        metadata: PublicationMetadataInput,
    ) {
        self.publication_metadata
            .write()
            .expect("rwlock should not be poisoned")
            .insert(publication_id.into(), metadata);
    }

    /// Record a collaboration edge between two authors.
    ///
    /// Stored bidirectionally so
    /// `get_author_collaborations` can
    /// look it up from either author's perspective. This is the real,
    /// in-process data source backing collaboration-derived statistics
    /// (e.g. `collaboration_score` in [`AuthorEmbedding`]) — nothing here
    /// is synthesized.
    pub async fn add_collaboration(&self, collaboration: Collaboration) -> Result<()> {
        let mut network = self
            .collaboration_network
            .write()
            .expect("rwlock should not be poisoned");
        network
            .collaborations
            .entry(collaboration.author1.clone())
            .or_default()
            .push(collaboration.clone());
        if collaboration.author2 != collaboration.author1 {
            network
                .collaborations
                .entry(collaboration.author2.clone())
                .or_default()
                .push(collaboration);
        }
        Ok(())
    }

    /// Start background analysis tasks
    pub async fn start(&mut self) -> Result<()> {
        info!("Starting research network analysis system");

        // Start citation network analysis task
        let citation_task = self.start_citation_analysis().await;
        self.analysis_tasks.push(citation_task);

        // Start collaboration analysis task
        let collaboration_task = self.start_collaboration_analysis().await;
        self.analysis_tasks.push(collaboration_task);

        // Start impact prediction task
        let impact_task = self.start_impact_prediction().await;
        self.analysis_tasks.push(impact_task);

        // Start topic modeling task
        let topic_task = self.start_topic_modeling().await;
        self.analysis_tasks.push(topic_task);

        info!("Research network analysis system started successfully");
        Ok(())
    }

    /// Stop analysis tasks
    pub async fn stop(&mut self) {
        info!("Stopping research network analysis system");

        for task in self.analysis_tasks.drain(..) {
            task.abort();
        }

        info!("Research network analysis system stopped");
    }

    /// Generate author embedding based on publications and collaborations
    pub async fn generate_author_embedding(&self, author_id: &str) -> Result<AuthorEmbedding> {
        // Check if already computed
        {
            let embeddings = self
                .author_embeddings
                .read()
                .expect("rwlock should not be poisoned");
            if let Some(existing) = embeddings.get(author_id) {
                return Ok(existing.clone());
            }
        }

        info!("Generating author embedding for: {}", author_id);

        // Author identity metadata has no external database to resolve
        // against in this analyzer; it must have been registered via
        // `register_author_profile` beforehand. Failing loudly here avoids
        // fabricating a name/affiliation that looks resolved but is not.
        let profile = {
            let profiles = self
                .author_profiles
                .read()
                .expect("rwlock should not be poisoned");
            profiles.get(author_id).cloned()
        }
        .ok_or_else(|| {
            anyhow::anyhow!(
                "No author profile registered for '{author_id}': \
                 ResearchNetworkAnalyzer has no external author database to resolve \
                 name/affiliations from. Call register_author_profile({author_id}, ...) \
                 before generating an embedding."
            )
        })?;

        // Collect author's publications
        let author_publications = self.get_author_publications(author_id).await?;

        // Get collaboration information
        let collaborations = self.get_author_collaborations(author_id).await?;

        // Compute research topics
        let research_topics = self
            .extract_author_topics(author_id, &author_publications)
            .await?;

        // Calculate metrics
        let h_index = self.calculate_h_index(&author_publications).await?;
        let citation_count = author_publications.iter().map(|p| p.citation_count).sum();
        let collaboration_score = self.calculate_collaboration_score(&collaborations).await?;
        let impact_score = self.calculate_author_impact_score(author_id).await?;

        // Generate embedding vector
        let embedding = self
            .compute_author_embedding_vector(
                &author_publications,
                &collaborations,
                &research_topics,
            )
            .await?;

        // Determine career stage
        let career_stage = self
            .classify_career_stage(citation_count, author_publications.len() as u64, h_index)
            .await?;

        let author_embedding = AuthorEmbedding {
            author_id: author_id.to_string(),
            name: profile.name,
            affiliations: profile.affiliations,
            research_topics,
            h_index,
            citation_count,
            publication_count: author_publications.len() as u64,
            embedding,
            collaboration_score,
            impact_score,
            career_stage,
            last_updated: Utc::now(),
        };

        // Cache the result
        {
            let mut embeddings = self
                .author_embeddings
                .write()
                .expect("rwlock should not be poisoned");
            embeddings.insert(author_id.to_string(), author_embedding.clone());
        }

        info!(
            "Generated author embedding for {} with h-index: {:.2}",
            author_id, h_index
        );
        Ok(author_embedding)
    }

    /// Generate publication embedding based on content and citations
    pub async fn generate_publication_embedding(
        &self,
        publication_id: &str,
    ) -> Result<PublicationEmbedding> {
        // Check if already computed
        {
            let embeddings = self
                .publication_embeddings
                .read()
                .expect("rwlock should not be poisoned");
            if let Some(existing) = embeddings.get(publication_id) {
                return Ok(existing.clone());
            }
        }

        info!("Generating publication embedding for: {}", publication_id);

        // Publication metadata has no external database to resolve against
        // in this analyzer; it must have been registered via
        // `register_publication_metadata` beforehand. Failing loudly here
        // avoids fabricating a title/venue/year that looks resolved but is
        // not.
        let metadata = {
            let records = self
                .publication_metadata
                .read()
                .expect("rwlock should not be poisoned");
            records.get(publication_id).cloned()
        }
        .ok_or_else(|| {
            anyhow::anyhow!(
                "No metadata registered for publication '{publication_id}': \
                 ResearchNetworkAnalyzer has no external publication database to resolve \
                 title/venue/year from. Call register_publication_metadata({publication_id}, ...) \
                 before generating an embedding."
            )
        })?;
        let PublicationMetadataInput {
            title,
            abstract_text,
            authors,
            venue,
            year,
            doi,
        } = metadata;

        // Get citation information
        let citation_count = self.get_publication_citation_count(publication_id).await?;

        // Extract topics
        let topic_distribution = self
            .extract_publication_topics(publication_id, &abstract_text)
            .await?;

        // Generate content embedding
        let embedding = self
            .compute_publication_embedding_vector(&title, &abstract_text, &topic_distribution)
            .await?;

        // Predict impact
        let predicted_impact = self
            .predict_publication_impact(citation_count, &topic_distribution, &embedding)
            .await?;

        let publication_embedding = PublicationEmbedding {
            publication_id: publication_id.to_string(),
            title,
            abstract_text,
            authors,
            venue,
            year,
            citation_count,
            topic_distribution,
            embedding,
            predicted_impact,
            publication_type: PublicationType::JournalArticle, // Default
            doi,
            last_updated: Utc::now(),
        };

        // Cache the result
        {
            let mut embeddings = self
                .publication_embeddings
                .write()
                .expect("rwlock should not be poisoned");
            embeddings.insert(publication_id.to_string(), publication_embedding.clone());
        }

        info!(
            "Generated publication embedding for {} with predicted impact: {:.3}",
            publication_id, predicted_impact
        );
        Ok(publication_embedding)
    }

    /// Analyze citation patterns and relationships
    pub async fn analyze_citation_patterns(&self, publication_id: &str) -> Result<Vec<Citation>> {
        let network = self
            .citation_network
            .read()
            .expect("rwlock should not be poisoned");

        if let Some(citations) = network.citations.get(publication_id) {
            Ok(citations.clone())
        } else {
            Ok(Vec::new())
        }
    }

    /// Find similar authors based on research interests and collaboration patterns
    pub async fn find_similar_authors(
        &self,
        author_id: &str,
        k: usize,
    ) -> Result<Vec<(String, f64)>> {
        let target_embedding = self.generate_author_embedding(author_id).await?;
        let embeddings_data: Vec<(String, AuthorEmbedding)> = {
            let embeddings = self
                .author_embeddings
                .read()
                .expect("rwlock should not be poisoned");
            embeddings
                .iter()
                .filter(|(other_id, _)| *other_id != author_id)
                .map(|(id, emb)| (id.clone(), emb.clone()))
                .collect()
        };

        let mut similarities = Vec::new();

        for (other_id, other_embedding) in embeddings_data {
            let similarity = self
                .calculate_author_similarity(&target_embedding, &other_embedding)
                .await?;
            similarities.push((other_id, similarity));
        }

        // Sort by similarity and take top k
        similarities.sort_by(|a, b| {
            b.1.partial_cmp(&a.1)
                .expect("similarity scores should be comparable")
        });
        similarities.truncate(k);

        Ok(similarities)
    }

    /// Predict research impact for a publication
    pub async fn predict_research_impact(&self, publication_id: &str) -> Result<f64> {
        let publication = self.generate_publication_embedding(publication_id).await?;
        Ok(publication.predicted_impact)
    }

    /// Analyze research trends over time
    pub async fn analyze_research_trends(
        &self,
        topic: &str,
        years: u32,
    ) -> Result<Vec<TopicTrend>> {
        let topics = self
            .topic_models
            .read()
            .expect("rwlock should not be poisoned");

        if let Some(topic_model) = topics.get(topic) {
            // Filter trends for the specified time period
            let cutoff_date = Utc::now() - chrono::Duration::days((years * 365) as i64);
            let recent_trends: Vec<TopicTrend> = topic_model
                .temporal_trend
                .iter()
                .filter(|trend| trend.timestamp > cutoff_date)
                .cloned()
                .collect();

            Ok(recent_trends)
        } else {
            Ok(Vec::new())
        }
    }

    /// Get research communities/clusters
    pub async fn get_research_communities(&self) -> Result<Vec<ResearchCommunity>> {
        let network = self
            .collaboration_network
            .read()
            .expect("rwlock should not be poisoned");
        Ok(network.research_communities.clone())
    }

    /// Update citation network with new citation
    pub async fn add_citation(&self, citation: Citation) -> Result<()> {
        let mut network = self
            .citation_network
            .write()
            .expect("rwlock should not be poisoned");

        network
            .citations
            .entry(citation.citing_paper.clone())
            .or_default()
            .push(citation);

        info!("Added new citation to network");
        Ok(())
    }

    // ===== PRIVATE HELPER METHODS =====

    /// Publications by `author_id`, drawn from
    /// [`publication_metadata`](Self::publication_metadata) (real
    /// registered data — nothing here is fabricated). An author with no
    /// registered publications simply has none; that is a legitimate
    /// (non-error) state, unlike missing identity metadata.
    async fn get_author_publications(&self, author_id: &str) -> Result<Vec<PublicationEmbedding>> {
        let publication_ids: Vec<String> = {
            let records = self
                .publication_metadata
                .read()
                .expect("rwlock should not be poisoned");
            records
                .iter()
                .filter(|(_, metadata)| metadata.authors.iter().any(|a| a == author_id))
                .map(|(id, _)| id.clone())
                .collect()
        };

        let mut publications = Vec::with_capacity(publication_ids.len());
        for publication_id in publication_ids {
            publications.push(self.generate_publication_embedding(&publication_id).await?);
        }
        Ok(publications)
    }

    /// Collaboration edges for `author_id`, drawn from
    /// [`collaboration_network`](Self::collaboration_network) as recorded
    /// via [`add_collaboration`](Self::add_collaboration) (real in-process
    /// data — nothing here is fabricated).
    async fn get_author_collaborations(&self, author_id: &str) -> Result<Vec<Collaboration>> {
        let network = self
            .collaboration_network
            .read()
            .expect("rwlock should not be poisoned");
        Ok(network
            .collaborations
            .get(author_id)
            .cloned()
            .unwrap_or_default())
    }

    async fn extract_author_topics(
        &self,
        _author_id: &str,
        _publications: &[PublicationEmbedding],
    ) -> Result<Vec<String>> {
        // Placeholder - would perform topic extraction
        Ok(vec![
            "machine_learning".to_string(),
            "natural_language_processing".to_string(),
        ])
    }

    async fn calculate_h_index(&self, publications: &[PublicationEmbedding]) -> Result<f64> {
        let mut citation_counts: Vec<u64> = publications.iter().map(|p| p.citation_count).collect();

        citation_counts.sort_by(|a, b| b.cmp(a));

        let mut h_index = 0;
        for (i, &citations) in citation_counts.iter().enumerate() {
            if citations >= (i + 1) as u64 {
                h_index = i + 1;
            } else {
                break;
            }
        }

        Ok(h_index as f64)
    }

    async fn calculate_collaboration_score(&self, collaborations: &[Collaboration]) -> Result<f64> {
        if collaborations.is_empty() {
            return Ok(0.0);
        }

        let total_strength: f64 = collaborations.iter().map(|c| c.strength).sum();
        Ok(total_strength / collaborations.len() as f64)
    }

    async fn calculate_author_impact_score(&self, _author_id: &str) -> Result<f64> {
        // Placeholder - would calculate based on citations, h-index, collaboration network position
        Ok(0.75)
    }

    async fn compute_author_embedding_vector(
        &self,
        _publications: &[PublicationEmbedding],
        _collaborations: &[Collaboration],
        _topics: &[String],
    ) -> Result<Vector> {
        // Placeholder - would compute actual embedding
        let values = (0..self.config.embedding_dimension)
            .map(|_| {
                let mut random = Random::default();
                random.random::<f32>()
            })
            .collect();
        Ok(Vector::new(values))
    }

    async fn classify_career_stage(
        &self,
        citation_count: u64,
        publication_count: u64,
        h_index: f64,
    ) -> Result<CareerStage> {
        if citation_count < 100 && publication_count < 10 && h_index < 5.0 {
            Ok(CareerStage::EarlyCareer)
        } else if citation_count < 1000 && publication_count < 50 && h_index < 20.0 {
            Ok(CareerStage::MidCareer)
        } else if citation_count >= 1000 || publication_count >= 50 || h_index >= 20.0 {
            Ok(CareerStage::SeniorCareer)
        } else {
            Ok(CareerStage::Unknown)
        }
    }

    async fn get_publication_citation_count(&self, _publication_id: &str) -> Result<u64> {
        // Placeholder - would query citation database
        let mut random = Random::default();
        Ok(random.random::<u64>() % 100)
    }

    async fn extract_publication_topics(
        &self,
        _publication_id: &str,
        _abstract_text: &str,
    ) -> Result<Vec<f64>> {
        // Placeholder - would perform topic modeling
        let num_topics = self.config.topic_config.num_topics;
        let mut distribution = vec![0.0; num_topics];

        // Generate random distribution that sums to 1.0
        let total: f64 = (0..num_topics)
            .map(|_| {
                let mut random = Random::default();
                random.random::<f64>()
            })
            .sum();
        for item in distribution.iter_mut().take(num_topics) {
            let mut random = Random::default();
            *item = random.random::<f64>() / total;
        }

        Ok(distribution)
    }

    async fn compute_publication_embedding_vector(
        &self,
        _title: &str,
        _abstract_text: &str,
        _topic_distribution: &[f64],
    ) -> Result<Vector> {
        // Placeholder - would compute actual embedding
        let values = (0..self.config.embedding_dimension)
            .map(|_| {
                let mut random = Random::default();
                random.random::<f32>()
            })
            .collect();
        Ok(Vector::new(values))
    }

    async fn predict_publication_impact(
        &self,
        citation_count: u64,
        _topic_distribution: &[f64],
        _embedding: &Vector,
    ) -> Result<f64> {
        // Placeholder - would use trained impact prediction model
        let base_impact = (citation_count as f64).ln() / 10.0;
        Ok(base_impact.clamp(0.0, 1.0))
    }

    async fn calculate_author_similarity(
        &self,
        author1: &AuthorEmbedding,
        author2: &AuthorEmbedding,
    ) -> Result<f64> {
        // Calculate cosine similarity between embeddings
        let embedding1 = &author1.embedding.values;
        let embedding2 = &author2.embedding.values;

        let dot_product: f32 = embedding1
            .iter()
            .zip(embedding2.iter())
            .map(|(a, b)| a * b)
            .sum();
        let norm1: f32 = embedding1.iter().map(|x| x * x).sum::<f32>().sqrt();
        let norm2: f32 = embedding2.iter().map(|x| x * x).sum::<f32>().sqrt();

        let cosine_similarity = if norm1 > 0.0 && norm2 > 0.0 {
            dot_product / (norm1 * norm2)
        } else {
            0.0
        };

        // Combine with topic similarity
        let topic_similarity = self
            .calculate_topic_similarity(&author1.research_topics, &author2.research_topics)
            .await?;

        // Weighted combination
        let final_similarity = 0.7 * cosine_similarity as f64 + 0.3 * topic_similarity;

        Ok(final_similarity)
    }

    async fn calculate_topic_similarity(
        &self,
        topics1: &[String],
        topics2: &[String],
    ) -> Result<f64> {
        let set1: HashSet<_> = topics1.iter().collect();
        let set2: HashSet<_> = topics2.iter().collect();

        let intersection = set1.intersection(&set2).count();
        let union = set1.union(&set2).count();

        if union > 0 {
            Ok(intersection as f64 / union as f64)
        } else {
            Ok(0.0)
        }
    }

    // ===== BACKGROUND ANALYSIS TASKS =====

    async fn start_citation_analysis(&self) -> JoinHandle<()> {
        let _citation_network = Arc::clone(&self.citation_network);
        let interval =
            std::time::Duration::from_secs(self.config.citation_update_interval_hours * 3600);

        tokio::spawn(async move {
            let mut interval_timer = tokio::time::interval(interval);

            loop {
                interval_timer.tick().await;

                // Perform citation network analysis
                info!("Performing citation network analysis");

                // Placeholder for actual analysis
                // Would analyze citation patterns, identify influential papers, etc.

                debug!("Citation network analysis completed");
            }
        })
    }

    async fn start_collaboration_analysis(&self) -> JoinHandle<()> {
        let _collaboration_network = Arc::clone(&self.collaboration_network);
        let interval = std::time::Duration::from_secs(
            self.config.collaboration_analysis_interval_hours * 3600,
        );

        tokio::spawn(async move {
            let mut interval_timer = tokio::time::interval(interval);

            loop {
                interval_timer.tick().await;

                // Perform collaboration network analysis
                info!("Performing collaboration network analysis");

                // Placeholder for actual analysis
                // Would detect research communities, analyze collaboration patterns, etc.

                debug!("Collaboration network analysis completed");
            }
        })
    }

    async fn start_impact_prediction(&self) -> JoinHandle<()> {
        let interval =
            std::time::Duration::from_secs(self.config.impact_prediction_refresh_hours * 3600);

        tokio::spawn(async move {
            let mut interval_timer = tokio::time::interval(interval);

            loop {
                interval_timer.tick().await;

                // Refresh impact prediction models
                info!("Refreshing impact prediction models");

                // Placeholder for actual model training/updating
                // Would retrain models based on recent citation data

                debug!("Impact prediction models refreshed");
            }
        })
    }

    async fn start_topic_modeling(&self) -> JoinHandle<()> {
        let topic_models = Arc::clone(&self.topic_models);
        let _config = self.config.clone();
        let interval = std::time::Duration::from_secs(24 * 3600); // Daily

        tokio::spawn(async move {
            let mut interval_timer = tokio::time::interval(interval);

            loop {
                interval_timer.tick().await;

                // Update topic models
                info!("Updating topic models");

                // Create sample topic model
                let topic_model = TopicModel {
                    topic_id: "machine_learning".to_string(),
                    topic_name: "Machine Learning".to_string(),
                    topic_words: vec![
                        ("neural".to_string(), 0.1),
                        ("network".to_string(), 0.09),
                        ("learning".to_string(), 0.08),
                        ("algorithm".to_string(), 0.07),
                        ("model".to_string(), 0.06),
                    ],
                    document_topics: HashMap::new(),
                    coherence_score: 0.75,
                    temporal_trend: vec![
                        TopicTrend {
                            timestamp: Utc::now() - chrono::Duration::days(365),
                            popularity: 0.6,
                            publication_count: 1000,
                            growth_rate: 0.15,
                        },
                        TopicTrend {
                            timestamp: Utc::now(),
                            popularity: 0.8,
                            publication_count: 1500,
                            growth_rate: 0.25,
                        },
                    ],
                };

                {
                    let mut models = topic_models.write().expect("rwlock should not be poisoned");
                    models.insert("machine_learning".to_string(), topic_model);
                }

                debug!("Topic models updated");
            }
        })
    }
}

/// Research network metrics and statistics
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct NetworkMetrics {
    /// Total number of authors
    pub total_authors: usize,
    /// Total number of publications
    pub total_publications: usize,
    /// Total number of citations
    pub total_citations: u64,
    /// Average citations per paper
    pub avg_citations_per_paper: f64,
    /// Network density
    pub network_density: f64,
    /// Clustering coefficient
    pub clustering_coefficient: f64,
    /// Average path length
    pub average_path_length: f64,
    /// Most influential authors
    pub top_authors: Vec<String>,
    /// Trending topics
    pub trending_topics: Vec<String>,
}

impl ResearchNetworkAnalyzer {
    /// Get comprehensive network metrics
    pub async fn get_network_metrics(&self) -> Result<NetworkMetrics> {
        let author_embeddings = self
            .author_embeddings
            .read()
            .expect("rwlock should not be poisoned");
        let publication_embeddings = self
            .publication_embeddings
            .read()
            .expect("rwlock should not be poisoned");

        let total_authors = author_embeddings.len();
        let total_publications = publication_embeddings.len();
        let total_citations = publication_embeddings
            .values()
            .map(|p| p.citation_count)
            .sum();

        let avg_citations_per_paper = if total_publications > 0 {
            total_citations as f64 / total_publications as f64
        } else {
            0.0
        };

        // Get top authors by impact score
        let mut author_scores: Vec<_> = author_embeddings
            .iter()
            .map(|(id, embedding)| (id.clone(), embedding.impact_score))
            .collect();
        author_scores.sort_by(|a, b| {
            b.1.partial_cmp(&a.1)
                .expect("similarity scores should be comparable")
        });
        let top_authors: Vec<String> = author_scores
            .into_iter()
            .take(10)
            .map(|(id, _)| id)
            .collect();

        let (network_density, clustering_coefficient, average_path_length) =
            Self::compute_coauthorship_graph_metrics(&publication_embeddings);

        Ok(NetworkMetrics {
            total_authors,
            total_publications,
            total_citations,
            avg_citations_per_paper,
            network_density,
            clustering_coefficient,
            average_path_length,
            top_authors,
            trending_topics: vec!["machine_learning".to_string(), "deep_learning".to_string()],
        })
    }

    /// Compute real co-authorship graph statistics — network density, average
    /// local clustering coefficient, and average shortest-path length —
    /// from the authors actually listed on each cached publication, instead
    /// of hardcoded placeholder constants.
    fn compute_coauthorship_graph_metrics(
        publication_embeddings: &HashMap<String, PublicationEmbedding>,
    ) -> (f64, f64, f64) {
        use std::collections::VecDeque;

        let mut adjacency: HashMap<String, HashSet<String>> = HashMap::new();
        for publication in publication_embeddings.values() {
            for i in 0..publication.authors.len() {
                for j in (i + 1)..publication.authors.len() {
                    let a = &publication.authors[i];
                    let b = &publication.authors[j];
                    if a == b {
                        continue;
                    }
                    adjacency.entry(a.clone()).or_default().insert(b.clone());
                    adjacency.entry(b.clone()).or_default().insert(a.clone());
                }
            }
        }

        let node_count = adjacency.len();
        if node_count < 2 {
            return (0.0, 0.0, 0.0);
        }

        let edge_count: usize = adjacency
            .values()
            .map(|neighbors| neighbors.len())
            .sum::<usize>()
            / 2;
        let max_edges = (node_count * (node_count - 1)) as f64 / 2.0;
        let network_density = if max_edges > 0.0 {
            edge_count as f64 / max_edges
        } else {
            0.0
        };

        // Average local clustering coefficient: for each node, the fraction
        // of its neighbor-pairs that are themselves connected.
        let mut clustering_sum = 0.0;
        let mut clustering_count = 0usize;
        for neighbors in adjacency.values() {
            let degree = neighbors.len();
            if degree < 2 {
                continue;
            }
            let neighbor_list: Vec<&String> = neighbors.iter().collect();
            let mut links = 0usize;
            for i in 0..neighbor_list.len() {
                for j in (i + 1)..neighbor_list.len() {
                    if adjacency
                        .get(neighbor_list[i].as_str())
                        .is_some_and(|n| n.contains(neighbor_list[j].as_str()))
                    {
                        links += 1;
                    }
                }
            }
            let possible = (degree * (degree - 1)) / 2;
            clustering_sum += links as f64 / possible as f64;
            clustering_count += 1;
        }
        let clustering_coefficient = if clustering_count > 0 {
            clustering_sum / clustering_count as f64
        } else {
            0.0
        };

        // Average shortest-path length via BFS, bounded to a deterministic
        // sample of source nodes so this stays cheap on very large networks.
        const MAX_BFS_SOURCES: usize = 200;
        let mut node_ids: Vec<&String> = adjacency.keys().collect();
        node_ids.sort();
        node_ids.truncate(MAX_BFS_SOURCES);

        let mut total_distance = 0.0f64;
        let mut total_pairs = 0usize;
        for source in &node_ids {
            let mut visited: HashSet<&String> = HashSet::new();
            visited.insert(source);
            let mut queue: VecDeque<(&String, usize)> = VecDeque::new();
            queue.push_back((source, 0));
            while let Some((node, dist)) = queue.pop_front() {
                if dist > 0 {
                    total_distance += dist as f64;
                    total_pairs += 1;
                }
                if let Some(neighbors) = adjacency.get(node.as_str()) {
                    for neighbor in neighbors {
                        if visited.insert(neighbor) {
                            queue.push_back((neighbor, dist + 1));
                        }
                    }
                }
            }
        }
        let average_path_length = if total_pairs > 0 {
            total_distance / total_pairs as f64
        } else {
            0.0
        };

        (network_density, clustering_coefficient, average_path_length)
    }
}

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

    fn make_publication(id: &str, authors: &[&str]) -> PublicationEmbedding {
        PublicationEmbedding {
            publication_id: id.to_string(),
            title: format!("Title {id}"),
            abstract_text: String::new(),
            authors: authors.iter().map(|a| a.to_string()).collect(),
            venue: "Venue".to_string(),
            year: 2024,
            citation_count: 0,
            topic_distribution: vec![],
            embedding: Vector::new(vec![0.0; 4]),
            predicted_impact: 0.0,
            publication_type: PublicationType::JournalArticle,
            doi: None,
            last_updated: Utc::now(),
        }
    }

    /// Regression test: co-authorship graph statistics must be computed for
    /// real from actual publication author lists, instead of the hardcoded
    /// (0.1, 0.3, 4.5) placeholder tuple.
    #[test]
    fn test_compute_coauthorship_graph_metrics_triangle() {
        // A, B, C all co-author one paper together => a complete triangle:
        // density = 1.0, clustering coefficient = 1.0, avg path length = 1.0.
        let mut publications = HashMap::new();
        publications.insert("p1".to_string(), make_publication("p1", &["A", "B", "C"]));

        let (density, clustering, avg_path) =
            ResearchNetworkAnalyzer::compute_coauthorship_graph_metrics(&publications);

        assert!((density - 1.0).abs() < 1e-9, "density = {density}");
        assert!((clustering - 1.0).abs() < 1e-9, "clustering = {clustering}");
        assert!((avg_path - 1.0).abs() < 1e-9, "avg_path = {avg_path}");
    }

    #[test]
    fn test_compute_coauthorship_graph_metrics_empty() {
        let publications = HashMap::new();
        let (density, clustering, avg_path) =
            ResearchNetworkAnalyzer::compute_coauthorship_graph_metrics(&publications);
        assert_eq!(density, 0.0);
        assert_eq!(clustering, 0.0);
        assert_eq!(avg_path, 0.0);
    }

    #[tokio::test]
    async fn test_research_network_analyzer_creation() {
        let config = ResearchNetworkConfig::default();
        let analyzer = ResearchNetworkAnalyzer::new(config);

        // Test that analyzer is created successfully
        assert_eq!(
            analyzer
                .author_embeddings
                .read()
                .expect("rwlock should not be poisoned")
                .len(),
            0
        );
        assert_eq!(
            analyzer
                .publication_embeddings
                .read()
                .expect("rwlock should not be poisoned")
                .len(),
            0
        );
    }

    /// Regression: generating an embedding for an author with no registered
    /// profile must fail loudly rather than fabricate a name/affiliation
    /// (there is no external author database to resolve them from).
    #[tokio::test]
    async fn test_author_embedding_requires_registered_profile() {
        let config = ResearchNetworkConfig::default();
        let analyzer = ResearchNetworkAnalyzer::new(config);

        let result = analyzer
            .generate_author_embedding("unregistered_author")
            .await;
        assert!(result.is_err());
        let msg = result.expect_err("must fail loudly").to_string();
        assert!(msg.contains("unregistered_author"));
        assert!(msg.contains("register_author_profile"));
    }

    #[tokio::test]
    async fn test_author_embedding_generation() {
        let config = ResearchNetworkConfig::default();
        let analyzer = ResearchNetworkAnalyzer::new(config);
        analyzer.register_author_profile(
            "test_author",
            AuthorProfile {
                name: "Dr. Ada Example".to_string(),
                affiliations: vec!["Example University".to_string()],
            },
        );

        let result = analyzer.generate_author_embedding("test_author").await;
        assert!(result.is_ok());

        let embedding = result.expect("should succeed");
        assert_eq!(embedding.author_id, "test_author");
        // Real metadata, not the old `Author_{id}` / `Unknown` fabrication.
        assert_eq!(embedding.name, "Dr. Ada Example");
        assert_eq!(
            embedding.affiliations,
            vec!["Example University".to_string()]
        );
        assert!(embedding.h_index >= 0.0);
        assert_eq!(embedding.embedding.values.len(), 512); // Default dimension
    }

    /// Regression: generating an embedding for a publication with no
    /// registered metadata must fail loudly rather than fabricate a
    /// title/venue/year (there is no external publication database to
    /// resolve them from).
    #[tokio::test]
    async fn test_publication_embedding_requires_registered_metadata() {
        let config = ResearchNetworkConfig::default();
        let analyzer = ResearchNetworkAnalyzer::new(config);

        let result = analyzer
            .generate_publication_embedding("unregistered_publication")
            .await;
        assert!(result.is_err());
        let msg = result.expect_err("must fail loudly").to_string();
        assert!(msg.contains("unregistered_publication"));
        assert!(msg.contains("register_publication_metadata"));
    }

    #[tokio::test]
    async fn test_publication_embedding_generation() {
        let config = ResearchNetworkConfig::default();
        let analyzer = ResearchNetworkAnalyzer::new(config);
        analyzer.register_publication_metadata(
            "test_publication",
            PublicationMetadataInput {
                title: "A Study of RDF Embeddings".to_string(),
                abstract_text: "We study embeddings of RDF graphs.".to_string(),
                authors: vec!["test_author".to_string()],
                venue: "Journal of Semantic Web".to_string(),
                year: 2025,
                doi: Some("10.1234/example".to_string()),
            },
        );

        let result = analyzer
            .generate_publication_embedding("test_publication")
            .await;
        assert!(result.is_ok());

        let embedding = result.expect("should succeed");
        assert_eq!(embedding.publication_id, "test_publication");
        // Real metadata, not the old `Publication_{id}` / "Unknown Venue" fabrication.
        assert_eq!(embedding.title, "A Study of RDF Embeddings");
        assert_eq!(embedding.venue, "Journal of Semantic Web");
        assert_eq!(embedding.year, 2025);
        assert!(embedding.predicted_impact >= 0.0);
        assert!(embedding.predicted_impact <= 1.0);
    }

    /// Regression: an author's publications and collaborations are drawn
    /// from real registered/recorded data, not fabricated.
    #[tokio::test]
    async fn test_author_publications_and_collaborations_are_real() {
        let config = ResearchNetworkConfig::default();
        let analyzer = ResearchNetworkAnalyzer::new(config);

        analyzer.register_author_profile(
            "author_a",
            AuthorProfile {
                name: "Author A".to_string(),
                affiliations: vec!["Uni A".to_string()],
            },
        );
        analyzer.register_publication_metadata(
            "pub1",
            PublicationMetadataInput {
                title: "Paper One".to_string(),
                abstract_text: String::new(),
                authors: vec!["author_a".to_string()],
                venue: "Venue".to_string(),
                year: 2024,
                doi: None,
            },
        );
        analyzer
            .add_collaboration(Collaboration {
                author1: "author_a".to_string(),
                author2: "author_b".to_string(),
                joint_publications: 1,
                strength: 0.8,
                shared_topics: vec![],
                first_collaboration: Utc::now(),
                last_collaboration: Utc::now(),
            })
            .await
            .expect("should succeed");

        let publications = analyzer
            .get_author_publications("author_a")
            .await
            .expect("should succeed");
        assert_eq!(publications.len(), 1);
        assert_eq!(publications[0].title, "Paper One");

        let collaborations = analyzer
            .get_author_collaborations("author_a")
            .await
            .expect("should succeed");
        assert_eq!(collaborations.len(), 1);
        assert_eq!(collaborations[0].author2, "author_b");

        // The collaboration is also visible from the other author's side.
        let collaborations_b = analyzer
            .get_author_collaborations("author_b")
            .await
            .expect("should succeed");
        assert_eq!(collaborations_b.len(), 1);
        assert_eq!(collaborations_b[0].author1, "author_a");
    }

    #[tokio::test]
    async fn test_h_index_calculation() {
        let config = ResearchNetworkConfig::default();
        let analyzer = ResearchNetworkAnalyzer::new(config);

        // Create test publications with different citation counts
        let publications = vec![
            PublicationEmbedding {
                publication_id: "p1".to_string(),
                title: "Test 1".to_string(),
                abstract_text: "Abstract 1".to_string(),
                authors: vec!["author1".to_string()],
                venue: "Venue 1".to_string(),
                year: 2023,
                citation_count: 10,
                topic_distribution: vec![],
                embedding: Vector::new(vec![]),
                predicted_impact: 0.5,
                publication_type: PublicationType::JournalArticle,
                doi: None,
                last_updated: Utc::now(),
            },
            PublicationEmbedding {
                publication_id: "p2".to_string(),
                title: "Test 2".to_string(),
                abstract_text: "Abstract 2".to_string(),
                authors: vec!["author1".to_string()],
                venue: "Venue 2".to_string(),
                year: 2023,
                citation_count: 5,
                topic_distribution: vec![],
                embedding: Vector::new(vec![]),
                predicted_impact: 0.3,
                publication_type: PublicationType::JournalArticle,
                doi: None,
                last_updated: Utc::now(),
            },
        ];

        let h_index = analyzer
            .calculate_h_index(&publications)
            .await
            .expect("should succeed");
        assert_eq!(h_index, 2.0); // Both papers have at least 2 citations
    }

    #[test]
    fn test_career_stage_classification() {
        // Test early career
        let rt = tokio::runtime::Runtime::new().expect("should succeed");
        let config = ResearchNetworkConfig::default();
        let analyzer = ResearchNetworkAnalyzer::new(config);

        let stage = rt
            .block_on(analyzer.classify_career_stage(50, 5, 3.0))
            .expect("should succeed");
        assert!(matches!(stage, CareerStage::EarlyCareer));

        // Test senior career
        let stage = rt
            .block_on(analyzer.classify_career_stage(2000, 100, 25.0))
            .expect("should succeed");
        assert!(matches!(stage, CareerStage::SeniorCareer));
    }

    #[tokio::test]
    async fn test_network_metrics() {
        let config = ResearchNetworkConfig::default();
        let analyzer = ResearchNetworkAnalyzer::new(config);

        // Add some test data
        analyzer.register_author_profile(
            "test_author",
            AuthorProfile {
                name: "Test Author".to_string(),
                affiliations: vec!["Test University".to_string()],
            },
        );
        analyzer.register_publication_metadata(
            "test_publication",
            PublicationMetadataInput {
                title: "Test Publication".to_string(),
                abstract_text: String::new(),
                authors: vec!["test_author".to_string()],
                venue: "Test Venue".to_string(),
                year: 2025,
                doi: None,
            },
        );
        let _author_embedding = analyzer
            .generate_author_embedding("test_author")
            .await
            .expect("should succeed");
        let _publication_embedding = analyzer
            .generate_publication_embedding("test_publication")
            .await
            .expect("should succeed");

        let metrics = analyzer
            .get_network_metrics()
            .await
            .expect("should succeed");
        assert_eq!(metrics.total_authors, 1);
        assert_eq!(metrics.total_publications, 1);
    }
}