tldr-core 0.1.6

Core analysis engine for TLDR code analysis tool
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
//! Architecture Rules Engine (Phase 3)
//!
//! This module implements `--generate-rules` and `--check-rules` for architecture validation.
//!
//! # Features
//!
//! - **Rule Generation**: Generate architecture rules from detected layer structure
//!   - L1, L2: Layer constraint rules (e.g., "LOW may not import HIGH")
//!   - C1, C2, ...: Cycle break rules from detected circular dependencies
//!
//! - **Rule Checking**: Validate code against architecture rules
//!   - Build import graph (NOT call graph - addresses A22)
//!   - Check each import edge against layer rules
//!   - Report violations with file/line information
//!
//! # Mitigations
//!
//! - A8: Missing fields for rule generation - uses RulesGenerationContext
//! - A22: Import graph vs call graph - builds separate import graph for rules checking
//!
//! # References
//!
//! - Spec: architecture-spec.md Section 1.2, 1.3
//! - Plan: architecture-phased-plan.yaml Phase 3
//! - Premortems: architecture-premortem-1.yaml (A8), architecture-premortem-2.yaml (A22)

use std::collections::{HashMap, HashSet};
use std::path::{Path, PathBuf};

use crate::ast::imports::get_imports;
use crate::fs::tree::{collect_files, get_file_tree};
use crate::types::{
    ArchRule, ArchRuleType, ArchRulesFile, ArchitectureReport, IgnoreSpec, ImportInfo, Language,
    LayerDefinition, LayerDefinitions, LayerType, RulesGenerationContext, Violation,
    ViolationInfo, ViolationReport,
};
use crate::TldrResult;

// =============================================================================
// Import Graph Types (A22)
// =============================================================================

/// An edge in the import graph (file A imports file B)
#[derive(Debug, Clone)]
pub struct ImportEdge {
    /// File containing the import statement
    pub from_file: PathBuf,
    /// File being imported
    pub to_file: PathBuf,
    /// Module name as written in the import
    pub module: String,
    /// Line number of the import statement
    pub line: u32,
}

/// Import graph for a project
///
/// This is distinct from the call graph. The call graph tracks function calls,
/// while the import graph tracks import statements at the file level.
/// Layer violations are about import dependencies, not call patterns.
#[derive(Debug, Clone, Default)]
pub struct ImportGraph {
    /// All import edges
    pub edges: Vec<ImportEdge>,
    /// Map from file to its imports (for fast lookup)
    pub file_to_imports: HashMap<PathBuf, Vec<ImportEdge>>,
    /// All files in the project
    pub files: HashSet<PathBuf>,
}

impl ImportGraph {
    /// Create a new empty import graph
    pub fn new() -> Self {
        Self::default()
    }

    /// Add an import edge
    pub fn add_edge(&mut self, edge: ImportEdge) {
        self.files.insert(edge.from_file.clone());
        self.files.insert(edge.to_file.clone());
        self.file_to_imports
            .entry(edge.from_file.clone())
            .or_default()
            .push(edge.clone());
        self.edges.push(edge);
    }
}

// =============================================================================
// Import Graph Builder (A22)
// =============================================================================

/// Build an import graph from a project directory.
///
/// This builds a file-level import graph (which file imports which file),
/// NOT a call graph (which function calls which function).
///
/// # Arguments
///
/// * `root` - Project root directory
/// * `language` - Programming language to analyze
///
/// # Returns
///
/// An `ImportGraph` with all import edges between project files.
///
/// # A22 Mitigation
///
/// This function specifically builds an IMPORT graph, not a call graph.
/// Layer rules should be checked against imports, not calls.
pub fn build_import_graph(root: &Path, language: Language) -> TldrResult<ImportGraph> {
    let extensions: HashSet<String> = language
        .extensions()
        .iter()
        .map(|s| s.to_string())
        .collect();

    let tree = get_file_tree(root, Some(&extensions), true, Some(&IgnoreSpec::default()))?;
    let files = collect_files(&tree, root);

    let mut graph = ImportGraph::new();

    // First, collect all files to enable import resolution
    let mut all_files: HashSet<PathBuf> = HashSet::new();
    for file_path in &files {
        all_files.insert(file_path.clone());
    }

    // Then, extract imports from each file
    for file_path in &files {
        graph.files.insert(file_path.clone());

        match get_imports(file_path, language) {
            Ok(imports) => {
                for import in imports {
                    // Try to resolve the import to a project file
                    if let Some(resolved) =
                        resolve_import(&import, file_path, root, &all_files, language)
                    {
                        graph.add_edge(ImportEdge {
                            from_file: file_path.clone(),
                            to_file: resolved,
                            module: import.module.clone(),
                            line: 1, // We don't have precise line info from get_imports
                        });
                    }
                }
            }
            Err(e) => {
                // Skip files with parse errors (non-fatal)
                if e.is_recoverable() {
                    continue;
                }
            }
        }
    }

    Ok(graph)
}

/// Resolve an import to a project file path.
///
/// Returns `None` if the import is external (third-party or stdlib).
fn resolve_import(
    import: &ImportInfo,
    from_file: &Path,
    project_root: &Path,
    all_files: &HashSet<PathBuf>,
    language: Language,
) -> Option<PathBuf> {
    match language {
        Language::Python => resolve_python_import(import, from_file, project_root, all_files),
        Language::TypeScript | Language::JavaScript => {
            resolve_ts_import(import, from_file, project_root, all_files)
        }
        Language::Go => resolve_go_import(import, all_files),
        Language::Rust => resolve_rust_import(import, from_file, project_root, all_files),
        _ => None,
    }
}

/// Resolve a Python import to a file path.
fn resolve_python_import(
    import: &ImportInfo,
    from_file: &Path,
    project_root: &Path,
    all_files: &HashSet<PathBuf>,
) -> Option<PathBuf> {
    let module = &import.module;

    // Handle relative imports (leading dots)
    let (dots, module_path) = count_leading_dots(module);

    if dots > 0 {
        // Relative import
        let from_dir = from_file.parent()?;
        let mut base_dir = from_dir.to_path_buf();

        // Go up `dots - 1` directories (1 dot = current dir, 2 dots = parent, etc.)
        for _ in 0..(dots.saturating_sub(1)) {
            base_dir = base_dir.parent()?.to_path_buf();
        }

        // Convert module path to file path
        let relative_path = module_path.replace('.', "/");

        // Try module.py first, then module/__init__.py
        let candidate = base_dir.join(format!("{}.py", relative_path));
        if all_files.contains(&candidate) {
            return Some(candidate);
        }

        let candidate = base_dir.join(&relative_path).join("__init__.py");
        if all_files.contains(&candidate) {
            return Some(candidate);
        }
    } else {
        // Absolute import
        let relative_path = module.replace('.', "/");

        // Try module.py first
        let candidate = project_root.join(format!("{}.py", relative_path));
        if all_files.contains(&candidate) {
            return Some(candidate);
        }

        // Try module/__init__.py
        let candidate = project_root.join(&relative_path).join("__init__.py");
        if all_files.contains(&candidate) {
            return Some(candidate);
        }

        // Try src/module.py pattern
        let candidate = project_root
            .join("src")
            .join(format!("{}.py", relative_path));
        if all_files.contains(&candidate) {
            return Some(candidate);
        }
    }

    None // External import
}

/// Count leading dots in a Python import module name.
fn count_leading_dots(module: &str) -> (usize, &str) {
    let dots = module.chars().take_while(|&c| c == '.').count();
    let rest = &module[dots..];
    (dots, rest)
}

/// Resolve a TypeScript/JavaScript import to a file path.
fn resolve_ts_import(
    import: &ImportInfo,
    from_file: &Path,
    project_root: &Path,
    all_files: &HashSet<PathBuf>,
) -> Option<PathBuf> {
    let module = &import.module;

    // Skip node_modules imports
    if !module.starts_with('.') && !module.starts_with('/') {
        // Could be a local alias, but likely external
        return None;
    }

    let from_dir = from_file.parent()?;
    let base_dir = if module.starts_with('.') {
        from_dir.to_path_buf()
    } else {
        project_root.to_path_buf()
    };

    let clean_path = module.trim_start_matches("./").trim_start_matches("../");
    let path_part = if module.starts_with("../") {
        let ups = module.matches("../").count();
        let mut dir = base_dir.clone();
        for _ in 0..ups {
            dir = dir.parent()?.to_path_buf();
        }
        dir.join(clean_path)
    } else {
        base_dir.join(clean_path)
    };

    // Try various extensions
    for ext in &[
        "",
        ".ts",
        ".tsx",
        ".js",
        ".jsx",
        "/index.ts",
        "/index.tsx",
        "/index.js",
    ] {
        let candidate = PathBuf::from(format!("{}{}", path_part.display(), ext));
        if all_files.contains(&candidate) {
            return Some(candidate);
        }
    }

    None
}

/// Resolve a Go import to a file path.
fn resolve_go_import(
    import: &ImportInfo,
    all_files: &HashSet<PathBuf>,
) -> Option<PathBuf> {
    // Go imports are package paths; we look for the package directory
    // This is simplified - full Go import resolution requires go.mod parsing

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

    // Try to find a matching directory with .go files
    if let Some(last) = parts.last() {
        // Look for files in a directory matching the last path component
        for file in all_files {
            if let Some(parent) = file.parent() {
                if parent.ends_with(last) && file.extension().map(|e| e == "go").unwrap_or(false) {
                    return Some(file.clone());
                }
            }
        }
    }

    None
}

/// Resolve a Rust import to a file path.
fn resolve_rust_import(
    import: &ImportInfo,
    from_file: &Path,
    project_root: &Path,
    all_files: &HashSet<PathBuf>,
) -> Option<PathBuf> {
    let module = &import.module;

    // Rust uses :: for paths
    let path_parts: Vec<&str> = module.split("::").collect();

    // Skip crate:: prefix or external crates
    let start_idx = if matches!(path_parts.first(), Some(&"crate") | Some(&"self")) {
        1
    } else if path_parts.first() == Some(&"super") {
        // Handle super:: prefix
        let from_dir = from_file.parent()?;
        let parent = from_dir.parent()?;
        let rest_path = path_parts[1..].join("/");
        let candidate = parent.join(format!("{}.rs", rest_path));
        if all_files.contains(&candidate) {
            return Some(candidate);
        }
        let candidate = parent.join(&rest_path).join("mod.rs");
        if all_files.contains(&candidate) {
            return Some(candidate);
        }
        return None;
    } else {
        // Likely external crate
        return None;
    };

    // Build relative path
    let relative_path = path_parts[start_idx..].join("/");

    // Try src/relative_path.rs
    let candidate = project_root
        .join("src")
        .join(format!("{}.rs", relative_path));
    if all_files.contains(&candidate) {
        return Some(candidate);
    }

    // Try src/relative_path/mod.rs
    let candidate = project_root.join("src").join(&relative_path).join("mod.rs");
    if all_files.contains(&candidate) {
        return Some(candidate);
    }

    None
}

// =============================================================================
// Rule Generation
// =============================================================================

/// Generate architecture rules from an architecture report.
///
/// This takes the detected layers and circular dependencies and generates
/// a set of rules that can be used to validate the architecture.
///
/// # Generated Rules
///
/// - **Layer Rules (L1, L2, ...):**
///   - LOW may not import HIGH
///   - MIDDLE may not import HIGH
///   - Custom rules based on detected layer violations
///
/// - **Cycle Break Rules (C1, C2, ...):**
///   - One rule per detected circular dependency
///   - Suggests which edge to break
///
/// # Arguments
///
/// * `arch_report` - Architecture analysis report with detected layers
/// * `context` - Additional context for rule generation
///
/// # Returns
///
/// An `ArchRulesFile` that can be serialized to YAML/JSON.
pub fn generate_rules(
    arch_report: &ArchitectureReport,
    context: &RulesGenerationContext,
) -> ArchRulesFile {
    let mut rules_file = ArchRulesFile::new();

    // Set timestamp using std::time
    let now = std::time::SystemTime::now();
    let timestamp = now
        .duration_since(std::time::UNIX_EPOCH)
        .map(|d| format!("{}", d.as_secs()))
        .unwrap_or_else(|_| "0".to_string());
    rules_file.generated_at = Some(timestamp);

    // Build layer definitions from detected layers
    rules_file.layers = build_layer_definitions(&arch_report.inferred_layers);

    // Generate layer constraint rules
    let layer_rules = generate_layer_rules(&arch_report.inferred_layers);
    for rule in layer_rules {
        rules_file.rules.push(rule);
    }

    // Generate cycle break rules from circular dependencies
    let cycle_rules = generate_cycle_rules(&arch_report.circular_dependencies, context);
    for rule in cycle_rules {
        rules_file.rules.push(rule);
    }

    rules_file
}

/// Build layer definitions from inferred layers.
fn build_layer_definitions(inferred_layers: &HashMap<PathBuf, LayerType>) -> LayerDefinitions {
    let mut high_dirs: Vec<String> = Vec::new();
    let mut middle_dirs: Vec<String> = Vec::new();
    let mut low_dirs: Vec<String> = Vec::new();

    for (dir, layer) in inferred_layers {
        let dir_str = format!("{}/", dir.display());
        match layer {
            LayerType::Entry | LayerType::DynamicDispatch => {
                high_dirs.push(dir_str);
            }
            LayerType::Service => {
                middle_dirs.push(dir_str);
            }
            LayerType::Utility => {
                low_dirs.push(dir_str);
            }
        }
    }

    // Sort for deterministic output
    high_dirs.sort();
    middle_dirs.sort();
    low_dirs.sort();

    LayerDefinitions {
        high: if high_dirs.is_empty() {
            None
        } else {
            Some(LayerDefinition::new(
                "Entry/Controller layer - handles external requests",
                high_dirs,
            ))
        },
        middle: if middle_dirs.is_empty() {
            None
        } else {
            Some(LayerDefinition::new(
                "Service/Business layer - core logic",
                middle_dirs,
            ))
        },
        low: if low_dirs.is_empty() {
            None
        } else {
            Some(LayerDefinition::new(
                "Utility/Data layer - shared utilities",
                low_dirs,
            ))
        },
    }
}

/// Generate layer constraint rules.
///
/// Standard rules:
/// - L1: LOW may not import HIGH
/// - L2: MIDDLE may not import HIGH
fn generate_layer_rules(inferred_layers: &HashMap<PathBuf, LayerType>) -> Vec<ArchRule> {
    let mut rules = Vec::new();

    // Check if we have layers to generate rules for
    let has_high = inferred_layers
        .values()
        .any(|l| matches!(l, LayerType::Entry | LayerType::DynamicDispatch));
    let has_middle = inferred_layers
        .values()
        .any(|l| matches!(l, LayerType::Service));
    let has_low = inferred_layers
        .values()
        .any(|l| matches!(l, LayerType::Utility));

    // L1: LOW may not import HIGH
    if has_low && has_high {
        rules.push(ArchRule::layer(
            "L1",
            "LOW may not import HIGH",
            vec!["LOW".to_string()],
            vec!["HIGH".to_string()],
            "Utility layers should not depend on entry layers",
        ));
    }

    // L2: MIDDLE may not import HIGH
    if has_middle && has_high {
        rules.push(ArchRule::layer(
            "L2",
            "MIDDLE may not import HIGH",
            vec!["MIDDLE".to_string()],
            vec!["HIGH".to_string()],
            "Service layers should not depend on entry layers",
        ));
    }

    rules
}

/// Generate cycle break rules from circular dependencies.
fn generate_cycle_rules(
    circular_deps: &[crate::types::CircularDep],
    _context: &RulesGenerationContext,
) -> Vec<ArchRule> {
    let mut rules = Vec::new();

    for (i, dep) in circular_deps.iter().enumerate() {
        let id = format!("C{}", i + 1);
        let constraint = format!(
            "Break cycle: {} should not import {}",
            dep.a.display(),
            dep.b.display()
        );
        let files = vec![
            dep.a.to_string_lossy().to_string(),
            dep.b.to_string_lossy().to_string(),
        ];

        rules.push(ArchRule::cycle_break(
            id,
            constraint,
            files,
            format!(
                "Circular dependency between {} and {}",
                dep.a.display(),
                dep.b.display()
            ),
        ));
    }

    rules
}

// =============================================================================
// Rule Checking
// =============================================================================

/// Check project code against architecture rules.
///
/// This builds an import graph and checks each import edge against the rules.
///
/// # Arguments
///
/// * `rules` - Architecture rules to check against
/// * `import_graph` - Import graph of the project (use `build_import_graph`)
/// * `layers` - File-to-layer mapping
///
/// # Returns
///
/// A `ViolationReport` with any violations found.
pub fn check_rules(
    rules: &ArchRulesFile,
    import_graph: &ImportGraph,
    layers: &HashMap<PathBuf, LayerType>,
) -> ViolationReport {
    let mut report = ViolationReport::new();
    report.summary.rules_checked = rules.rules.len();
    report.summary.files_scanned = import_graph.files.len();

    // Pre-compute file-to-layer mapping with canonical names
    let file_layers = compute_file_layers(layers, rules);

    // Check each import edge against layer rules
    for edge in &import_graph.edges {
        let from_layer = get_file_layer(&edge.from_file, &file_layers);
        let to_layer = get_file_layer(&edge.to_file, &file_layers);

        // Only check if both files are in known layers
        if let (Some(from), Some(to)) = (from_layer, to_layer) {
            for rule in &rules.rules {
                if let Some(violation) = check_layer_rule(rule, edge, from, to) {
                    report.add_violation(violation);
                }
            }
        }
    }

    // Check cycle break rules
    for rule in &rules.rules {
        if rule.rule_type == ArchRuleType::CycleBreak {
            check_cycle_rule(rule, import_graph, &mut report);
        }
    }

    report
}

/// Compute file-to-layer mapping with canonical layer names (HIGH, MIDDLE, LOW).
fn compute_file_layers(
    layers: &HashMap<PathBuf, LayerType>,
    rules: &ArchRulesFile,
) -> HashMap<PathBuf, String> {
    let mut file_layers: HashMap<PathBuf, String> = HashMap::new();

    for (dir, layer_type) in layers {
        let layer_name = match layer_type {
            LayerType::Entry | LayerType::DynamicDispatch => "HIGH",
            LayerType::Service => "MIDDLE",
            LayerType::Utility => "LOW",
        };

        // Mark all files in this directory as belonging to this layer
        // We use the directory itself as a prefix match
        file_layers.insert(dir.clone(), layer_name.to_string());
    }

    // Also add files based on layer definitions in rules
    if let Some(high) = &rules.layers.high {
        for dir in &high.directories {
            let dir_path = PathBuf::from(dir.trim_end_matches('/'));
            file_layers.insert(dir_path, "HIGH".to_string());
        }
    }
    if let Some(middle) = &rules.layers.middle {
        for dir in &middle.directories {
            let dir_path = PathBuf::from(dir.trim_end_matches('/'));
            file_layers.insert(dir_path, "MIDDLE".to_string());
        }
    }
    if let Some(low) = &rules.layers.low {
        for dir in &low.directories {
            let dir_path = PathBuf::from(dir.trim_end_matches('/'));
            file_layers.insert(dir_path, "LOW".to_string());
        }
    }

    file_layers
}

/// Get the layer for a file based on its directory.
fn get_file_layer<'a>(
    file: &Path,
    file_layers: &'a HashMap<PathBuf, String>,
) -> Option<&'a String> {
    // Check the file's parent directories
    let mut current = file.parent();
    while let Some(dir) = current {
        if let Some(layer) = file_layers.get(dir) {
            return Some(layer);
        }
        current = dir.parent();
    }

    // Also check if the file path starts with any known layer directory
    for (layer_dir, layer) in file_layers {
        if file.starts_with(layer_dir) {
            return Some(layer);
        }
    }

    None
}

/// Check a single import edge against a layer rule.
fn check_layer_rule(
    rule: &ArchRule,
    edge: &ImportEdge,
    from_layer: &String,
    to_layer: &String,
) -> Option<Violation> {
    if rule.rule_type != ArchRuleType::Layer {
        return None;
    }

    // Check if this import violates the rule
    // Rule: from_layers may not import to_layers
    let from_matches = rule.from_layers.iter().any(|l| l == from_layer);
    let to_matches = rule.to_layers.iter().any(|l| l == to_layer);

    if from_matches && to_matches {
        Some(Violation::direct(ViolationInfo {
            rule_id: rule.id.clone(),
            rule_constraint: rule.constraint.clone(),
            from_file: edge.from_file.clone(),
            from_line: edge.line,
            imports_file: edge.to_file.clone(),
            from_layer: from_layer.clone(),
            to_layer: to_layer.clone(),
            severity: rule.severity,
        }))
    } else {
        None
    }
}

/// Check cycle break rules against the import graph.
fn check_cycle_rule(rule: &ArchRule, import_graph: &ImportGraph, report: &mut ViolationReport) {
    if rule.files.len() < 2 {
        return;
    }

    // Check if any of the files import each other
    let files: HashSet<&str> = rule.files.iter().map(|s| s.as_str()).collect();

    for edge in &import_graph.edges {
        let from_str = edge.from_file.to_string_lossy();
        let to_str = edge.to_file.to_string_lossy();

        // Check if this edge is part of the cycle we're trying to break
        let from_in_rule = files.iter().any(|f| from_str.ends_with(*f));
        let to_in_rule = files.iter().any(|f| to_str.ends_with(*f));

        if from_in_rule && to_in_rule {
            report.add_violation(Violation::direct(ViolationInfo {
                rule_id: rule.id.clone(),
                rule_constraint: rule.constraint.clone(),
                from_file: edge.from_file.clone(),
                from_line: edge.line,
                imports_file: edge.to_file.clone(),
                from_layer: "CYCLE".to_string(),
                to_layer: "CYCLE".to_string(),
                severity: rule.severity,
            }));
        }
    }
}

/// Check for transitive violations.
///
/// A transitive violation occurs when A imports B, B imports C,
/// and the chain A -> ... -> C violates a layer rule.
///
/// # Arguments
///
/// * `rules` - Architecture rules
/// * `import_graph` - Import graph
/// * `file_layers` - File-to-layer mapping
///
/// # Returns
///
/// Vector of transitive violations found.
pub fn check_transitive_violations(
    rules: &ArchRulesFile,
    import_graph: &ImportGraph,
    layers: &HashMap<PathBuf, LayerType>,
) -> Vec<Violation> {
    let mut violations = Vec::new();
    let file_layers = compute_file_layers(layers, rules);

    // Build adjacency list for BFS
    let mut adjacency: HashMap<PathBuf, Vec<PathBuf>> = HashMap::new();
    for edge in &import_graph.edges {
        adjacency
            .entry(edge.from_file.clone())
            .or_default()
            .push(edge.to_file.clone());
    }

    // For each file in a "from" layer, find all reachable files and check for violations
    for rule in &rules.rules {
        if rule.rule_type != ArchRuleType::Layer {
            continue;
        }

        // Find all files in "from" layers
        for (dir, layer_name) in &file_layers {
            if !rule.from_layers.contains(layer_name) {
                continue;
            }

            // BFS from this directory to find transitive imports
            for start_file in import_graph.files.iter() {
                if !start_file.starts_with(dir) {
                    continue;
                }

                // BFS to find all reachable files
                let mut visited: HashSet<PathBuf> = HashSet::new();
                let mut queue: Vec<(PathBuf, Vec<PathBuf>)> =
                    vec![(start_file.clone(), vec![start_file.clone()])];

                while let Some((current, path)) = queue.pop() {
                    if visited.contains(&current) {
                        continue;
                    }
                    visited.insert(current.clone());

                    // Check if this file is in a forbidden "to" layer
                    if let Some(current_layer) = get_file_layer(&current, &file_layers) {
                        if rule.to_layers.contains(current_layer) && path.len() > 2 {
                            // Transitive violation found
                            violations.push(Violation::transitive(
                                ViolationInfo {
                                    rule_id: rule.id.clone(),
                                    rule_constraint: rule.constraint.clone(),
                                    from_file: start_file.clone(),
                                    from_line: 1,
                                    imports_file: current.clone(),
                                    from_layer: layer_name.clone(),
                                    to_layer: current_layer.clone(),
                                    severity: rule.severity,
                                },
                                path.clone(),
                            ));
                        }
                    }

                    // Continue BFS
                    if let Some(neighbors) = adjacency.get(&current) {
                        for neighbor in neighbors {
                            if !visited.contains(neighbor) {
                                let mut new_path = path.clone();
                                new_path.push(neighbor.clone());
                                queue.push((neighbor.clone(), new_path));
                            }
                        }
                    }
                }
            }
        }
    }

    violations
}

// =============================================================================
// Tests
// =============================================================================

#[cfg(test)]
mod tests {
    use super::*;
    use crate::types::{CircularDep, RuleSeverity};

    // -------------------------------------------------------------------------
    // Rule Generation Tests
    // -------------------------------------------------------------------------

    #[test]
    fn generate_rules_yaml_format() {
        let mut inferred_layers = HashMap::new();
        inferred_layers.insert(PathBuf::from("api"), LayerType::Entry);
        inferred_layers.insert(PathBuf::from("services"), LayerType::Service);
        inferred_layers.insert(PathBuf::from("utils"), LayerType::Utility);

        let arch_report = ArchitectureReport {
            entry_layer: Vec::new(),
            middle_layer: Vec::new(),
            leaf_layer: Vec::new(),
            directories: HashMap::new(),
            circular_dependencies: Vec::new(),
            inferred_layers,
        };

        let context = RulesGenerationContext::new(PathBuf::from("."));
        let rules = generate_rules(&arch_report, &context);

        // Check version
        assert_eq!(rules.version, "1.0");

        // Check timestamp exists
        assert!(rules.generated_at.is_some());

        // Check layers
        assert!(rules.layers.high.is_some());
        assert!(rules.layers.middle.is_some());
        assert!(rules.layers.low.is_some());

        // Check rules
        assert!(!rules.rules.is_empty());
    }

    #[test]
    fn generate_rules_includes_layer_constraints() {
        let mut inferred_layers = HashMap::new();
        inferred_layers.insert(PathBuf::from("api"), LayerType::Entry);
        inferred_layers.insert(PathBuf::from("services"), LayerType::Service);
        inferred_layers.insert(PathBuf::from("utils"), LayerType::Utility);

        let arch_report = ArchitectureReport {
            entry_layer: Vec::new(),
            middle_layer: Vec::new(),
            leaf_layer: Vec::new(),
            directories: HashMap::new(),
            circular_dependencies: Vec::new(),
            inferred_layers,
        };

        let context = RulesGenerationContext::new(PathBuf::from("."));
        let rules = generate_rules(&arch_report, &context);

        // Should have L1 and L2 rules
        let l1 = rules.rules.iter().find(|r| r.id == "L1");
        assert!(l1.is_some(), "Expected L1 rule");

        let l1 = l1.unwrap();
        assert_eq!(l1.from_layers, vec!["LOW"]);
        assert_eq!(l1.to_layers, vec!["HIGH"]);
        assert_eq!(l1.rule_type, ArchRuleType::Layer);

        let l2 = rules.rules.iter().find(|r| r.id == "L2");
        assert!(l2.is_some(), "Expected L2 rule");
    }

    #[test]
    fn generate_rules_includes_cycle_breaks() {
        let mut inferred_layers = HashMap::new();
        inferred_layers.insert(PathBuf::from("api"), LayerType::Entry);
        inferred_layers.insert(PathBuf::from("services"), LayerType::Service);

        let circular_deps = vec![CircularDep {
            a: PathBuf::from("services/auth.py"),
            b: PathBuf::from("api/routes.py"),
        }];

        let arch_report = ArchitectureReport {
            entry_layer: Vec::new(),
            middle_layer: Vec::new(),
            leaf_layer: Vec::new(),
            directories: HashMap::new(),
            circular_dependencies: circular_deps,
            inferred_layers,
        };

        let context = RulesGenerationContext::new(PathBuf::from("."));
        let rules = generate_rules(&arch_report, &context);

        // Should have C1 cycle break rule
        let c1 = rules.rules.iter().find(|r| r.id == "C1");
        assert!(c1.is_some(), "Expected C1 cycle break rule");

        let c1 = c1.unwrap();
        assert_eq!(c1.rule_type, ArchRuleType::CycleBreak);
        assert_eq!(c1.severity, RuleSeverity::Warn);
        assert_eq!(c1.files.len(), 2);
    }

    // -------------------------------------------------------------------------
    // Rule Checking Tests
    // -------------------------------------------------------------------------

    #[test]
    fn check_rules_detects_layer_violation() {
        // Create a rules file
        let rules = ArchRulesFile::new().with_rule(ArchRule::layer(
            "L1",
            "LOW may not import HIGH",
            vec!["LOW".to_string()],
            vec!["HIGH".to_string()],
            "Test rationale",
        ));

        // Create an import graph with a violation (utils imports api)
        let mut import_graph = ImportGraph::new();
        import_graph.add_edge(ImportEdge {
            from_file: PathBuf::from("utils/helpers.py"),
            to_file: PathBuf::from("api/routes.py"),
            module: "api.routes".to_string(),
            line: 5,
        });

        // Create layer mappings
        let mut layers = HashMap::new();
        layers.insert(PathBuf::from("api"), LayerType::Entry);
        layers.insert(PathBuf::from("utils"), LayerType::Utility);

        let report = check_rules(&rules, &import_graph, &layers);

        assert!(!report.pass, "Expected violations");
        assert!(!report.violations.is_empty());
        assert_eq!(report.violations[0].rule_id, "L1");
    }

    #[test]
    fn check_rules_detects_cycle_violation() {
        // Create a rules file with a cycle break rule
        let rules = ArchRulesFile::new().with_rule(ArchRule::cycle_break(
            "C1",
            "Break cycle",
            vec!["services/auth.py".to_string(), "api/routes.py".to_string()],
            "Test rationale",
        ));

        // Create an import graph where the cycle exists
        let mut import_graph = ImportGraph::new();
        import_graph.add_edge(ImportEdge {
            from_file: PathBuf::from("services/auth.py"),
            to_file: PathBuf::from("api/routes.py"),
            module: "api.routes".to_string(),
            line: 1,
        });
        import_graph.add_edge(ImportEdge {
            from_file: PathBuf::from("api/routes.py"),
            to_file: PathBuf::from("services/auth.py"),
            module: "services.auth".to_string(),
            line: 1,
        });

        let layers = HashMap::new();
        let report = check_rules(&rules, &import_graph, &layers);

        assert!(report.has_violations());
        let cycle_violations: Vec<_> = report
            .violations
            .iter()
            .filter(|v| v.rule_id == "C1")
            .collect();
        assert!(!cycle_violations.is_empty());
    }

    #[test]
    fn check_rules_allows_valid_dependencies() {
        // Create rules
        let rules = ArchRulesFile::new().with_rule(ArchRule::layer(
            "L1",
            "LOW may not import HIGH",
            vec!["LOW".to_string()],
            vec!["HIGH".to_string()],
            "Test",
        ));

        // Create a valid import graph (api -> services -> utils)
        let mut import_graph = ImportGraph::new();
        import_graph.add_edge(ImportEdge {
            from_file: PathBuf::from("api/routes.py"),
            to_file: PathBuf::from("services/user.py"),
            module: "services.user".to_string(),
            line: 1,
        });
        import_graph.add_edge(ImportEdge {
            from_file: PathBuf::from("services/user.py"),
            to_file: PathBuf::from("utils/db.py"),
            module: "utils.db".to_string(),
            line: 1,
        });

        let mut layers = HashMap::new();
        layers.insert(PathBuf::from("api"), LayerType::Entry);
        layers.insert(PathBuf::from("services"), LayerType::Service);
        layers.insert(PathBuf::from("utils"), LayerType::Utility);

        let report = check_rules(&rules, &import_graph, &layers);

        assert!(report.pass, "Valid dependencies should pass");
        assert!(report.violations.is_empty());
    }

    // -------------------------------------------------------------------------
    // Import Graph Tests
    // -------------------------------------------------------------------------

    #[test]
    fn import_graph_add_edge() {
        let mut graph = ImportGraph::new();

        graph.add_edge(ImportEdge {
            from_file: PathBuf::from("a.py"),
            to_file: PathBuf::from("b.py"),
            module: "b".to_string(),
            line: 1,
        });

        assert_eq!(graph.edges.len(), 1);
        assert!(graph.files.contains(&PathBuf::from("a.py")));
        assert!(graph.files.contains(&PathBuf::from("b.py")));
    }

    #[test]
    fn resolve_python_import_absolute() {
        let all_files: HashSet<PathBuf> = [
            PathBuf::from("/project/services/user.py"),
            PathBuf::from("/project/utils/db.py"),
        ]
        .into_iter()
        .collect();

        let import = ImportInfo {
            module: "services.user".to_string(),
            names: Vec::new(),
            is_from: false,
            alias: None,
        };

        let resolved = resolve_python_import(
            &import,
            &PathBuf::from("/project/api/routes.py"),
            &PathBuf::from("/project"),
            &all_files,
        );

        assert_eq!(resolved, Some(PathBuf::from("/project/services/user.py")));
    }

    #[test]
    fn resolve_python_import_relative() {
        let all_files: HashSet<PathBuf> = [
            PathBuf::from("/project/api/routes.py"),
            PathBuf::from("/project/api/utils.py"),
        ]
        .into_iter()
        .collect();

        let import = ImportInfo {
            module: ".utils".to_string(),
            names: Vec::new(),
            is_from: true,
            alias: None,
        };

        let resolved = resolve_python_import(
            &import,
            &PathBuf::from("/project/api/routes.py"),
            &PathBuf::from("/project"),
            &all_files,
        );

        assert_eq!(resolved, Some(PathBuf::from("/project/api/utils.py")));
    }
}