scribe-graph 0.5.1

Graph-based code representation and analysis for Scribe
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
//! # Centrality Calculator with Heuristics Integration
//!
//! Main interface for PageRank centrality calculation and integration with the
//! heuristic scoring system used by Scribe. This module provides the high-level API for:
//!
//! ## Key Features
//! - **PageRank Centrality Computation**: Research-grade algorithm with convergence detection
//! - **Import Graph Construction**: Builds dependency graphs from file scan results  
//! - **Heuristics Integration**: Seamless integration with V2 scoring system
//! - **Performance Optimization**: Efficient computation for large codebases
//! - **Multi-language Support**: Import detection across programming languages
//! - **Comprehensive Analysis**: Full graph statistics and structural insights
//!
//! ## Integration with Scribe Heuristics
//! The centrality scores are integrated into the heuristic scoring formula:
//! ```text
//! final_score = Σ(weight_i × normalized_score_i) + priority_boost + template_boost
//! ```
//! Where `centrality_score` becomes a weighted component when V2 features are enabled.

use rayon::prelude::*;
use scribe_analysis::heuristics::ScanResult;
use scribe_core::{file, Result};
use serde::{Deserialize, Serialize};
use std::collections::{HashMap, HashSet};
use std::path::{Path, PathBuf};

use crate::graph::{DependencyGraph, NodeId};
use crate::pagerank::{PageRankComputer, PageRankConfig, PageRankResults};
use crate::statistics::{GraphAnalysisResults, GraphStatisticsAnalyzer};

/// Complete centrality calculation results with comprehensive metadata
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct CentralityResults {
    /// PageRank scores (file path -> centrality score)
    pub pagerank_scores: HashMap<NodeId, f64>,

    /// Graph analysis results
    pub graph_analysis: GraphAnalysisResults,

    /// PageRank computation details
    pub pagerank_details: PageRankResults,

    /// Import detection statistics
    pub import_stats: ImportDetectionStats,

    /// Integration metadata
    pub integration_metadata: IntegrationMetadata,
}

/// Statistics about import detection and graph construction
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct ImportDetectionStats {
    /// Number of files processed for import detection
    pub files_processed: usize,

    /// Number of import relationships detected
    pub imports_detected: usize,

    /// Number of resolved imports (mapped to actual files)
    pub imports_resolved: usize,

    /// Import resolution success rate
    pub resolution_rate: f64,

    /// Language breakdown of processed files
    pub language_breakdown: HashMap<String, usize>,

    /// Import patterns by language
    pub import_patterns: HashMap<String, ImportPatternStats>,
}

/// Import pattern statistics for a specific language
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct ImportPatternStats {
    /// Total imports found
    pub total_imports: usize,

    /// Relative imports (./,../)
    pub relative_imports: usize,

    /// Absolute imports
    pub absolute_imports: usize,

    /// Standard library imports
    pub stdlib_imports: usize,

    /// Third-party imports
    pub third_party_imports: usize,
}

/// Metadata about centrality-heuristics integration
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct IntegrationMetadata {
    /// When the analysis was performed
    pub timestamp: chrono::DateTime<chrono::Utc>,

    /// Total computation time
    pub computation_time_ms: u64,

    /// Whether centrality was successfully integrated
    pub integration_successful: bool,

    /// Centrality weight used in integration
    pub centrality_weight: f64,

    /// Number of files with centrality scores
    pub files_with_centrality: usize,

    /// Configuration used
    pub config: CentralityConfig,
}

/// Configuration for centrality calculation
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct CentralityConfig {
    /// PageRank algorithm configuration
    pub pagerank_config: PageRankConfig,

    /// Whether to perform expensive graph analysis
    pub analyze_graph_structure: bool,

    /// Import resolution configuration
    pub import_resolution: ImportResolutionConfig,

    /// Integration parameters
    pub integration: IntegrationConfig,
}

/// Configuration for import resolution
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct ImportResolutionConfig {
    /// Maximum search depth for import resolution
    pub max_search_depth: usize,

    /// Whether to resolve relative imports
    pub resolve_relative_imports: bool,

    /// Whether to resolve absolute imports
    pub resolve_absolute_imports: bool,

    /// Whether to exclude standard library imports
    pub exclude_stdlib_imports: bool,

    /// Custom import path mappings
    pub path_mappings: HashMap<String, String>,
}

/// Configuration for heuristics integration
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct IntegrationConfig {
    /// Weight for centrality in final score
    pub centrality_weight: f64,

    /// Normalization method for centrality scores
    pub normalization_method: NormalizationMethod,

    /// Minimum centrality score threshold
    pub min_centrality_threshold: f64,

    /// Whether to boost entrypoint centrality
    pub boost_entrypoints: bool,

    /// Entrypoint boost factor
    pub entrypoint_boost_factor: f64,
}

/// Methods for normalizing centrality scores
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub enum NormalizationMethod {
    /// Normalize to \[0,1\] range
    MinMax,
    /// Z-score normalization
    ZScore,
    /// Rank-based normalization
    Rank,
    /// No normalization
    None,
}

impl Default for CentralityConfig {
    fn default() -> Self {
        Self {
            pagerank_config: PageRankConfig::for_code_analysis(),
            analyze_graph_structure: true,
            import_resolution: ImportResolutionConfig::default(),
            integration: IntegrationConfig::default(),
        }
    }
}

impl Default for ImportResolutionConfig {
    fn default() -> Self {
        Self {
            max_search_depth: 3,
            resolve_relative_imports: true,
            resolve_absolute_imports: true,
            exclude_stdlib_imports: true,
            path_mappings: HashMap::new(),
        }
    }
}

impl Default for IntegrationConfig {
    fn default() -> Self {
        Self {
            centrality_weight: 0.15, // 15% weight in V2 scoring
            normalization_method: NormalizationMethod::MinMax,
            min_centrality_threshold: 1e-6,
            boost_entrypoints: true,
            entrypoint_boost_factor: 1.5,
        }
    }
}

/// Main centrality calculator with heuristics integration
#[derive(Debug)]
pub struct CentralityCalculator {
    /// Configuration
    config: CentralityConfig,

    /// PageRank computer
    pagerank_computer: PageRankComputer,

    /// Graph statistics analyzer
    stats_analyzer: GraphStatisticsAnalyzer,

    /// Import detector
    import_detector: ImportDetector,
}

impl CentralityCalculator {
    /// Create a new centrality calculator with default configuration
    pub fn new() -> Result<Self> {
        let config = CentralityConfig::default();
        Self::with_config(config)
    }

    /// Create with custom configuration
    pub fn with_config(config: CentralityConfig) -> Result<Self> {
        let pagerank_computer = PageRankComputer::with_config(config.pagerank_config.clone())?;

        let stats_analyzer = if config.analyze_graph_structure {
            GraphStatisticsAnalyzer::new()
        } else {
            GraphStatisticsAnalyzer::for_large_graphs()
        };

        let import_detector = ImportDetector::with_config(config.import_resolution.clone());

        Ok(Self {
            config,
            pagerank_computer,
            stats_analyzer,
            import_detector,
        })
    }

    /// Create optimized for large codebases
    pub fn for_large_codebases() -> Result<Self> {
        let config = CentralityConfig {
            pagerank_config: PageRankConfig::for_large_codebases(),
            analyze_graph_structure: false,
            ..CentralityConfig::default()
        };
        Self::with_config(config)
    }

    /// Calculate centrality scores for a collection of scan results
    pub fn calculate_centrality<T>(&self, scan_results: &[T]) -> Result<CentralityResults>
    where
        T: ScanResult + Sync,
    {
        let start_time = std::time::Instant::now();

        // Build dependency graph from scan results
        let (graph, import_stats) = self.build_dependency_graph(scan_results)?;

        // Compute PageRank centrality
        let pagerank_results = self.pagerank_computer.compute(&graph)?;

        // Perform graph analysis if enabled
        let graph_analysis = if self.config.analyze_graph_structure {
            self.stats_analyzer.analyze(&graph)?
        } else {
            // Create minimal analysis for large graphs
            self.create_minimal_analysis(&graph)?
        };

        // Create integration metadata
        let computation_time = start_time.elapsed().as_millis() as u64;
        let integration_metadata = IntegrationMetadata {
            timestamp: chrono::Utc::now(),
            computation_time_ms: computation_time,
            integration_successful: true,
            centrality_weight: self.config.integration.centrality_weight,
            files_with_centrality: pagerank_results.scores.len(),
            config: self.config.clone(),
        };

        Ok(CentralityResults {
            pagerank_scores: pagerank_results.scores.clone(),
            graph_analysis,
            pagerank_details: pagerank_results,
            import_stats,
            integration_metadata,
        })
    }

    /// Integrate centrality scores with existing heuristic scores
    pub fn integrate_with_heuristics(
        &self,
        centrality_results: &CentralityResults,
        heuristic_scores: &HashMap<String, f64>,
    ) -> Result<HashMap<String, f64>> {
        let normalized_centrality = self
            .normalize_centrality_scores(&centrality_results.pagerank_scores, heuristic_scores)?;

        let mut integrated_scores = HashMap::new();
        let centrality_weight = self.config.integration.centrality_weight;
        let heuristic_weight = 1.0 - centrality_weight;

        // Combine heuristic and centrality scores
        for (file_path, heuristic_score) in heuristic_scores {
            let centrality_score = normalized_centrality.get(file_path).copied().unwrap_or(0.0);

            // Apply entrypoint boost if configured
            let boosted_centrality = if self.config.integration.boost_entrypoints
                && self.is_entrypoint_file(file_path)
            {
                centrality_score * self.config.integration.entrypoint_boost_factor
            } else {
                centrality_score
            };

            let integrated_score =
                heuristic_weight * heuristic_score + centrality_weight * boosted_centrality;

            integrated_scores.insert(file_path.clone(), integrated_score);
        }

        // Add centrality-only files (not in heuristic scores)
        for (file_path, centrality_score) in &normalized_centrality {
            if !integrated_scores.contains_key(file_path) {
                let boosted_centrality = if self.config.integration.boost_entrypoints
                    && self.is_entrypoint_file(file_path)
                {
                    centrality_score * self.config.integration.entrypoint_boost_factor
                } else {
                    *centrality_score
                };

                integrated_scores.insert(file_path.clone(), centrality_weight * boosted_centrality);
            }
        }

        Ok(integrated_scores)
    }

    /// Build dependency graph from scan results
    fn build_dependency_graph<T>(
        &self,
        scan_results: &[T],
    ) -> Result<(DependencyGraph, ImportDetectionStats)>
    where
        T: ScanResult + Sync,
    {
        let mut graph = DependencyGraph::with_capacity(scan_results.len());

        // Create optimized import detector with pre-computed lookup maps
        let mut optimized_detector =
            ImportDetector::with_file_index(self.import_detector.config.clone(), scan_results);

        // Add all files as nodes first
        for result in scan_results {
            graph.add_node(result.path().to_string())?;
        }

        // Detect imports and build edges using optimized detector
        let import_stats = if self.config.pagerank_config.use_parallel {
            self.build_edges_parallel_optimized(&mut graph, scan_results, &optimized_detector)?
        } else {
            self.build_edges_sequential_optimized(&mut graph, scan_results, &optimized_detector)?
        };

        Ok((graph, import_stats))
    }

    /// Build graph edges sequentially - OPTIMIZED
    fn build_edges_sequential_optimized<T>(
        &self,
        graph: &mut DependencyGraph,
        scan_results: &[T],
        optimized_detector: &ImportDetector,
    ) -> Result<ImportDetectionStats>
    where
        T: ScanResult,
    {
        let mut stats = ImportDetectionStats {
            files_processed: 0,
            imports_detected: 0,
            imports_resolved: 0,
            resolution_rate: 0.0,
            language_breakdown: HashMap::new(),
            import_patterns: HashMap::new(),
        };

        // Create file path lookup for resolution
        let file_path_map: HashMap<&str, &T> = scan_results
            .iter()
            .map(|result| (result.path(), result))
            .collect();

        for result in scan_results {
            stats.files_processed += 1;

            // Track language
            if let Some(lang) = optimized_detector.detect_language(result.path()) {
                *stats.language_breakdown.entry(lang.clone()).or_insert(0) += 1;
            }

            // Extract and resolve imports using optimized detector
            if let Some(imports) = result.imports() {
                stats.imports_detected += imports.len();

                for import_str in imports {
                    if let Some(resolved_path) =
                        optimized_detector.resolve_import(import_str, result.path(), &file_path_map)
                    {
                        graph.add_edge(result.path().to_string(), resolved_path)?;
                        stats.imports_resolved += 1;
                    }
                }
            }
        }

        stats.resolution_rate = if stats.imports_detected > 0 {
            stats.imports_resolved as f64 / stats.imports_detected as f64
        } else {
            0.0
        };

        Ok(stats)
    }

    /// Build graph edges sequentially - LEGACY
    fn build_edges_sequential<T>(
        &self,
        graph: &mut DependencyGraph,
        scan_results: &[T],
    ) -> Result<ImportDetectionStats>
    where
        T: ScanResult,
    {
        let optimized_detector =
            ImportDetector::with_file_index(self.import_detector.config.clone(), scan_results);
        self.build_edges_sequential_optimized(graph, scan_results, &optimized_detector)
    }

    /// Build graph edges in parallel - OPTIMIZED
    fn build_edges_parallel_optimized<T>(
        &self,
        graph: &mut DependencyGraph,
        scan_results: &[T],
        optimized_detector: &ImportDetector,
    ) -> Result<ImportDetectionStats>
    where
        T: ScanResult + Sync,
    {
        // Create file path lookup
        let file_path_map: HashMap<&str, &T> = scan_results
            .iter()
            .map(|result| (result.path(), result))
            .collect();

        // Process imports in parallel using optimized detector
        let import_edges: Vec<_> = scan_results
            .par_iter()
            .flat_map(|result| {
                let mut edges = Vec::new();

                if let Some(imports) = result.imports() {
                    for import_str in imports {
                        if let Some(resolved_path) = optimized_detector.resolve_import(
                            import_str,
                            result.path(),
                            &file_path_map,
                        ) {
                            edges.push((result.path().to_string(), resolved_path));
                        }
                    }
                }

                edges
            })
            .collect();

        // Add edges to graph
        for (from, to) in &import_edges {
            graph.add_edge(from.clone(), to.clone())?;
        }

        // Calculate statistics
        let total_imports: usize = scan_results
            .iter()
            .map(|result| result.imports().map_or(0, |imports| imports.len()))
            .sum();

        let language_breakdown: HashMap<String, usize> = scan_results
            .iter()
            .filter_map(|result| {
                optimized_detector
                    .detect_language(result.path())
                    .map(|lang| (lang, 1))
            })
            .fold(HashMap::new(), |mut acc, (lang, count)| {
                *acc.entry(lang).or_insert(0) += count;
                acc
            });

        let stats = ImportDetectionStats {
            files_processed: scan_results.len(),
            imports_detected: total_imports,
            imports_resolved: import_edges.len(),
            resolution_rate: if total_imports > 0 {
                import_edges.len() as f64 / total_imports as f64
            } else {
                0.0
            },
            language_breakdown,
            import_patterns: HashMap::new(), // TODO: Implement detailed pattern analysis
        };

        Ok(stats)
    }

    /// Build graph edges in parallel - LEGACY
    fn build_edges_parallel<T>(
        &self,
        graph: &mut DependencyGraph,
        scan_results: &[T],
    ) -> Result<ImportDetectionStats>
    where
        T: ScanResult + Sync,
    {
        let optimized_detector =
            ImportDetector::with_file_index(self.import_detector.config.clone(), scan_results);
        self.build_edges_parallel_optimized(graph, scan_results, &optimized_detector)
    }

    /// Normalize centrality scores for integration with heuristics
    fn normalize_centrality_scores(
        &self,
        centrality_scores: &HashMap<String, f64>,
        heuristic_scores: &HashMap<String, f64>,
    ) -> Result<HashMap<String, f64>> {
        if centrality_scores.is_empty() {
            return Ok(HashMap::new());
        }

        match self.config.integration.normalization_method {
            NormalizationMethod::MinMax => {
                self.normalize_min_max(centrality_scores, heuristic_scores)
            }
            NormalizationMethod::ZScore => self.normalize_z_score(centrality_scores),
            NormalizationMethod::Rank => self.normalize_rank(centrality_scores),
            NormalizationMethod::None => Ok(centrality_scores.clone()),
        }
    }

    /// Min-max normalization to match heuristic score range
    fn normalize_min_max(
        &self,
        centrality_scores: &HashMap<String, f64>,
        heuristic_scores: &HashMap<String, f64>,
    ) -> Result<HashMap<String, f64>> {
        let centrality_values: Vec<f64> = centrality_scores.values().copied().collect();
        let min_centrality = centrality_values
            .iter()
            .fold(f64::INFINITY, |a, &b| a.min(b));
        let max_centrality = centrality_values
            .iter()
            .fold(f64::NEG_INFINITY, |a, &b| a.max(b));

        // Target range based on heuristic scores
        let heuristic_values: Vec<f64> = heuristic_scores.values().copied().collect();
        let max_heuristic = if heuristic_values.is_empty() {
            1.0
        } else {
            heuristic_values
                .iter()
                .fold(f64::NEG_INFINITY, |a, &b| a.max(b))
        };

        let mut normalized = HashMap::new();

        if (max_centrality - min_centrality).abs() < f64::EPSILON {
            // All scores are the same
            for (path, _) in centrality_scores {
                normalized.insert(path.clone(), max_heuristic * 0.5); // Use half of max heuristic
            }
        } else {
            for (path, &score) in centrality_scores {
                let normalized_score =
                    ((score - min_centrality) / (max_centrality - min_centrality)) * max_heuristic;
                if normalized_score >= self.config.integration.min_centrality_threshold {
                    normalized.insert(path.clone(), normalized_score);
                }
            }
        }

        Ok(normalized)
    }

    /// Z-score normalization
    fn normalize_z_score(
        &self,
        centrality_scores: &HashMap<String, f64>,
    ) -> Result<HashMap<String, f64>> {
        let values: Vec<f64> = centrality_scores.values().copied().collect();
        let mean = values.iter().sum::<f64>() / values.len() as f64;
        let variance =
            values.iter().map(|&x| (x - mean).powi(2)).sum::<f64>() / values.len() as f64;
        let std_dev = variance.sqrt();

        let mut normalized = HashMap::new();

        if std_dev > f64::EPSILON {
            for (path, &score) in centrality_scores {
                let z_score = (score - mean) / std_dev;
                // Shift and scale to positive range
                let normalized_score = (z_score + 3.0) / 6.0; // Roughly [0,1] for most values
                if normalized_score >= self.config.integration.min_centrality_threshold {
                    normalized.insert(path.clone(), normalized_score);
                }
            }
        } else {
            // All scores are the same
            for (path, _) in centrality_scores {
                normalized.insert(path.clone(), 0.5);
            }
        }

        Ok(normalized)
    }

    /// Rank-based normalization
    fn normalize_rank(
        &self,
        centrality_scores: &HashMap<String, f64>,
    ) -> Result<HashMap<String, f64>> {
        let mut scored_files: Vec<_> = centrality_scores
            .iter()
            .map(|(path, &score)| (path.clone(), score))
            .collect();

        scored_files.sort_by(|a, b| b.1.partial_cmp(&a.1).unwrap_or(std::cmp::Ordering::Equal));

        let mut normalized = HashMap::new();
        let total_files = scored_files.len();

        for (rank, (path, _)) in scored_files.into_iter().enumerate() {
            let normalized_score = 1.0 - (rank as f64 / total_files as f64);
            if normalized_score >= self.config.integration.min_centrality_threshold {
                normalized.insert(path, normalized_score);
            }
        }

        Ok(normalized)
    }

    /// Create minimal analysis for large graphs (performance optimization)
    fn create_minimal_analysis(&self, graph: &DependencyGraph) -> Result<GraphAnalysisResults> {
        // Use a simplified analyzer for large graphs
        let minimal_analyzer = GraphStatisticsAnalyzer::for_large_graphs();
        minimal_analyzer.analyze(graph)
    }

    /// Check if a file is an entrypoint
    fn is_entrypoint_file(&self, file_path: &str) -> bool {
        let path = Path::new(file_path);
        let language = file::detect_language_from_path(path);
        file::is_entrypoint_path(path, &language)
    }
}

impl Default for CentralityCalculator {
    fn default() -> Self {
        Self::new().expect("Failed to create CentralityCalculator")
    }
}

/// Import detection and resolution engine with pre-computed lookup optimization
#[derive(Debug, Clone)]
pub struct ImportDetector {
    config: ImportResolutionConfig,
    /// Pre-computed lookup map: file stem -> full paths (massive performance improvement)
    stem_to_paths: HashMap<String, Vec<String>>,
    /// Pre-computed lookup map: filename -> full paths
    filename_to_paths: HashMap<String, Vec<String>>,
    /// Set of all available file paths for quick existence checks
    available_paths: HashSet<String>,
}

const PYTHON_FILE_EXTENSIONS: &[&str] = &["py"];
const PYTHON_SUFFIXES: &[&str] = &[".py"];
const JS_FILE_EXTENSIONS: &[&str] = &["js", "jsx", "ts", "tsx", "mjs", "cjs"];
const JS_SUFFIXES: &[&str] = &[".js", ".jsx", ".ts", ".tsx", ".mjs", ".cjs"];
const RUST_FILE_EXTENSIONS: &[&str] = &["rs"];
const RUST_SUFFIXES: &[&str] = &[".rs"];

fn strip_known_suffix<'a>(value: &'a str, suffixes: &[&str]) -> &'a str {
    for suffix in suffixes {
        if value.ends_with(suffix) {
            return &value[..value.len() - suffix.len()];
        }
    }
    value
}

impl ImportDetector {
    /// Create with configuration
    pub fn with_config(config: ImportResolutionConfig) -> Self {
        Self {
            config,
            stem_to_paths: HashMap::new(),
            filename_to_paths: HashMap::new(),
            available_paths: HashSet::new(),
        }
    }

    /// Create with pre-computed lookup maps for massive performance improvement
    pub fn with_file_index<T>(config: ImportResolutionConfig, scan_results: &[T]) -> Self
    where
        T: ScanResult,
    {
        let mut detector = Self::with_config(config);
        detector.build_lookup_maps(scan_results);
        detector
    }

    /// Build inverted index mapping file stems/names to full paths
    /// This eliminates the O(n) scan-all-files bottleneck
    fn build_lookup_maps<T>(&mut self, scan_results: &[T])
    where
        T: ScanResult,
    {
        self.stem_to_paths.clear();
        self.filename_to_paths.clear();
        self.available_paths.clear();

        for result in scan_results {
            let full_path = result.path().to_string();
            self.available_paths.insert(full_path.clone());

            let path = Path::new(result.path());

            // Index by file stem (name without extension)
            if let Some(stem) = path.file_stem().and_then(|s| s.to_str()) {
                let stem_lower = stem.to_lowercase();
                self.stem_to_paths
                    .entry(stem_lower)
                    .or_insert_with(Vec::new)
                    .push(full_path.clone());
            }

            // Index by full filename
            if let Some(filename) = path.file_name().and_then(|s| s.to_str()) {
                let filename_lower = filename.to_lowercase();
                self.filename_to_paths
                    .entry(filename_lower)
                    .or_insert_with(Vec::new)
                    .push(full_path);
            }
        }
    }

    /// Detect programming language from file extension
    pub fn detect_language(&self, file_path: &str) -> Option<String> {
        let path = Path::new(file_path);
        let ext = path.extension()?.to_str()?.to_lowercase();

        match ext.as_str() {
            "py" => Some("python".to_string()),
            "js" | "jsx" | "mjs" => Some("javascript".to_string()),
            "ts" | "tsx" => Some("typescript".to_string()),
            "rs" => Some("rust".to_string()),
            "go" => Some("go".to_string()),
            "java" | "kt" => Some("java".to_string()),
            "cpp" | "cc" | "cxx" | "hpp" | "h" => Some("cpp".to_string()),
            "c" => Some("c".to_string()),
            "rb" => Some("ruby".to_string()),
            "php" => Some("php".to_string()),
            "cs" => Some("csharp".to_string()),
            "swift" => Some("swift".to_string()),
            _ => None,
        }
    }

    /// Resolve import string to actual file path
    pub fn resolve_import<T>(
        &self,
        import_str: &str,
        current_file: &str,
        file_map: &HashMap<&str, &T>,
    ) -> Option<String>
    where
        T: ScanResult,
    {
        // Check custom path mappings first
        if let Some(mapped_path) = self.config.path_mappings.get(import_str) {
            if file_map.contains_key(mapped_path.as_str()) {
                return Some(mapped_path.clone());
            }
        }

        let current_path = Path::new(current_file);
        let language = self.detect_language(current_file);

        match language.as_deref() {
            Some("python") => self.resolve_python_import(import_str, current_path, file_map),
            Some("javascript") | Some("typescript") => {
                self.resolve_js_import(import_str, current_path, file_map)
            }
            Some("rust") => self.resolve_rust_import(import_str, current_path, file_map),
            Some("go") => self.resolve_go_import(import_str, current_path, file_map),
            _ => self.resolve_generic_import(import_str, current_path, file_map),
        }
    }

    /// Resolve Python import
    fn resolve_python_import<T>(
        &self,
        import_str: &str,
        current_path: &Path,
        file_map: &HashMap<&str, &T>,
    ) -> Option<String>
    where
        T: ScanResult,
    {
        let cleaned_import = import_str.trim();
        if cleaned_import.is_empty() {
            return None;
        }

        if self.config.exclude_stdlib_imports && self.is_python_stdlib(cleaned_import) {
            return None;
        }

        let mut module = cleaned_import;
        if let Some(alias_index) = module.find(" as ") {
            module = &module[..alias_index];
        }

        let mut base_dir = current_path.parent().unwrap_or(current_path).to_path_buf();
        let mut relative_levels = 0;
        while module.starts_with('.') {
            relative_levels += 1;
            module = &module[1..];
        }

        for _ in 0..relative_levels {
            if let Some(parent) = base_dir.parent() {
                base_dir = parent.to_path_buf();
            }
        }

        module = module.trim();
        let module = strip_known_suffix(module, PYTHON_SUFFIXES);
        let module_parts: Vec<&str> = if module.is_empty() {
            Vec::new()
        } else {
            module.split('.').filter(|part| !part.is_empty()).collect()
        };

        if !module_parts.is_empty() {
            if let Some(resolved) = self.resolve_relative_python(&base_dir, &module_parts, file_map)
            {
                return Some(resolved);
            }
        }

        if module_parts.is_empty() {
            return None;
        }

        self.find_module_candidate(&module_parts, PYTHON_FILE_EXTENSIONS)
    }

    /// Resolve JavaScript/TypeScript import
    fn resolve_js_import<T>(
        &self,
        import_str: &str,
        current_path: &Path,
        file_map: &HashMap<&str, &T>,
    ) -> Option<String>
    where
        T: ScanResult,
    {
        let cleaned_import = import_str.trim();
        if cleaned_import.is_empty() {
            return None;
        }

        let parent_dir = current_path.parent().unwrap_or(current_path);

        if cleaned_import.starts_with("./") || cleaned_import.starts_with("../") {
            if !self.config.resolve_relative_imports {
                return None;
            }

            if let Some(resolved) = self.resolve_relative_js(parent_dir, cleaned_import, file_map) {
                return Some(resolved);
            }
        } else {
            // Attempt to resolve within the same directory first
            if let Some(resolved) = self.resolve_relative_js(parent_dir, cleaned_import, file_map) {
                return Some(resolved);
            }

            if !self.config.resolve_absolute_imports {
                return None;
            }

            let normalized = strip_known_suffix(cleaned_import, JS_SUFFIXES);
            let module_parts: Vec<&str> = normalized
                .split('/')
                .filter(|segment| !segment.is_empty())
                .collect();

            if module_parts.is_empty() {
                return None;
            }

            return self.find_module_candidate(&module_parts, JS_FILE_EXTENSIONS);
        }

        None
    }

    /// Resolve Rust import (use/mod statements)
    fn resolve_rust_import<T>(
        &self,
        import_str: &str,
        current_path: &Path,
        file_map: &HashMap<&str, &T>,
    ) -> Option<String>
    where
        T: ScanResult,
    {
        let cleaned_import = import_str.trim();
        if cleaned_import.is_empty() {
            return None;
        }

        if self.config.exclude_stdlib_imports && self.is_rust_stdlib(cleaned_import) {
            return None;
        }

        let mut module = cleaned_import;

        if let Some(stripped) = module.strip_prefix("crate::") {
            module = stripped;
        }

        while let Some(stripped) = module.strip_prefix("self::") {
            module = stripped;
        }

        let mut base_dir = current_path.parent().unwrap_or(current_path).to_path_buf();
        while let Some(stripped) = module.strip_prefix("super::") {
            module = stripped;
            if let Some(parent) = base_dir.parent() {
                base_dir = parent.to_path_buf();
            }
        }

        module = strip_known_suffix(module, RUST_SUFFIXES);
        let module_parts: Vec<&str> = module
            .split("::")
            .filter(|segment| !segment.is_empty())
            .collect();

        if module_parts.is_empty() {
            return None;
        }

        if let Some(resolved) = self.resolve_relative_rust(&base_dir, &module_parts, file_map) {
            return Some(resolved);
        }

        if module_parts.len() == 1 {
            let crate_lib = base_dir.join("lib.rs");
            if let Some(candidate_str) = crate_lib.to_str() {
                if file_map.contains_key(candidate_str) {
                    return Some(candidate_str.to_string());
                }
            }
        }

        self.find_module_candidate(&module_parts, RUST_FILE_EXTENSIONS)
    }

    /// Resolve Go import
    fn resolve_go_import<T>(
        &self,
        import_str: &str,
        _current_path: &Path,
        file_map: &HashMap<&str, &T>,
    ) -> Option<String>
    where
        T: ScanResult,
    {
        let cleaned_import = import_str.trim().trim_matches('"');

        // Skip standard library
        if self.config.exclude_stdlib_imports && !cleaned_import.contains('.') {
            return None;
        }

        let parts: Vec<&str> = cleaned_import.split('/').collect();

        // Try various Go file patterns
        let mut candidates = Vec::new();

        // Package directory
        candidates.push(format!("{}.go", parts.last()?));
        candidates.push(format!("{}/main.go", cleaned_import));
        candidates.push(format!("{}/{}.go", cleaned_import, parts.last()?));

        for candidate in &candidates {
            if file_map.contains_key(candidate.as_str()) {
                return Some(candidate.clone());
            }
        }

        self.fuzzy_match_import(&parts, file_map)
    }

    /// Generic import resolution
    fn resolve_generic_import<T>(
        &self,
        import_str: &str,
        _current_path: &Path,
        file_map: &HashMap<&str, &T>,
    ) -> Option<String>
    where
        T: ScanResult,
    {
        let cleaned_import = import_str.trim();
        let parts: Vec<&str> = cleaned_import.split(&['/', '.', ':']).collect();
        self.fuzzy_match_import(&parts, file_map)
    }

    fn resolve_relative_python<T>(
        &self,
        base_dir: &Path,
        module_parts: &[&str],
        file_map: &HashMap<&str, &T>,
    ) -> Option<String>
    where
        T: ScanResult,
    {
        if module_parts.is_empty() {
            return None;
        }

        let mut module_path = base_dir.to_path_buf();
        for part in module_parts {
            module_path.push(part);
        }

        let mut candidate = module_path.clone();
        candidate.set_extension("py");
        if let Some(candidate_str) = candidate.to_str() {
            if file_map.contains_key(candidate_str) {
                return Some(candidate_str.to_string());
            }
        }

        let init_candidate = module_path.join("__init__.py");
        if let Some(candidate_str) = init_candidate.to_str() {
            if file_map.contains_key(candidate_str) {
                return Some(candidate_str.to_string());
            }
        }

        None
    }

    fn resolve_relative_js<T>(
        &self,
        base_dir: &Path,
        import_path: &str,
        file_map: &HashMap<&str, &T>,
    ) -> Option<String>
    where
        T: ScanResult,
    {
        let normalized = strip_known_suffix(import_path, JS_SUFFIXES);
        let target = self.build_relative_js_path(base_dir, normalized);

        for ext in JS_FILE_EXTENSIONS {
            let mut candidate = target.clone();
            candidate.set_extension(ext);
            if let Some(candidate_str) = candidate.to_str() {
                if file_map.contains_key(candidate_str) {
                    return Some(candidate_str.to_string());
                }
            }
        }

        for ext in JS_FILE_EXTENSIONS {
            let index_candidate = target.join(format!("index.{}", ext));
            if let Some(candidate_str) = index_candidate.to_str() {
                if file_map.contains_key(candidate_str) {
                    return Some(candidate_str.to_string());
                }
            }
        }

        None
    }

    fn build_relative_js_path(&self, base_dir: &Path, import_path: &str) -> PathBuf {
        let mut resolved = base_dir.to_path_buf();
        for segment in import_path.split('/') {
            match segment {
                "" | "." => {}
                ".." => {
                    if let Some(parent) = resolved.parent() {
                        resolved = parent.to_path_buf();
                    }
                }
                _ => resolved.push(segment),
            }
        }
        resolved
    }

    fn resolve_relative_rust<T>(
        &self,
        base_dir: &Path,
        module_parts: &[&str],
        file_map: &HashMap<&str, &T>,
    ) -> Option<String>
    where
        T: ScanResult,
    {
        if module_parts.is_empty() {
            return None;
        }

        let mut module_path = base_dir.to_path_buf();
        for part in module_parts {
            module_path.push(part);
        }

        let mut candidate = module_path.clone();
        candidate.set_extension("rs");
        if let Some(candidate_str) = candidate.to_str() {
            if file_map.contains_key(candidate_str) {
                return Some(candidate_str.to_string());
            }
        }

        let mod_candidate = module_path.join("mod.rs");
        if let Some(candidate_str) = mod_candidate.to_str() {
            if file_map.contains_key(candidate_str) {
                return Some(candidate_str.to_string());
            }
        }

        None
    }

    fn find_module_candidate(&self, module_parts: &[&str], extensions: &[&str]) -> Option<String> {
        if module_parts.is_empty() {
            return None;
        }

        let stem = module_parts.last().unwrap().to_lowercase();
        let candidates = self.stem_to_paths.get(&stem)?;

        for candidate in candidates {
            if self.module_path_matches(candidate, module_parts, extensions) {
                return Some(candidate.clone());
            }
        }

        None
    }

    fn module_path_matches(
        &self,
        candidate: &str,
        module_parts: &[&str],
        extensions: &[&str],
    ) -> bool {
        let path = Path::new(candidate);
        let file_name = match path.file_name().and_then(|n| n.to_str()) {
            Some(name) => name,
            None => return false,
        };

        let lower_file = file_name.to_lowercase();
        if lower_file == "__init__.py" {
            return self.dir_path_matches(path.parent(), module_parts);
        }

        let ext = Path::new(file_name)
            .extension()
            .and_then(|e| e.to_str())
            .map(|s| s.to_lowercase())
            .unwrap_or_default();

        if !extensions
            .iter()
            .any(|allowed| allowed.eq_ignore_ascii_case(&ext))
        {
            return false;
        }

        let stem = Path::new(file_name)
            .file_stem()
            .and_then(|s| s.to_str())
            .map(|s| s.to_lowercase())
            .unwrap_or_default();

        if stem == "index" && !module_parts.is_empty() {
            return self.dir_path_matches(path.parent(), module_parts);
        }

        if module_parts.is_empty() {
            return false;
        }

        if stem != module_parts.last().unwrap().to_lowercase() {
            return false;
        }

        self.dir_path_matches(
            path.parent(),
            &module_parts[..module_parts.len().saturating_sub(1)],
        )
    }

    fn dir_path_matches(&self, dir: Option<&Path>, module_parts: &[&str]) -> bool {
        if module_parts.is_empty() {
            return true;
        }

        let mut current = dir;
        for expected in module_parts.iter().rev() {
            match current {
                Some(path) => {
                    let name = path.file_name().and_then(|n| n.to_str());
                    match name {
                        Some(name) if name.eq_ignore_ascii_case(expected) => {
                            current = path.parent();
                        }
                        _ => return false,
                    }
                }
                None => return false,
            }
        }

        true
    }

    /// Fuzzy matching for import resolution - OPTIMIZED with pre-computed maps
    fn fuzzy_match_import<T>(
        &self,
        import_parts: &[&str],
        _file_map: &HashMap<&str, &T>,
    ) -> Option<String>
    where
        T: ScanResult,
    {
        if import_parts.is_empty() {
            return None;
        }

        let last_part = import_parts.last()?.to_lowercase();

        // MASSIVE PERFORMANCE IMPROVEMENT: Use pre-computed lookup maps instead of O(n) scan
        // 1. First try exact stem match (most common case)
        if let Some(paths) = self.stem_to_paths.get(&last_part) {
            // Return first match (could be made smarter with scoring)
            if let Some(first_path) = paths.first() {
                return Some(first_path.clone());
            }
        }

        // 2. Try filename match
        if let Some(paths) = self.filename_to_paths.get(&last_part) {
            if let Some(first_path) = paths.first() {
                return Some(first_path.clone());
            }
        }

        // 3. Try partial matching against stems
        for (stem, paths) in &self.stem_to_paths {
            if stem.contains(&last_part) || last_part.contains(stem) {
                if let Some(first_path) = paths.first() {
                    return Some(first_path.clone());
                }
            }
        }

        // 4. Fallback: check if path contains all import parts
        for path in &self.available_paths {
            let path_lower = path.to_lowercase();
            if import_parts
                .iter()
                .all(|&part| path_lower.contains(&part.to_lowercase()))
            {
                return Some(path.clone());
            }
        }

        None
    }

    /// Check if import is Python standard library
    fn is_python_stdlib(&self, import_str: &str) -> bool {
        let stdlib_modules = [
            "os",
            "sys",
            "re",
            "json",
            "collections",
            "itertools",
            "functools",
            "typing",
            "datetime",
            "math",
            "random",
            "string",
            "pathlib",
            "io",
            "csv",
            "xml",
            "html",
            "urllib",
            "http",
            "email",
            "logging",
            "unittest",
            "asyncio",
            "concurrent",
            "multiprocessing",
            "threading",
            "subprocess",
        ];

        let first_part = import_str.split('.').next().unwrap_or(import_str);
        stdlib_modules.contains(&first_part)
    }

    /// Check if import is Rust standard library
    fn is_rust_stdlib(&self, import_str: &str) -> bool {
        import_str.starts_with("std::")
            || import_str.starts_with("core::")
            || import_str.starts_with("alloc::")
    }
}

/// Utility functions for centrality results analysis
impl CentralityResults {
    /// Get files sorted by centrality score (descending)
    pub fn top_files_by_centrality(&self, k: usize) -> Vec<(String, f64)> {
        let mut scored_files: Vec<_> = self
            .pagerank_scores
            .iter()
            .map(|(path, &score)| (path.clone(), score))
            .collect();

        scored_files.sort_by(|a, b| b.1.partial_cmp(&a.1).unwrap_or(std::cmp::Ordering::Equal));
        scored_files.into_iter().take(k).collect()
    }

    /// Get summary statistics about centrality computation
    pub fn summary(&self) -> String {
        format!(
            "Centrality Analysis Summary:\n\
             - Files with centrality scores: {}\n\
             - PageRank iterations: {} (converged: {})\n\
             - Graph: {} nodes, {} edges (density: {:.4})\n\
             - Import resolution: {:.1}% ({}/{})\n\
             - Top languages: {}\n\
             - Computation time: {}ms\n\
             - Integration weight: {:.2}",
            self.pagerank_scores.len(),
            self.pagerank_details.iterations_converged,
            self.pagerank_details.converged(),
            self.graph_analysis.basic_stats.total_nodes,
            self.graph_analysis.basic_stats.total_edges,
            self.graph_analysis.basic_stats.graph_density,
            self.import_stats.resolution_rate * 100.0,
            self.import_stats.imports_resolved,
            self.import_stats.imports_detected,
            self.import_stats
                .language_breakdown
                .iter()
                .max_by_key(|(_, &count)| count)
                .map(|(lang, count)| format!("{} ({})", lang, count))
                .unwrap_or_else(|| "None".to_string()),
            self.integration_metadata.computation_time_ms,
            self.integration_metadata.centrality_weight,
        )
    }
}

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

    // Mock scan result for testing
    #[derive(Debug, Clone)]
    struct MockScanResult {
        path: String,
        relative_path: String,
        depth: usize,
        imports: Option<Vec<String>>,
        is_docs: bool,
        is_readme: bool,
        is_test: bool,
        is_entrypoint: bool,
        has_examples: bool,
        priority_boost: f64,
        churn_score: f64,
        centrality_in: f64,
        doc_analysis: Option<DocumentAnalysis>,
    }

    impl MockScanResult {
        fn new(path: &str) -> Self {
            Self {
                path: path.to_string(),
                relative_path: path.to_string(),
                depth: path.matches('/').count(),
                imports: None,
                is_docs: path.contains("doc") || path.ends_with(".md"),
                is_readme: path.to_lowercase().contains("readme"),
                is_test: path.contains("test"),
                is_entrypoint: path.contains("main") || path.contains("index"),
                has_examples: path.contains("example"),
                priority_boost: 0.0,
                churn_score: 0.5,
                centrality_in: 0.0,
                doc_analysis: Some(DocumentAnalysis::new()),
            }
        }

        fn with_imports(mut self, imports: Vec<String>) -> Self {
            self.imports = Some(imports);
            self
        }
    }

    impl ScanResult for MockScanResult {
        fn path(&self) -> &str {
            &self.path
        }
        fn relative_path(&self) -> &str {
            &self.relative_path
        }
        fn depth(&self) -> usize {
            self.depth
        }
        fn is_docs(&self) -> bool {
            self.is_docs
        }
        fn is_readme(&self) -> bool {
            self.is_readme
        }
        fn is_test(&self) -> bool {
            self.is_test
        }
        fn is_entrypoint(&self) -> bool {
            self.is_entrypoint
        }
        fn has_examples(&self) -> bool {
            self.has_examples
        }
        fn priority_boost(&self) -> f64 {
            self.priority_boost
        }
        fn churn_score(&self) -> f64 {
            self.churn_score
        }
        fn centrality_in(&self) -> f64 {
            self.centrality_in
        }
        fn imports(&self) -> Option<&[String]> {
            self.imports.as_deref()
        }
        fn doc_analysis(&self) -> Option<&DocumentAnalysis> {
            self.doc_analysis.as_ref()
        }
    }

    #[test]
    fn test_centrality_calculator_creation() {
        let calculator = CentralityCalculator::new();
        assert!(calculator.is_ok());

        let large_calc = CentralityCalculator::for_large_codebases();
        assert!(large_calc.is_ok());
    }

    #[test]
    fn test_import_detection() {
        let detector = ImportDetector::with_config(ImportResolutionConfig::default());

        // Test language detection
        assert_eq!(
            detector.detect_language("main.py"),
            Some("python".to_string())
        );
        assert_eq!(
            detector.detect_language("app.js"),
            Some("javascript".to_string())
        );
        assert_eq!(detector.detect_language("lib.rs"), Some("rust".to_string()));

        // Test Python stdlib detection
        assert!(detector.is_python_stdlib("os"));
        assert!(detector.is_python_stdlib("sys.path"));
        assert!(!detector.is_python_stdlib("custom_module"));

        // Test Rust stdlib detection
        assert!(detector.is_rust_stdlib("std::collections::HashMap"));
        assert!(detector.is_rust_stdlib("core::fmt"));
        assert!(!detector.is_rust_stdlib("serde::Deserialize"));
    }

    #[test]
    fn test_centrality_calculation() {
        let calculator = CentralityCalculator::new().unwrap();

        let scan_results = vec![
            MockScanResult::new("main.py")
                .with_imports(vec!["utils".to_string(), "config".to_string()]),
            MockScanResult::new("utils.py").with_imports(vec!["config".to_string()]),
            MockScanResult::new("config.py"),
            MockScanResult::new("test.py").with_imports(vec!["main".to_string()]),
        ];

        let results = calculator.calculate_centrality(&scan_results).unwrap();

        // Basic checks
        assert!(!results.pagerank_scores.is_empty());
        assert!(results.integration_metadata.integration_successful);
        assert_eq!(
            results.integration_metadata.files_with_centrality,
            results.pagerank_scores.len()
        );

        // config.py should have high centrality (imported by main.py and utils.py)
        let config_score = results.pagerank_scores.get("config.py");
        assert!(config_score.is_some());

        println!("Centrality scores:");
        for (file, score) in &results.pagerank_scores {
            println!("  {}: {:.6}", file, score);
        }

        println!("\n{}", results.summary());
    }

    #[test]
    fn test_heuristics_integration() {
        let calculator = CentralityCalculator::new().unwrap();

        let scan_results = vec![
            MockScanResult::new("main.py").with_imports(vec!["utils".to_string()]),
            MockScanResult::new("utils.py"),
        ];

        let centrality_results = calculator.calculate_centrality(&scan_results).unwrap();

        // Mock heuristic scores
        let mut heuristic_scores = HashMap::new();
        heuristic_scores.insert("main.py".to_string(), 0.8);
        heuristic_scores.insert("utils.py".to_string(), 0.6);

        let integrated_scores = calculator
            .integrate_with_heuristics(&centrality_results, &heuristic_scores)
            .unwrap();

        assert!(!integrated_scores.is_empty());

        // Integrated scores should be different from original heuristic scores
        for (file, integrated_score) in &integrated_scores {
            let original_score = heuristic_scores.get(file).unwrap();
            println!(
                "File {}: heuristic={:.3}, integrated={:.3}",
                file, original_score, integrated_score
            );
        }
    }

    #[test]
    fn test_normalization_methods() {
        let calculator = CentralityCalculator::new().unwrap();

        let centrality_scores = vec![
            ("file1".to_string(), 0.1),
            ("file2".to_string(), 0.3),
            ("file3".to_string(), 0.6),
            ("file4".to_string(), 1.0),
        ]
        .into_iter()
        .collect();

        let heuristic_scores = vec![
            ("file1".to_string(), 0.5),
            ("file2".to_string(), 0.7),
            ("file3".to_string(), 0.9),
            ("file4".to_string(), 1.2),
        ]
        .into_iter()
        .collect();

        // Test min-max normalization
        let normalized = calculator
            .normalize_min_max(&centrality_scores, &heuristic_scores)
            .unwrap();
        assert!(!normalized.is_empty());

        // Test z-score normalization
        let z_normalized = calculator.normalize_z_score(&centrality_scores).unwrap();
        assert!(!z_normalized.is_empty());

        // Test rank normalization
        let rank_normalized = calculator.normalize_rank(&centrality_scores).unwrap();
        assert!(!rank_normalized.is_empty());

        println!("Original scores: {:?}", centrality_scores);
        println!("Min-max normalized: {:?}", normalized);
        println!("Z-score normalized: {:?}", z_normalized);
        println!("Rank normalized: {:?}", rank_normalized);
    }

    #[test]
    fn test_import_resolution() {
        let detector = ImportDetector::with_config(ImportResolutionConfig::default());

        // Create mock file map
        let scan_results = vec![
            MockScanResult::new("src/main.py"),
            MockScanResult::new("src/utils.py"),
            MockScanResult::new("src/config.py"),
            MockScanResult::new("tests/test_main.py"),
        ];

        let file_map: HashMap<&str, &MockScanResult> = scan_results
            .iter()
            .map(|result| (result.path(), result))
            .collect();

        // Test Python import resolution
        let resolved = detector.resolve_import("utils", "src/main.py", &file_map);
        assert!(resolved.is_some());

        // Test module path resolution
        let resolved_config = detector.resolve_import("src.config", "src/main.py", &file_map);
        // Should resolve to src/config.py through fuzzy matching
        assert!(resolved_config.is_some());

        println!("Resolved imports:");
        if let Some(path) = resolved {
            println!("  utils -> {}", path);
        }
        if let Some(path) = resolved_config {
            println!("  src.config -> {}", path);
        }
    }

    #[test]
    fn test_entrypoint_detection() {
        let calculator = CentralityCalculator::new().unwrap();

        assert!(calculator.is_entrypoint_file("main.py"));
        assert!(calculator.is_entrypoint_file("src/main.rs"));
        assert!(calculator.is_entrypoint_file("index.js"));
        assert!(calculator.is_entrypoint_file("app.py"));
        assert!(calculator.is_entrypoint_file("lib.rs"));
        assert!(calculator.is_entrypoint_file("__init__.py"));

        assert!(!calculator.is_entrypoint_file("utils.py"));
        assert!(!calculator.is_entrypoint_file("config.rs"));
        assert!(!calculator.is_entrypoint_file("helper.js"));
    }

    #[test]
    fn test_top_files_by_centrality() {
        let mut pagerank_scores = HashMap::new();
        pagerank_scores.insert("file1.py".to_string(), 0.4);
        pagerank_scores.insert("file2.py".to_string(), 0.6);
        pagerank_scores.insert("file3.py".to_string(), 0.2);
        pagerank_scores.insert("file4.py".to_string(), 0.8);

        let results = CentralityResults {
            pagerank_scores,
            graph_analysis: GraphAnalysisResults {
                basic_stats: crate::graph::GraphStatistics::empty(),
                degree_distribution: Default::default(),
                connectivity: Default::default(),
                structural_patterns: Default::default(),
                import_insights: Default::default(),
                performance_profile: Default::default(),
                analysis_metadata: Default::default(),
            },
            pagerank_details: PageRankResults {
                scores: HashMap::new(),
                iterations_converged: 10,
                convergence_epsilon: 1e-6,
                graph_stats: crate::graph::GraphStatistics::empty(),
                parameters: PageRankConfig::default(),
                performance_metrics: Default::default(),
            },
            import_stats: ImportDetectionStats {
                files_processed: 4,
                imports_detected: 0,
                imports_resolved: 0,
                resolution_rate: 0.0,
                language_breakdown: HashMap::new(),
                import_patterns: HashMap::new(),
            },
            integration_metadata: IntegrationMetadata {
                timestamp: chrono::Utc::now(),
                computation_time_ms: 100,
                integration_successful: true,
                centrality_weight: 0.15,
                files_with_centrality: 4,
                config: CentralityConfig::default(),
            },
        };

        let top_files = results.top_files_by_centrality(2);
        assert_eq!(top_files.len(), 2);
        assert_eq!(top_files[0].0, "file4.py");
        assert_eq!(top_files[0].1, 0.8);
        assert_eq!(top_files[1].0, "file2.py");
        assert_eq!(top_files[1].1, 0.6);
    }
}