voirs-sdk 0.1.0-rc.1

Unified SDK and public API for VoiRS speech synthesis
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
//! Configuration hierarchy and inheritance system.
//!
//! This module handles the hierarchical structure of configurations, allowing
//! for composition, inheritance, and override mechanisms. It supports:
//!
//! - Configuration merging and inheritance
//! - Default value resolution
//! - Nested configuration structures
//! - Configuration profiles and environments
//! - Override and fallback mechanisms

use crate::types::{AudioFormat, QualityLevel, SynthesisConfig};
use serde::{Deserialize, Serialize};
use std::{collections::HashMap, path::PathBuf};

/// Trait for configuration types that support hierarchical composition
pub trait ConfigHierarchy: Clone + Default {
    /// Merge this configuration with another, giving priority to the other
    fn merge_with(&mut self, other: &Self);

    /// Create a configuration that inherits from a parent
    fn inherit_from(parent: &Self) -> Self {
        let mut config = parent.clone();
        config.merge_with(&Self::default());
        config
    }

    /// Validate the configuration and return any errors
    fn validate(&self) -> Result<(), ConfigValidationError> {
        Ok(())
    }
}

/// Configuration validation error
#[derive(Debug, Clone)]
pub struct ConfigValidationError {
    pub field: String,
    pub message: String,
}

impl std::fmt::Display for ConfigValidationError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(
            f,
            "Configuration validation error in '{}': {}",
            self.field, self.message
        )
    }
}

impl std::error::Error for ConfigValidationError {}

/// Main pipeline configuration with hierarchical support
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct PipelineConfig {
    /// Device to use for computation (cpu, cuda, metal, etc.)
    pub device: String,

    /// Enable GPU acceleration if available
    pub use_gpu: bool,

    /// Number of threads for CPU computation
    pub num_threads: Option<usize>,

    /// Cache directory for models and temporary files
    pub cache_dir: Option<PathBuf>,

    /// Maximum cache size in MB
    pub max_cache_size_mb: u32,

    /// Default synthesis configuration
    pub default_synthesis: SynthesisConfig,

    /// Model loading configuration
    pub model_loading: ModelLoadingConfig,

    /// Audio processing configuration
    pub audio_processing: AudioProcessingConfig,

    /// Logging configuration
    pub logging: LoggingConfig,

    /// Configuration profile name (for environment-specific configs)
    pub profile: Option<String>,

    /// Parent configuration to inherit from
    pub inherits_from: Option<String>,

    /// G2P (Grapheme-to-Phoneme) model to use
    pub g2p_model: Option<String>,

    /// Acoustic model to use
    pub acoustic_model: Option<String>,

    /// Vocoder model to use
    pub vocoder_model: Option<String>,

    /// Language code for G2P processing
    pub language_code: Option<crate::types::LanguageCode>,
}

impl Default for PipelineConfig {
    fn default() -> Self {
        Self {
            device: "cpu".to_string(),
            use_gpu: false,
            num_threads: None,
            cache_dir: None,
            max_cache_size_mb: 1024,
            default_synthesis: SynthesisConfig::default(),
            model_loading: ModelLoadingConfig::default(),
            audio_processing: AudioProcessingConfig::default(),
            logging: LoggingConfig::default(),
            profile: None,
            inherits_from: None,
            g2p_model: None,
            acoustic_model: None,
            vocoder_model: None,
            language_code: None,
        }
    }
}

impl ConfigHierarchy for PipelineConfig {
    fn merge_with(&mut self, other: &Self) {
        // Merge primitive fields (other takes precedence if not default)
        if other.device != "cpu" {
            self.device = other.device.clone();
        }
        if other.use_gpu {
            self.use_gpu = other.use_gpu;
        }
        if other.num_threads.is_some() {
            self.num_threads = other.num_threads;
        }
        if other.cache_dir.is_some() {
            self.cache_dir = other.cache_dir.clone();
        }
        if other.max_cache_size_mb != 1024 {
            self.max_cache_size_mb = other.max_cache_size_mb;
        }
        if other.profile.is_some() {
            self.profile = other.profile.clone();
        }
        if other.inherits_from.is_some() {
            self.inherits_from = other.inherits_from.clone();
        }
        if other.g2p_model.is_some() {
            self.g2p_model = other.g2p_model.clone();
        }
        if other.acoustic_model.is_some() {
            self.acoustic_model = other.acoustic_model.clone();
        }
        if other.vocoder_model.is_some() {
            self.vocoder_model = other.vocoder_model.clone();
        }
        if other.language_code.is_some() {
            self.language_code = other.language_code;
        }

        // Merge nested configurations
        self.default_synthesis.merge_with(&other.default_synthesis);
        self.model_loading.merge_with(&other.model_loading);
        self.audio_processing.merge_with(&other.audio_processing);
        self.logging.merge_with(&other.logging);
    }

    fn validate(&self) -> Result<(), ConfigValidationError> {
        // Validate device
        let valid_devices = ["cpu", "cuda", "metal", "vulkan", "opencl"];
        if !valid_devices.contains(&self.device.as_str()) {
            return Err(ConfigValidationError {
                field: "device".to_string(),
                message: format!(
                    "Invalid device '{}'. Must be one of: {}",
                    self.device,
                    valid_devices.join(", ")
                ),
            });
        }

        // Validate thread count
        if let Some(threads) = self.num_threads {
            if threads == 0 || threads > 256 {
                return Err(ConfigValidationError {
                    field: "num_threads".to_string(),
                    message: "Thread count must be between 1 and 256".to_string(),
                });
            }
        }

        // Validate cache size
        if self.max_cache_size_mb > 100_000 {
            return Err(ConfigValidationError {
                field: "max_cache_size_mb".to_string(),
                message: "Cache size must be less than 100GB".to_string(),
            });
        }

        // Validate nested configurations
        self.default_synthesis.validate()?;
        self.model_loading.validate()?;
        self.audio_processing.validate()?;
        self.logging.validate()?;

        Ok(())
    }
}

impl PipelineConfig {
    /// Create new configuration with defaults
    pub fn new() -> Self {
        Self::default()
    }

    /// Create configuration for a specific profile
    #[allow(clippy::field_reassign_with_default)]
    pub fn for_profile(profile: &str) -> Self {
        let mut config = Self::default();
        config.profile = Some(profile.to_string());

        match profile {
            "development" => {
                config.logging.level = "debug".to_string();
                config.logging.metrics = true;
                config.model_loading.auto_download = true;
            }
            "production" => {
                config.logging.level = "warn".to_string();
                config.logging.structured = true;
                config.model_loading.verify_checksums = true;
                config.model_loading.preload_models = true;
            }
            "testing" => {
                config.max_cache_size_mb = 256;
                config.logging.level = "error".to_string();
                config.model_loading.auto_download = false;
            }
            "high_performance" => {
                config.use_gpu = true;
                config.device = "cuda".to_string();
                config.audio_processing.buffer_size = 16384;
                config.default_synthesis.quality = QualityLevel::Ultra;
            }
            "low_latency" => {
                config.audio_processing.buffer_size = 2048;
                config.default_synthesis.quality = QualityLevel::Medium;
                config.model_loading.preload_models = true;
            }
            _ => {}
        }

        config
    }

    /// Get effective cache directory
    pub fn effective_cache_dir(&self) -> PathBuf {
        self.cache_dir
            .clone()
            .unwrap_or_else(|| std::env::temp_dir().join("voirs-cache"))
    }

    /// Get effective thread count
    pub fn effective_thread_count(&self) -> usize {
        self.num_threads.unwrap_or_else(|| {
            std::thread::available_parallelism()
                .map(|n| n.get())
                .unwrap_or(4)
        })
    }

    /// Check if GPU is available and enabled
    pub fn should_use_gpu(&self) -> bool {
        self.use_gpu && self.device != "cpu"
    }

    /// Set speaking speed (fluent method)
    pub fn speed(mut self, speed: f32) -> Self {
        self.default_synthesis.speaking_rate = speed;
        self
    }

    /// Set pitch shift (fluent method)
    pub fn pitch(mut self, pitch: f32) -> Self {
        self.default_synthesis.pitch_shift = pitch;
        self
    }

    /// Set volume gain (fluent method)
    pub fn volume(mut self, volume: f32) -> Self {
        self.default_synthesis.volume_gain = volume;
        self
    }

    /// Set sample rate (fluent method)
    pub fn sample_rate(mut self, sample_rate: u32) -> Self {
        self.default_synthesis.sample_rate = sample_rate;
        self
    }
}

/// Model loading configuration
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct ModelLoadingConfig {
    /// Timeout for model downloads in seconds
    pub download_timeout_secs: u64,

    /// Number of download retries
    pub download_retries: u32,

    /// Base URL for model downloads
    pub download_base_url: Option<String>,

    /// Automatically download missing models
    pub auto_download: bool,

    /// Verify model checksums
    pub verify_checksums: bool,

    /// Model repositories to use
    pub repositories: Vec<String>,

    /// Preload models on startup
    pub preload_models: bool,

    /// Model-specific overrides
    pub model_overrides: HashMap<String, ModelOverride>,
}

impl Default for ModelLoadingConfig {
    fn default() -> Self {
        Self {
            download_timeout_secs: 300,
            download_retries: 3,
            download_base_url: None,
            auto_download: true,
            verify_checksums: true,
            repositories: vec![
                "https://huggingface.co/voirs/models".to_string(),
                "https://github.com/cool-japan/voirs-models".to_string(),
            ],
            preload_models: false,
            model_overrides: HashMap::new(),
        }
    }
}

impl ConfigHierarchy for ModelLoadingConfig {
    fn merge_with(&mut self, other: &Self) {
        if other.download_timeout_secs != 300 {
            self.download_timeout_secs = other.download_timeout_secs;
        }
        if other.download_retries != 3 {
            self.download_retries = other.download_retries;
        }
        if other.download_base_url.is_some() {
            self.download_base_url = other.download_base_url.clone();
        }
        if !other.auto_download {
            self.auto_download = other.auto_download;
        }
        if !other.verify_checksums {
            self.verify_checksums = other.verify_checksums;
        }
        if other.preload_models {
            self.preload_models = other.preload_models;
        }

        // Merge repositories (union)
        for repo in &other.repositories {
            if !self.repositories.contains(repo) {
                self.repositories.push(repo.clone());
            }
        }

        // Merge model overrides
        for (key, value) in &other.model_overrides {
            self.model_overrides.insert(key.clone(), value.clone());
        }
    }

    fn validate(&self) -> Result<(), ConfigValidationError> {
        if self.download_timeout_secs > 3600 {
            return Err(ConfigValidationError {
                field: "download_timeout_secs".to_string(),
                message: "Download timeout must be less than 1 hour".to_string(),
            });
        }

        if self.download_retries > 10 {
            return Err(ConfigValidationError {
                field: "download_retries".to_string(),
                message: "Download retries must be 10 or less".to_string(),
            });
        }

        if self.repositories.is_empty() {
            return Err(ConfigValidationError {
                field: "repositories".to_string(),
                message: "At least one repository must be configured".to_string(),
            });
        }

        Ok(())
    }
}

/// Model-specific configuration override
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct ModelOverride {
    /// Custom download URL
    pub url: Option<String>,

    /// Expected checksum
    pub checksum: Option<String>,

    /// Local path override
    pub local_path: Option<PathBuf>,

    /// Priority for this model
    pub priority: Option<u32>,
}

/// Audio processing configuration
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct AudioProcessingConfig {
    /// Buffer size for audio processing
    pub buffer_size: usize,

    /// Enable audio enhancement by default
    pub enable_enhancement: bool,

    /// Normalization target level (0.0-1.0)
    pub normalization_level: f32,

    /// Apply noise reduction
    pub noise_reduction: bool,

    /// Dynamic range compression
    pub compression: bool,

    /// High-pass filter frequency (Hz)
    pub highpass_freq: Option<f32>,

    /// Low-pass filter frequency (Hz)
    pub lowpass_freq: Option<f32>,

    /// Effect chain configuration
    pub effects: Vec<EffectConfig>,
}

impl Default for AudioProcessingConfig {
    fn default() -> Self {
        Self {
            buffer_size: 8192,
            enable_enhancement: true,
            normalization_level: 0.95,
            noise_reduction: false,
            compression: false,
            highpass_freq: None,
            lowpass_freq: None,
            effects: Vec::new(),
        }
    }
}

impl ConfigHierarchy for AudioProcessingConfig {
    fn merge_with(&mut self, other: &Self) {
        if other.buffer_size != 8192 {
            self.buffer_size = other.buffer_size;
        }
        if !other.enable_enhancement {
            self.enable_enhancement = other.enable_enhancement;
        }
        if (other.normalization_level - 0.95).abs() > f32::EPSILON {
            self.normalization_level = other.normalization_level;
        }
        if other.noise_reduction {
            self.noise_reduction = other.noise_reduction;
        }
        if other.compression {
            self.compression = other.compression;
        }
        if other.highpass_freq.is_some() {
            self.highpass_freq = other.highpass_freq;
        }
        if other.lowpass_freq.is_some() {
            self.lowpass_freq = other.lowpass_freq;
        }

        // Merge effects (append, allowing duplicates for complex chains)
        self.effects.extend(other.effects.clone());
    }

    fn validate(&self) -> Result<(), ConfigValidationError> {
        if self.buffer_size < 512 || self.buffer_size > 65536 {
            return Err(ConfigValidationError {
                field: "buffer_size".to_string(),
                message: "Buffer size must be between 512 and 65536".to_string(),
            });
        }

        if !self.buffer_size.is_power_of_two() {
            return Err(ConfigValidationError {
                field: "buffer_size".to_string(),
                message: "Buffer size must be a power of 2".to_string(),
            });
        }

        if self.normalization_level <= 0.0 || self.normalization_level > 1.0 {
            return Err(ConfigValidationError {
                field: "normalization_level".to_string(),
                message: "Normalization level must be between 0.0 and 1.0".to_string(),
            });
        }

        if let Some(freq) = self.highpass_freq {
            if freq <= 0.0 || freq > 20000.0 {
                return Err(ConfigValidationError {
                    field: "highpass_freq".to_string(),
                    message: "High-pass frequency must be between 0 and 20000 Hz".to_string(),
                });
            }
        }

        if let Some(freq) = self.lowpass_freq {
            if freq <= 0.0 || freq > 20000.0 {
                return Err(ConfigValidationError {
                    field: "lowpass_freq".to_string(),
                    message: "Low-pass frequency must be between 0 and 20000 Hz".to_string(),
                });
            }
        }

        Ok(())
    }
}

/// Audio effect configuration
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct EffectConfig {
    /// Effect type
    pub effect_type: String,

    /// Effect parameters
    pub parameters: HashMap<String, f32>,

    /// Whether effect is enabled
    pub enabled: bool,
}

/// Logging configuration
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct LoggingConfig {
    /// Log level (trace, debug, info, warn, error)
    pub level: String,

    /// Enable structured logging (JSON format)
    pub structured: bool,

    /// Log to file
    pub file_path: Option<PathBuf>,

    /// Log file rotation size in MB
    pub max_file_size_mb: u32,

    /// Number of log files to keep
    pub max_files: u32,

    /// Enable performance metrics logging
    pub metrics: bool,

    /// Module-specific log levels
    pub module_levels: HashMap<String, String>,
}

impl Default for LoggingConfig {
    fn default() -> Self {
        Self {
            level: "info".to_string(),
            structured: false,
            file_path: None,
            max_file_size_mb: 10,
            max_files: 5,
            metrics: false,
            module_levels: HashMap::new(),
        }
    }
}

impl ConfigHierarchy for LoggingConfig {
    fn merge_with(&mut self, other: &Self) {
        if other.level != "info" {
            self.level = other.level.clone();
        }
        if other.structured {
            self.structured = other.structured;
        }
        if other.file_path.is_some() {
            self.file_path = other.file_path.clone();
        }
        if other.max_file_size_mb != 10 {
            self.max_file_size_mb = other.max_file_size_mb;
        }
        if other.max_files != 5 {
            self.max_files = other.max_files;
        }
        if other.metrics {
            self.metrics = other.metrics;
        }

        // Merge module levels
        for (module, level) in &other.module_levels {
            self.module_levels.insert(module.clone(), level.clone());
        }
    }

    fn validate(&self) -> Result<(), ConfigValidationError> {
        let valid_levels = ["trace", "debug", "info", "warn", "error", "off"];
        if !valid_levels.contains(&self.level.as_str()) {
            return Err(ConfigValidationError {
                field: "level".to_string(),
                message: format!(
                    "Invalid log level '{}'. Must be one of: {}",
                    self.level,
                    valid_levels.join(", ")
                ),
            });
        }

        if self.max_file_size_mb == 0 || self.max_file_size_mb > 1000 {
            return Err(ConfigValidationError {
                field: "max_file_size_mb".to_string(),
                message: "Max file size must be between 1 and 1000 MB".to_string(),
            });
        }

        if self.max_files == 0 || self.max_files > 100 {
            return Err(ConfigValidationError {
                field: "max_files".to_string(),
                message: "Max files must be between 1 and 100".to_string(),
            });
        }

        // Validate module levels
        for (module, level) in &self.module_levels {
            if !valid_levels.contains(&level.as_str()) {
                return Err(ConfigValidationError {
                    field: format!("module_levels.{module}"),
                    message: format!("Invalid log level '{level}' for module '{module}'"),
                });
            }
        }

        Ok(())
    }
}

/// Application-wide configuration with hierarchy support
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Default)]
pub struct AppConfig {
    /// Pipeline configuration
    pub pipeline: PipelineConfig,

    /// CLI-specific configuration
    pub cli: CliConfig,

    /// Server configuration
    pub server: ServerConfig,

    /// Environment name
    pub environment: Option<String>,
}

impl ConfigHierarchy for AppConfig {
    fn merge_with(&mut self, other: &Self) {
        self.pipeline.merge_with(&other.pipeline);
        self.cli.merge_with(&other.cli);
        self.server.merge_with(&other.server);

        if other.environment.is_some() {
            self.environment = other.environment.clone();
        }
    }

    fn validate(&self) -> Result<(), ConfigValidationError> {
        self.pipeline.validate()?;
        self.cli.validate()?;
        self.server.validate()?;
        Ok(())
    }
}

/// CLI configuration
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct CliConfig {
    /// Default output directory
    pub output_dir: Option<PathBuf>,

    /// Default voice to use
    pub default_voice: Option<String>,

    /// Default output format
    pub default_format: AudioFormat,

    /// Show progress bars
    pub show_progress: bool,

    /// Colored output
    pub colored_output: bool,

    /// Command aliases
    pub aliases: HashMap<String, String>,
}

impl Default for CliConfig {
    fn default() -> Self {
        Self {
            output_dir: None,
            default_voice: None,
            default_format: AudioFormat::Wav,
            show_progress: true,
            colored_output: true,
            aliases: HashMap::new(),
        }
    }
}

impl ConfigHierarchy for CliConfig {
    fn merge_with(&mut self, other: &Self) {
        if other.output_dir.is_some() {
            self.output_dir = other.output_dir.clone();
        }
        if other.default_voice.is_some() {
            self.default_voice = other.default_voice.clone();
        }
        if other.default_format != AudioFormat::Wav {
            self.default_format = other.default_format;
        }
        if !other.show_progress {
            self.show_progress = other.show_progress;
        }
        if !other.colored_output {
            self.colored_output = other.colored_output;
        }

        // Merge aliases
        for (alias, command) in &other.aliases {
            self.aliases.insert(alias.clone(), command.clone());
        }
    }

    fn validate(&self) -> Result<(), ConfigValidationError> {
        // Validate output directory exists if specified
        if let Some(ref path) = self.output_dir {
            if path.exists() && !path.is_dir() {
                return Err(ConfigValidationError {
                    field: "output_dir".to_string(),
                    message: "Output directory path exists but is not a directory".to_string(),
                });
            }
        }

        Ok(())
    }
}

/// Server configuration
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct ServerConfig {
    /// Server bind address
    pub bind_address: String,

    /// Server port
    pub port: u16,

    /// Maximum concurrent requests
    pub max_concurrent_requests: usize,

    /// Request timeout in seconds
    pub request_timeout_secs: u64,

    /// Enable CORS
    pub enable_cors: bool,

    /// API key for authentication
    pub api_key: Option<String>,
}

impl Default for ServerConfig {
    fn default() -> Self {
        Self {
            bind_address: "127.0.0.1".to_string(),
            port: 8080,
            max_concurrent_requests: 100,
            request_timeout_secs: 30,
            enable_cors: true,
            api_key: None,
        }
    }
}

impl ConfigHierarchy for ServerConfig {
    fn merge_with(&mut self, other: &Self) {
        if other.bind_address != "127.0.0.1" {
            self.bind_address = other.bind_address.clone();
        }
        if other.port != 8080 {
            self.port = other.port;
        }
        if other.max_concurrent_requests != 100 {
            self.max_concurrent_requests = other.max_concurrent_requests;
        }
        if other.request_timeout_secs != 30 {
            self.request_timeout_secs = other.request_timeout_secs;
        }
        if !other.enable_cors {
            self.enable_cors = other.enable_cors;
        }
        if other.api_key.is_some() {
            self.api_key = other.api_key.clone();
        }
    }

    fn validate(&self) -> Result<(), ConfigValidationError> {
        if self.port == 0 {
            return Err(ConfigValidationError {
                field: "port".to_string(),
                message: "Port must be greater than 0".to_string(),
            });
        }

        if self.max_concurrent_requests == 0 {
            return Err(ConfigValidationError {
                field: "max_concurrent_requests".to_string(),
                message: "Max concurrent requests must be greater than 0".to_string(),
            });
        }

        if self.request_timeout_secs == 0 || self.request_timeout_secs > 3600 {
            return Err(ConfigValidationError {
                field: "request_timeout_secs".to_string(),
                message: "Request timeout must be between 1 and 3600 seconds".to_string(),
            });
        }

        Ok(())
    }
}

/// Configuration resolver for handling inheritance and profiles
pub struct ConfigResolver {
    /// Registered configuration profiles
    profiles: HashMap<String, AppConfig>,

    /// Base configuration
    base_config: AppConfig,
}

impl ConfigResolver {
    /// Create new configuration resolver
    pub fn new() -> Self {
        Self {
            profiles: HashMap::new(),
            base_config: AppConfig::default(),
        }
    }

    /// Register a configuration profile
    pub fn register_profile(&mut self, name: &str, config: AppConfig) {
        self.profiles.insert(name.to_string(), config);
    }

    /// Set base configuration
    pub fn set_base_config(&mut self, config: AppConfig) {
        self.base_config = config;
    }

    /// Resolve configuration for a given profile with inheritance
    pub fn resolve(&self, profile: Option<&str>) -> Result<AppConfig, ConfigValidationError> {
        let mut resolved = self.base_config.clone();

        if let Some(profile_name) = profile {
            if let Some(profile_config) = self.profiles.get(profile_name) {
                let inherits_from = profile_config.pipeline.inherits_from.clone();
                resolved.merge_with(profile_config);

                // Handle pipeline profile inheritance
                if let Some(inherits_from) = inherits_from {
                    if let Some(parent_config) = self.profiles.get(&inherits_from) {
                        let mut pipeline_with_inheritance = parent_config.pipeline.clone();
                        pipeline_with_inheritance.merge_with(&resolved.pipeline);
                        resolved.pipeline = pipeline_with_inheritance;
                    }
                }
            }
        }

        resolved.validate()?;
        Ok(resolved)
    }

    /// Get available profiles
    pub fn available_profiles(&self) -> Vec<&String> {
        self.profiles.keys().collect()
    }
}

impl Default for ConfigResolver {
    fn default() -> Self {
        let mut resolver = Self::new();

        // Register standard profiles
        resolver.register_profile(
            "development",
            AppConfig {
                pipeline: PipelineConfig::for_profile("development"),
                ..Default::default()
            },
        );

        resolver.register_profile(
            "production",
            AppConfig {
                pipeline: PipelineConfig::for_profile("production"),
                ..Default::default()
            },
        );

        resolver.register_profile(
            "testing",
            AppConfig {
                pipeline: PipelineConfig::for_profile("testing"),
                ..Default::default()
            },
        );

        resolver
    }
}

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

    #[test]
    fn test_config_hierarchy_merge() {
        let mut base = PipelineConfig::default();
        let override_config = PipelineConfig {
            device: "cuda".to_string(),
            use_gpu: true,
            ..Default::default()
        };

        base.merge_with(&override_config);

        assert_eq!(base.device, "cuda");
        assert!(base.use_gpu);
        assert_eq!(base.max_cache_size_mb, 1024); // Should remain default
    }

    #[test]
    fn test_config_validation() {
        let invalid_config = PipelineConfig {
            device: "invalid_device".to_string(),
            ..Default::default()
        };

        assert!(invalid_config.validate().is_err());

        let valid_config = PipelineConfig {
            device: "cuda".to_string(),
            ..Default::default()
        };

        assert!(valid_config.validate().is_ok());
    }

    #[test]
    fn test_profile_creation() {
        let dev_config = PipelineConfig::for_profile("development");
        assert_eq!(dev_config.logging.level, "debug");
        assert!(dev_config.logging.metrics);

        let prod_config = PipelineConfig::for_profile("production");
        assert_eq!(prod_config.logging.level, "warn");
        assert!(prod_config.logging.structured);
    }

    #[test]
    fn test_config_resolver() {
        let resolver = ConfigResolver::default();

        let dev_config = resolver.resolve(Some("development")).unwrap();
        assert_eq!(dev_config.pipeline.logging.level, "debug");

        let prod_config = resolver.resolve(Some("production")).unwrap();
        assert_eq!(prod_config.pipeline.logging.level, "warn");
    }

    #[test]
    fn test_audio_config_validation() {
        let invalid_buffer = AudioProcessingConfig {
            buffer_size: 1000, // Not power of 2
            ..Default::default()
        };
        assert!(invalid_buffer.validate().is_err());

        let invalid_normalization = AudioProcessingConfig {
            normalization_level: 1.5, // > 1.0
            ..Default::default()
        };
        assert!(invalid_normalization.validate().is_err());

        let valid_config = AudioProcessingConfig::default();
        assert!(valid_config.validate().is_ok());
    }

    #[test]
    fn test_logging_config_merge() {
        let mut base = LoggingConfig::default();
        let override_config = LoggingConfig {
            level: "debug".to_string(),
            module_levels: [("voirs".to_string(), "trace".to_string())]
                .into_iter()
                .collect(),
            ..Default::default()
        };

        base.merge_with(&override_config);

        assert_eq!(base.level, "debug");
        assert_eq!(base.module_levels.get("voirs").unwrap(), "trace");
    }

    #[test]
    fn test_model_overrides() {
        let mut config = ModelLoadingConfig::default();
        config.model_overrides.insert(
            "test-model".to_string(),
            ModelOverride {
                url: Some("https://example.com/model.bin".to_string()),
                checksum: Some("abc123".to_string()),
                local_path: None,
                priority: Some(1),
            },
        );

        let other = ModelLoadingConfig {
            model_overrides: [(
                "other-model".to_string(),
                ModelOverride {
                    url: Some("https://example.com/other.bin".to_string()),
                    checksum: None,
                    local_path: None,
                    priority: Some(2),
                },
            )]
            .into_iter()
            .collect(),
            ..Default::default()
        };

        config.merge_with(&other);

        assert_eq!(config.model_overrides.len(), 2);
        assert!(config.model_overrides.contains_key("test-model"));
        assert!(config.model_overrides.contains_key("other-model"));
    }
}