turboprop 0.1.2

Fast semantic code search and indexing tool
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
//! Configuration management for TurboProp.
//!
//! This module handles loading and managing configuration from various sources
//! including command line arguments, configuration files, and environment variables.

use anyhow::{Context, Result};
use serde::{Deserialize, Serialize};
use std::path::{Path, PathBuf};
use tracing::{debug, info};

use crate::embeddings::EmbeddingConfig;
use crate::types::{parse_filesize, ChunkingConfig, FileDiscoveryConfig};
use crate::validation;

/// Default configuration file name
pub const CONFIG_FILE_NAME: &str = "turboprop.json";

/// YAML configuration file name
pub const YAML_CONFIG_FILE_NAME: &str = ".turboprop.yml";

/// A type-safe wrapper for embedding model names
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub struct ModelName(String);

impl ModelName {
    /// Create a new ModelName
    pub fn new(name: impl Into<String>) -> Self {
        Self(name.into())
    }

    /// Get the model name as a string
    pub fn as_str(&self) -> &str {
        &self.0
    }

    /// Convert into the inner String
    pub fn into_string(self) -> String {
        self.0
    }
}

impl From<String> for ModelName {
    fn from(name: String) -> Self {
        Self(name)
    }
}

impl From<&str> for ModelName {
    fn from(name: &str) -> Self {
        Self(name.to_string())
    }
}

/// Search configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SearchConfig {
    /// Default number of results to return
    pub default_limit: usize,
    /// Minimum similarity threshold for results
    pub min_similarity: f32,
    /// Whether to rehydrate actual content from source files in search results
    /// If false, shows placeholder content for better performance
    pub rehydrate_content: bool,
}

/// Filter limits configuration for glob patterns and file extensions
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct FilterLimitsConfig {
    /// Maximum allowed length for file extensions (including the dot)
    pub max_extension_length: usize,
    /// Maximum allowed length for glob patterns
    pub max_glob_pattern_length: usize,
    /// Maximum number of patterns to cache (0 = unlimited, default: 1000)
    pub max_cache_size: usize,
}

impl Default for SearchConfig {
    fn default() -> Self {
        Self {
            default_limit: 10,
            min_similarity: 0.1,
            rehydrate_content: true,
        }
    }
}

impl Default for FilterLimitsConfig {
    fn default() -> Self {
        Self {
            max_extension_length: 10,
            max_glob_pattern_length: 1000,
            max_cache_size: 1000,
        }
    }
}

/// YAML configuration structure that maps to the .turboprop.yml format
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct YamlConfig {
    /// Embedding configuration
    pub embedding: Option<YamlEmbeddingConfig>,
    /// Indexing configuration (combines chunking and file discovery)
    pub indexing: Option<YamlIndexingConfig>,
    /// Search configuration
    pub search: Option<YamlSearchConfig>,
    /// Directory configuration
    pub directories: Option<YamlDirectoriesConfig>,
    /// Filtering configuration
    pub filtering: Option<YamlFilterConfig>,
}

/// Configuration for embedding generation settings in YAML format
///
/// This struct maps to the `embedding` section in .turboprop.yml files.
/// All fields are optional and will use defaults if not specified.
///
/// # Example
/// ```yaml
/// embedding:
///   model: "sentence-transformers/all-MiniLM-L6-v2"
///   batch_size: 32
///   gguf:
///     device: "cpu"
///     memory_limit: "2GB"
///     context_length: 512
/// ```
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct YamlEmbeddingConfig {
    /// Name of the embedding model to use (e.g., "sentence-transformers/all-MiniLM-L6-v2")
    pub model: Option<ModelName>,
    /// Batch size for embedding generation (default: 32)
    pub batch_size: Option<usize>,
    /// GGUF-specific configuration options
    pub gguf: Option<YamlGGUFConfig>,
}

/// Configuration for GGUF model behavior in YAML format
///
/// This struct maps to the `embedding.gguf` section in .turboprop.yml files.
/// Controls GGUF model loading and inference settings.
///
/// # Example
/// ```yaml
/// embedding:
///   gguf:
///     device: "cpu"  # or "gpu", "cuda", "metal"
///     memory_limit: "2GB"
///     context_length: 512
///     enable_batching: true
///     gpu_layers: 0  # Number of layers to offload to GPU
/// ```
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct YamlGGUFConfig {
    /// Device to use for GGUF model inference ("cpu", "gpu", "cuda", "metal")
    pub device: Option<String>,
    /// Maximum memory limit for model loading (e.g., "2GB", "4096MB")
    pub memory_limit: Option<String>,
    /// Maximum context length for text processing (default: 512)
    pub context_length: Option<usize>,
    /// Whether to enable batch processing for better performance (default: true)
    pub enable_batching: Option<bool>,
    /// Number of transformer layers to offload to GPU (0 = CPU only, default: 0)
    pub gpu_layers: Option<u32>,
    /// Number of threads to use for CPU inference (default: auto-detect)
    pub cpu_threads: Option<usize>,
}

/// Configuration for indexing behavior in YAML format
///
/// This struct maps to the `indexing` section in .turboprop.yml files.
/// Controls how files are processed and chunked for indexing.
///
/// # Example
/// ```yaml
/// indexing:
///   max_filesize: "10MB"
///   chunk_size: 512
///   chunk_overlap: 50
/// ```
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct YamlIndexingConfig {
    /// Maximum file size to index (e.g., "10MB", "1GB")
    pub max_filesize: Option<String>,
    /// Target size for text chunks in tokens (default: 512)
    pub chunk_size: Option<usize>,
    /// Number of overlapping tokens between chunks (default: 50)
    pub chunk_overlap: Option<usize>,
}

/// Configuration for search behavior in YAML format
///
/// This struct maps to the `search` section in .turboprop.yml files.
/// Controls default search parameters and result filtering.
///
/// # Example
/// ```yaml
/// search:
///   default_limit: 10
///   min_similarity: 0.5
/// ```
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct YamlSearchConfig {
    /// Default maximum number of search results to return (default: 10)
    pub default_limit: Option<usize>,
    /// Minimum similarity threshold for search results (0.0-1.0, default: 0.3)
    pub min_similarity: Option<f32>,
    /// Whether to rehydrate actual content from source files in search results (default: true)
    pub rehydrate_content: Option<bool>,
}

/// Configuration for directory paths in YAML format
///
/// This struct maps to the `directories` section in .turboprop.yml files.
/// Specifies where indexes and models are stored.
///
/// # Example
/// ```yaml
/// directories:
///   index_dir: "./my-index"
///   models_dir: "~/.cache/turboprop/models"
/// ```
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct YamlDirectoriesConfig {
    /// Directory where indexes are stored (default: "./turboprop-index")
    pub index_dir: Option<PathBuf>,
    /// Directory where embedding models are cached (default: system cache dir)
    pub models_dir: Option<PathBuf>,
}

/// Configuration for filtering limits in YAML format
///
/// This struct maps to the `filtering` section in .turboprop.yml files.
/// Controls size limits for glob patterns and file extensions.
///
/// # Example
/// ```yaml
/// filtering:
///   max_extension_length: 15
///   max_glob_pattern_length: 2000
///   max_cache_size: 500
/// ```
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct YamlFilterConfig {
    /// Maximum allowed length for file extensions including the dot (default: 10)
    pub max_extension_length: Option<usize>,
    /// Maximum allowed length for glob patterns (default: 1000)
    pub max_glob_pattern_length: Option<usize>,
    /// Maximum number of patterns to cache, 0 = unlimited (default: 1000)
    pub max_cache_size: Option<usize>,
}

/// Model-specific configuration settings
#[derive(Debug, Clone, Serialize, Deserialize, Default)]
pub struct ModelConfig {
    /// Custom instruction for this model
    pub instruction: Option<String>,
    /// Model-specific cache directory
    pub cache_dir: Option<PathBuf>,
    /// Custom download URL override
    pub download_url: Option<String>,
    /// Number of dimensions in the model's embedding vectors
    pub dimensions: Option<usize>,
    /// Size of the model in bytes
    pub size_bytes: Option<u64>,
}

/// Main configuration structure for TurboProp
#[derive(Debug, Clone, Serialize, Deserialize, Default)]
pub struct TurboPropConfig {
    /// Embedding generation configuration
    pub embedding: EmbeddingConfig,
    /// File discovery configuration
    pub file_discovery: FileDiscoveryConfig,
    /// Chunking configuration
    pub chunking: ChunkingConfig,
    /// Search configuration
    pub search: SearchConfig,
    /// Filtering limits configuration
    pub filtering: FilterLimitsConfig,
    /// General application settings
    pub general: GeneralConfig,
    /// Default embedding model to use
    pub default_model: Option<String>,
    /// Model-specific configurations
    pub models: Option<std::collections::HashMap<String, ModelConfig>>,
    /// Current instruction for embeddings (used by instruction-capable models)
    pub current_instruction: Option<String>,
}

/// General application configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct GeneralConfig {
    /// Default index path when none specified
    pub default_index_path: PathBuf,
    /// Cache directory for models and other data
    pub cache_dir: PathBuf,
    /// Enable verbose logging
    pub verbose: bool,
    /// Number of parallel workers for processing
    pub worker_threads: Option<usize>,
    /// Storage schema version for index compatibility
    pub storage_version: String,
}

impl Default for GeneralConfig {
    fn default() -> Self {
        Self {
            default_index_path: PathBuf::from("."),
            cache_dir: PathBuf::from(".turboprop"),
            verbose: false,
            worker_threads: None, // Use system default
            storage_version: "1.0.0".to_string(),
        }
    }
}

impl YamlConfig {
    /// Convert YamlConfig to TurboPropConfig by merging with defaults
    pub fn to_turboprop_config(self) -> Result<TurboPropConfig> {
        // Validate the YAML configuration first
        validation::validate_yaml_config(&self).context("YAML configuration validation failed")?;

        let mut config = TurboPropConfig::default();

        // Apply each configuration section
        self.apply_embedding_config(&mut config);
        self.apply_indexing_config(&mut config)?;
        self.apply_search_config(&mut config);
        self.apply_directories_config(&mut config);
        self.apply_filtering_config(&mut config);

        // Validate the final configuration
        validation::validate_config(&config).context("Final configuration validation failed")?;

        Ok(config)
    }

    /// Apply embedding configuration to the base config
    fn apply_embedding_config(&self, config: &mut TurboPropConfig) {
        if let Some(embedding) = &self.embedding {
            if let Some(model) = &embedding.model {
                config.embedding.model_name = model.as_str().to_string();
            }
            if let Some(batch_size) = embedding.batch_size {
                config.embedding.batch_size = batch_size;
            }
        }
    }

    /// Apply indexing configuration to the base config
    fn apply_indexing_config(&self, config: &mut TurboPropConfig) -> Result<()> {
        if let Some(indexing) = &self.indexing {
            if let Some(max_filesize_str) = &indexing.max_filesize {
                let max_filesize = parse_filesize(max_filesize_str)
                    .map_err(|e| anyhow::anyhow!("Failed to parse max_filesize: {}", e))?;
                config.file_discovery.max_filesize_bytes = Some(max_filesize);
            }
            if let Some(chunk_size) = indexing.chunk_size {
                config.chunking.target_chunk_size_tokens = chunk_size;
            }
            if let Some(chunk_overlap) = indexing.chunk_overlap {
                config.chunking.overlap_tokens = chunk_overlap;
            }
        }
        Ok(())
    }

    /// Apply search configuration to the base config
    fn apply_search_config(&self, config: &mut TurboPropConfig) {
        if let Some(search) = &self.search {
            if let Some(default_limit) = search.default_limit {
                config.search.default_limit = default_limit;
            }
            if let Some(min_similarity) = search.min_similarity {
                config.search.min_similarity = min_similarity;
            }
            if let Some(rehydrate_content) = search.rehydrate_content {
                config.search.rehydrate_content = rehydrate_content;
            }
        }
    }

    /// Apply directories configuration to the base config
    fn apply_directories_config(&self, config: &mut TurboPropConfig) {
        if let Some(directories) = &self.directories {
            if let Some(index_dir) = &directories.index_dir {
                config.general.default_index_path = index_dir.clone();
            }
            if let Some(models_dir) = &directories.models_dir {
                config.general.cache_dir = models_dir.clone();
                config.embedding.cache_dir = models_dir.clone();
            }
        }
    }

    /// Apply filtering configuration to the base config
    fn apply_filtering_config(&self, config: &mut TurboPropConfig) {
        if let Some(filtering) = &self.filtering {
            if let Some(max_extension_length) = filtering.max_extension_length {
                config.filtering.max_extension_length = max_extension_length;
            }
            if let Some(max_glob_pattern_length) = filtering.max_glob_pattern_length {
                config.filtering.max_glob_pattern_length = max_glob_pattern_length;
            }
            if let Some(max_cache_size) = filtering.max_cache_size {
                config.filtering.max_cache_size = max_cache_size;
            }
        }
    }
}

impl TurboPropConfig {
    /// Load configuration from the default location
    pub fn load() -> Result<Self> {
        Self::load_from_default_paths()
    }

    /// Load configuration from a specific file
    pub fn load_from_file(config_path: &Path) -> Result<Self> {
        if !config_path.exists() {
            debug!("Config file not found: {:?}, using defaults", config_path);
            return Ok(Self::default());
        }

        info!("Loading configuration from: {:?}", config_path);

        let config_content = std::fs::read_to_string(config_path)
            .with_context(|| format!("Failed to read config file: {:?}", config_path))?;

        let config = if Self::is_yaml_file(config_path) {
            let yaml_config: YamlConfig = serde_yaml::from_str(&config_content)
                .with_context(|| format!("Failed to parse YAML config file: {:?}", config_path))?;
            yaml_config.to_turboprop_config()?
        } else {
            serde_json::from_str(&config_content)
                .with_context(|| format!("Failed to parse JSON config file: {:?}", config_path))?
        };

        debug!("Configuration loaded successfully");
        Ok(config)
    }

    /// Check if a file is a YAML configuration file based on extension or name
    fn is_yaml_file(path: &Path) -> bool {
        if let Some(filename) = path.file_name().and_then(|n| n.to_str()) {
            if filename == ".turboprop.yml" {
                return true;
            }
        }

        if let Some(extension) = path.extension().and_then(|ext| ext.to_str()) {
            matches!(extension, "yml" | "yaml")
        } else {
            false
        }
    }

    /// Load configuration searching in default locations
    fn load_from_default_paths() -> Result<Self> {
        let search_paths = Self::get_config_search_paths();

        for path in search_paths {
            if path.exists() {
                return Self::load_from_file(&path);
            }
        }

        debug!("No configuration file found, using defaults");
        Ok(Self::default())
    }

    /// Build the list of paths to search for configuration files
    /// Search order (CLI args > .turboprop.yml > turboprop.json > defaults):
    /// 1. Current directory: ./.turboprop.yml
    /// 2. Current directory: ./turboprop.json
    /// 3. User config directory: ~/.config/turboprop/.turboprop.yml
    /// 4. User config directory: ~/.config/turboprop/turboprop.json
    /// 5. User home directory: ~/.turboprop.yml
    /// 6. User home directory: ~/turboprop.json
    fn get_config_search_paths() -> Vec<PathBuf> {
        vec![
            // Current directory - YAML first
            PathBuf::from(YAML_CONFIG_FILE_NAME),
            PathBuf::from(CONFIG_FILE_NAME),
            // User config directory - YAML first
            dirs::config_dir()
                .map(|p| p.join("turboprop").join(YAML_CONFIG_FILE_NAME))
                .unwrap_or_else(|| {
                    PathBuf::from("~/.config/turboprop").join(YAML_CONFIG_FILE_NAME)
                }),
            dirs::config_dir()
                .map(|p| p.join("turboprop").join(CONFIG_FILE_NAME))
                .unwrap_or_else(|| PathBuf::from("~/.config/turboprop").join(CONFIG_FILE_NAME)),
            // User home directory - YAML first
            dirs::home_dir()
                .map(|p| p.join(YAML_CONFIG_FILE_NAME))
                .unwrap_or_else(|| PathBuf::from("~").join(YAML_CONFIG_FILE_NAME)),
            dirs::home_dir()
                .map(|p| p.join(CONFIG_FILE_NAME))
                .unwrap_or_else(|| PathBuf::from("~").join(CONFIG_FILE_NAME)),
        ]
    }

    /// Save configuration to a specific file
    pub fn save_to_file(&self, config_path: &Path) -> Result<()> {
        // Create parent directory if it doesn't exist
        if let Some(parent) = config_path.parent() {
            std::fs::create_dir_all(parent)
                .with_context(|| format!("Failed to create config directory: {:?}", parent))?;
        }

        let config_content =
            serde_json::to_string_pretty(self).context("Failed to serialize configuration")?;

        std::fs::write(config_path, config_content)
            .with_context(|| format!("Failed to write config file: {:?}", config_path))?;

        info!("Configuration saved to: {:?}", config_path);
        Ok(())
    }

    /// Save configuration to the default location
    pub fn save(&self) -> Result<()> {
        let config_path = PathBuf::from(CONFIG_FILE_NAME);
        self.save_to_file(&config_path)
    }

    /// Override configuration with command-line arguments
    pub fn merge_cli_args(mut self, args: &CliConfigOverrides) -> Self {
        if let Some(ref model) = args.model {
            self.embedding.model_name = model.clone();
            debug!("Overriding embedding model from CLI: {}", model);
        }

        if let Some(ref instruction) = args.instruction {
            self.current_instruction = Some(instruction.clone());
            debug!("Overriding instruction from CLI: {}", instruction);
        }

        if let Some(ref cache_dir) = args.cache_dir {
            self.general.cache_dir = cache_dir.clone();
            self.embedding.cache_dir = cache_dir.join("models");
            debug!("Overriding cache directory from CLI: {:?}", cache_dir);
        }

        if args.verbose {
            self.general.verbose = true;
            debug!("Enabling verbose mode from CLI");
        }

        if let Some(max_filesize) = args.max_filesize {
            self.file_discovery = self.file_discovery.with_max_filesize(max_filesize);
            debug!("Overriding max filesize from CLI: {} bytes", max_filesize);
        }

        if let Some(threads) = args.worker_threads {
            self.general.worker_threads = Some(threads);
            debug!("Overriding worker threads from CLI: {}", threads);
        }

        if let Some(batch_size) = args.batch_size {
            self.embedding.batch_size = batch_size;
            debug!("Overriding batch size from CLI: {}", batch_size);
        }

        if let Some(search_limit) = args.search_limit {
            self.search.default_limit = search_limit;
            debug!("Overriding search limit from CLI: {}", search_limit);
        }

        if let Some(min_similarity) = args.min_similarity {
            self.search.min_similarity = min_similarity;
            debug!("Overriding min similarity from CLI: {}", min_similarity);
        }

        if let Some(chunk_size) = args.chunk_size {
            self.chunking.target_chunk_size_tokens = chunk_size;
            debug!("Overriding chunk size from CLI: {}", chunk_size);
        }

        if let Some(chunk_overlap) = args.chunk_overlap {
            self.chunking.overlap_tokens = chunk_overlap;
            debug!("Overriding chunk overlap from CLI: {}", chunk_overlap);
        }

        self
    }

    /// Get model dimensions with defaults for known models
    pub fn get_model_dimensions(&self, model_name: &str) -> usize {
        // Check for custom configuration first
        if let Some(models) = &self.models {
            if let Some(model_config) = models.get(model_name) {
                if let Some(dimensions) = model_config.dimensions {
                    return dimensions;
                }
            }
        }

        // Default dimensions for known models
        match model_name {
            "nomic-embed-code.Q5_K_S.gguf" => 768,
            "Qwen/Qwen3-Embedding-0.6B" => 1024,
            "sentence-transformers/all-MiniLM-L6-v2" => 384,
            _ => 384, // Default to MiniLM dimensions
        }
    }

    /// Get model size in bytes with defaults for known models
    pub fn get_model_size_bytes(&self, model_name: &str) -> u64 {
        // Check for custom configuration first
        if let Some(models) = &self.models {
            if let Some(model_config) = models.get(model_name) {
                if let Some(size_bytes) = model_config.size_bytes {
                    return size_bytes;
                }
            }
        }

        // Default sizes for known models
        match model_name {
            "nomic-embed-code.Q5_K_S.gguf" => 2_500_000_000, // ~2.5GB
            "Qwen/Qwen3-Embedding-0.6B" => 600_000_000,      // ~600MB
            "sentence-transformers/all-MiniLM-L6-v2" => 44_000_000, // ~44MB
            _ => 44_000_000,                                 // Default to MiniLM size
        }
    }

    /// Validate the configuration and return any issues
    pub fn validate(&self) -> Result<()> {
        validation::validate_config(self).map_err(anyhow::Error::from)
    }
}

/// Command-line configuration overrides
#[derive(Debug, Clone, Default)]
pub struct CliConfigOverrides {
    pub model: Option<String>,
    pub instruction: Option<String>,
    pub cache_dir: Option<PathBuf>,
    pub verbose: bool,
    pub max_filesize: Option<u64>,
    pub worker_threads: Option<usize>,
    pub batch_size: Option<usize>,
    pub search_limit: Option<usize>,
    pub min_similarity: Option<f32>,
    pub chunk_size: Option<usize>,
    pub chunk_overlap: Option<usize>,
}

impl CliConfigOverrides {
    pub fn new() -> Self {
        Self::default()
    }

    pub fn with_model(mut self, model: impl Into<String>) -> Self {
        self.model = Some(model.into());
        self
    }

    pub fn with_instruction(mut self, instruction: impl Into<String>) -> Self {
        self.instruction = Some(instruction.into());
        self
    }

    pub fn with_cache_dir(mut self, cache_dir: impl Into<PathBuf>) -> Self {
        self.cache_dir = Some(cache_dir.into());
        self
    }

    pub fn with_verbose(mut self, verbose: bool) -> Self {
        self.verbose = verbose;
        self
    }

    pub fn with_max_filesize(mut self, max_filesize: u64) -> Self {
        self.max_filesize = Some(max_filesize);
        self
    }

    pub fn with_worker_threads(mut self, worker_threads: usize) -> Self {
        self.worker_threads = Some(worker_threads);
        self
    }

    pub fn with_batch_size(mut self, batch_size: usize) -> Self {
        self.batch_size = Some(batch_size);
        self
    }

    pub fn with_search_limit(mut self, search_limit: usize) -> Self {
        self.search_limit = Some(search_limit);
        self
    }

    pub fn with_min_similarity(mut self, min_similarity: f32) -> Self {
        self.min_similarity = Some(min_similarity);
        self
    }

    pub fn with_chunk_size(mut self, chunk_size: usize) -> Self {
        self.chunk_size = Some(chunk_size);
        self
    }

    pub fn with_chunk_overlap(mut self, chunk_overlap: usize) -> Self {
        self.chunk_overlap = Some(chunk_overlap);
        self
    }
}

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

    #[test]
    fn test_default_config() {
        let config = TurboPropConfig::default();
        assert_eq!(config.general.default_index_path, PathBuf::from("."));
        assert!(!config.general.verbose);
        assert_eq!(config.general.cache_dir, PathBuf::from(".turboprop"));
    }

    #[test]
    fn test_config_serialization() {
        let config = TurboPropConfig::default();
        let serialized = serde_json::to_string_pretty(&config).unwrap();
        let deserialized: TurboPropConfig = serde_json::from_str(&serialized).unwrap();

        assert_eq!(
            config.general.default_index_path,
            deserialized.general.default_index_path
        );
        assert_eq!(config.general.verbose, deserialized.general.verbose);
    }

    #[test]
    fn test_save_and_load_config() {
        let temp_dir = TempDir::new().unwrap();
        let config_path = temp_dir.path().join("test_config.json");

        let original_config = TurboPropConfig::default();
        original_config.save_to_file(&config_path).unwrap();

        let loaded_config = TurboPropConfig::load_from_file(&config_path).unwrap();
        assert_eq!(
            original_config.general.verbose,
            loaded_config.general.verbose
        );
    }

    #[test]
    fn test_load_nonexistent_config() {
        let temp_dir = TempDir::new().unwrap();
        let config_path = temp_dir.path().join("nonexistent.json");

        let config = TurboPropConfig::load_from_file(&config_path).unwrap();
        // Should return default config
        assert_eq!(config.general.default_index_path, PathBuf::from("."));
    }

    #[test]
    fn test_cli_overrides() {
        let config = TurboPropConfig::default();
        let overrides = CliConfigOverrides::new()
            .with_model("custom-model")
            .with_verbose(true)
            .with_worker_threads(8);

        let merged = config.merge_cli_args(&overrides);
        assert_eq!(merged.embedding.model_name, "custom-model");
        assert!(merged.general.verbose);
        assert_eq!(merged.general.worker_threads, Some(8));
    }

    #[test]
    fn test_config_validation_success() {
        let config = TurboPropConfig::default();
        assert!(config.validate().is_ok());
    }

    #[test]
    fn test_config_validation_empty_cache() {
        let mut config = TurboPropConfig::default();
        config.general.cache_dir = PathBuf::new();
        assert!(config.validate().is_err());
    }

    #[test]
    fn test_config_validation_zero_threads() {
        let mut config = TurboPropConfig::default();
        config.general.worker_threads = Some(0);
        assert!(config.validate().is_err());
    }

    #[test]
    fn test_load_yaml_config_file() {
        let temp_dir = TempDir::new().unwrap();
        let config_path = temp_dir.path().join(".turboprop.yml");

        let yaml_content = r#"
embedding:
  model: "sentence-transformers/all-MiniLM-L6-v2"
  batch_size: 64
  
indexing:
  max_filesize: "5mb"
  chunk_size: 500
  chunk_overlap: 75

search:
  default_limit: 20
  min_similarity: 0.2

directories:
  index_dir: ".turboprop"
  models_dir: ".turboprop/models"
"#;
        std::fs::write(&config_path, yaml_content).unwrap();

        let config = TurboPropConfig::load_from_file(&config_path).unwrap();

        assert_eq!(
            config.embedding.model_name,
            "sentence-transformers/all-MiniLM-L6-v2"
        );
        assert_eq!(config.embedding.batch_size, 64);
        assert_eq!(config.chunking.target_chunk_size_tokens, 500);
        assert_eq!(config.chunking.overlap_tokens, 75);
        assert_eq!(config.search.default_limit, 20);
        assert_eq!(config.search.min_similarity, 0.2);
        assert_eq!(
            config.file_discovery.max_filesize_bytes,
            Some(5 * 1024 * 1024)
        );
    }

    #[test]
    fn test_configuration_precedence() {
        let temp_dir = TempDir::new().unwrap();
        let config_path = temp_dir.path().join(".turboprop.yml");

        // Create a YAML config with specific values
        let yaml_content = r#"
embedding:
  model: "yaml-model"
  batch_size: 32

search:
  default_limit: 15
  min_similarity: 0.3
"#;
        std::fs::write(&config_path, yaml_content).unwrap();

        let base_config = TurboPropConfig::load_from_file(&config_path).unwrap();

        // Verify YAML values are loaded
        assert_eq!(base_config.embedding.model_name, "yaml-model");
        assert_eq!(base_config.embedding.batch_size, 32);
        assert_eq!(base_config.search.default_limit, 15);

        // Create CLI overrides that should take precedence
        let cli_overrides = CliConfigOverrides::new()
            .with_model("cli-model")
            .with_batch_size(128)
            .with_search_limit(50);

        let final_config = base_config.merge_cli_args(&cli_overrides);

        // Verify CLI args override YAML values
        assert_eq!(final_config.embedding.model_name, "cli-model");
        assert_eq!(final_config.embedding.batch_size, 128);
        assert_eq!(final_config.search.default_limit, 50);

        // Verify non-overridden YAML values remain
        assert_eq!(final_config.search.min_similarity, 0.3);
    }
}