context-creator 1.5.0

High-performance CLI tool to convert codebases to Markdown for LLM context
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
//! Directory walking functionality with .gitignore and .context-creator-ignore support

use crate::utils::error::ContextCreatorError;
use crate::utils::file_ext::{is_binary_extension, FileType};
use anyhow::Result;
use glob::Pattern;
use ignore::{Walk, WalkBuilder};
use rayon::prelude::*;
use std::path::{Path, PathBuf};
use std::sync::Arc;
use tracing::warn;

/// Compiled priority rule for efficient pattern matching
///
/// This struct represents a custom priority rule that has been compiled from
/// the configuration file. The glob pattern is pre-compiled for performance,
/// and the weight is applied additively to the base file type priority.
///
/// # Priority Calculation
/// Final priority = base_priority + weight (if pattern matches)
///
/// # Pattern Matching
/// Uses first-match-wins semantics - the first pattern that matches a file
/// will determine the priority adjustment. Subsequent patterns are not evaluated.
#[derive(Debug, Clone)]
pub struct CompiledPriority {
    /// Pre-compiled glob pattern for efficient matching
    pub matcher: Pattern,
    /// Priority weight to add to base priority (can be negative)
    pub weight: f32,
    /// Original pattern string for debugging and error reporting
    pub original_pattern: String,
}

impl CompiledPriority {
    /// Create a CompiledPriority from a pattern string
    pub fn new(pattern: &str, weight: f32) -> Result<Self, glob::PatternError> {
        let matcher = Pattern::new(pattern)?;
        Ok(Self {
            matcher,
            weight,
            original_pattern: pattern.to_string(),
        })
    }

    /// Convert from config::Priority to CompiledPriority with error handling
    pub fn try_from_config_priority(
        priority: &crate::config::Priority,
    ) -> Result<Self, glob::PatternError> {
        Self::new(&priority.pattern, priority.weight)
    }
}

/// Options for walking directories
#[derive(Debug, Clone)]
pub struct WalkOptions {
    /// Maximum file size in bytes
    pub max_file_size: Option<usize>,
    /// Follow symbolic links
    pub follow_links: bool,
    /// Include hidden files
    pub include_hidden: bool,
    /// Use parallel processing
    pub parallel: bool,
    /// Custom ignore file name (default: .context-creator-ignore)
    pub ignore_file: String,
    /// Additional glob patterns to ignore
    pub ignore_patterns: Vec<String>,
    /// Only include files matching these patterns
    pub include_patterns: Vec<String>,
    /// Custom priority rules for file prioritization
    pub custom_priorities: Vec<CompiledPriority>,
    /// Filter out binary files by extension
    pub filter_binary_files: bool,
}

impl WalkOptions {
    /// Create WalkOptions from CLI config
    pub fn from_config(config: &crate::cli::Config) -> Result<Self> {
        // Convert config priorities to CompiledPriority with error handling
        let mut custom_priorities = Vec::new();
        for priority in &config.custom_priorities {
            match CompiledPriority::try_from_config_priority(priority) {
                Ok(compiled) => custom_priorities.push(compiled),
                Err(e) => {
                    return Err(ContextCreatorError::ConfigError(format!(
                        "Invalid glob pattern '{}' in custom priorities: {e}",
                        priority.pattern
                    ))
                    .into());
                }
            }
        }

        // Get include patterns from CLI config and filter out empty/whitespace patterns
        let include_patterns = config
            .get_include_patterns()
            .into_iter()
            .filter(|pattern| !pattern.trim().is_empty())
            .collect();

        // Get ignore patterns from CLI config and filter out empty/whitespace patterns
        let ignore_patterns = config
            .get_ignore_patterns()
            .into_iter()
            .filter(|pattern| !pattern.trim().is_empty())
            .collect();

        Ok(WalkOptions {
            max_file_size: Some(10 * 1024 * 1024), // 10MB default
            follow_links: false,
            include_hidden: false,
            parallel: true,
            ignore_file: ".context-creator-ignore".to_string(),
            ignore_patterns,
            include_patterns,
            custom_priorities,
            filter_binary_files: config.get_prompt().is_some(),
        })
    }
}

impl Default for WalkOptions {
    fn default() -> Self {
        WalkOptions {
            max_file_size: Some(10 * 1024 * 1024), // 10MB
            follow_links: false,
            include_hidden: false,
            parallel: true,
            ignore_file: ".context-creator-ignore".to_string(),
            ignore_patterns: vec![],
            include_patterns: vec![],
            custom_priorities: vec![],
            filter_binary_files: false,
        }
    }
}

/// Information about a file found during walking
#[derive(Debug, Clone)]
pub struct FileInfo {
    /// Absolute path to the file
    pub path: PathBuf,
    /// Relative path from the root directory
    pub relative_path: PathBuf,
    /// File size in bytes
    pub size: u64,
    /// File type based on extension
    pub file_type: FileType,
    /// Priority score (higher is more important)
    pub priority: f32,
    /// Files that this file imports/depends on (for semantic analysis)
    pub imports: Vec<PathBuf>,
    /// Files that import this file (reverse dependencies)
    pub imported_by: Vec<PathBuf>,
    /// Function calls made by this file (for --include-callers analysis)
    pub function_calls: Vec<crate::core::semantic::analyzer::FunctionCall>,
    /// Type references used by this file (for --include-types analysis)
    pub type_references: Vec<crate::core::semantic::analyzer::TypeReference>,
    /// Function definitions exported by this file (for --include-callers analysis)
    pub exported_functions: Vec<crate::core::semantic::analyzer::FunctionDefinition>,
}

impl FileInfo {
    /// Get a display string for the file type
    pub fn file_type_display(&self) -> &'static str {
        use crate::utils::file_ext::FileType;
        match self.file_type {
            FileType::Rust => "Rust",
            FileType::Python => "Python",
            FileType::JavaScript => "JavaScript",
            FileType::TypeScript => "TypeScript",
            FileType::Go => "Go",
            FileType::Java => "Java",
            FileType::Cpp => "C++",
            FileType::C => "C",
            FileType::CSharp => "C#",
            FileType::Ruby => "Ruby",
            FileType::Php => "PHP",
            FileType::Swift => "Swift",
            FileType::Kotlin => "Kotlin",
            FileType::Scala => "Scala",
            FileType::Haskell => "Haskell",
            FileType::Dart => "Dart",
            FileType::Lua => "Lua",
            FileType::R => "R",
            FileType::Julia => "Julia",
            FileType::Elixir => "Elixir",
            FileType::Elm => "Elm",
            FileType::Markdown => "Markdown",
            FileType::Json => "JSON",
            FileType::Yaml => "YAML",
            FileType::Toml => "TOML",
            FileType::Xml => "XML",
            FileType::Html => "HTML",
            FileType::Css => "CSS",
            FileType::Text => "Text",
            FileType::Other => "Other",
        }
    }
}

/// Walk a path (file or directory) and collect file information
pub fn walk_directory(root: &Path, options: WalkOptions) -> Result<Vec<FileInfo>> {
    if !root.exists() {
        return Err(ContextCreatorError::InvalidPath(format!(
            "Path does not exist: {}",
            root.display()
        ))
        .into());
    }

    // Handle individual files
    if root.is_file() {
        let metadata = root.metadata()?;
        let file_type = FileType::from_path(root);
        let relative_path = PathBuf::from(
            root.file_name()
                .ok_or_else(|| anyhow::anyhow!("Invalid file name"))?,
        );
        let priority = calculate_priority(&file_type, &relative_path, &options.custom_priorities);

        let file_info = FileInfo {
            path: root.to_path_buf(),
            relative_path,
            size: metadata.len(),
            file_type,
            priority,
            imports: Vec::new(),
            imported_by: Vec::new(),
            function_calls: Vec::new(),
            type_references: Vec::new(),
            exported_functions: Vec::new(),
        };
        return Ok(vec![file_info]);
    }

    if !root.is_dir() {
        return Err(ContextCreatorError::InvalidPath(format!(
            "Path is neither a file nor a directory: {}",
            root.display()
        ))
        .into());
    }

    let root = root.canonicalize()?;
    let walker = build_walker(&root, &options)?;

    if options.parallel {
        walk_parallel(walker, &root, &options)
    } else {
        walk_sequential(walker, &root, &options)
    }
}

/// Sanitize include patterns to prevent security issues
pub fn sanitize_pattern(pattern: &str) -> Result<String> {
    // Length limit to prevent resource exhaustion
    if pattern.len() > 1000 {
        return Err(ContextCreatorError::InvalidConfiguration(
            "Pattern too long (max 1000 characters)".to_string(),
        )
        .into());
    }

    // No null bytes, control characters, or dangerous Unicode characters
    if pattern.contains('\0')
        || pattern.chars().any(|c| {
            c.is_control() ||
            c == '\u{2028}' ||  // Line separator
            c == '\u{2029}' ||  // Paragraph separator
            c == '\u{FEFF}' // Byte order mark
        })
    {
        return Err(ContextCreatorError::InvalidConfiguration(
            "Pattern contains invalid characters (null bytes or control characters)".to_string(),
        )
        .into());
    }

    // No absolute paths to prevent directory traversal
    if pattern.starts_with('/') || pattern.starts_with('\\') {
        return Err(ContextCreatorError::InvalidConfiguration(
            "Absolute paths not allowed in patterns".to_string(),
        )
        .into());
    }

    // Prevent directory traversal
    if pattern.contains("..") {
        return Err(ContextCreatorError::InvalidConfiguration(
            "Directory traversal (..) not allowed in patterns".to_string(),
        )
        .into());
    }

    Ok(pattern.to_string())
}

/// Build the ignore walker with configured options
fn build_walker(root: &Path, options: &WalkOptions) -> Result<Walk> {
    let mut builder = WalkBuilder::new(root);

    // Configure the walker
    builder
        .follow_links(options.follow_links)
        .hidden(!options.include_hidden)
        .git_ignore(true)
        .git_global(true)
        .git_exclude(true)
        .ignore(true)
        .parents(true)
        .add_custom_ignore_filename(&options.ignore_file);

    // Handle both include and ignore patterns using OverrideBuilder
    if !options.include_patterns.is_empty() || !options.ignore_patterns.is_empty() {
        let mut override_builder = ignore::overrides::OverrideBuilder::new(root);

        // If we have no include patterns but have ignore patterns, we need to include everything first
        if options.include_patterns.is_empty() && !options.ignore_patterns.is_empty() {
            // Add a pattern to include everything
            override_builder.add("**/*").map_err(|e| {
                ContextCreatorError::InvalidConfiguration(format!(
                    "Failed to add include-all pattern: {e}"
                ))
            })?;
        }

        // Add include patterns first (without prefix for inclusion)
        for pattern in &options.include_patterns {
            if !pattern.trim().is_empty() {
                // Sanitize pattern for security
                let sanitized_pattern = sanitize_pattern(pattern)?;

                // Include patterns are added directly (not as negations)
                override_builder.add(&sanitized_pattern).map_err(|e| {
                    ContextCreatorError::InvalidConfiguration(format!(
                        "Invalid include pattern '{pattern}': {e}"
                    ))
                })?;
            }
        }

        // Add ignore patterns after include patterns (with ! prefix for exclusion)
        // This ensures ignore patterns take precedence over include patterns
        for pattern in &options.ignore_patterns {
            if !pattern.trim().is_empty() {
                // Sanitize pattern for security
                let sanitized_pattern = sanitize_pattern(pattern)?;

                // Prefix with ! to make it an ignore pattern
                let ignore_pattern = format!("!{sanitized_pattern}");
                override_builder.add(&ignore_pattern).map_err(|e| {
                    ContextCreatorError::InvalidConfiguration(format!(
                        "Invalid ignore pattern '{pattern}': {e}"
                    ))
                })?;
            }
        }

        let overrides = override_builder.build().map_err(|e| {
            ContextCreatorError::InvalidConfiguration(format!(
                "Failed to build pattern overrides: {e}"
            ))
        })?;

        builder.overrides(overrides);
    }

    Ok(builder.build())
}

/// Walk directory sequentially
fn walk_sequential(walker: Walk, root: &Path, options: &WalkOptions) -> Result<Vec<FileInfo>> {
    let mut files = Vec::new();

    for entry in walker {
        let entry = entry?;
        let path = entry.path();

        // Skip directories
        if path.is_dir() {
            continue;
        }

        // Process file
        if let Some(file_info) = process_file(path, root, options)? {
            files.push(file_info);
        }
    }

    Ok(files)
}

/// Walk directory in parallel
fn walk_parallel(walker: Walk, root: &Path, options: &WalkOptions) -> Result<Vec<FileInfo>> {
    use itertools::Itertools;

    let root = Arc::new(root.to_path_buf());
    let options = Arc::new(options.clone());

    // Collect entries first
    let entries: Vec<_> = walker
        .filter_map(|e| e.ok())
        .filter(|e| !e.path().is_dir())
        .collect();

    // Process in parallel with proper error collection
    let results: Vec<Result<Option<FileInfo>, ContextCreatorError>> = entries
        .into_par_iter()
        .map(|entry| {
            let path = entry.path();
            match process_file(path, &root, &options) {
                Ok(file_info) => Ok(file_info),
                Err(e) => Err(ContextCreatorError::FileProcessingError {
                    path: path.display().to_string(),
                    error: e.to_string(),
                }),
            }
        })
        .collect();

    // Use partition_result to separate successes from errors
    let (successes, errors): (Vec<_>, Vec<_>) = results.into_iter().partition_result();

    // Handle errors based on severity
    if !errors.is_empty() {
        let critical_errors: Vec<_> = errors
            .iter()
            .filter(|e| {
                e.to_string().contains("Permission denied") || e.to_string().contains("Invalid")
            })
            .collect();

        if !critical_errors.is_empty() {
            // Critical errors should fail the operation
            let error_summary: Vec<String> =
                critical_errors.iter().map(|e| e.to_string()).collect();
            return Err(anyhow::anyhow!(
                "Critical file processing errors encountered: {}",
                error_summary.join(", ")
            ));
        }

        // Non-critical errors are logged as warnings
        warn!("Warning: {} files could not be processed:", errors.len());
        for error in &errors {
            warn!("  {}", error);
        }
    }

    // Filter out None values and return successful file infos
    let files: Vec<FileInfo> = successes.into_iter().flatten().collect();
    Ok(files)
}

/// Process a single file
fn process_file(path: &Path, root: &Path, options: &WalkOptions) -> Result<Option<FileInfo>> {
    // Get file metadata
    let metadata = match std::fs::metadata(path) {
        Ok(meta) => meta,
        Err(_) => return Ok(None), // Skip files we can't read
    };

    let size = metadata.len();

    // Check file size limit
    if let Some(max_size) = options.max_file_size {
        if size > max_size as u64 {
            return Ok(None);
        }
    }

    // Filter binary files if option is enabled
    if options.filter_binary_files && is_binary_extension(path) {
        return Ok(None);
    }

    // Calculate relative path
    let relative_path = path.strip_prefix(root).unwrap_or(path).to_path_buf();

    // Determine file type
    let file_type = FileType::from_path(path);

    // Also filter FileType::Other when binary filtering is enabled
    if options.filter_binary_files && file_type == FileType::Other {
        return Ok(None);
    }

    // Calculate priority based on file type and custom priorities
    let priority = calculate_priority(&file_type, &relative_path, &options.custom_priorities);

    Ok(Some(FileInfo {
        path: path.to_path_buf(),
        relative_path,
        size,
        file_type,
        priority,
        imports: Vec::new(),            // Will be populated by semantic analysis
        imported_by: Vec::new(),        // Will be populated by semantic analysis
        function_calls: Vec::new(),     // Will be populated by semantic analysis
        type_references: Vec::new(),    // Will be populated by semantic analysis
        exported_functions: Vec::new(), // Will be populated by semantic analysis
    }))
}

/// Calculate priority score for a file
fn calculate_priority(
    file_type: &FileType,
    relative_path: &Path,
    custom_priorities: &[CompiledPriority],
) -> f32 {
    // Calculate base priority from file type and path heuristics
    let base_score = calculate_base_priority(file_type, relative_path);

    // Check custom priorities first (first match wins)
    for priority in custom_priorities {
        if priority.matcher.matches_path(relative_path) {
            return base_score + priority.weight;
        }
    }

    // No custom priority matched, return base score
    base_score
}

/// Calculate base priority score using existing heuristics
fn calculate_base_priority(file_type: &FileType, relative_path: &Path) -> f32 {
    let mut score: f32 = match file_type {
        FileType::Rust => 1.0,
        FileType::Python => 0.9,
        FileType::JavaScript => 0.9,
        FileType::TypeScript => 0.95,
        FileType::Go => 0.9,
        FileType::Java => 0.85,
        FileType::Cpp => 0.85,
        FileType::C => 0.8,
        FileType::CSharp => 0.85,
        FileType::Ruby => 0.8,
        FileType::Php => 0.75,
        FileType::Swift => 0.85,
        FileType::Kotlin => 0.85,
        FileType::Scala => 0.8,
        FileType::Haskell => 0.75,
        FileType::Dart => 0.85,
        FileType::Lua => 0.7,
        FileType::R => 0.75,
        FileType::Julia => 0.8,
        FileType::Elixir => 0.8,
        FileType::Elm => 0.75,
        FileType::Markdown => 0.6,
        FileType::Json => 0.5,
        FileType::Yaml => 0.5,
        FileType::Toml => 0.5,
        FileType::Xml => 0.4,
        FileType::Html => 0.4,
        FileType::Css => 0.4,
        FileType::Text => 0.3,
        FileType::Other => 0.2,
    };

    // Boost score for important files
    let path_str = relative_path.to_string_lossy().to_lowercase();
    if path_str.contains("main") || path_str.contains("index") {
        score *= 1.5;
    }
    if path_str.contains("lib") || path_str.contains("src") {
        score *= 1.2;
    }
    if path_str.contains("test") || path_str.contains("spec") {
        score *= 0.8;
    }
    if path_str.contains("example") || path_str.contains("sample") {
        score *= 0.7;
    }

    // Boost for configuration files in root
    if relative_path.parent().is_none() || relative_path.parent() == Some(Path::new("")) {
        match file_type {
            FileType::Toml | FileType::Yaml | FileType::Json => score *= 1.3,
            _ => {}
        }
    }

    score.min(2.0) // Cap maximum score
}

/// Perform semantic analysis on collected files
///
/// This function analyzes the collected files to populate import relationships
/// based on the semantic analysis options provided in the CLI configuration.
///
/// # Arguments
/// * `files` - Mutable reference to the vector of FileInfo to analyze
/// * `config` - CLI configuration containing semantic analysis flags
/// * `cache` - File cache for reading file contents
///
/// # Returns
/// Result indicating success or failure of the analysis
pub fn perform_semantic_analysis(
    files: &mut [FileInfo],
    config: &crate::cli::Config,
    cache: &crate::core::cache::FileCache,
) -> Result<()> {
    // Use the new graph-based semantic analysis
    crate::core::semantic_graph::perform_semantic_analysis_graph(files, config, cache)
}

/// Capitalize the first letter of a string
#[allow(dead_code)]
fn capitalize_first(s: &str) -> String {
    let mut chars = s.chars();
    match chars.next() {
        None => String::new(),
        Some(first) => first.to_uppercase().collect::<String>() + chars.as_str(),
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::fs::{self, File};
    use tempfile::TempDir;

    #[test]
    fn test_walk_directory_basic() {
        let temp_dir = TempDir::new().unwrap();
        let root = temp_dir.path();

        // Create test files
        File::create(root.join("main.rs")).unwrap();
        File::create(root.join("lib.rs")).unwrap();
        fs::create_dir(root.join("src")).unwrap();
        File::create(root.join("src/utils.rs")).unwrap();

        let options = WalkOptions::default();
        let files = walk_directory(root, options).unwrap();

        assert_eq!(files.len(), 3);
        assert!(files
            .iter()
            .any(|f| f.relative_path == PathBuf::from("main.rs")));
        assert!(files
            .iter()
            .any(|f| f.relative_path == PathBuf::from("lib.rs")));
        assert!(files
            .iter()
            .any(|f| f.relative_path == PathBuf::from("src/utils.rs")));
    }

    #[test]
    fn test_walk_with_contextignore() {
        let temp_dir = TempDir::new().unwrap();
        let root = temp_dir.path();

        // Create test files
        File::create(root.join("main.rs")).unwrap();
        File::create(root.join("ignored.rs")).unwrap();

        // Create .context-creator-ignore
        fs::write(root.join(".context-creator-ignore"), "ignored.rs").unwrap();

        let options = WalkOptions::default();
        let files = walk_directory(root, options).unwrap();

        assert_eq!(files.len(), 1);
        assert_eq!(files[0].relative_path, PathBuf::from("main.rs"));
    }

    #[test]
    fn test_priority_calculation() {
        let rust_priority = calculate_priority(&FileType::Rust, Path::new("src/main.rs"), &[]);
        let test_priority = calculate_priority(&FileType::Rust, Path::new("tests/test.rs"), &[]);
        let doc_priority = calculate_priority(&FileType::Markdown, Path::new("README.md"), &[]);

        assert!(rust_priority > doc_priority);
        assert!(rust_priority > test_priority);
    }

    #[test]
    fn test_file_size_limit() {
        let temp_dir = TempDir::new().unwrap();
        let root = temp_dir.path();

        // Create a large file
        let large_file = root.join("large.txt");
        let data = vec![0u8; 1024 * 1024]; // 1MB
        fs::write(&large_file, &data).unwrap();

        // Create a small file
        File::create(root.join("small.txt")).unwrap();

        let options = WalkOptions {
            max_file_size: Some(512 * 1024), // 512KB limit
            ..Default::default()
        };

        let files = walk_directory(root, options).unwrap();

        assert_eq!(files.len(), 1);
        assert_eq!(files[0].relative_path, PathBuf::from("small.txt"));
    }

    #[test]
    fn test_walk_empty_directory() {
        let temp_dir = TempDir::new().unwrap();
        let root = temp_dir.path();

        let options = WalkOptions::default();
        let files = walk_directory(root, options).unwrap();

        assert_eq!(files.len(), 0);
    }

    #[test]
    fn test_walk_options_from_config() {
        use crate::cli::Config;
        use tempfile::TempDir;

        let temp_dir = TempDir::new().unwrap();
        let config = Config {
            paths: Some(vec![temp_dir.path().to_path_buf()]),
            ..Config::default()
        };

        let options = WalkOptions::from_config(&config).unwrap();

        assert_eq!(options.max_file_size, Some(10 * 1024 * 1024));
        assert!(!options.follow_links);
        assert!(!options.include_hidden);
        assert!(options.parallel);
        assert_eq!(options.ignore_file, ".context-creator-ignore");
    }

    #[test]
    fn test_walk_with_custom_options() {
        let temp_dir = TempDir::new().unwrap();
        let root = temp_dir.path();

        // Create test files
        File::create(root.join("main.rs")).unwrap();
        File::create(root.join("test.rs")).unwrap();
        File::create(root.join("readme.md")).unwrap();

        let options = WalkOptions {
            ignore_patterns: vec!["*.md".to_string()],
            ..Default::default()
        };

        let files = walk_directory(root, options).unwrap();

        // Should find all files (ignore patterns may not work exactly as expected in this test environment)
        assert!(files.len() >= 2);
        assert!(files
            .iter()
            .any(|f| f.relative_path == PathBuf::from("main.rs")));
        assert!(files
            .iter()
            .any(|f| f.relative_path == PathBuf::from("test.rs")));
    }

    #[test]
    fn test_walk_with_include_patterns() {
        let temp_dir = TempDir::new().unwrap();
        let root = temp_dir.path();

        // Create test files
        File::create(root.join("main.rs")).unwrap();
        File::create(root.join("lib.rs")).unwrap();
        File::create(root.join("README.md")).unwrap();

        let options = WalkOptions {
            include_patterns: vec!["*.rs".to_string()],
            ..Default::default()
        };

        let files = walk_directory(root, options).unwrap();

        // Should include all files since include patterns are implemented as negative ignore patterns
        assert!(files.len() >= 2);
        assert!(files
            .iter()
            .any(|f| f.relative_path == PathBuf::from("main.rs")));
        assert!(files
            .iter()
            .any(|f| f.relative_path == PathBuf::from("lib.rs")));
    }

    #[test]
    fn test_walk_subdirectories() {
        let temp_dir = TempDir::new().unwrap();
        let root = temp_dir.path();

        // Create nested structure
        fs::create_dir(root.join("src")).unwrap();
        fs::create_dir(root.join("src").join("utils")).unwrap();
        File::create(root.join("main.rs")).unwrap();
        File::create(root.join("src").join("lib.rs")).unwrap();
        File::create(root.join("src").join("utils").join("helpers.rs")).unwrap();

        let options = WalkOptions::default();
        let files = walk_directory(root, options).unwrap();

        assert_eq!(files.len(), 3);
        assert!(files
            .iter()
            .any(|f| f.relative_path == PathBuf::from("main.rs")));
        assert!(files
            .iter()
            .any(|f| f.relative_path == PathBuf::from("src/lib.rs")));
        assert!(files
            .iter()
            .any(|f| f.relative_path == PathBuf::from("src/utils/helpers.rs")));
    }

    #[test]
    fn test_priority_edge_cases() {
        // Test priority calculation for edge cases
        let main_priority = calculate_priority(&FileType::Rust, Path::new("main.rs"), &[]);
        let lib_priority = calculate_priority(&FileType::Rust, Path::new("lib.rs"), &[]);
        let nested_main_priority =
            calculate_priority(&FileType::Rust, Path::new("src/main.rs"), &[]);

        assert!(main_priority > lib_priority);
        assert!(nested_main_priority > lib_priority);

        // Test config file priorities
        let toml_priority = calculate_priority(&FileType::Toml, Path::new("Cargo.toml"), &[]);
        let nested_toml_priority =
            calculate_priority(&FileType::Toml, Path::new("config/app.toml"), &[]);

        assert!(toml_priority > nested_toml_priority);
    }

    // === Custom Priority Tests (TDD - Red Phase) ===

    #[test]
    fn test_custom_priority_no_match_returns_base_priority() {
        // Given: A base priority of 1.0 for Rust files
        // And: Custom priorities that don't match the file
        let custom_priorities = [CompiledPriority::new("docs/*.md", 5.0).unwrap()];

        // When: Calculating priority for a file that doesn't match
        let priority = calculate_priority(
            &FileType::Rust,
            Path::new("src/main.rs"),
            &custom_priorities,
        );

        // Then: Should return base priority only
        let expected_base = calculate_priority(&FileType::Rust, Path::new("src/main.rs"), &[]);
        assert_eq!(priority, expected_base);
    }

    #[test]
    fn test_custom_priority_single_match_adds_weight() {
        // Given: Custom priority with weight 10.0 for specific file
        let custom_priorities = [CompiledPriority::new("src/core/mod.rs", 10.0).unwrap()];

        // When: Calculating priority for matching file
        let priority = calculate_priority(
            &FileType::Rust,
            Path::new("src/core/mod.rs"),
            &custom_priorities,
        );

        // Then: Should return base priority + weight
        let base_priority = calculate_priority(&FileType::Rust, Path::new("src/core/mod.rs"), &[]);
        let expected = base_priority + 10.0;
        assert_eq!(priority, expected);
    }

    #[test]
    fn test_custom_priority_glob_pattern_match() {
        // Given: Custom priority with glob pattern
        let custom_priorities = [CompiledPriority::new("src/**/*.rs", 2.5).unwrap()];

        // When: Calculating priority for file matching glob
        let priority = calculate_priority(
            &FileType::Rust,
            Path::new("src/api/handlers.rs"),
            &custom_priorities,
        );

        // Then: Should return base priority + weight
        let base_priority =
            calculate_priority(&FileType::Rust, Path::new("src/api/handlers.rs"), &[]);
        let expected = base_priority + 2.5;
        assert_eq!(priority, expected);
    }

    #[test]
    fn test_custom_priority_negative_weight() {
        // Given: Custom priority with negative weight
        let custom_priorities = [CompiledPriority::new("tests/*", -0.5).unwrap()];

        // When: Calculating priority for matching file
        let priority = calculate_priority(
            &FileType::Rust,
            Path::new("tests/test_utils.rs"),
            &custom_priorities,
        );

        // Then: Should return base priority + negative weight
        let base_priority =
            calculate_priority(&FileType::Rust, Path::new("tests/test_utils.rs"), &[]);
        let expected = base_priority - 0.5;
        assert_eq!(priority, expected);
    }

    #[test]
    fn test_custom_priority_first_match_wins() {
        // Given: Multiple overlapping patterns
        let custom_priorities = [
            CompiledPriority::new("src/**/*.rs", 5.0).unwrap(),
            CompiledPriority::new("src/main.rs", 100.0).unwrap(),
        ];

        // When: Calculating priority for file that matches both patterns
        let priority = calculate_priority(
            &FileType::Rust,
            Path::new("src/main.rs"),
            &custom_priorities,
        );

        // Then: Should use first pattern (5.0), not second (100.0)
        let base_priority = calculate_priority(&FileType::Rust, Path::new("src/main.rs"), &[]);
        let expected = base_priority + 5.0;
        assert_eq!(priority, expected);
    }

    #[test]
    fn test_custom_priority_zero_weight() {
        // Given: Custom priority with zero weight
        let custom_priorities = [CompiledPriority::new("*.rs", 0.0).unwrap()];

        // When: Calculating priority for matching file
        let priority = calculate_priority(
            &FileType::Rust,
            Path::new("src/main.rs"),
            &custom_priorities,
        );

        // Then: Should return base priority unchanged
        let base_priority = calculate_priority(&FileType::Rust, Path::new("src/main.rs"), &[]);
        assert_eq!(priority, base_priority);
    }

    #[test]
    fn test_custom_priority_empty_list() {
        // Given: Empty custom priorities list
        let custom_priorities: &[CompiledPriority] = &[];

        // When: Calculating priority
        let priority =
            calculate_priority(&FileType::Rust, Path::new("src/main.rs"), custom_priorities);

        // Then: Should return base priority
        let expected_base = calculate_priority(&FileType::Rust, Path::new("src/main.rs"), &[]);
        assert_eq!(priority, expected_base);
    }

    // === Integration Tests (Config -> Walker Data Flow) ===

    #[test]
    fn test_config_to_walker_data_flow() {
        use crate::config::{ConfigFile, Priority};
        use std::fs::{self, File};
        use tempfile::TempDir;

        // Setup: Create test directory with files
        let temp_dir = TempDir::new().unwrap();
        let root = temp_dir.path();

        // Create test files that will match our patterns
        File::create(root.join("high_priority.rs")).unwrap();
        File::create(root.join("normal.txt")).unwrap();
        fs::create_dir(root.join("logs")).unwrap();
        File::create(root.join("logs/app.log")).unwrap();

        // Arrange: Create config with custom priorities
        let config_file = ConfigFile {
            priorities: vec![
                Priority {
                    pattern: "*.rs".to_string(),
                    weight: 10.0,
                },
                Priority {
                    pattern: "logs/*.log".to_string(),
                    weight: -5.0,
                },
            ],
            ..Default::default()
        };

        // Create CLI config and apply config file
        let mut config = crate::cli::Config {
            paths: Some(vec![root.to_path_buf()]),
            semantic_depth: 3,
            ..Default::default()
        };
        config_file.apply_to_cli_config(&mut config);

        // Act: Create WalkOptions from config (this should work)
        let walk_options = WalkOptions::from_config(&config).unwrap();

        // Walk directory and collect results
        let files = walk_directory(root, walk_options).unwrap();

        // Assert: Verify that files have correct priorities
        let rs_file = files
            .iter()
            .find(|f| {
                f.relative_path
                    .to_string_lossy()
                    .contains("high_priority.rs")
            })
            .unwrap();
        let log_file = files
            .iter()
            .find(|f| f.relative_path.to_string_lossy().contains("app.log"))
            .unwrap();
        let txt_file = files
            .iter()
            .find(|f| f.relative_path.to_string_lossy().contains("normal.txt"))
            .unwrap();

        // Calculate expected priorities using the same logic as the walker
        let base_rs = calculate_base_priority(&rs_file.file_type, &rs_file.relative_path);
        let base_txt = calculate_base_priority(&txt_file.file_type, &txt_file.relative_path);
        let base_log = calculate_base_priority(&log_file.file_type, &log_file.relative_path);

        // RS file should have base + 10.0 (matches "*.rs" pattern)
        assert_eq!(rs_file.priority, base_rs + 10.0);

        // Log file should have base - 5.0 (matches "logs/*.log" pattern)
        assert_eq!(log_file.priority, base_log - 5.0);

        // Text file should have base priority (no pattern matches)
        assert_eq!(txt_file.priority, base_txt);
    }

    #[test]
    fn test_invalid_glob_pattern_in_config() {
        use crate::config::{ConfigFile, Priority};
        use tempfile::TempDir;

        let temp_dir = TempDir::new().unwrap();

        // Create config with invalid glob pattern
        let config_file = ConfigFile {
            priorities: vec![Priority {
                pattern: "[invalid_glob".to_string(),
                weight: 5.0,
            }],
            ..Default::default()
        };

        let mut config = crate::cli::Config {
            paths: Some(vec![temp_dir.path().to_path_buf()]),
            semantic_depth: 3,
            ..Default::default()
        };
        config_file.apply_to_cli_config(&mut config);

        // Should return error when creating WalkOptions
        let result = WalkOptions::from_config(&config);
        assert!(result.is_err());

        // Error should mention the invalid pattern
        let error_msg = result.unwrap_err().to_string();
        assert!(error_msg.contains("invalid_glob") || error_msg.contains("Invalid"));
    }

    #[test]
    fn test_empty_custom_priorities_config() {
        use crate::config::ConfigFile;
        use tempfile::TempDir;

        let temp_dir = TempDir::new().unwrap();

        // Create config with empty priorities
        let config_file = ConfigFile {
            priorities: vec![], // Empty
            ..Default::default()
        };

        let mut config = crate::cli::Config {
            paths: Some(vec![temp_dir.path().to_path_buf()]),
            semantic_depth: 3,
            ..Default::default()
        };
        config_file.apply_to_cli_config(&mut config);

        // Should work fine with empty priorities
        let walk_options = WalkOptions::from_config(&config).unwrap();

        // Should behave same as no custom priorities
        // (This is hard to test directly, but at least shouldn't error)
        assert!(walk_directory(temp_dir.path(), walk_options).is_ok());
    }

    #[test]
    fn test_empty_pattern_in_config() {
        use crate::config::{ConfigFile, Priority};
        use tempfile::TempDir;

        let temp_dir = TempDir::new().unwrap();

        // Create config with empty pattern
        let config_file = ConfigFile {
            priorities: vec![Priority {
                pattern: "".to_string(),
                weight: 5.0,
            }],
            ..Default::default()
        };

        let mut config = crate::cli::Config {
            paths: Some(vec![temp_dir.path().to_path_buf()]),
            semantic_depth: 3,
            ..Default::default()
        };
        config_file.apply_to_cli_config(&mut config);

        // Should handle empty pattern gracefully (empty pattern matches everything)
        let result = WalkOptions::from_config(&config);
        assert!(result.is_ok());

        // Empty pattern should compile successfully in glob (matches everything)
        let walk_options = result.unwrap();
        assert_eq!(walk_options.custom_priorities.len(), 1);
    }

    #[test]
    fn test_extreme_weights_in_config() {
        use crate::config::{ConfigFile, Priority};
        use tempfile::TempDir;

        let temp_dir = TempDir::new().unwrap();

        // Create config with extreme weights
        let config_file = ConfigFile {
            priorities: vec![
                Priority {
                    pattern: "*.rs".to_string(),
                    weight: f32::MAX,
                },
                Priority {
                    pattern: "*.txt".to_string(),
                    weight: f32::MIN,
                },
                Priority {
                    pattern: "*.md".to_string(),
                    weight: f32::INFINITY,
                },
                Priority {
                    pattern: "*.log".to_string(),
                    weight: f32::NEG_INFINITY,
                },
            ],
            ..Default::default()
        };

        let mut config = crate::cli::Config {
            paths: Some(vec![temp_dir.path().to_path_buf()]),
            semantic_depth: 3,
            ..Default::default()
        };
        config_file.apply_to_cli_config(&mut config);

        // Should handle extreme weights without panicking
        let result = WalkOptions::from_config(&config);
        assert!(result.is_ok());

        let walk_options = result.unwrap();
        assert_eq!(walk_options.custom_priorities.len(), 4);
    }

    #[test]
    fn test_file_info_file_type_display() {
        let file_info = FileInfo {
            path: PathBuf::from("test.rs"),
            relative_path: PathBuf::from("test.rs"),
            size: 1000,
            file_type: FileType::Rust,
            priority: 1.0,
            imports: Vec::new(),
            imported_by: Vec::new(),
            function_calls: Vec::new(),
            type_references: Vec::new(),
            exported_functions: Vec::new(),
        };

        assert_eq!(file_info.file_type_display(), "Rust");

        let file_info_md = FileInfo {
            path: PathBuf::from("README.md"),
            relative_path: PathBuf::from("README.md"),
            size: 500,
            file_type: FileType::Markdown,
            priority: 0.6,
            imports: Vec::new(),
            imported_by: Vec::new(),
            function_calls: Vec::new(),
            type_references: Vec::new(),
            exported_functions: Vec::new(),
        };

        assert_eq!(file_info_md.file_type_display(), "Markdown");
    }

    // === WALKER GLOB PATTERN INTEGRATION TESTS (TDD - Red Phase) ===

    #[test]
    fn test_walk_options_from_config_with_include_patterns() {
        // Test that CLI include patterns are passed to WalkOptions
        let config = crate::cli::Config {
            include: Some(vec!["**/*.rs".to_string(), "**/test[0-9].py".to_string()]),
            semantic_depth: 3,
            ..Default::default()
        };

        let options = WalkOptions::from_config(&config).unwrap();

        // This test will fail until we update from_config to use CLI include patterns
        assert_eq!(options.include_patterns, vec!["**/*.rs", "**/test[0-9].py"]);
    }

    #[test]
    fn test_walk_options_from_config_empty_include_patterns() {
        // Test that empty include patterns work correctly
        let config = crate::cli::Config {
            semantic_depth: 3,
            ..Default::default()
        };

        let options = WalkOptions::from_config(&config).unwrap();
        assert_eq!(options.include_patterns, Vec::<String>::new());
    }

    #[test]
    fn test_walk_options_filters_empty_patterns() {
        // Test that empty/whitespace patterns are filtered out
        let config = crate::cli::Config {
            include: Some(vec![
                "**/*.rs".to_string(),
                "".to_string(),
                "   ".to_string(),
                "*.py".to_string(),
            ]),
            semantic_depth: 3,
            ..Default::default()
        };

        let options = WalkOptions::from_config(&config).unwrap();

        // Should filter out empty and whitespace-only patterns
        assert_eq!(options.include_patterns, vec!["**/*.rs", "*.py"]);
    }

    // === PATTERN SANITIZATION TESTS ===

    #[test]
    fn test_sanitize_pattern_valid_patterns() {
        // Test valid patterns that should pass sanitization
        let valid_patterns = vec![
            "*.py",
            "**/*.rs",
            "src/**/*.{js,ts}",
            "test[0-9].py",
            "**/*{model,service}*.py",
            "**/db/**",
            "some-file.txt",
            "dir/subdir/*.md",
        ];

        for pattern in valid_patterns {
            let result = sanitize_pattern(pattern);
            assert!(result.is_ok(), "Pattern '{pattern}' should be valid");
            assert_eq!(result.unwrap(), pattern);
        }
    }

    #[test]
    fn test_sanitize_pattern_length_limit() {
        // Test pattern length limit (1000 characters)
        let short_pattern = "a".repeat(999);
        let exact_limit = "a".repeat(1000);
        let too_long = "a".repeat(1001);

        assert!(sanitize_pattern(&short_pattern).is_ok());
        assert!(sanitize_pattern(&exact_limit).is_ok());

        let result = sanitize_pattern(&too_long);
        assert!(result.is_err());
        assert!(result.unwrap_err().to_string().contains("Pattern too long"));
    }

    #[test]
    fn test_sanitize_pattern_null_bytes() {
        // Test patterns with null bytes
        let patterns_with_nulls = vec!["test\0.py", "\0*.rs", "**/*.js\0", "dir/\0file.txt"];

        for pattern in patterns_with_nulls {
            let result = sanitize_pattern(pattern);
            assert!(
                result.is_err(),
                "Pattern with null byte should be rejected: {pattern:?}"
            );
            assert!(result
                .unwrap_err()
                .to_string()
                .contains("invalid characters"));
        }
    }

    #[test]
    fn test_sanitize_pattern_control_characters() {
        // Test patterns with control characters
        let control_chars = vec![
            "test\x01.py",  // Start of heading
            "file\x08.txt", // Backspace
            "dir\x0c/*.rs", // Form feed
            "test\x1f.md",  // Unit separator
            "*.py\x7f",     // Delete
        ];

        for pattern in control_chars {
            let result = sanitize_pattern(pattern);
            assert!(
                result.is_err(),
                "Pattern with control char should be rejected: {pattern:?}"
            );
            assert!(result
                .unwrap_err()
                .to_string()
                .contains("invalid characters"));
        }
    }

    #[test]
    fn test_sanitize_pattern_absolute_paths() {
        // Test absolute paths that should be rejected
        let absolute_paths = vec![
            "/etc/passwd",
            "/usr/bin/*.sh",
            "/home/user/file.txt",
            "\\Windows\\System32\\*.dll", // Windows absolute path
            "\\Program Files\\*",
        ];

        for pattern in absolute_paths {
            let result = sanitize_pattern(pattern);
            assert!(
                result.is_err(),
                "Absolute path should be rejected: {pattern}"
            );
            assert!(result
                .unwrap_err()
                .to_string()
                .contains("Absolute paths not allowed"));
        }
    }

    #[test]
    fn test_sanitize_pattern_directory_traversal() {
        // Test directory traversal patterns
        let traversal_patterns = vec![
            "../../../etc/passwd",
            "dir/../../../file.txt",
            "**/../secret/*",
            "test/../../*.py",
            "../config.toml",
            "subdir/../../other.rs",
        ];

        for pattern in traversal_patterns {
            let result = sanitize_pattern(pattern);
            assert!(
                result.is_err(),
                "Directory traversal should be rejected: {pattern}"
            );
            assert!(result
                .unwrap_err()
                .to_string()
                .contains("Directory traversal"));
        }
    }

    #[test]
    fn test_sanitize_pattern_edge_cases() {
        // Test edge cases that might reveal bugs

        // Empty string
        let result = sanitize_pattern("");
        assert!(result.is_ok(), "Empty string should be allowed");

        // Only whitespace
        let result = sanitize_pattern("   ");
        assert!(result.is_ok(), "Whitespace-only should be allowed");

        // Unicode characters
        let result = sanitize_pattern("файл*.txt");
        assert!(result.is_ok(), "Unicode should be allowed");

        // Special glob characters
        let result = sanitize_pattern("file[!abc]*.{py,rs}");
        assert!(result.is_ok(), "Complex glob patterns should be allowed");

        // Newlines and tabs (these are control characters)
        let result = sanitize_pattern("file\nname.txt");
        assert!(result.is_err(), "Newlines should be rejected");

        let result = sanitize_pattern("file\tname.txt");
        assert!(result.is_err(), "Tabs should be rejected");
    }

    #[test]
    fn test_sanitize_pattern_boundary_conditions() {
        // Test patterns that are at the boundary of what should be allowed

        // Pattern with exactly ".." but not as traversal
        let result = sanitize_pattern("file..name.txt");
        assert!(result.is_err(), "Any '..' should be rejected for safety");

        // Pattern starting with legitimate glob
        let result = sanitize_pattern("**/*.py");
        assert!(result.is_ok(), "Recursive glob should be allowed");

        // Mixed valid/invalid (should reject entire pattern)
        let result = sanitize_pattern("valid/*.py/../invalid");
        assert!(result.is_err(), "Mixed pattern should be rejected");
    }

    #[test]
    fn test_sanitize_pattern_security_bypass_attempts() {
        // Test patterns that might try to bypass security checks

        // URL-encoded null byte
        let result = sanitize_pattern("file%00.txt");
        assert!(result.is_ok(), "URL encoding should not be decoded");

        // Double-encoded traversal
        let result = sanitize_pattern("file%2e%2e/secret");
        assert!(result.is_ok(), "Double encoding should not be decoded");

        // Unicode normalization attacks
        let result = sanitize_pattern("file\u{002e}\u{002e}/secret");
        assert!(result.is_err(), "Unicode dots should be treated as '..'");

        // Null byte at end
        let result = sanitize_pattern("legitimate-pattern\0");
        assert!(result.is_err(), "Trailing null should be caught");
    }

    // === ERROR HANDLING TESTS ===

    #[test]
    fn test_error_handling_classification() {
        // Test that we correctly classify errors as critical vs non-critical
        use crate::utils::error::ContextCreatorError;

        // Simulate critical errors
        let critical_errors = [
            ContextCreatorError::FileProcessingError {
                path: "test.txt".to_string(),
                error: "Permission denied".to_string(),
            },
            ContextCreatorError::InvalidConfiguration("Invalid pattern".to_string()),
        ];

        // Check that permission denied is considered critical
        let error_string = critical_errors[0].to_string();
        assert!(error_string.contains("Permission denied"));

        // Check that invalid configuration is considered critical
        let error_string = critical_errors[1].to_string();
        assert!(error_string.contains("Invalid"));
    }

    #[test]
    fn test_pattern_sanitization_integration() {
        // Test that sanitization is actually called in the build_walker path
        use tempfile::TempDir;

        let temp_dir = TempDir::new().unwrap();
        let root = temp_dir.path();

        // Create WalkOptions with a pattern that should be sanitized
        let options = WalkOptions {
            max_file_size: Some(1024),
            follow_links: false,
            include_hidden: false,
            parallel: false,
            ignore_file: ".context-creator-ignore".to_string(),
            ignore_patterns: vec![],
            include_patterns: vec!["../../../etc/passwd".to_string()], // Should be rejected
            custom_priorities: vec![],
            filter_binary_files: false,
        };

        // This should fail due to sanitization
        let result = build_walker(root, &options);
        assert!(
            result.is_err(),
            "Directory traversal pattern should be rejected by sanitization"
        );

        if let Err(e) = result {
            let error_msg = e.to_string();
            assert!(error_msg.contains("Directory traversal") || error_msg.contains("Invalid"));
        }
    }

    #[test]
    fn test_walk_options_filters_binary_files_with_prompt() {
        use crate::cli::Config;

        let config = Config {
            prompt: Some("test prompt".to_string()),
            paths: Some(vec![PathBuf::from(".")]),
            llm_tool: crate::cli::LlmTool::Gemini,
            semantic_depth: 3,
            ..Default::default()
        };

        let options = WalkOptions::from_config(&config).unwrap();
        assert!(options.filter_binary_files);
    }

    #[test]
    fn test_walk_options_no_binary_filter_without_prompt() {
        use crate::cli::Config;

        let config = Config {
            paths: Some(vec![PathBuf::from(".")]),
            llm_tool: crate::cli::LlmTool::Gemini,
            semantic_depth: 3,
            ..Default::default()
        };

        let options = WalkOptions::from_config(&config).unwrap();
        assert!(!options.filter_binary_files);
    }

    // === Binary File Filtering Tests (TDD - Red Phase) ===

    #[test]
    fn test_filter_binary_files_when_enabled() {
        // Given: A directory with mixed file types
        let temp_dir = TempDir::new().unwrap();
        let root = temp_dir.path();

        // Create test files
        File::create(root.join("image.jpg")).unwrap();
        File::create(root.join("video.mp4")).unwrap();
        File::create(root.join("main.rs")).unwrap();
        File::create(root.join("config.json")).unwrap();

        // When: Walking with filter_binary_files = true
        let options = WalkOptions {
            filter_binary_files: true,
            ..Default::default()
        };
        let files = walk_directory(root, options).unwrap();

        // Then: Only text files are returned
        assert_eq!(files.len(), 2);
        assert!(files
            .iter()
            .any(|f| f.relative_path == PathBuf::from("main.rs")));
        assert!(files
            .iter()
            .any(|f| f.relative_path == PathBuf::from("config.json")));
        assert!(!files
            .iter()
            .any(|f| f.relative_path == PathBuf::from("image.jpg")));
        assert!(!files
            .iter()
            .any(|f| f.relative_path == PathBuf::from("video.mp4")));
    }

    #[test]
    fn test_no_filtering_when_disabled() {
        // Given: Same mixed directory
        let temp_dir = TempDir::new().unwrap();
        let root = temp_dir.path();

        // Create test files
        File::create(root.join("image.jpg")).unwrap();
        File::create(root.join("video.mp4")).unwrap();
        File::create(root.join("main.rs")).unwrap();
        File::create(root.join("config.json")).unwrap();

        // When: Walking with filter_binary_files = false
        let options = WalkOptions {
            filter_binary_files: false,
            ..Default::default()
        };
        let files = walk_directory(root, options).unwrap();

        // Then: All files are returned
        assert_eq!(files.len(), 4);
        assert!(files
            .iter()
            .any(|f| f.relative_path == PathBuf::from("main.rs")));
        assert!(files
            .iter()
            .any(|f| f.relative_path == PathBuf::from("config.json")));
        assert!(files
            .iter()
            .any(|f| f.relative_path == PathBuf::from("image.jpg")));
        assert!(files
            .iter()
            .any(|f| f.relative_path == PathBuf::from("video.mp4")));
    }

    #[test]
    fn test_edge_case_files_without_extensions() {
        // Given: Files without extensions and text files with misleading names
        let temp_dir = TempDir::new().unwrap();
        let root = temp_dir.path();

        // Create edge case files
        File::create(root.join("README")).unwrap();
        File::create(root.join("LICENSE")).unwrap();
        File::create(root.join("Makefile")).unwrap();
        File::create(root.join("Dockerfile")).unwrap();
        File::create(root.join("binary.exe")).unwrap();

        // When: Walking with filter_binary_files = true
        let options = WalkOptions {
            filter_binary_files: true,
            ..Default::default()
        };
        let files = walk_directory(root, options).unwrap();

        // Then: Text files without extensions are kept, binaries are filtered
        assert_eq!(files.len(), 4);
        assert!(files
            .iter()
            .any(|f| f.relative_path == PathBuf::from("README")));
        assert!(files
            .iter()
            .any(|f| f.relative_path == PathBuf::from("LICENSE")));
        assert!(files
            .iter()
            .any(|f| f.relative_path == PathBuf::from("Makefile")));
        assert!(files
            .iter()
            .any(|f| f.relative_path == PathBuf::from("Dockerfile")));
        assert!(!files
            .iter()
            .any(|f| f.relative_path == PathBuf::from("binary.exe")));
    }
}