loctree 0.8.16

Structural code intelligence for AI agents. Scan once, query everything.
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
//! Refactor plan generation for architectural reorganization.
//!
//! Analyzes module coupling and suggests safe file reorganization strategies.
//! Uses existing building blocks (impact, focus, cycles) with layer detection
//! heuristics and topological ordering.
//!
//! VibeCrafted with AI Agents (c)2026 Loctree Team

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

use serde::{Deserialize, Serialize};

use crate::focuser::{FocusConfig, HolographicFocus};
use crate::impact::{ImpactOptions, analyze_impact};
use crate::snapshot::Snapshot;

pub mod output;
pub use output::{output_as_json, output_as_markdown, output_as_script};

// ============================================================================
// Data Structures
// ============================================================================

/// Architectural layer classification for files.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum Layer {
    /// UI layer: components, views, pages
    UI,
    /// Application layer: hooks, services, stores
    App,
    /// Kernel layer: core business logic, domain models
    Kernel,
    /// Infrastructure layer: utils, helpers, adapters, API clients
    Infra,
    /// Test layer: test files and fixtures
    Test,
    /// Unknown layer: could not classify
    Unknown,
}

impl Layer {
    /// Get the canonical directory name for this layer.
    pub fn canonical_dir(&self) -> &'static str {
        match self {
            Layer::UI => "ui",
            Layer::App => "app",
            Layer::Kernel => "kernel",
            Layer::Infra => "infra",
            Layer::Test => "tests",
            Layer::Unknown => "misc",
        }
    }

    /// Get display name for reports.
    pub fn display_name(&self) -> &'static str {
        match self {
            Layer::UI => "UI",
            Layer::App => "App",
            Layer::Kernel => "Kernel",
            Layer::Infra => "Infra",
            Layer::Test => "Test",
            Layer::Unknown => "Unknown",
        }
    }

    /// Parse from string (for target-layout option).
    pub fn parse(s: &str) -> Option<Self> {
        match s.to_lowercase().as_str() {
            "ui" | "components" | "views" | "pages" => Some(Layer::UI),
            "app" | "application" | "services" | "hooks" => Some(Layer::App),
            "kernel" | "core" | "domain" | "models" => Some(Layer::Kernel),
            "infra" | "infrastructure" | "utils" | "lib" => Some(Layer::Infra),
            "test" | "tests" | "spec" | "specs" => Some(Layer::Test),
            _ => None,
        }
    }
}

/// Parse `--target-layout` spec like `kernel=src/kernel,ui=src/ui`.
pub fn parse_target_layout_spec(spec: &str) -> Result<HashMap<Layer, String>, String> {
    let mut map: HashMap<Layer, String> = HashMap::new();

    for entry in spec.split(',').map(|s| s.trim()).filter(|s| !s.is_empty()) {
        let (raw_key, raw_value) = entry.split_once('=').ok_or_else(|| {
            format!(
                "Invalid --target-layout entry '{}': expected KEY=PATH",
                entry
            )
        })?;

        let key = raw_key.trim();
        let value = raw_value.trim();
        if value.is_empty() {
            return Err(format!(
                "Invalid --target-layout entry '{}': PATH cannot be empty",
                entry
            ));
        }

        let layer = Layer::parse(key).ok_or_else(|| {
            format!(
                "Unknown layer '{}' in --target-layout (use ui|app|kernel|infra|tests)",
                key
            )
        })?;

        map.insert(layer, value.to_string());
    }

    if map.is_empty() {
        return Err("Invalid --target-layout: empty spec".to_string());
    }

    Ok(map)
}

/// Risk level for a file move operation.
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum RiskLevel {
    /// Low risk: few consumers, small file, not in cycle
    Low,
    /// Medium risk: moderate consumers or file size
    Medium,
    /// High risk: many consumers, large file, or part of a cycle
    High,
}

impl RiskLevel {
    /// Get display label.
    pub fn label(&self) -> &'static str {
        match self {
            RiskLevel::Low => "LOW",
            RiskLevel::Medium => "MEDIUM",
            RiskLevel::High => "HIGH",
        }
    }

    /// Get color for terminal output.
    pub fn color(&self) -> &'static str {
        match self {
            RiskLevel::Low => "green",
            RiskLevel::Medium => "yellow",
            RiskLevel::High => "red",
        }
    }
}

/// A proposed file move operation.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Move {
    /// Source file path (relative to project root)
    pub source: String,
    /// Suggested target path
    pub target: String,
    /// Current detected layer
    pub current_layer: Layer,
    /// Suggested target layer
    pub target_layer: Layer,
    /// Risk level for this move
    pub risk: RiskLevel,
    /// Number of direct consumers (files that import this)
    pub direct_consumers: usize,
    /// Number of transitive consumers (indirect dependents)
    pub transitive_consumers: usize,
    /// Lines of code in the file
    pub loc: usize,
    /// Reason for the suggested move
    pub reason: String,
    /// Verification command to run after move
    pub verify_cmd: String,
    /// List of affected files (consumers that need import updates)
    pub affected_files: Vec<String>,
}

/// A re-export shim to maintain backward compatibility.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Shim {
    /// Original file path (where the shim will be created)
    pub old_path: String,
    /// New file location
    pub new_path: String,
    /// Symbols to re-export
    pub symbols: Vec<String>,
    /// Number of files that import from old path
    pub importer_count: usize,
    /// Generated re-export code
    pub code: String,
}

/// Statistics for the refactor plan.
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct PlanStats {
    /// Total files analyzed
    pub total_files: usize,
    /// Files that need to be moved
    pub files_to_move: usize,
    /// Shims needed for backward compatibility
    pub shims_needed: usize,
    /// Layer distribution before refactoring
    pub layer_before: HashMap<String, usize>,
    /// Layer distribution after refactoring
    pub layer_after: HashMap<String, usize>,
    /// Count by risk level
    pub by_risk: HashMap<String, usize>,
}

/// A phase of execution (grouped by risk level).
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct RefactorPhase {
    /// Phase name (e.g., "Phase 1: LOW Risk")
    pub name: String,
    /// Risk level for this phase
    pub risk: RiskLevel,
    /// Moves in this phase
    pub moves: Vec<Move>,
    /// Git commands for this phase
    pub git_script: String,
}

/// Complete refactor plan for a directory.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct RefactorPlan {
    /// Target directory that was analyzed
    pub target: String,
    /// All proposed moves, ordered by risk (LOW first)
    pub moves: Vec<Move>,
    /// Shims needed for backward compatibility
    pub shims: Vec<Shim>,
    /// Files involved in cyclic dependencies
    pub cyclic_groups: Vec<Vec<String>>,
    /// Execution phases
    pub phases: Vec<RefactorPhase>,
    /// Statistics
    pub stats: PlanStats,
}

// ============================================================================
// Layer Detection
// ============================================================================

/// Detect the architectural layer of a file based on path heuristics.
pub fn detect_layer(file_path: &str) -> Layer {
    let path_lower = file_path.to_lowercase();

    // Test files
    if path_lower.contains("/test/")
        || path_lower.contains("/tests/")
        || path_lower.contains("/__tests__/")
        || path_lower.contains(".test.")
        || path_lower.contains(".spec.")
        || path_lower.contains("_test.")
        || path_lower.ends_with("_test.rs")
        || path_lower.ends_with("_test.go")
    {
        return Layer::Test;
    }

    // UI layer patterns
    if path_lower.contains("/components/")
        || path_lower.contains("/views/")
        || path_lower.contains("/pages/")
        || path_lower.contains("/ui/")
        || path_lower.contains("/widgets/")
        || path_lower.contains("/screens/")
        || path_lower.ends_with(".tsx")
        || path_lower.ends_with(".vue")
        || path_lower.ends_with(".svelte")
    {
        // But not if it's a hook or store
        if !path_lower.contains("use") && !path_lower.contains("store") {
            return Layer::UI;
        }
    }

    // App layer patterns (hooks, services, stores)
    if path_lower.contains("/hooks/")
        || path_lower.contains("/services/")
        || path_lower.contains("/stores/")
        || path_lower.contains("/state/")
        || path_lower.contains("/context/")
        || path_lower.contains("/providers/")
    {
        return Layer::App;
    }

    // Check for React hook files (useXxx.ts)
    if let Some(filename) = Path::new(file_path).file_stem().and_then(|s| s.to_str())
        && filename.starts_with("use")
        && filename.len() > 3
        && filename.chars().nth(3).unwrap_or('_').is_uppercase()
    {
        return Layer::App;
    }

    // Kernel layer patterns (core, domain, models)
    if path_lower.contains("/core/")
        || path_lower.contains("/domain/")
        || path_lower.contains("/models/")
        || path_lower.contains("/entities/")
        || path_lower.contains("/kernel/")
        || path_lower.contains("/business/")
    {
        return Layer::Kernel;
    }

    // Infra layer patterns (utils, helpers, lib, adapters)
    if path_lower.contains("/utils/")
        || path_lower.contains("/helpers/")
        || path_lower.contains("/lib/")
        || path_lower.contains("/infra/")
        || path_lower.contains("/infrastructure/")
        || path_lower.contains("/adapters/")
        || path_lower.contains("/api/")
        || path_lower.contains("/clients/")
    {
        return Layer::Infra;
    }

    Layer::Unknown
}

// ============================================================================
// Risk Calculation
// ============================================================================

/// Calculate risk level based on impact metrics.
pub fn calculate_risk(
    direct_consumers: usize,
    transitive_consumers: usize,
    loc: usize,
    in_cycle: bool,
) -> RiskLevel {
    // High risk thresholds
    if in_cycle {
        return RiskLevel::High;
    }
    if direct_consumers >= 10 || transitive_consumers >= 50 {
        return RiskLevel::High;
    }
    if loc >= 500 {
        return RiskLevel::High;
    }

    // Medium risk thresholds
    if direct_consumers >= 5 || transitive_consumers >= 20 {
        return RiskLevel::Medium;
    }
    if loc >= 200 {
        return RiskLevel::Medium;
    }

    RiskLevel::Low
}

// ============================================================================
// Cycle Detection (simplified Tarjan for file groups)
// ============================================================================

/// Detect groups of files involved in cyclic dependencies.
pub fn detect_cyclic_groups(snapshot: &Snapshot, files: &[String]) -> Vec<Vec<String>> {
    let file_set: HashSet<&str> = files.iter().map(|s| s.as_str()).collect();

    // Build adjacency list for files within scope
    let mut adjacency: HashMap<&str, Vec<&str>> = HashMap::new();
    for edge in &snapshot.edges {
        if file_set.contains(edge.from.as_str()) && file_set.contains(edge.to.as_str()) {
            adjacency
                .entry(edge.from.as_str())
                .or_default()
                .push(edge.to.as_str());
        }
    }

    // Tarjan's SCC algorithm
    let mut index_counter = 0;
    let mut stack: Vec<&str> = Vec::new();
    let mut on_stack: HashSet<&str> = HashSet::new();
    let mut index: HashMap<&str, usize> = HashMap::new();
    let mut lowlink: HashMap<&str, usize> = HashMap::new();
    let mut sccs: Vec<Vec<String>> = Vec::new();

    #[allow(clippy::too_many_arguments)]
    fn strongconnect<'a>(
        v: &'a str,
        adjacency: &HashMap<&'a str, Vec<&'a str>>,
        index_counter: &mut usize,
        stack: &mut Vec<&'a str>,
        on_stack: &mut HashSet<&'a str>,
        index: &mut HashMap<&'a str, usize>,
        lowlink: &mut HashMap<&'a str, usize>,
        sccs: &mut Vec<Vec<String>>,
    ) {
        index.insert(v, *index_counter);
        lowlink.insert(v, *index_counter);
        *index_counter += 1;
        stack.push(v);
        on_stack.insert(v);

        if let Some(neighbors) = adjacency.get(v) {
            for &w in neighbors {
                if !index.contains_key(w) {
                    strongconnect(
                        w,
                        adjacency,
                        index_counter,
                        stack,
                        on_stack,
                        index,
                        lowlink,
                        sccs,
                    );
                    let w_lowlink = *lowlink.get(w).unwrap();
                    let v_lowlink = lowlink.get_mut(v).unwrap();
                    *v_lowlink = (*v_lowlink).min(w_lowlink);
                } else if on_stack.contains(w) {
                    let w_index = *index.get(w).unwrap();
                    let v_lowlink = lowlink.get_mut(v).unwrap();
                    *v_lowlink = (*v_lowlink).min(w_index);
                }
            }
        }

        if lowlink.get(v) == index.get(v) {
            let mut scc: Vec<String> = Vec::new();
            loop {
                let w = stack.pop().unwrap();
                on_stack.remove(w);
                scc.push(w.to_string());
                if w == v {
                    break;
                }
            }
            // Only include SCCs with more than 1 node (actual cycles)
            if scc.len() > 1 {
                sccs.push(scc);
            }
        }
    }

    for file in files {
        if !index.contains_key(file.as_str()) {
            strongconnect(
                file.as_str(),
                &adjacency,
                &mut index_counter,
                &mut stack,
                &mut on_stack,
                &mut index,
                &mut lowlink,
                &mut sccs,
            );
        }
    }

    sccs
}

// ============================================================================
// Shim Detection
// ============================================================================

/// Detect which moves need re-export shims for backward compatibility.
pub fn detect_needed_shims(snapshot: &Snapshot, moves: &[Move]) -> Vec<Shim> {
    let mut shims = Vec::new();

    for mv in moves {
        if mv.direct_consumers > 3 {
            // Build list of symbols from file's exports
            let symbols: Vec<String> = snapshot
                .files
                .iter()
                .find(|f| f.path == mv.source)
                .map(|f| f.exports.iter().map(|e| e.name.clone()).collect())
                .unwrap_or_default();

            if !symbols.is_empty() {
                // Generate re-export code based on file type
                let code = generate_shim_code(&mv.source, &mv.target, &symbols);

                shims.push(Shim {
                    old_path: mv.source.clone(),
                    new_path: mv.target.clone(),
                    symbols,
                    importer_count: mv.direct_consumers,
                    code,
                });
            }
        }
    }

    shims
}

/// Generate re-export shim code based on file extension.
fn generate_shim_code(old_path: &str, new_path: &str, symbols: &[String]) -> String {
    let ext = Path::new(old_path)
        .extension()
        .and_then(|e| e.to_str())
        .unwrap_or("");

    // Calculate relative path from old to new
    let relative_path = calculate_relative_import(old_path, new_path);

    match ext {
        "ts" | "tsx" | "js" | "jsx" | "mjs" => {
            if symbols.len() <= 5 {
                format!(
                    "// Shim for backward compatibility\nexport {{ {} }} from '{}';",
                    symbols.join(", "),
                    relative_path
                )
            } else {
                format!(
                    "// Shim for backward compatibility\nexport * from '{}';",
                    relative_path
                )
            }
        }
        "rs" => {
            format!(
                "// Shim for backward compatibility\npub use {}::*;",
                relative_path.replace('/', "::")
            )
        }
        "py" => {
            format!(
                "# Shim for backward compatibility\nfrom {} import *",
                relative_path.replace('/', ".")
            )
        }
        _ => format!("// TODO: Create re-export shim pointing to {}", new_path),
    }
}

/// Calculate relative import path between two files.
fn calculate_relative_import(from: &str, to: &str) -> String {
    let from_parts: Vec<&str> = from.split('/').collect();
    let to_parts: Vec<&str> = to.split('/').collect();

    // Find common prefix length
    let common_len = from_parts
        .iter()
        .zip(to_parts.iter())
        .take_while(|(a, b)| a == b)
        .count();

    // Build relative path
    let ups = from_parts.len() - common_len - 1; // -1 for filename
    let downs: Vec<&str> = to_parts[common_len..].to_vec();

    let mut result = String::new();
    if ups == 0 {
        result.push_str("./");
    } else {
        for _ in 0..ups {
            result.push_str("../");
        }
    }
    result.push_str(&downs.join("/"));

    // Remove extension for import
    if let Some(pos) = result.rfind('.') {
        result.truncate(pos);
    }

    result
}

// ============================================================================
// Move Ordering
// ============================================================================

/// Order moves by risk (LOW first) for safe incremental execution.
pub fn order_moves(moves: &mut [Move]) {
    moves.sort_by(|a, b| {
        // Primary: risk level (Low < Medium < High)
        match a.risk.cmp(&b.risk) {
            std::cmp::Ordering::Equal => {
                // Secondary: fewer consumers first
                match a.direct_consumers.cmp(&b.direct_consumers) {
                    std::cmp::Ordering::Equal => {
                        // Tertiary: smaller files first
                        a.loc.cmp(&b.loc)
                    }
                    other => other,
                }
            }
            other => other,
        }
    });
}

// ============================================================================
// Main Plan Generation
// ============================================================================

/// Generate a refactor plan for a target directory.
///
/// Returns `None` if no files need reorganization.
pub fn generate_refactor_plan(
    snapshot: &Snapshot,
    target_dir: &str,
    target_layout: Option<&HashMap<Layer, String>>,
) -> Option<RefactorPlan> {
    // Get holographic focus for the target directory
    let config = FocusConfig {
        include_consumers: true,
        max_depth: 3,
    };

    let focus = HolographicFocus::from_path(snapshot, target_dir, &config)?;

    if focus.core.is_empty() {
        return None;
    }

    // Collect all file paths for cycle detection
    let file_paths: Vec<String> = focus.core.iter().map(|f| f.path.clone()).collect();

    // Detect cyclic groups
    let cyclic_groups = detect_cyclic_groups(snapshot, &file_paths);
    let files_in_cycles: HashSet<&str> = cyclic_groups
        .iter()
        .flat_map(|g| g.iter())
        .map(|s| s.as_str())
        .collect();

    // Generate moves
    let impact_opts = ImpactOptions::default();
    let mut moves: Vec<Move> = Vec::new();
    let mut layer_before: HashMap<String, usize> = HashMap::new();
    let mut layer_after: HashMap<String, usize> = HashMap::new();

    for file in &focus.core {
        let current_layer = detect_layer(&file.path);
        *layer_before
            .entry(current_layer.display_name().to_string())
            .or_default() += 1;

        // Analyze impact for this file
        let impact = analyze_impact(snapshot, &file.path, &impact_opts);

        // Determine target layer
        let target_layer = if current_layer == Layer::Unknown {
            // Try to infer from content or neighbors
            infer_target_layer(&file.path, snapshot)
        } else {
            current_layer
        };

        *layer_after
            .entry(target_layer.display_name().to_string())
            .or_default() += 1;

        // Only create a move if layer changed or file is misplaced
        let needs_move =
            current_layer == Layer::Unknown || !file.path.contains(current_layer.canonical_dir());

        if needs_move {
            let in_cycle = files_in_cycles.contains(file.path.as_str());
            let risk = calculate_risk(
                impact.direct_consumers.len(),
                impact.transitive_consumers.len(),
                file.loc,
                in_cycle,
            );

            // Build target path
            let target_path =
                build_target_path(&file.path, target_layer, target_layout, target_dir);

            let reason = build_move_reason(current_layer, target_layer, in_cycle);
            let verify_cmd = format!("loct impact {}", target_path);

            let affected_files: Vec<String> = impact
                .direct_consumers
                .iter()
                .map(|e| e.file.clone())
                .collect();

            moves.push(Move {
                source: file.path.clone(),
                target: target_path,
                current_layer,
                target_layer,
                risk,
                direct_consumers: impact.direct_consumers.len(),
                transitive_consumers: impact.transitive_consumers.len(),
                loc: file.loc,
                reason,
                verify_cmd,
                affected_files,
            });
        }
    }

    if moves.is_empty() {
        return None;
    }

    // Order moves by risk
    order_moves(&mut moves);

    // Detect needed shims
    let shims = detect_needed_shims(snapshot, &moves);

    // Build phases
    let phases = build_phases(&moves);

    // Build stats
    let mut by_risk: HashMap<String, usize> = HashMap::new();
    for mv in &moves {
        *by_risk.entry(mv.risk.label().to_string()).or_default() += 1;
    }

    let stats = PlanStats {
        total_files: focus.core.len(),
        files_to_move: moves.len(),
        shims_needed: shims.len(),
        layer_before,
        layer_after,
        by_risk,
    };

    Some(RefactorPlan {
        target: target_dir.to_string(),
        moves,
        shims,
        cyclic_groups,
        phases,
        stats,
    })
}

/// Infer target layer from file content and neighbors.
fn infer_target_layer(file_path: &str, snapshot: &Snapshot) -> Layer {
    // Check what the file imports
    let imports: Vec<&str> = snapshot
        .edges
        .iter()
        .filter(|e| e.from == file_path)
        .map(|e| e.to.as_str())
        .collect();

    // Count layers of imported files
    let mut layer_counts: HashMap<Layer, usize> = HashMap::new();
    for imp in imports {
        let layer = detect_layer(imp);
        if layer != Layer::Unknown {
            *layer_counts.entry(layer).or_default() += 1;
        }
    }

    // Return most common layer, or Infra as default
    layer_counts
        .into_iter()
        .max_by_key(|(_, count)| *count)
        .map(|(layer, _)| layer)
        .unwrap_or(Layer::Infra)
}

/// Build target path for a file move.
fn build_target_path(
    source: &str,
    layer: Layer,
    custom_layout: Option<&HashMap<Layer, String>>,
    base_dir: &str,
) -> String {
    let filename = Path::new(source)
        .file_name()
        .and_then(|n| n.to_str())
        .unwrap_or(source);

    let layer_dir = custom_layout
        .and_then(|m| m.get(&layer))
        .map(|s| s.as_str())
        .unwrap_or_else(|| layer.canonical_dir());

    format!(
        "{}/{}/{}",
        base_dir.trim_end_matches('/'),
        layer_dir,
        filename
    )
}

/// Build reason string for a move.
fn build_move_reason(current: Layer, target: Layer, in_cycle: bool) -> String {
    let mut parts = Vec::new();

    if current == Layer::Unknown {
        parts.push(format!("Unclassified → {}", target.display_name()));
    } else if current != target {
        parts.push(format!(
            "{} → {}",
            current.display_name(),
            target.display_name()
        ));
    }

    if in_cycle {
        parts.push("In cycle".to_string());
    }

    if parts.is_empty() {
        "Misplaced file".to_string()
    } else {
        parts.join(", ")
    }
}

/// Build execution phases from ordered moves.
fn build_phases(moves: &[Move]) -> Vec<RefactorPhase> {
    let mut phases: Vec<RefactorPhase> = Vec::new();
    let mut current_risk: Option<RiskLevel> = None;
    let mut current_moves: Vec<Move> = Vec::new();

    for mv in moves {
        if Some(mv.risk) != current_risk {
            if !current_moves.is_empty()
                && let Some(risk) = current_risk
            {
                phases.push(RefactorPhase {
                    name: format!("Phase {}: {} Risk", phases.len() + 1, risk.label()),
                    risk,
                    git_script: build_git_script(&current_moves),
                    moves: std::mem::take(&mut current_moves),
                });
            }
            current_risk = Some(mv.risk);
        }
        current_moves.push(mv.clone());
    }

    // Add final phase
    if !current_moves.is_empty()
        && let Some(risk) = current_risk
    {
        phases.push(RefactorPhase {
            name: format!("Phase {}: {} Risk", phases.len() + 1, risk.label()),
            risk,
            git_script: build_git_script(&current_moves),
            moves: current_moves,
        });
    }

    phases
}

/// Build git script for a set of moves.
fn build_git_script(moves: &[Move]) -> String {
    let mut lines: Vec<String> = Vec::new();

    for mv in moves {
        // Ensure parent directory exists
        if let Some(parent) = Path::new(&mv.target).parent() {
            lines.push(format!("mkdir -p {}", parent.display()));
        }
        lines.push(format!("git mv {} {}", mv.source, mv.target));
    }

    lines.join("\n")
}

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

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

    #[test]
    fn test_detect_layer_ui() {
        assert_eq!(detect_layer("src/components/Button.tsx"), Layer::UI);
        assert_eq!(detect_layer("src/views/Home.vue"), Layer::UI);
        assert_eq!(detect_layer("src/pages/index.tsx"), Layer::UI);
    }

    #[test]
    fn test_detect_layer_app() {
        assert_eq!(detect_layer("src/hooks/useAuth.ts"), Layer::App);
        assert_eq!(detect_layer("src/services/api.ts"), Layer::App);
        assert_eq!(detect_layer("src/stores/userStore.ts"), Layer::App);
    }

    #[test]
    fn test_detect_layer_kernel() {
        assert_eq!(detect_layer("src/core/engine.ts"), Layer::Kernel);
        assert_eq!(detect_layer("src/domain/Patient.ts"), Layer::Kernel);
        assert_eq!(detect_layer("src/models/User.ts"), Layer::Kernel);
    }

    #[test]
    fn test_detect_layer_infra() {
        assert_eq!(detect_layer("src/utils/format.ts"), Layer::Infra);
        assert_eq!(detect_layer("src/lib/helpers.ts"), Layer::Infra);
        assert_eq!(detect_layer("src/api/client.ts"), Layer::Infra);
    }

    #[test]
    fn test_detect_layer_test() {
        assert_eq!(detect_layer("src/__tests__/Button.test.tsx"), Layer::Test);
        assert_eq!(detect_layer("tests/integration.spec.ts"), Layer::Test);
        assert_eq!(detect_layer("src/utils_test.go"), Layer::Test);
    }

    #[test]
    fn test_calculate_risk_low() {
        assert_eq!(calculate_risk(2, 5, 100, false), RiskLevel::Low);
    }

    #[test]
    fn test_calculate_risk_medium() {
        assert_eq!(calculate_risk(6, 10, 100, false), RiskLevel::Medium);
        assert_eq!(calculate_risk(2, 25, 100, false), RiskLevel::Medium);
        assert_eq!(calculate_risk(2, 5, 300, false), RiskLevel::Medium);
    }

    #[test]
    fn test_calculate_risk_high() {
        assert_eq!(calculate_risk(12, 10, 100, false), RiskLevel::High);
        assert_eq!(calculate_risk(2, 60, 100, false), RiskLevel::High);
        assert_eq!(calculate_risk(2, 5, 600, false), RiskLevel::High);
        assert_eq!(calculate_risk(2, 5, 100, true), RiskLevel::High); // in cycle
    }

    #[test]
    fn test_calculate_relative_import_same_dir() {
        let result = calculate_relative_import("src/utils/a.ts", "src/utils/b.ts");
        assert_eq!(result, "./b");
    }

    #[test]
    fn test_calculate_relative_import_parent_dir() {
        let result = calculate_relative_import("src/components/Button.tsx", "src/utils/format.ts");
        assert_eq!(result, "../utils/format");
    }

    #[test]
    fn test_order_moves() {
        let mut moves = vec![
            Move {
                source: "a.ts".to_string(),
                target: "".to_string(),
                current_layer: Layer::Unknown,
                target_layer: Layer::Infra,
                risk: RiskLevel::High,
                direct_consumers: 5,
                transitive_consumers: 10,
                loc: 100,
                reason: "".to_string(),
                verify_cmd: "".to_string(),
                affected_files: vec![],
            },
            Move {
                source: "b.ts".to_string(),
                target: "".to_string(),
                current_layer: Layer::Unknown,
                target_layer: Layer::Infra,
                risk: RiskLevel::Low,
                direct_consumers: 1,
                transitive_consumers: 2,
                loc: 50,
                reason: "".to_string(),
                verify_cmd: "".to_string(),
                affected_files: vec![],
            },
        ];

        order_moves(&mut moves);

        assert_eq!(moves[0].source, "b.ts"); // Low risk first
        assert_eq!(moves[1].source, "a.ts"); // High risk last
    }
}