cqlite-cli 0.11.0

Command-line interface for CQLite — read Apache Cassandra 5.0 SSTables without a cluster
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
use anyhow::{Context, Result};
use serde::{Deserialize, Serialize};
use std::fs;
use std::path::{Path, PathBuf};

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Config {
    pub default_database: Option<PathBuf>,
    #[serde(default)]
    pub connection: ConnectionConfig,
    #[serde(default)]
    pub output: OutputSettings,
    #[serde(default)]
    pub performance: PerformanceConfig,
    #[serde(default)]
    pub logging: LoggingConfig,
    #[serde(default)]
    pub repl: ReplConfig,
    pub data_directory: Option<PathBuf>,
    pub default_keyspace: Option<String>,

    // Legacy fields for backward compatibility
    #[serde(skip_serializing_if = "Option::is_none")]
    pub enable_history: Option<bool>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub enable_completion: Option<bool>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub show_timing: Option<bool>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub page_size: Option<usize>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub enable_paging: Option<bool>,
    #[serde(default)]
    pub no_color: bool,

    // M2 one-shot mode fields
    /// Schema file paths (supports multiple sources)
    #[serde(default)]
    pub schema_paths: Vec<PathBuf>,

    /// One-shot execution query (from -e flag)
    #[serde(skip)]
    pub execution_query: Option<String>,

    /// One-shot execution file (from -f flag)
    #[serde(skip)]
    pub execution_file: Option<PathBuf>,

    /// Output mode for query results (table/json/csv)
    pub output_mode: Option<String>,

    /// Maximum rows for queries
    pub query_limit: Option<usize>,

    // Version hint resolution (Issue #130)
    /// Cassandra version hint from CLI flag (for precedence chain)
    #[serde(skip)]
    pub cassandra_version: Option<String>,

    /// Resolved version information (computed async after config load)
    /// TODO(Issue #130): Used by :status meta-command (not yet implemented)
    #[serde(skip)]
    #[allow(dead_code)]
    pub resolved_version: Option<cqlite_core::version_hints::ResolvedVersion>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ConnectionConfig {
    pub timeout_ms: u64,
    pub retry_attempts: u32,
    pub pool_size: u32,
}

impl Default for ConnectionConfig {
    fn default() -> Self {
        Self {
            timeout_ms: 30000,
            retry_attempts: 3,
            pool_size: 10,
        }
    }
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct OutputSettings {
    pub max_rows: Option<usize>,
    pub pager: Option<String>,
    pub colors: bool,
    pub timestamp_format: String,
}

impl Default for OutputSettings {
    fn default() -> Self {
        Self {
            max_rows: Some(1000),
            pager: None,
            colors: true,
            timestamp_format: "%Y-%m-%d %H:%M:%S".to_string(),
        }
    }
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PerformanceConfig {
    pub query_timeout_ms: u64,
    pub memory_limit_mb: Option<u64>,
    pub cache_size_mb: u64,
}

impl Default for PerformanceConfig {
    fn default() -> Self {
        Self {
            query_timeout_ms: 30000,
            memory_limit_mb: None,
            cache_size_mb: 64,
        }
    }
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct LoggingConfig {
    pub level: String,
    pub file: Option<PathBuf>,
    pub format: LogFormat,
}

impl Default for LoggingConfig {
    fn default() -> Self {
        Self {
            level: "info".to_string(),
            file: None,
            format: LogFormat::Pretty,
        }
    }
}

#[derive(Debug, Clone, Serialize, Deserialize, Default)]
pub enum LogFormat {
    Plain,
    Json,
    #[default]
    Pretty,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ReplConfig {
    pub enable_history: bool,
    pub enable_completion: bool,
    pub enable_colors: bool,
    pub show_timing: bool,
    pub page_size: usize,
    pub enable_paging: bool,
    pub max_history_size: usize,
    pub prompt: String,
    pub prompt_continuation: String,
    pub history_file: Option<PathBuf>,
}

impl Default for Config {
    fn default() -> Self {
        Self {
            default_database: None,
            connection: ConnectionConfig::default(),
            output: OutputSettings::default(),
            performance: PerformanceConfig::default(),
            logging: LoggingConfig::default(),
            repl: ReplConfig::default(),
            data_directory: None,
            default_keyspace: None,
            enable_history: None,
            enable_completion: None,
            show_timing: None,
            page_size: None,
            enable_paging: None,
            no_color: false,
            schema_paths: Vec::new(),
            execution_query: None,
            execution_file: None,
            output_mode: None,
            query_limit: None,
            cassandra_version: None,
            resolved_version: None,
        }
    }
}

impl Config {
    pub fn load(config_path: Option<PathBuf>, cli: &crate::cli_types::Cli) -> Result<Self> {
        let mut builder = ConfigBuilder::from_defaults()
            .with_user_config()? // 1. User config (lowest precedence)
            .with_project_config()?; // 2. Project config (overrides user)

        // 3. Explicit --config flag (overrides discovered configs)
        if let Some(path) = config_path {
            builder = builder.with_explicit_config(path)?;
        }

        // 4. Environment variables (override files)
        // 5. CLI flags (highest precedence)
        Ok(builder.with_env()?.with_flags(cli).build())
    }

    /// Resolve Cassandra version using precedence chain (Issue #130)
    ///
    /// This method implements the version hint precedence:
    /// 1. User override (--cassandra-version flag)
    /// 2. SSTable metadata
    /// 3. metadata.yml
    /// 4. Unknown
    ///
    /// # Arguments
    ///
    /// * `platform` - Platform abstraction for file I/O
    ///
    /// # Errors
    ///
    /// Returns an error only for fatal I/O errors. Missing metadata is not an error.
    ///
    /// TODO(Issue #130): Used by :status meta-command (not yet implemented)
    #[allow(dead_code)]
    pub async fn resolve_version(
        &mut self,
        platform: std::sync::Arc<cqlite_core::Platform>,
    ) -> Result<()> {
        use cqlite_core::version_hints::VersionHintResolver;
        use std::path::PathBuf;

        // Use data_directory if available, otherwise use current directory
        let default_path = PathBuf::from(".");
        let data_dir = self
            .data_directory
            .as_deref()
            .unwrap_or(default_path.as_path());

        self.resolved_version = Some(
            VersionHintResolver::resolve(self.cassandra_version.clone(), data_dir, platform)
                .await?,
        );

        Ok(())
    }

    /// Get resolved version information for display/diagnostics
    ///
    /// Returns `None` if version resolution has not been performed yet.
    /// Call `resolve_version()` first to populate this field.
    ///
    /// TODO(Issue #130): Used by :status meta-command (not yet implemented)
    #[allow(dead_code)]
    pub fn version_info(&self) -> Option<&cqlite_core::version_hints::ResolvedVersion> {
        self.resolved_version.as_ref()
    }

    /// Get version string for display (returns "unknown" if not resolved)
    ///
    /// TODO(Issue #130): Used by :status meta-command (not yet implemented)
    #[allow(dead_code)]
    pub fn version_string(&self) -> String {
        self.resolved_version
            .as_ref()
            .map(|rv| rv.version_or_unknown().to_string())
            .unwrap_or_else(|| "not resolved".to_string())
    }

    /// Get version source description
    ///
    /// TODO(Issue #130): Used by :status meta-command (not yet implemented)
    #[allow(dead_code)]
    pub fn version_source(&self) -> String {
        self.resolved_version
            .as_ref()
            .map(|rv| rv.source.description().to_string())
            .unwrap_or_else(|| "not resolved".to_string())
    }

    fn load_from_file(path: &Path) -> Result<Self> {
        let content = fs::read_to_string(path)
            .with_context(|| format!("Failed to read config file: {}", path.display()))?;

        let config: Config = match path.extension().and_then(|ext| ext.to_str()) {
            Some("toml") => {
                toml::from_str(&content).with_context(|| "Failed to parse TOML config")?
            }
            Some("yaml") | Some("yml") => {
                serde_yaml::from_str(&content).with_context(|| "Failed to parse YAML config")?
            }
            Some("json") => {
                serde_json::from_str(&content).with_context(|| "Failed to parse JSON config")?
            }
            _ => return Err(anyhow::anyhow!("Unsupported config file format")),
        };

        Ok(config)
    }

    #[allow(dead_code)]
    fn load_default() -> Result<Self> {
        // Look for config file in standard locations
        let config_paths = [
            "cqlite.toml",
            "cqlite.yaml",
            "cqlite.yml",
            "cqlite.json",
            ".cqlite.toml",
            ".cqlite.yaml",
            ".cqlite.yml",
            ".cqlite.json",
        ];

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

        // Also check XDG config directory
        if let Some(config_dir) = dirs::config_dir() {
            let xdg_paths = [
                config_dir.join("cqlite").join("config.toml"),
                config_dir.join("cqlite").join("config.yaml"),
                config_dir.join("cqlite").join("config.yml"),
                config_dir.join("cqlite").join("config.json"),
            ];

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

        // Return default config if no file found
        Ok(Self::default())
    }

    #[allow(dead_code)]
    pub fn save_to_file(&self, path: &Path) -> Result<()> {
        let content = match path.extension().and_then(|ext| ext.to_str()) {
            Some("toml") => toml::to_string_pretty(self)
                .with_context(|| "Failed to serialize config to TOML")?,
            Some("yaml") | Some("yml") => {
                serde_yaml::to_string(self).with_context(|| "Failed to serialize config to YAML")?
            }
            Some("json") => serde_json::to_string_pretty(self)
                .with_context(|| "Failed to serialize config to JSON")?,
            _ => return Err(anyhow::anyhow!("Unsupported config file format")),
        };

        if let Some(parent) = path.parent() {
            fs::create_dir_all(parent).with_context(|| {
                format!("Failed to create config directory: {}", parent.display())
            })?;
        }

        fs::write(path, content)
            .with_context(|| format!("Failed to write config file: {}", path.display()))?;

        Ok(())
    }
}

impl Default for ReplConfig {
    fn default() -> Self {
        Self {
            enable_history: true,
            enable_completion: true,
            enable_colors: true,
            show_timing: false,
            page_size: 50,
            enable_paging: true,
            max_history_size: 1000,
            prompt: "cqlite> ".to_string(),
            prompt_continuation: "    -> ".to_string(),
            history_file: None,
        }
    }
}

/// Configuration for table formatter output behavior
///
/// This struct controls how query results are formatted and displayed,
/// including color support, row limits, pagination settings, and output destination.
#[derive(Debug, Clone)]
pub struct OutputConfig {
    /// Whether to enable colored output in table formatting.
    /// This is the inverse of the `--no-color` CLI flag.
    /// When `true`, output will include ANSI color codes for better readability.
    pub color_enabled: bool,

    /// Maximum number of rows to display in query results.
    /// When `None`, all rows will be displayed.
    /// This can be used to prevent overwhelming output from large result sets.
    pub limit: Option<usize>,

    /// Number of rows per page for pagination.
    /// When `None`, pagination is disabled and all rows are shown at once.
    /// Default is 50 rows per page, matching cqlsh behavior.
    #[allow(dead_code)]
    pub page_size: Option<usize>,

    /// Output target (stdout or file path).
    /// When `Stdout`, output is written to standard output.
    /// When `File(path)`, output is written atomically to the specified file.
    pub target: crate::output::OutputTarget,

    /// Whether to overwrite existing files when writing to a file target.
    /// Only relevant when `target` is `File`.
    pub overwrite: bool,
}

impl OutputConfig {
    /// Create a new OutputConfig from resolved Config and CLI flags
    ///
    /// This method respects the precedence chain: CLI flags > env vars > config file > defaults.
    /// The `Config` object passed in has already resolved this chain via ConfigBuilder.
    ///
    /// # Arguments
    ///
    /// * `config` - The resolved Config object containing env/file/default values
    /// * `no_color_flag` - The `--no-color` CLI flag (if present, overrides config)
    /// * `limit_flag` - The `--limit` CLI flag (if present, overrides config)
    /// * `page_size_flag` - The `--page-size` CLI flag (if present, overrides config)
    /// * `output_flag` - The `--output` CLI flag for file destination
    /// * `overwrite_flag` - The `--overwrite` CLI flag for overwriting existing files
    ///
    /// # Precedence
    ///
    /// - `color_enabled`: --no-color flag > CQLITE_NO_COLOR env > config.output.colors > default (true)
    /// - `limit`: --limit flag > CQLITE_LIMIT env > config.query_limit > default (None)
    /// - `page_size`: --page-size flag > CQLITE_PAGE_SIZE env > config.repl.page_size > default (50)
    /// - `target`: --output flag > CQLITE_OUTPUT env > default (Stdout)
    /// - `overwrite`: --overwrite flag > default (false)
    ///
    /// # Examples
    ///
    /// ```
    /// use cqlite_cli::config::{Config, OutputConfig};
    /// use cqlite_cli::cli_types::Cli;
    /// use clap::Parser;
    ///
    /// // Create config with defaults
    /// let cli = Cli::parse_from(&["cqlite"]);
    /// let config = Config::load(None, &cli).unwrap();
    /// let output = OutputConfig::from_cli(&config, false, None, None, None, false);
    /// assert!(output.color_enabled);
    /// assert_eq!(output.page_size, Some(50));
    ///
    /// // CLI flag overrides config
    /// let output = OutputConfig::from_cli(&config, true, Some(100), Some(25), None, false);
    /// assert!(!output.color_enabled);
    /// assert_eq!(output.limit, Some(100));
    /// assert_eq!(output.page_size, Some(25));
    /// ```
    pub fn from_cli(
        config: &Config,
        no_color_flag: bool,
        limit_flag: Option<usize>,
        page_size_flag: Option<usize>,
        output_flag: Option<std::path::PathBuf>,
        overwrite_flag: bool,
    ) -> Self {
        use crate::output::OutputTarget;

        Self {
            // CLI flag overrides config value
            color_enabled: if no_color_flag {
                false
            } else {
                config.output.colors
            },
            // CLI flag overrides config.query_limit (which already has env/file/default precedence)
            limit: limit_flag.or(config.query_limit),
            // CLI flag overrides config.repl.page_size (which already has env/file/default precedence)
            page_size: page_size_flag.or(Some(config.repl.page_size)),
            // Output target from CLI flag
            target: output_flag
                .map(OutputTarget::File)
                .unwrap_or(OutputTarget::Stdout),
            // Overwrite flag
            overwrite: overwrite_flag,
        }
    }
}

impl Default for OutputConfig {
    /// Default output configuration
    ///
    /// Returns an OutputConfig with:
    /// - `color_enabled`: `true` (colors enabled by default)
    /// - `limit`: `None` (no row limit)
    /// - `page_size`: `Some(50)` (50 rows per page, matching cqlsh)
    /// - `target`: `Stdout` (write to standard output)
    /// - `overwrite`: `false` (don't overwrite existing files)
    fn default() -> Self {
        Self {
            color_enabled: true,
            limit: None,
            page_size: Some(50),
            target: crate::output::OutputTarget::Stdout,
            overwrite: false,
        }
    }
}

/// Merge two configs with partial override semantics
/// Only non-default values from overlay replace base values
fn merge_partial_config(base: Config, overlay: Config) -> Config {
    // Determine final no_color value (true if either is true)
    let final_no_color = overlay.no_color || base.no_color;

    // Determine output colors
    // Priority: if no_color is true, colors must be false
    // Otherwise, use overlay value (which has defaults applied during TOML parsing)
    let final_output_colors = if final_no_color {
        false
    } else {
        overlay.output.colors
    };

    Config {
        // Use overlay value if present, otherwise keep base
        data_directory: overlay.data_directory.or(base.data_directory),
        default_keyspace: overlay.default_keyspace.or(base.default_keyspace),

        // For schema_paths, use overlay if non-empty
        schema_paths: if overlay.schema_paths.is_empty() {
            base.schema_paths
        } else {
            overlay.schema_paths
        },

        // Output mode
        output_mode: overlay.output_mode.or(base.output_mode),

        // Numeric limits
        query_limit: overlay.query_limit.or(base.query_limit),

        // Nested structs - merge carefully
        connection: overlay.connection,
        output: OutputSettings {
            max_rows: overlay.output.max_rows.or(base.output.max_rows),
            pager: overlay.output.pager.or(base.output.pager),
            colors: final_output_colors,
            timestamp_format: overlay.output.timestamp_format,
        },
        repl: ReplConfig {
            enable_history: overlay.repl.enable_history,
            enable_completion: overlay.repl.enable_completion,
            enable_colors: overlay.repl.enable_colors,
            show_timing: overlay.repl.show_timing,
            page_size: overlay.repl.page_size,
            enable_paging: overlay.repl.enable_paging,
            max_history_size: overlay.repl.max_history_size,
            prompt: overlay.repl.prompt,
            prompt_continuation: overlay.repl.prompt_continuation,
            history_file: overlay.repl.history_file.or(base.repl.history_file),
        },
        performance: overlay.performance,
        logging: overlay.logging,

        // Legacy fields (backward compat)
        enable_history: overlay.enable_history.or(base.enable_history),
        enable_completion: overlay.enable_completion.or(base.enable_completion),
        show_timing: overlay.show_timing.or(base.show_timing),
        page_size: overlay.page_size.or(base.page_size),
        enable_paging: overlay.enable_paging.or(base.enable_paging),
        no_color: final_no_color,

        // Skip serialization fields
        execution_query: base.execution_query,
        execution_file: base.execution_file,
        cassandra_version: overlay.cassandra_version.or(base.cassandra_version),
        resolved_version: base.resolved_version,

        // Deprecated - keep base
        default_database: base.default_database,
    }
}

/// Builder for Config with precedence: flags > env > file > defaults
pub struct ConfigBuilder {
    config: Config,
}

impl ConfigBuilder {
    /// Start with default configuration
    pub fn from_defaults() -> Self {
        Self {
            config: Config::default(),
        }
    }

    /// Layer config file (overrides defaults)
    ///
    /// Deprecated: Use `with_explicit_config()` instead for --config flag handling
    #[allow(dead_code)]
    pub fn with_file(mut self, path: Option<PathBuf>) -> Result<Self> {
        if let Some(p) = path {
            let loaded = Config::load_from_file(&p)?;
            // Merge loaded config, preserving defaults for unset fields
            self.config = loaded;
        }
        Ok(self)
    }

    /// Layer user config (overrides defaults)
    pub fn with_user_config(mut self) -> Result<Self> {
        if let Some(user_path) = Self::user_config_path() {
            if user_path.exists() {
                let loaded = Config::load_from_file(&user_path).with_context(|| {
                    format!("Failed to load user config: {}", user_path.display())
                })?;
                self.config = merge_partial_config(self.config, loaded);
            }
        }
        Ok(self)
    }

    /// Layer project config (overrides user config and defaults)
    pub fn with_project_config(mut self) -> Result<Self> {
        let project_path = PathBuf::from("./.cqlite.toml");
        if project_path.exists() {
            let loaded = Config::load_from_file(&project_path)
                .with_context(|| "Failed to load project config")?;
            self.config = merge_partial_config(self.config, loaded);
        }
        Ok(self)
    }

    /// Layer explicit config from --config flag (overrides discovered configs)
    pub fn with_explicit_config(mut self, path: PathBuf) -> Result<Self> {
        let loaded = Config::load_from_file(&path)
            .with_context(|| format!("Failed to load config file: {}", path.display()))?;
        self.config = merge_partial_config(self.config, loaded);
        Ok(self)
    }

    /// Get platform-specific user config path
    fn user_config_path() -> Option<PathBuf> {
        #[cfg(target_os = "macos")]
        {
            dirs::home_dir().map(|h| h.join("Library/Application Support/cqlite/config.toml"))
        }
        #[cfg(target_os = "windows")]
        {
            dirs::config_dir().map(|d| d.join("cqlite").join("config.toml"))
        }
        #[cfg(not(any(target_os = "macos", target_os = "windows")))]
        {
            std::env::var("XDG_CONFIG_HOME")
                .ok()
                .map(|p| PathBuf::from(p).join("cqlite/config.toml"))
                .or_else(|| dirs::home_dir().map(|h| h.join(".config/cqlite/config.toml")))
        }
    }

    /// Layer environment variables (overrides file and defaults)
    pub fn with_env(mut self) -> Result<Self> {
        use std::env;

        // CQLITE_DATA_DIR
        if let Ok(val) = env::var("CQLITE_DATA_DIR") {
            self.config.data_directory = Some(PathBuf::from(val));
        }

        // CQLITE_SCHEMA (can be comma-separated paths)
        if let Ok(val) = env::var("CQLITE_SCHEMA") {
            let paths: Vec<PathBuf> = val.split(',').map(|s| PathBuf::from(s.trim())).collect();
            self.config.schema_paths = paths; // Replace, not extend (Issue #126)
        }

        // CQLITE_LIMIT
        if let Ok(val) = env::var("CQLITE_LIMIT") {
            let limit: usize = val.parse().with_context(|| "Invalid CQLITE_LIMIT value")?;
            if limit == 0 {
                return Err(anyhow::anyhow!("CQLITE_LIMIT must be greater than 0"));
            }
            self.config.query_limit = Some(limit);
        }

        // CQLITE_PAGE_SIZE
        if let Ok(val) = env::var("CQLITE_PAGE_SIZE") {
            let page_size: usize = val
                .parse()
                .with_context(|| "Invalid CQLITE_PAGE_SIZE value")?;
            if page_size == 0 {
                return Err(anyhow::anyhow!("CQLITE_PAGE_SIZE must be greater than 0"));
            }
            self.config.repl.page_size = page_size;
        }

        // CQLITE_NO_COLOR
        if let Ok(val) = env::var("CQLITE_NO_COLOR") {
            let no_color = matches!(val.to_lowercase().as_str(), "1" | "true" | "yes" | "on");
            self.config.no_color = no_color;
            self.config.output.colors = !no_color;
        }

        // CQLITE_OUT
        if let Ok(val) = env::var("CQLITE_OUT") {
            self.config.output_mode = Some(val);
        }

        Ok(self)
    }

    /// Layer CLI flags (highest precedence)
    ///
    /// Note: CLI flags completely override environment variables and config file
    /// values for the same setting. For example, --schema replaces CQLITE_SCHEMA
    /// entirely rather than merging paths. This ensures clear precedence semantics.
    pub fn with_flags(mut self, cli: &crate::cli_types::Cli) -> Self {
        // Schema path
        if let Some(ref schema) = cli.schema {
            self.config.schema_paths = vec![schema.clone()];
        }

        // Data directory
        if let Some(ref data_dir) = cli.data_dir {
            self.config.data_directory = Some(data_dir.clone());
        }

        // Execute query
        if let Some(ref query) = cli.execute {
            self.config.execution_query = Some(query.clone());
        }

        // Execute file
        if let Some(ref file) = cli.file {
            self.config.execution_file = Some(file.clone());
        }

        // Output mode
        if let Some(ref out) = cli.out {
            self.config.output_mode = Some(out.as_str().to_string());
        }

        // Limit
        if let Some(limit) = cli.limit {
            self.config.query_limit = Some(limit);
        }

        // Page size
        if let Some(page_size) = cli.page_size {
            self.config.repl.page_size = page_size;
        }

        // No color
        if cli.no_color {
            self.config.no_color = true;
            self.config.output.colors = false;
        }

        // Cassandra version hint (Issue #130)
        if let Some(ref version) = cli.cassandra_version {
            self.config.cassandra_version = Some(version.clone());
        }

        self
    }

    /// Build final configuration
    pub fn build(self) -> Config {
        self.config
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::cli_types::Cli;
    use clap::Parser;
    use serial_test::serial;
    use std::sync::Arc;
    use tempfile::TempDir;

    #[tokio::test]
    async fn test_cassandra_version_flag_passed_to_config() {
        // Parse CLI args with cassandra_version flag
        let cli = Cli::parse_from(&[
            "cqlite",
            "--cassandra-version",
            "5.0",
            "--data-dir",
            "/tmp/data",
        ]);

        // Build config
        let config = Config::load(None, &cli).unwrap();

        // Verify cassandra_version was captured
        assert_eq!(config.cassandra_version, Some("5.0".to_string()));
    }

    #[tokio::test]
    async fn test_version_resolution_user_override() {
        let temp_dir = TempDir::new().unwrap();

        // Create CLI with user override
        let cli = Cli::parse_from(&[
            "cqlite",
            "--cassandra-version",
            "5.0",
            "--data-dir",
            temp_dir.path().to_str().unwrap(),
        ]);

        // Build config
        let mut config = Config::load(None, &cli).unwrap();

        // Initialize platform and resolve version
        let core_config = cqlite_core::Config::default();
        let platform = Arc::new(cqlite_core::Platform::new(&core_config).await.unwrap());

        config.resolve_version(platform).await.unwrap();

        // Verify user override takes precedence
        let version_info = config.version_info().unwrap();
        assert_eq!(
            version_info.source,
            cqlite_core::version_hints::VersionSource::UserFlag
        );
        assert_eq!(version_info.version, Some("5.0".to_string()));
    }

    #[tokio::test]
    async fn test_version_resolution_metadata_yml() {
        let temp_dir = TempDir::new().unwrap();

        // Create metadata.yml with version
        let metadata_content = "cassandra_version: \"4.0\"\nkeyspaces: []\n";
        let metadata_path = temp_dir.path().join("metadata.yml");
        std::fs::write(&metadata_path, metadata_content).unwrap();

        // Create CLI without user override
        let cli = Cli::parse_from(&["cqlite", "--data-dir", temp_dir.path().to_str().unwrap()]);

        // Build config
        let mut config = Config::load(None, &cli).unwrap();

        // Initialize platform and resolve version
        let core_config = cqlite_core::Config::default();
        let platform = Arc::new(cqlite_core::Platform::new(&core_config).await.unwrap());

        config.resolve_version(platform).await.unwrap();

        // Verify metadata.yml was used
        let version_info = config.version_info().unwrap();
        assert_eq!(
            version_info.source,
            cqlite_core::version_hints::VersionSource::DatasetMetadata
        );
        assert_eq!(version_info.version, Some("4.0".to_string()));
    }

    #[tokio::test]
    async fn test_version_resolution_unknown() {
        let temp_dir = TempDir::new().unwrap();

        // Create CLI without version and no metadata.yml
        let cli = Cli::parse_from(&["cqlite", "--data-dir", temp_dir.path().to_str().unwrap()]);

        // Build config
        let mut config = Config::load(None, &cli).unwrap();

        // Initialize platform and resolve version
        let core_config = cqlite_core::Config::default();
        let platform = Arc::new(cqlite_core::Platform::new(&core_config).await.unwrap());

        config.resolve_version(platform).await.unwrap();

        // Verify unknown fallback
        let version_info = config.version_info().unwrap();
        assert_eq!(
            version_info.source,
            cqlite_core::version_hints::VersionSource::Unknown
        );
        assert_eq!(version_info.version, None);
        assert_eq!(version_info.version_or_unknown(), "unknown");
    }

    #[tokio::test]
    async fn test_version_precedence_user_overrides_metadata() {
        let temp_dir = TempDir::new().unwrap();

        // Create metadata.yml with version 4.0
        let metadata_content = "cassandra_version: \"4.0\"\nkeyspaces: []\n";
        let metadata_path = temp_dir.path().join("metadata.yml");
        std::fs::write(&metadata_path, metadata_content).unwrap();

        // Create CLI with user override 5.0
        let cli = Cli::parse_from(&[
            "cqlite",
            "--cassandra-version",
            "5.0",
            "--data-dir",
            temp_dir.path().to_str().unwrap(),
        ]);

        // Build config
        let mut config = Config::load(None, &cli).unwrap();

        // Initialize platform and resolve version
        let core_config = cqlite_core::Config::default();
        let platform = Arc::new(cqlite_core::Platform::new(&core_config).await.unwrap());

        config.resolve_version(platform).await.unwrap();

        // Verify user override takes precedence over metadata.yml
        let version_info = config.version_info().unwrap();
        assert_eq!(
            version_info.source,
            cqlite_core::version_hints::VersionSource::UserFlag
        );
        assert_eq!(version_info.version, Some("5.0".to_string()));
    }

    #[tokio::test]
    async fn test_version_string_helpers() {
        let temp_dir = TempDir::new().unwrap();

        // Create CLI with version
        let cli = Cli::parse_from(&[
            "cqlite",
            "--cassandra-version",
            "5.0",
            "--data-dir",
            temp_dir.path().to_str().unwrap(),
        ]);

        // Build config
        let mut config = Config::load(None, &cli).unwrap();

        // Before resolution
        assert_eq!(config.version_string(), "not resolved");
        assert_eq!(config.version_source(), "not resolved");

        // After resolution
        let core_config = cqlite_core::Config::default();
        let platform = Arc::new(cqlite_core::Platform::new(&core_config).await.unwrap());
        config.resolve_version(platform).await.unwrap();

        assert_eq!(config.version_string(), "5.0");
        assert!(config.version_source().contains("User-provided flag"));
    }

    #[test]
    fn test_config_default_includes_version_fields() {
        let config = Config::default();
        assert_eq!(config.cassandra_version, None);
        assert_eq!(config.resolved_version, None);
    }

    #[test]
    fn test_config_builder_preserves_version_flag() {
        let cli = Cli::parse_from(&["cqlite", "--cassandra-version", "4.0"]);

        let config = ConfigBuilder::from_defaults().with_flags(&cli).build();

        assert_eq!(config.cassandra_version, Some("4.0".to_string()));
    }

    #[test]
    #[serial]
    fn test_env_var_replaces_config_file_schema_paths() {
        use std::env;

        // Set up environment variable
        env::set_var("CQLITE_SCHEMA", "/env/path1,/env/path2");

        // Create config with file-based schema paths
        let mut config = Config::default();
        config.schema_paths = vec![PathBuf::from("/file/path1"), PathBuf::from("/file/path2")];

        // Apply env vars
        let builder = ConfigBuilder { config };
        let result = builder.with_env().unwrap();

        // Verify env var REPLACED file paths, not extended
        assert_eq!(result.config.schema_paths.len(), 2);
        assert_eq!(result.config.schema_paths[0], PathBuf::from("/env/path1"));
        assert_eq!(result.config.schema_paths[1], PathBuf::from("/env/path2"));

        // Clean up
        env::remove_var("CQLITE_SCHEMA");
    }

    #[test]
    #[serial]
    fn test_env_var_single_schema_path_replaces_multiple() {
        use std::env;

        // Set up environment variable with single path
        env::set_var("CQLITE_SCHEMA", "/env/single/path");

        // Create config with multiple file-based schema paths
        let mut config = Config::default();
        config.schema_paths = vec![
            PathBuf::from("/file/path1"),
            PathBuf::from("/file/path2"),
            PathBuf::from("/file/path3"),
        ];

        // Apply env vars
        let builder = ConfigBuilder { config };
        let result = builder.with_env().unwrap();

        // Verify single env path replaced all file paths
        assert_eq!(result.config.schema_paths.len(), 1);
        assert_eq!(
            result.config.schema_paths[0],
            PathBuf::from("/env/single/path")
        );

        // Clean up
        env::remove_var("CQLITE_SCHEMA");
    }

    #[test]
    #[serial]
    fn test_cli_flag_overrides_env_var_schema() {
        use std::env;

        // Set up environment variable
        env::set_var("CQLITE_SCHEMA", "/env/path1,/env/path2");

        // Create config with file paths and apply env
        let mut config = Config::default();
        config.schema_paths = vec![PathBuf::from("/file/path")];

        let builder = ConfigBuilder { config };
        let result = builder.with_env().unwrap();

        // At this point, env var should have replaced file paths
        assert_eq!(result.config.schema_paths.len(), 2);

        // Now apply CLI flag
        let cli = Cli::parse_from(&["cqlite", "--schema", "/cli/path"]);
        let final_config = result.with_flags(&cli).build();

        // Verify CLI flag replaced everything
        assert_eq!(final_config.schema_paths.len(), 1);
        assert_eq!(final_config.schema_paths[0], PathBuf::from("/cli/path"));

        // Clean up
        env::remove_var("CQLITE_SCHEMA");
    }

    #[test]
    #[serial]
    fn test_schema_precedence_chain_complete() {
        use std::env;

        // Test: file < env < CLI flag

        // Start with file-based config
        let mut config = Config::default();
        config.schema_paths = vec![PathBuf::from("/file/path")];

        // Apply env var (should replace file)
        env::set_var("CQLITE_SCHEMA", "/env/path");
        let builder = ConfigBuilder { config };
        let with_env = builder.with_env().unwrap();
        assert_eq!(
            with_env.config.schema_paths,
            vec![PathBuf::from("/env/path")]
        );

        // Apply CLI flag (should replace env)
        let cli = Cli::parse_from(&["cqlite", "--schema", "/cli/path"]);
        let final_config = with_env.with_flags(&cli).build();
        assert_eq!(final_config.schema_paths, vec![PathBuf::from("/cli/path")]);

        // Clean up
        env::remove_var("CQLITE_SCHEMA");
    }

    #[test]
    #[serial]
    fn test_no_env_var_preserves_file_schema() {
        use std::env;

        // Make sure env var is NOT set
        env::remove_var("CQLITE_SCHEMA");

        // Create config with file-based schema paths
        let mut config = Config::default();
        config.schema_paths = vec![PathBuf::from("/file/path1"), PathBuf::from("/file/path2")];

        // Apply env vars (should not change anything)
        let builder = ConfigBuilder { config };
        let result = builder.with_env().unwrap();

        // Verify file paths are preserved
        assert_eq!(result.config.schema_paths.len(), 2);
        assert_eq!(result.config.schema_paths[0], PathBuf::from("/file/path1"));
        assert_eq!(result.config.schema_paths[1], PathBuf::from("/file/path2"));
    }

    #[test]
    #[serial]
    fn test_env_var_with_whitespace_trimming() {
        use std::env;

        // Set up environment variable with whitespace
        env::set_var("CQLITE_SCHEMA", " /path1 , /path2 , /path3 ");

        // Create config
        let config = Config::default();

        // Apply env vars
        let builder = ConfigBuilder { config };
        let result = builder.with_env().unwrap();

        // Verify paths are trimmed
        assert_eq!(result.config.schema_paths.len(), 3);
        assert_eq!(result.config.schema_paths[0], PathBuf::from("/path1"));
        assert_eq!(result.config.schema_paths[1], PathBuf::from("/path2"));
        assert_eq!(result.config.schema_paths[2], PathBuf::from("/path3"));

        // Clean up
        env::remove_var("CQLITE_SCHEMA");
    }

    // OutputConfig precedence chain tests (Issue #118)

    #[test]
    #[serial]
    fn test_output_config_uses_defaults_when_no_flags_or_env() {
        use std::env;

        // Ensure no env vars are set
        env::remove_var("CQLITE_LIMIT");
        env::remove_var("CQLITE_PAGE_SIZE");
        env::remove_var("CQLITE_NO_COLOR");

        // Create CLI with no flags
        let cli = Cli::parse_from(&["cqlite"]);

        // Build config (should have defaults)
        let config = Config::load(None, &cli).unwrap();

        // Create OutputConfig with no CLI flags
        let output = OutputConfig::from_cli(&config, false, None, None, None, false);

        // Verify defaults
        assert!(output.color_enabled); // Default is true
        assert_eq!(output.limit, None); // Default is None
        assert_eq!(output.page_size, Some(50)); // Default is 50
    }

    #[test]
    #[serial]
    fn test_output_config_env_vars_override_defaults() {
        use std::env;

        // Set env vars
        env::set_var("CQLITE_LIMIT", "100");
        env::set_var("CQLITE_PAGE_SIZE", "25");
        env::set_var("CQLITE_NO_COLOR", "true");

        // Create CLI with no flags
        let cli = Cli::parse_from(&["cqlite"]);

        // Build config (should pick up env vars)
        let config = Config::load(None, &cli).unwrap();

        // Create OutputConfig with no CLI flags
        let output = OutputConfig::from_cli(&config, false, None, None, None, false);

        // Verify env vars were used
        assert!(!output.color_enabled); // CQLITE_NO_COLOR=true
        assert_eq!(output.limit, Some(100)); // CQLITE_LIMIT=100
        assert_eq!(output.page_size, Some(25)); // CQLITE_PAGE_SIZE=25

        // Clean up
        env::remove_var("CQLITE_LIMIT");
        env::remove_var("CQLITE_PAGE_SIZE");
        env::remove_var("CQLITE_NO_COLOR");
    }

    #[test]
    #[serial]
    fn test_output_config_cli_flags_override_env_vars() {
        use std::env;

        // Set env vars
        env::set_var("CQLITE_LIMIT", "100");
        env::set_var("CQLITE_PAGE_SIZE", "25");
        env::set_var("CQLITE_NO_COLOR", "false");

        // Create CLI with flags (should override env)
        let cli = Cli::parse_from(&[
            "cqlite",
            "--limit",
            "200",
            "--page-size",
            "10",
            "--no-color",
        ]);

        // Build config
        let config = Config::load(None, &cli).unwrap();

        // Create OutputConfig with CLI flags
        let output = OutputConfig::from_cli(
            &config,
            cli.no_color,
            cli.limit,
            cli.page_size,
            cli.output.clone(),
            cli.overwrite,
        );

        // Verify CLI flags overrode env vars
        assert!(!output.color_enabled); // --no-color flag
        assert_eq!(output.limit, Some(200)); // --limit 200
        assert_eq!(output.page_size, Some(10)); // --page-size 10

        // Clean up
        env::remove_var("CQLITE_LIMIT");
        env::remove_var("CQLITE_PAGE_SIZE");
        env::remove_var("CQLITE_NO_COLOR");
    }

    #[test]
    #[serial]
    fn test_output_config_partial_cli_flags_preserve_env_vars() {
        use std::env;

        // Set env vars
        env::set_var("CQLITE_LIMIT", "100");
        env::set_var("CQLITE_PAGE_SIZE", "25");

        // Create CLI with only --no-color flag
        let cli = Cli::parse_from(&["cqlite", "--no-color"]);

        // Build config
        let config = Config::load(None, &cli).unwrap();

        // Create OutputConfig with partial CLI flags
        let output = OutputConfig::from_cli(
            &config,
            cli.no_color,
            cli.limit,
            cli.page_size,
            cli.output.clone(),
            cli.overwrite,
        );

        // Verify: --no-color overrides, but env vars are used for limit/page_size
        assert!(!output.color_enabled); // --no-color flag
        assert_eq!(output.limit, Some(100)); // From CQLITE_LIMIT env
        assert_eq!(output.page_size, Some(25)); // From CQLITE_PAGE_SIZE env

        // Clean up
        env::remove_var("CQLITE_LIMIT");
        env::remove_var("CQLITE_PAGE_SIZE");
    }

    #[test]
    #[serial]
    fn test_output_config_no_color_flag_false_preserves_env() {
        use std::env;

        // Set env var to disable colors
        env::set_var("CQLITE_NO_COLOR", "true");

        // Create CLI without --no-color flag
        let cli = Cli::parse_from(&["cqlite"]);

        // Build config (should pick up env var)
        let config = Config::load(None, &cli).unwrap();

        // Create OutputConfig with no_color_flag=false (no flag provided)
        let output = OutputConfig::from_cli(&config, false, None, None, None, false);

        // Verify env var was respected
        assert!(!output.color_enabled); // CQLITE_NO_COLOR=true from env

        // Clean up
        env::remove_var("CQLITE_NO_COLOR");
    }

    #[test]
    #[serial]
    fn test_output_config_complete_precedence_chain() {
        use std::env;

        // Test the complete chain: flags > env > defaults

        // Step 1: Defaults only
        env::remove_var("CQLITE_LIMIT");
        env::remove_var("CQLITE_PAGE_SIZE");
        env::remove_var("CQLITE_NO_COLOR");

        let cli = Cli::parse_from(&["cqlite"]);
        let config = Config::load(None, &cli).unwrap();
        let output = OutputConfig::from_cli(&config, false, None, None, None, false);

        assert!(output.color_enabled);
        assert_eq!(output.limit, None);
        assert_eq!(output.page_size, Some(50));

        // Step 2: Env vars override defaults
        env::set_var("CQLITE_LIMIT", "150");
        env::set_var("CQLITE_PAGE_SIZE", "30");
        env::set_var("CQLITE_NO_COLOR", "true");

        let cli = Cli::parse_from(&["cqlite"]);
        let config = Config::load(None, &cli).unwrap();
        let output = OutputConfig::from_cli(&config, false, None, None, None, false);

        assert!(!output.color_enabled);
        assert_eq!(output.limit, Some(150));
        assert_eq!(output.page_size, Some(30));

        // Step 3: CLI flags override env vars
        let cli = Cli::parse_from(&["cqlite", "--limit", "300", "--page-size", "15"]);
        let config = Config::load(None, &cli).unwrap();
        let output = OutputConfig::from_cli(
            &config,
            cli.no_color,
            cli.limit,
            cli.page_size,
            cli.output.clone(),
            cli.overwrite,
        );

        // CLI flags override env, but --no-color not provided so env var still applies
        assert!(!output.color_enabled); // Still from CQLITE_NO_COLOR env
        assert_eq!(output.limit, Some(300)); // From --limit flag
        assert_eq!(output.page_size, Some(15)); // From --page-size flag

        // Clean up
        env::remove_var("CQLITE_LIMIT");
        env::remove_var("CQLITE_PAGE_SIZE");
        env::remove_var("CQLITE_NO_COLOR");
    }
}