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
use crate::instruction_presets::get_instruction_preset_library;
use crate::llm_providers::{
    get_available_providers, get_provider_metadata, LLMProviderConfig, LLMProviderType,
};
use crate::log_debug;
use anyhow::{anyhow, Result};
use dirs::config_dir;
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::fs;
use std::path::PathBuf;
use std::str::FromStr;

/// Configuration structure for the Git-Iris application
#[derive(Deserialize, Serialize, Clone, Debug)]
pub struct Config {
    /// Default LLM provider
    pub default_provider: String,
    /// Provider-specific configurations
    pub providers: HashMap<String, ProviderConfig>,
    /// Flag indicating whether to use Gitmoji
    #[serde(default = "default_gitmoji")]
    pub use_gitmoji: bool,
    /// Instructions for commit messages
    #[serde(default)]
    pub instructions: String,
    #[serde(default = "default_instruction_preset")]
    pub instruction_preset: String,
    #[serde(skip)]
    pub temp_instructions: Option<String>,
    #[serde(skip)]
    pub temp_preset: Option<String>,
}

/// Provider-specific configuration structure
#[derive(Deserialize, Serialize, Clone, Debug, Default)]
pub struct ProviderConfig {
    /// API key for the provider
    pub api_key: String,
    /// Model to be used with the provider
    pub model: String,
    /// Additional parameters for the provider
    #[serde(default)]
    pub additional_params: HashMap<String, String>,
    /// Token limit, if set by the user
    pub token_limit: Option<usize>,
}

/// Default function for use_gitmoji
fn default_gitmoji() -> bool {
    true
}

// Default instruction preset to use
fn default_instruction_preset() -> String {
    "default".to_string()
}

impl Config {
    /// Load the configuration from the file
    pub fn load() -> Result<Self> {
        let config_path = Config::get_config_path()?;
        if !config_path.exists() {
            return Ok(Config::default());
        }
        let config_content = fs::read_to_string(config_path)?;
        let config: Config = toml::from_str(&config_content)?;
        log_debug!("Configuration loaded: {:?}", config);
        Ok(config)
    }

    /// Save the configuration to the file
    pub fn save(&self) -> Result<()> {
        let config_path = Config::get_config_path()?;
        let config_content = toml::to_string(self)?;
        fs::write(config_path, config_content)?;
        log_debug!("Configuration saved: {:?}", self);
        Ok(())
    }

    /// Get the path to the configuration file
    fn get_config_path() -> Result<PathBuf> {
        let mut path =
            config_dir().ok_or_else(|| anyhow!("Unable to determine config directory"))?;
        path.push("git-iris");
        std::fs::create_dir_all(&path)?;
        path.push("config.toml");
        Ok(path)
    }

    /// Check the environment for necessary prerequisites
    pub fn check_environment() -> Result<()> {
        crate::git::check_environment()?;

        // Check if we're in a git repository
        if !crate::git::is_inside_work_tree()? {
            return Err(anyhow!(
                "Not in a Git repository. Please run this command from within a Git repository."
            ));
        }

        Ok(())
    }

    pub fn set_temp_instructions(&mut self, instructions: Option<String>) {
        self.temp_instructions = instructions;
    }

    pub fn set_temp_preset(&mut self, preset: Option<String>) {
        self.temp_preset = preset;
    }

    pub fn get_effective_instructions(&self) -> String {
        let preset_library = get_instruction_preset_library();
        let preset_instructions = self
            .temp_preset
            .as_ref()
            .or(Some(&self.instruction_preset))
            .and_then(|p| preset_library.get_preset(p))
            .map(|p| p.instructions.clone())
            .unwrap_or_default();

        let custom_instructions = self
            .temp_instructions
            .as_ref()
            .unwrap_or(&self.instructions);

        format!("{}\n\n{}", preset_instructions, custom_instructions)
            .trim()
            .to_string()
    }

    /// Update the configuration with new values
    pub fn update(
        &mut self,
        provider: Option<String>,
        api_key: Option<String>,
        model: Option<String>,
        additional_params: Option<HashMap<String, String>>,
        use_gitmoji: Option<bool>,
        instructions: Option<String>,
        token_limit: Option<usize>,
    ) {
        if let Some(provider) = provider {
            self.default_provider = provider.clone();
            if !self.providers.contains_key(&provider) {
                // Only insert a new provider if it requires configuration
                let provider_type =
                    LLMProviderType::from_str(&provider).unwrap_or(LLMProviderType::OpenAI);
                if get_provider_metadata(&provider_type).requires_api_key {
                    self.providers
                        .insert(provider.clone(), ProviderConfig::default_for(&provider));
                }
            }
        }

        let provider_config = self.providers.get_mut(&self.default_provider).unwrap();

        if let Some(key) = api_key {
            provider_config.api_key = key;
        }
        if let Some(model) = model {
            provider_config.model = model;
        }
        if let Some(params) = additional_params {
            provider_config.additional_params.extend(params);
        }
        if let Some(gitmoji) = use_gitmoji {
            self.use_gitmoji = gitmoji;
        }
        if let Some(instr) = instructions {
            self.instructions = instr;
        }
        if let Some(limit) = token_limit {
            provider_config.token_limit = Some(limit);
        }

        log_debug!("Configuration updated: {:?}", self);
    }

    /// Get the configuration for a specific provider
    pub fn get_provider_config(&self, provider: &str) -> Option<&ProviderConfig> {
        self.providers.get(provider).or_else(|| {
            // If the provider is not in the config, check if it's a valid provider
            if LLMProviderType::from_str(provider).is_ok() {
                // Return None for valid providers not in the config
                // This allows the code to use default values for providers like Ollama
                None
            } else {
                // Return None for invalid providers
                None
            }
        })
    }
}

impl Default for Config {
    fn default() -> Self {
        let mut providers = HashMap::new();
        for provider in get_available_providers() {
            providers.insert(
                provider.to_string(),
                ProviderConfig::default_for(&provider.to_string()),
            );
        }

        Config {
            default_provider: get_available_providers().first().unwrap().to_string(),
            providers,
            use_gitmoji: true,
            instructions: String::new(),
            instruction_preset: default_instruction_preset(),
            temp_instructions: None,
            temp_preset: None,
        }
    }
}

impl ProviderConfig {
    /// Create a default provider configuration for a given provider
    pub fn default_for(provider: &str) -> Self {
        let provider_type =
            LLMProviderType::from_str(provider).unwrap_or_else(|_| get_available_providers()[0]);
        let metadata = get_provider_metadata(&provider_type);
        Self {
            api_key: String::new(),
            model: metadata.default_model.to_string(),
            additional_params: HashMap::new(),
            token_limit: Some(metadata.default_token_limit),
        }
    }

    /// Get the token limit for this provider configuration
    pub fn get_token_limit(&self) -> usize {
        self.token_limit.unwrap_or_else(|| {
            let provider_type = LLMProviderType::from_str(&self.model)
                .unwrap_or_else(|_| get_available_providers()[0]);
            get_provider_metadata(&provider_type).default_token_limit
        })
    }

    /// Convert to LLMProviderConfig
    pub fn to_llm_provider_config(&self) -> LLMProviderConfig {
        LLMProviderConfig {
            api_key: self.api_key.clone(),
            model: self.model.clone(),
            additional_params: self.additional_params.clone(),
        }
    }
}