agpm-cli 0.4.14

AGent Package Manager - A Git-based package manager for coding agents
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
//! Display dependency trees for installed resources.
//!
//! This module provides the `tree` command which visualizes dependencies and their
//! transitive dependencies in a hierarchical tree format, similar to `cargo tree`.
//! It helps users understand the dependency graph and identify duplicate or
//! redundant dependencies.
//!
//! # Features
//!
//! - **Hierarchical Display**: Shows dependencies in a tree structure
//! - **Transitive Dependencies**: Visualizes the full dependency graph
//! - **Deduplication**: Marks duplicate dependencies with (*)
//! - **Filtering**: Filter by resource type (agents, snippets, commands, etc.)
//! - **Multiple Formats**: Tree, JSON, and text output formats
//! - **Depth Limiting**: Control how deep to traverse the tree
//! - **Colored Output**: Uses colors to highlight different elements
//!
//! # Examples
//!
//! Display the full dependency tree:
//! ```bash
//! agpm tree
//! ```
//!
//! Limit tree depth:
//! ```bash
//! agpm tree --depth 2
//! ```
//!
//! Show tree for a specific package:
//! ```bash
//! agpm tree --package my-agent
//! ```
//!
//! Show only duplicates:
//! ```bash
//! agpm tree --duplicates
//! ```
//!
//! Output as JSON:
//! ```bash
//! agpm tree --format json
//! ```
//!
//! # Output Format
//!
//! ## Tree Format (Default)
//! ```text
//! my-project
//! ├── agent/code-reviewer v1.0.0 (community)
//! │   ├── agent/rust-helper v1.0.0 (community)
//! │   └── snippet/utils v2.1.0 (community)
//! ├── command/git-commit v1.0.0 (local)
//! │   ├── agent/rust-helper v1.0.0 (community) (*)
//! │   └── snippet/commit-msg v1.0.0 (local)
//! └── snippet/logging v1.5.0 (community)
//!
//! (*) = duplicate dependency
//! ```

use anyhow::{Context, Result};
use clap::Args;
use colored::Colorize;
use std::collections::{BTreeMap, HashMap, HashSet};
use std::path::PathBuf;

use crate::cache::Cache;
use crate::core::ResourceType;
use crate::lockfile::patch_display::extract_patch_displays;
use crate::lockfile::{LockFile, LockedResource};
use crate::manifest::find_manifest_with_optional;

/// Command to display dependency trees.
///
/// This command reads the lockfile to show the complete dependency tree,
/// including transitive dependencies. It provides various filtering and
/// formatting options to help users understand their dependency structure.
#[derive(Args, Debug)]
pub struct TreeCommand {
    /// Maximum depth to display (unlimited if not specified)
    ///
    /// Limits how many levels deep the tree will traverse. This is useful
    /// for large dependency graphs where you only want to see top-level
    /// dependencies.
    ///
    /// # Examples
    ///
    /// ```bash
    /// agpm tree --depth 1    # Show only direct dependencies
    /// agpm tree --depth 3    # Show up to 3 levels
    /// ```
    #[arg(short = 'd', long)]
    depth: Option<usize>,

    /// Output format (tree, json, text)
    ///
    /// Controls how the dependency information is displayed:
    /// - `tree`: Hierarchical tree with box-drawing characters (default)
    /// - `json`: JSON format for scripting and programmatic access
    /// - `text`: Simple indented text format
    #[arg(short = 'f', long, default_value = "tree")]
    format: String,

    /// Show only duplicate dependencies
    ///
    /// When enabled, only shows dependencies that appear multiple times
    /// in the tree. This helps identify redundant dependencies.
    #[arg(long)]
    duplicates: bool,

    /// Don't deduplicate repeated dependencies
    ///
    /// By default, repeated dependencies are marked with (*) and only
    /// shown in full the first time. This flag shows them in full every time.
    #[arg(long)]
    no_dedupe: bool,

    /// Show tree for specific package only
    ///
    /// Displays the dependency tree starting from the specified package.
    /// The package name should match a dependency name from the manifest.
    ///
    /// # Examples
    ///
    /// ```bash
    /// agpm tree --package my-agent
    /// agpm tree -p code-reviewer
    /// ```
    #[arg(short = 'p', long)]
    package: Option<String>,

    /// Show only agents
    #[arg(long)]
    agents: bool,

    /// Show only snippets
    #[arg(long)]
    snippets: bool,

    /// Show only commands
    #[arg(long)]
    commands: bool,

    /// Show only scripts
    #[arg(long)]
    scripts: bool,

    /// Show only hooks
    #[arg(long)]
    hooks: bool,

    /// Show only MCP servers
    #[arg(long, name = "mcp-servers")]
    mcp_servers: bool,

    /// Invert tree to show what depends on each package
    ///
    /// Instead of showing what each package depends on, shows what depends
    /// on each package. Useful for understanding the impact of changes.
    #[arg(short = 'i', long)]
    invert: bool,

    /// Show detailed information including installation paths and applied patches
    ///
    /// When enabled, displays the absolute installation path for each resource
    /// and lists all applied patches with their values. This is useful for
    /// debugging patch application and understanding where resources are installed.
    ///
    /// # Examples
    ///
    /// ```bash
    /// agpm tree --detailed
    /// ```
    #[arg(long)]
    detailed: bool,
}

impl TreeCommand {
    /// Execute the tree command with an optional manifest path.
    pub async fn execute_with_manifest_path(self, manifest_path: Option<PathBuf>) -> Result<()> {
        // Validate arguments
        self.validate_arguments()?;

        // Find manifest file
        let manifest_path = find_manifest_with_optional(manifest_path)
            .context("No agpm.toml found. Please create one to define your dependencies.")?;

        self.execute_from_path(manifest_path).await
    }

    async fn execute_from_path(self, manifest_path: PathBuf) -> Result<()> {
        // Validate arguments
        self.validate_arguments()?;

        // Require the manifest to exist
        if !manifest_path.exists() {
            return Err(anyhow::anyhow!("Manifest file {} not found", manifest_path.display()));
        }

        let project_dir = manifest_path.parent().unwrap();
        let lockfile_path = project_dir.join("agpm.lock");

        // Derive project name from directory
        let project_name =
            project_dir.file_name().and_then(|n| n.to_str()).unwrap_or("project").to_string();

        // Check if lockfile exists
        if !lockfile_path.exists() {
            if self.format == "json" {
                println!("{{}}");
            } else {
                println!("No lockfile found.");
                println!("⚠️  Run 'agpm install' first to generate agpm.lock");
            }
            return Ok(());
        }

        // Create command context for enhanced lockfile loading
        let manifest_path = project_dir.join("agpm.toml");
        let manifest = crate::manifest::Manifest::load(&manifest_path)?;
        let command_context =
            crate::cli::common::CommandContext::new(manifest, project_dir.to_path_buf())?;

        // Use enhanced lockfile loading with automatic regeneration
        let lockfile = match command_context.load_lockfile_with_regeneration(true, "tree")? {
            Some(lockfile) => lockfile,
            None => {
                // Lockfile was regenerated and doesn't exist yet
                if self.format == "json" {
                    println!("{{}}");
                } else {
                    println!("No lockfile found.");
                    println!(
                        "⚠️  Lockfile was invalid and has been removed. Run 'agpm install' to regenerate it."
                    );
                }
                return Ok(());
            }
        };

        // Create cache if needed for detailed mode with patches
        let cache = if self.detailed {
            Some(Cache::new().context("Failed to initialize cache")?)
        } else {
            None
        };

        // Build and display tree
        let builder = TreeBuilder::new(&lockfile, project_name);
        let tree = builder.build(&self)?;

        match self.format.as_str() {
            "json" => self.output_json(&tree)?,
            "text" => self.output_text(&tree),
            _ => self.output_tree_with_cache(&tree, &lockfile, cache.as_ref()).await,
        }

        Ok(())
    }

    fn validate_arguments(&self) -> Result<()> {
        // Validate format
        match self.format.as_str() {
            "tree" | "json" | "text" => {}
            _ => {
                return Err(anyhow::anyhow!(
                    "Invalid format '{}'. Valid formats are: tree, json, text",
                    self.format
                ));
            }
        }

        // Validate depth
        if let Some(depth) = self.depth
            && depth == 0
        {
            return Err(anyhow::anyhow!("Depth must be at least 1"));
        }

        Ok(())
    }

    /// Check if a resource type should be shown based on filters
    const fn should_show_resource_type(&self, resource_type: ResourceType) -> bool {
        // If no type filters are set, show all types
        let any_filter = self.agents
            || self.snippets
            || self.commands
            || self.scripts
            || self.hooks
            || self.mcp_servers;

        if !any_filter {
            return true;
        }

        // Check individual flags
        match resource_type {
            ResourceType::Agent => self.agents,
            ResourceType::Snippet => self.snippets,
            ResourceType::Command => self.commands,
            ResourceType::Script => self.scripts,
            ResourceType::Hook => self.hooks,
            ResourceType::McpServer => self.mcp_servers,
            ResourceType::Skill => true, // Show skills when no filter is set
        }
    }

    async fn output_tree_with_cache(
        &self,
        tree: &DependencyTree,
        lockfile: &LockFile,
        cache: Option<&Cache>,
    ) {
        if tree.roots.is_empty() {
            println!("No dependencies found.");
            return;
        }

        // Print project root
        println!("{}", tree.project_name.cyan().bold());

        // Track which nodes we've already displayed in full
        let mut displayed = HashSet::new();

        for (i, root) in tree.roots.iter().enumerate() {
            let is_last = i == tree.roots.len() - 1;
            self.print_node_with_cache(root, "", is_last, &mut displayed, tree, 0, lockfile, cache)
                .await;
        }

        // Print legend if there are duplicates
        if !self.no_dedupe && tree.has_duplicates() {
            println!();
            println!("{}", "(*) = duplicate dependency (already shown above)".blue());
        }
    }

    #[allow(clippy::too_many_arguments)]
    fn print_node_with_cache<'a>(
        &'a self,
        node: &'a TreeNode,
        prefix: &'a str,
        is_last: bool,
        displayed: &'a mut HashSet<String>,
        tree: &'a DependencyTree,
        current_depth: usize,
        lockfile: &'a LockFile,
        cache: Option<&'a Cache>,
    ) -> std::pin::Pin<Box<dyn std::future::Future<Output = ()> + 'a>> {
        Box::pin(async move {
            // Check depth limit
            if let Some(max_depth) = self.depth
                && current_depth >= max_depth
            {
                return;
            }

            let node_id = format!("{}/{}", node.resource_type, node.name);
            let is_duplicate = !self.no_dedupe && displayed.contains(&node_id);

            // Print connector
            let connector = if is_last {
                "└── "
            } else {
                "├── "
            };

            // Format node display: type/name version (source) [tool] (patched)
            // Use blue for secondary text - readable on both light and dark backgrounds
            let type_str = format!("{}", node.resource_type).blue();
            let name_str = node.name.cyan();
            let version_str =
                node.version.as_deref().map(|v| format!(" {}", v.blue())).unwrap_or_default();
            let source_str = node
                .source
                .as_deref()
                .map_or_else(|| " (local)".blue().to_string(), |s| format!(" ({})", s.blue()));
            let tool_str = node
                .tool
                .as_deref()
                .map(|tool| format!(" [{}]", tool.bright_yellow()))
                .unwrap_or_default();
            let patch_marker = if node.has_patches {
                format!(" {}", "(patched)".blue())
            } else {
                String::new()
            };
            let dup_marker = if is_duplicate {
                " (*)".blue().to_string()
            } else {
                String::new()
            };
            let token_str = node
                .approximate_token_count
                .map(|c| {
                    format!(" (~{} tok)", crate::tokens::format_token_count(c as usize))
                        .bright_black()
                        .to_string()
                })
                .unwrap_or_default();

            println!(
                "{prefix}{connector}{type_str}/{name_str}{version_str}{source_str}{tool_str}{patch_marker}{dup_marker}{token_str}"
            );

            // Show detailed information if --detailed flag is set
            if self.detailed && !is_duplicate {
                let detail_prefix = if is_last {
                    format!("{prefix}    ")
                } else {
                    format!("{prefix}")
                };

                // Show installation path
                if !node.installed_at.is_empty() {
                    println!(
                        "{}    {}: {}",
                        detail_prefix,
                        "Installed at".bright_yellow(),
                        node.installed_at.bright_black()
                    );
                }

                // Show token count
                if let Some(token_count) = node.approximate_token_count {
                    let formatted = crate::tokens::format_token_count(token_count as usize);
                    println!(
                        "{}    {}: ~{}",
                        detail_prefix,
                        "Tokens".bright_yellow(),
                        formatted.bright_black()
                    );
                }

                // Show applied patches with original → overridden comparison
                if !node.applied_patches.is_empty() {
                    println!("{}    {}", detail_prefix, "Applied patches:".bright_yellow());

                    // If we have cache, try to get original values
                    if let Some(cache) = cache {
                        // Find the locked resource for this node
                        if let Some(locked_resource) =
                            self.find_locked_resource_for_node(node, lockfile)
                        {
                            let patch_displays =
                                extract_patch_displays(locked_resource, cache).await;
                            for display in patch_displays {
                                let formatted = display.format();
                                // Indent each line of the multi-line diff output
                                for (i, line) in formatted.lines().enumerate() {
                                    if i == 0 {
                                        // First line: bullet point
                                        println!("{}{}", detail_prefix, line);
                                    } else {
                                        // Subsequent lines: indent to align with content
                                        println!("{}         {}", detail_prefix, line);
                                    }
                                }
                            }
                        } else {
                            // Fallback: just show overridden values
                            self.print_patches_fallback_tree(&node.applied_patches, &detail_prefix);
                        }
                    } else {
                        // No cache: just show overridden values
                        self.print_patches_fallback_tree(&node.applied_patches, &detail_prefix);
                    }
                }
            }

            // If this is a duplicate and we're deduplicating, don't show children
            if is_duplicate {
                return;
            }

            // Mark as displayed
            displayed.insert(node_id);

            // Print children
            if !node.dependencies.is_empty() {
                let child_prefix = if is_last {
                    format!("{prefix}    ")
                } else {
                    format!("{prefix}")
                };

                for (i, dep_id) in node.dependencies.iter().enumerate() {
                    if let Some(child_node) = tree.nodes.get(dep_id) {
                        let is_last_child = i == node.dependencies.len() - 1;
                        self.print_node_with_cache(
                            child_node,
                            &child_prefix,
                            is_last_child,
                            displayed,
                            tree,
                            current_depth + 1,
                            lockfile,
                            cache,
                        )
                        .await;
                    }
                }
            }
        })
    }

    /// Fallback patch display without original values for tree output
    fn print_patches_fallback_tree(&self, patches: &BTreeMap<String, toml::Value>, prefix: &str) {
        let mut patch_keys: Vec<_> = patches.keys().collect();
        patch_keys.sort();
        for key in patch_keys {
            let value = &patches[key];
            let value_str = format_patch_value(value);
            println!("{}{}: {}", prefix, key.blue(), value_str);
        }
    }

    /// Find the locked resource corresponding to a tree node
    fn find_locked_resource_for_node<'a>(
        &self,
        node: &TreeNode,
        lockfile: &'a LockFile,
    ) -> Option<&'a LockedResource> {
        // Find matching resource in lockfile by name and resource type
        lockfile.get_resources(&node.resource_type).iter().find(|r| {
            // Extract display name from locked resource's unique name
            let display_name = TreeBuilder::extract_display_name(&r.name);
            display_name == node.name && r.source == node.source && r.version == node.version
        })
    }

    fn output_json(&self, tree: &DependencyTree) -> Result<()> {
        let json = serde_json::json!({
            "project": tree.project_name,
            "roots": tree.roots.iter().map(|n| self.node_to_json(n, tree, 0)).collect::<Vec<_>>(),
        });

        println!("{}", serde_json::to_string_pretty(&json)?);
        Ok(())
    }

    fn node_to_json(
        &self,
        node: &TreeNode,
        tree: &DependencyTree,
        depth: usize,
    ) -> serde_json::Value {
        // Check depth limit
        let children = if let Some(max_depth) = self.depth {
            if depth >= max_depth {
                vec![]
            } else {
                node.dependencies
                    .iter()
                    .filter_map(|id| tree.nodes.get(id))
                    .map(|child| self.node_to_json(child, tree, depth + 1))
                    .collect()
            }
        } else {
            node.dependencies
                .iter()
                .filter_map(|id| tree.nodes.get(id))
                .map(|child| self.node_to_json(child, tree, depth + 1))
                .collect()
        };

        serde_json::json!({
            "name": node.name,
            "type": node.resource_type.to_string(),
            "version": node.version,
            "source": node.source,
            "tool": node.tool.as_deref().unwrap_or("claude-code"),
            "has_patches": node.has_patches,
            "dependencies": children,
        })
    }

    fn output_text(&self, tree: &DependencyTree) {
        if tree.roots.is_empty() {
            println!("No dependencies found.");
            return;
        }

        println!("{}", tree.project_name);

        let mut displayed = HashSet::new();
        for root in &tree.roots {
            self.print_text_node(root, 0, &mut displayed, tree, 0);
        }
    }

    fn print_text_node(
        &self,
        node: &TreeNode,
        indent: usize,
        displayed: &mut HashSet<String>,
        tree: &DependencyTree,
        current_depth: usize,
    ) {
        // Check depth limit
        if let Some(max_depth) = self.depth
            && current_depth >= max_depth
        {
            return;
        }

        let node_id = format!("{}/{}", node.resource_type, node.name);
        let is_duplicate = !self.no_dedupe && displayed.contains(&node_id);

        let indent_str = "  ".repeat(indent);
        let version_str = node.version.as_deref().unwrap_or("latest");
        let source_str = node.source.as_deref().unwrap_or("local");
        let patch_marker = if node.has_patches {
            format!(" {}", "(patched)".blue())
        } else {
            String::new()
        };
        let dup_marker = if is_duplicate {
            " (*)"
        } else {
            ""
        };

        println!(
            "{}{}/{} {} ({}) {}{}{}",
            indent_str,
            node.resource_type,
            node.name,
            version_str,
            source_str,
            node.tool.as_deref().map(|tool| format!("[{}] ", tool)).unwrap_or_default(),
            patch_marker,
            dup_marker
        );

        if is_duplicate {
            return;
        }

        displayed.insert(node_id);

        for dep_id in &node.dependencies {
            if let Some(child_node) = tree.nodes.get(dep_id) {
                self.print_text_node(child_node, indent + 1, displayed, tree, current_depth + 1);
            }
        }
    }
}

/// A node in the dependency tree
#[derive(Debug, Clone)]
struct TreeNode {
    name: String,
    resource_type: ResourceType,
    version: Option<String>,
    source: Option<String>,
    tool: Option<String>,
    dependencies: Vec<String>, // IDs of dependency nodes
    has_patches: bool,         // True if resource has applied patches
    installed_at: String,      // Installation path for detailed output
    applied_patches: std::collections::BTreeMap<String, toml::Value>, // Patch field -> value mapping
    approximate_token_count: Option<u64>, // Approximate token count for display
}

/// The complete dependency tree structure
#[derive(Debug)]
struct DependencyTree {
    project_name: String,
    nodes: HashMap<String, TreeNode>,
    roots: Vec<TreeNode>,
}

impl DependencyTree {
    fn has_duplicates(&self) -> bool {
        let mut seen = HashSet::new();
        for root in &self.roots {
            if self.has_duplicates_recursive(root, &mut seen) {
                return true;
            }
        }
        false
    }

    fn has_duplicates_recursive(&self, node: &TreeNode, seen: &mut HashSet<String>) -> bool {
        let node_id = format!("{}/{}", node.resource_type, node.name);

        if !seen.insert(node_id) {
            return true;
        }

        for dep_id in &node.dependencies {
            if let Some(child) = self.nodes.get(dep_id)
                && self.has_duplicates_recursive(child, seen)
            {
                return true;
            }
        }

        false
    }
}

/// Builds the dependency tree from the lockfile
struct TreeBuilder<'a> {
    lockfile: &'a LockFile,
    project_name: String,
}

impl<'a> TreeBuilder<'a> {
    const fn new(lockfile: &'a LockFile, project_name: String) -> Self {
        Self {
            lockfile,
            project_name,
        }
    }

    fn build(&self, cmd: &TreeCommand) -> Result<DependencyTree> {
        let mut nodes = HashMap::new();
        let mut roots = Vec::new();

        // If a specific package is requested, find it
        if let Some(ref package_name) = cmd.package {
            let found = self.find_package(package_name)?;
            let node = self.build_node(found, cmd)?;
            let node_id = self.node_id(&node);

            nodes.insert(node_id, node.clone());
            self.build_dependencies(&node, &mut nodes, cmd)?;
            roots.push(node);
        } else {
            // First, build all nodes and their dependencies
            for resource_type in ResourceType::all() {
                if !cmd.should_show_resource_type(*resource_type) {
                    continue;
                }

                for resource in self.lockfile.get_resources(resource_type) {
                    let node = self.build_node(resource, cmd)?;
                    let node_id = self.node_id(&node);

                    nodes.insert(node_id.clone(), node.clone());
                    self.build_dependencies(&node, &mut nodes, cmd)?;
                }
            }

            // Determine if any resource type filters are active
            let has_type_filter = cmd.agents
                || cmd.snippets
                || cmd.commands
                || cmd.scripts
                || cmd.hooks
                || cmd.mcp_servers;

            if has_type_filter {
                // When filtering by resource type, show ALL resources of that type as roots
                // (don't exclude dependencies)
                for node in nodes.values() {
                    if cmd.should_show_resource_type(node.resource_type) {
                        roots.push(node.clone());
                    }
                }
            } else {
                // Normal mode: identify roots as resources that are NOT dependencies of anything else
                // Build a set of all dependency IDs (already in singular "type/name" format)
                let mut all_dependencies = HashSet::new();
                for resource_type in ResourceType::all() {
                    for resource in self.lockfile.get_resources(resource_type) {
                        for dep_id in &resource.dependencies {
                            // Dependencies are already in singular form (e.g., "agent/foo")
                            all_dependencies.insert(dep_id.clone());
                        }
                    }
                }

                // Roots are nodes that are not in the dependencies set
                // All IDs use singular "type/name" format
                for node in nodes.values() {
                    let simple_id = format!("{}/{}", node.resource_type, node.name);
                    if !all_dependencies.contains(&simple_id) {
                        roots.push(node.clone());
                    }
                }
            }

            // Sort roots by tool, then by resource type alphabetically, then by name
            roots.sort_by(|a, b| {
                a.tool
                    .cmp(&b.tool)
                    .then_with(|| a.resource_type.to_string().cmp(&b.resource_type.to_string()))
                    .then_with(|| a.name.cmp(&b.name))
            });
        }

        // Filter to only duplicates if requested
        if cmd.duplicates {
            let duplicate_ids = self.find_duplicates(&roots, &nodes);
            roots.retain(|n| duplicate_ids.contains(&self.node_id(n)));
        }

        Ok(DependencyTree {
            project_name: self.project_name.clone(),
            nodes,
            roots,
        })
    }

    fn find_package(&self, name: &str) -> Result<&LockedResource> {
        for resource_type in ResourceType::all() {
            for resource in self.lockfile.get_resources(resource_type) {
                if resource.name == name {
                    return Ok(resource);
                }
            }
        }

        Err(anyhow::anyhow!("Package '{name}' not found in lockfile"))
    }

    fn build_node(&self, resource: &LockedResource, _cmd: &TreeCommand) -> Result<TreeNode> {
        // Extract display name from unique lockfile name
        // Unique name format: "source:name@version" or "name@version"
        // We want just "name" for display
        let display_name = Self::extract_display_name(&resource.name);

        // Convert dependencies to use the node ID format
        // Pass parent resource's source to correctly resolve dependencies from the same source
        let dependency_node_ids: Vec<String> = resource
            .dependencies
            .iter()
            .filter_map(|dep_id| {
                // Find the resource for this dependency (prefer same source as parent)
                if let Some(dep_resource) =
                    self.find_resource_by_id(dep_id, resource.source.as_deref())
                {
                    // Build a temporary node to get its ID in the same format used by tree.nodes
                    let dep_node = TreeNode {
                        name: Self::extract_display_name(&dep_resource.name),
                        resource_type: dep_resource.resource_type,
                        version: dep_resource.version.clone(),
                        source: dep_resource.source.clone(),
                        tool: dep_resource.tool.clone(),
                        dependencies: vec![], // Don't need dependencies for ID generation
                        has_patches: !dep_resource.applied_patches.is_empty(),
                        installed_at: dep_resource.installed_at.clone(),
                        applied_patches: dep_resource.applied_patches.clone(),
                        approximate_token_count: dep_resource.approximate_token_count,
                    };
                    Some(self.node_id(&dep_node))
                } else {
                    None
                }
            })
            .collect();

        Ok(TreeNode {
            name: display_name,
            resource_type: resource.resource_type,
            version: resource.version.clone(),
            source: resource.source.clone(),
            tool: resource.tool.clone(),
            dependencies: dependency_node_ids,
            has_patches: !resource.applied_patches.is_empty(),
            installed_at: resource.installed_at.clone(),
            applied_patches: resource.applied_patches.clone(),
            approximate_token_count: resource.approximate_token_count,
        })
    }

    /// Extracts the display name from a unique lockfile identifier.
    ///
    /// Converts from:
    /// - "source:name@version" → "name" (e.g., "community:api-designer@main" → "api-designer")
    /// - "source:name" → "name" (e.g., "local-deps:rust-haiku" → "rust-haiku")
    /// - "name@version" → "name"
    /// - "name" → "name"
    pub fn extract_display_name(unique_name: &str) -> String {
        // Remove source prefix if present (e.g., "local-deps:name" → "name")
        let after_source = if let Some((_source, rest)) = unique_name.split_once(':') {
            rest
        } else {
            unique_name
        };

        // Remove version suffix if present (e.g., "name@version" → "name")
        if let Some((name, _version)) = after_source.split_once('@') {
            name.to_string()
        } else {
            after_source.to_string()
        }
    }

    fn build_dependencies(
        &self,
        node: &TreeNode,
        nodes: &mut HashMap<String, TreeNode>,
        cmd: &TreeCommand,
    ) -> Result<()> {
        // Dependencies are already in tree node ID format (type/name)
        for dep_node_id in &node.dependencies {
            if nodes.contains_key(dep_node_id) {
                continue; // Already processed
            }

            // Find the dependency in lockfile (prefer same source as parent)
            if let Some(dep_resource) =
                self.find_resource_by_id(dep_node_id, node.source.as_deref())
            {
                let dep_node = self.build_node(dep_resource, cmd)?;
                let actual_dep_node_id = self.node_id(&dep_node);

                nodes.insert(actual_dep_node_id.clone(), dep_node.clone());
                self.build_dependencies(&dep_node, nodes, cmd)?;
            }
        }

        Ok(())
    }

    fn find_resource_by_id(
        &self,
        id: &str,
        preferred_source: Option<&str>,
    ) -> Option<&LockedResource> {
        // Dependencies use LockfileDependencyRef format: "type:name" or "source/type:name@version"
        // Parse using centralized LockfileDependencyRef logic
        use std::str::FromStr;
        let dep_ref =
            crate::lockfile::lockfile_dependency_ref::LockfileDependencyRef::from_str(id).ok()?;
        let resource_type = dep_ref.resource_type;
        let name = &dep_ref.path;

        // Find the resource by matching the display name extracted from the unique name
        // Prefer resources from the same source as the parent (transitive deps should be same-source)
        let resources = self.lockfile.get_resources(&resource_type);

        // First try to find a match with the preferred source
        if let Some(source) = preferred_source {
            for resource in resources {
                let display_name = Self::extract_display_name(&resource.name);
                if display_name == *name && resource.source.as_deref() == Some(source) {
                    return Some(resource);
                }
            }
        }

        // Fall back to any match if no preferred source match found
        for resource in resources {
            let display_name = Self::extract_display_name(&resource.name);
            if display_name == *name {
                return Some(resource);
            }
        }

        None
    }

    fn node_id(&self, node: &TreeNode) -> String {
        // Generate unique ID matching the lockfile name format:
        // - "source:name@version" for remote sources (e.g., "community:api-designer@main")
        // - "source:name" for local sources (e.g., "local-deps:rust-haiku")
        // - "name" for resources without a source (e.g., "rust-haiku")

        match (&node.source, &node.version) {
            (Some(source), Some(version)) if version != "local" => {
                // Remote source with version: source:name@version
                format!("{}:{}@{}", source, node.name, version)
            }
            (Some(source), _) => {
                // Local source (version is "local" or None): source:name
                format!("{}:{}", source, node.name)
            }
            (None, Some(version)) if version != "local" => {
                // No source but has version: name@version
                format!("{}@{}", node.name, version)
            }
            (None, _) => {
                // No source and no version (or version is "local"): name
                node.name.clone()
            }
        }
    }

    fn find_duplicates(
        &self,
        roots: &[TreeNode],
        nodes: &HashMap<String, TreeNode>,
    ) -> HashSet<String> {
        let mut counts: HashMap<String, usize> = HashMap::new();
        let mut seen = HashSet::new();

        for root in roots {
            self.count_occurrences(root, &mut counts, &mut seen, nodes);
        }

        counts.iter().filter(|&(_, &count)| count > 1).map(|(id, _)| id.clone()).collect()
    }

    fn count_occurrences(
        &self,
        node: &TreeNode,
        counts: &mut HashMap<String, usize>,
        seen: &mut HashSet<String>,
        nodes: &HashMap<String, TreeNode>,
    ) {
        let node_id = self.node_id(node);
        *counts.entry(node_id.clone()).or_insert(0) += 1;

        if seen.contains(&node_id) {
            return; // Prevent infinite loops
        }
        seen.insert(node_id);

        for dep_id in &node.dependencies {
            if let Some(child) = nodes.get(dep_id) {
                self.count_occurrences(child, counts, seen, nodes);
            }
        }
    }
}

/// Format a toml::Value for display in patch output.
///
/// Produces clean, readable output:
/// - Strings: wrapped in quotes `"value"`
/// - Numbers/Booleans: plain text
/// - Arrays/Tables: formatted as TOML syntax
fn format_patch_value(value: &toml::Value) -> String {
    match value {
        toml::Value::String(s) => format!("\"{}\"", s),
        toml::Value::Integer(i) => i.to_string(),
        toml::Value::Float(f) => f.to_string(),
        toml::Value::Boolean(b) => b.to_string(),
        toml::Value::Array(arr) => {
            let elements: Vec<String> = arr.iter().map(format_patch_value).collect();
            format!("[{}]", elements.join(", "))
        }
        toml::Value::Table(_) | toml::Value::Datetime(_) => {
            // For complex types, use to_string() as fallback
            value.to_string()
        }
    }
}

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

    fn create_default_command() -> TreeCommand {
        TreeCommand {
            depth: None,
            format: "tree".to_string(),
            duplicates: false,
            no_dedupe: false,
            package: None,
            agents: false,
            snippets: false,
            commands: false,
            scripts: false,
            hooks: false,
            mcp_servers: false,
            invert: false,
            detailed: false,
        }
    }

    #[test]
    fn test_validate_arguments_valid_format() -> Result<()> {
        let valid_formats = ["tree", "json", "text"];

        for format in valid_formats {
            let cmd = TreeCommand {
                format: format.to_string(),
                ..create_default_command()
            };
            cmd.validate_arguments()?;
        }
        Ok(())
    }

    #[test]
    fn test_validate_arguments_invalid_format() {
        let cmd = TreeCommand {
            format: "invalid".to_string(),
            ..create_default_command()
        };

        let result = cmd.validate_arguments();
        assert!(result.is_err());
        assert!(result.unwrap_err().to_string().contains("Invalid format"));
    }

    #[test]
    fn test_validate_arguments_zero_depth() {
        let cmd = TreeCommand {
            depth: Some(0),
            ..create_default_command()
        };

        let result = cmd.validate_arguments();
        assert!(result.is_err());
        assert!(result.unwrap_err().to_string().contains("must be at least 1"));
    }

    #[test]
    fn test_should_show_resource_type_no_filters() {
        let cmd = create_default_command();

        // With no filters, all types should be shown
        assert!(cmd.should_show_resource_type(ResourceType::Agent));
        assert!(cmd.should_show_resource_type(ResourceType::Snippet));
        assert!(cmd.should_show_resource_type(ResourceType::Command));
    }

    #[test]
    fn test_should_show_resource_type_with_filters() {
        let cmd = TreeCommand {
            agents: true,
            ..create_default_command()
        };

        assert!(cmd.should_show_resource_type(ResourceType::Agent));
        assert!(!cmd.should_show_resource_type(ResourceType::Snippet));
        assert!(!cmd.should_show_resource_type(ResourceType::Command));
    }

    #[test]
    fn test_node_id() {
        let lockfile = LockFile::new();
        let builder = TreeBuilder::new(&lockfile, "test-project".to_string());

        // Test with source and version
        let node = TreeNode {
            name: "test-agent".to_string(),
            resource_type: ResourceType::Agent,
            version: Some("v1.0.0".to_string()),
            source: Some("community".to_string()),
            tool: Some("claude-code".to_string()),
            dependencies: vec![],
            has_patches: false,
            installed_at: ".claude/agents/test-agent.md".to_string(),
            applied_patches: BTreeMap::new(),
            approximate_token_count: None,
        };
        assert_eq!(builder.node_id(&node), "community:test-agent@v1.0.0");

        // Test with source and local version (should omit @local)
        let node_local_source = TreeNode {
            name: "local-agent".to_string(),
            resource_type: ResourceType::Agent,
            version: Some("local".to_string()),
            source: Some("local-deps".to_string()),
            tool: Some("claude-code".to_string()),
            dependencies: vec![],
            has_patches: false,
            installed_at: ".claude/agents/local-agent.md".to_string(),
            applied_patches: BTreeMap::new(),
            approximate_token_count: None,
        };
        assert_eq!(builder.node_id(&node_local_source), "local-deps:local-agent");

        // Test without source but with local version (should omit @local)
        let node_local = TreeNode {
            name: "local-agent".to_string(),
            resource_type: ResourceType::Agent,
            version: Some("local".to_string()),
            source: None,
            tool: Some("claude-code".to_string()),
            dependencies: vec![],
            has_patches: false,
            installed_at: ".claude/agents/local-agent.md".to_string(),
            applied_patches: BTreeMap::new(),
            approximate_token_count: None,
        };
        assert_eq!(builder.node_id(&node_local), "local-agent");

        // Test without version but with source
        let node_no_version = TreeNode {
            name: "test-agent".to_string(),
            resource_type: ResourceType::Agent,
            version: None,
            source: Some("community".to_string()),
            tool: Some("claude-code".to_string()),
            dependencies: vec![],
            has_patches: false,
            installed_at: ".claude/agents/test-agent.md".to_string(),
            applied_patches: BTreeMap::new(),
            approximate_token_count: None,
        };
        assert_eq!(builder.node_id(&node_no_version), "community:test-agent");
    }
}