budget_tracker_tui 1.3.1

A feature rich TUI budget tracker app
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
use super::state::App;
use crate::app::settings_types::{SettingType, SettingsState};
use crate::config::{save_settings, AppSettings};
use crate::persistence::{load_categories, load_transactions, save_transactions};
use chrono::Duration;
use std::path::PathBuf;

impl App {
    // --- Settings Mode Logic ---
    // Handles entering/exiting settings mode, saving settings, and resetting the data file path.
    pub(crate) fn enter_settings_mode(&mut self) {
        self.mode = crate::app::state::AppMode::Settings;

        // Initialize Settings State
        self.settings_state = SettingsState::new();

        // Load Config
        let loaded_settings = crate::config::load_settings().unwrap_or_default();

        // --- Data Management Section ---
        self.settings_state.add_setting(
            "header_data",
            "Data Management",
            "".to_string(),
            SettingType::SectionHeader,
            "",
        );

        // 1. Data File Path
        let path_str = self.data_file_path.to_string_lossy().to_string();
        let path_val = crate::validation::strip_path_quotes(&path_str);
        self.settings_state.add_setting(
            "data_file_path",
            "Data File Path",
            path_val,
            SettingType::Path,
            "Absolute path to your transactions CSV file.",
        );
        // 2. SQLite Database Path
        let database_path_str = self.database_path.to_string_lossy().to_string();
        let database_path_val = crate::validation::strip_path_quotes(&database_path_str);
        self.settings_state.add_setting(
            "database_path",
            "Database Path",
            database_path_val,
            SettingType::Path,
            "Absolute path to your categories SQLite database.",
        );
        // 3. Manage Categories
        self.settings_state.add_setting(
            "manage_categories",
            "Manage Categories",
            "Open Category Catalog".to_string(),
            SettingType::Action,
            "Open the category catalog to add, edit, or delete categories.",
        );

        // --- Monthly Summary View Section ---
        self.settings_state.add_setting(
            "header_monthly",
            "Monthly Summary View",
            "".to_string(),
            SettingType::SectionHeader,
            "",
        );

        // 4. Target Budget
        let budget_val = loaded_settings
            .target_budget
            .map(|v| v.to_string())
            .unwrap_or_default();
        self.settings_state.add_setting(
            "target_budget",
            "Target Budget",
            budget_val,
            SettingType::Number,
            "Monthly spending goal. Displayed in Monthly Summary view only when cumulative mode.",
        );

        // --- Transaction View Section ---
        self.settings_state.add_setting(
            "header_transactions",
            "Transaction View",
            "".to_string(),
            SettingType::SectionHeader,
            "",
        );

        // 5. Hourly Rate
        let hourly_rate_val = loaded_settings
            .hourly_rate
            .map(|v| v.to_string())
            .unwrap_or_default();
        self.settings_state.add_setting(
            "hourly_rate",
            "Hourly Rate ($)",
            hourly_rate_val.clone(),
            SettingType::Number,
            "Optional. Enter your hourly earning rate to see costs in hours.",
        );

        // 6. Show Hours Toggle
        // Only show this if hourly rate is present
        if !hourly_rate_val.is_empty() {
            let show_hours_val = if loaded_settings.show_hours.unwrap_or(false) {
                "◀ Yes "
            } else {
                " No ▶"
            };
            self.settings_state.add_setting(
                "show_hours",
                "Show Costs in Hours",
                show_hours_val.to_string(),
                SettingType::Toggle,
                "Toggle to display transaction amounts as hours worked.",
            );
        }

        // --- Input Preferences Section ---
        self.settings_state.add_setting(
            "header_input",
            "Input Preferences",
            "".to_string(),
            SettingType::SectionHeader,
            "",
        );

        // 7. Fuzzy Search Mode
        let fuzzy_search_val = if loaded_settings.fuzzy_search_mode.unwrap_or(false) {
            "◀ Yes "
        } else {
            " No ▶"
        };
        self.settings_state.add_setting(
            "fuzzy_search_mode",
            "Fuzzy Search Categories",
            fuzzy_search_val.to_string(),
            SettingType::Toggle,
            "Toggle to enable fuzzy searching for categories/subcategories.",
        );

        // --- General Preferences Section ---
        self.settings_state.add_setting(
            "header_general",
            "General Preferences",
            "".to_string(),
            SettingType::SectionHeader,
            "",
        );

        // 8. Hide Help Bar
        let hide_help_bar_val = if loaded_settings.hide_help_bar.unwrap_or(false) {
            "◀ Yes "
        } else {
            " No ▶"
        };
        self.settings_state.add_setting(
            "hide_help_bar",
            "Hide Help Bar (NOT RECOMMENDED)",
            hide_help_bar_val.to_string(),
            SettingType::Toggle,
            "Toggle to hide the bottom help bar (Ctrl+H will still work).",
        );

        // Select first valid item (skip headers)
        self.settings_state.selected_index = 0;
        while self.settings_state.selected_index < self.settings_state.items.len() {
            if self.settings_state.items[self.settings_state.selected_index].setting_type
                != SettingType::SectionHeader
            {
                break;
            }
            self.settings_state.selected_index += 1;
        }

        if let Some(item) = self
            .settings_state
            .items
            .get(self.settings_state.selected_index)
        {
            self.settings_state.edit_cursor = item.value.len();
        }

        self.clear_status_message();
    }

    pub(crate) fn exit_settings_mode(&mut self) {
        self.mode = crate::app::state::AppMode::Normal;
        self.settings_state = SettingsState::default();
        self.clear_status_message();
    }

    pub(crate) fn save_settings(&mut self) {
        // Retrieve values from state
        let mut new_path_str = String::new();
        let mut new_database_path_str = String::new();
        let mut target_budget_str = String::new();
        let mut hourly_rate_str = String::new();
        let mut show_hours_val = None;
        let mut fuzzy_search_val = None;
        let mut hide_help_bar_val = None;

        if let Some(val) = self.settings_state.get_value("data_file_path") {
            new_path_str = crate::validation::strip_path_quotes(val);
        }
        if let Some(val) = self.settings_state.get_value("database_path") {
            new_database_path_str = crate::validation::strip_path_quotes(val);
        }
        if let Some(val) = self.settings_state.get_value("target_budget") {
            target_budget_str = val.trim().to_string();
        }
        if let Some(val) = self.settings_state.get_value("hourly_rate") {
            hourly_rate_str = val.trim().to_string();
        }
        if let Some(val) = self.settings_state.get_value("show_hours") {
            show_hours_val = Some(val.to_lowercase().contains("yes"));
        }
        if let Some(val) = self.settings_state.get_value("fuzzy_search_mode") {
            fuzzy_search_val = Some(val.to_lowercase().contains("yes"));
        }
        if let Some(val) = self.settings_state.get_value("hide_help_bar") {
            hide_help_bar_val = Some(val.to_lowercase().contains("yes"));
        }

        // Validate Target Budget
        let target_budget = if target_budget_str.is_empty() {
            None
        } else {
            match crate::validation::validate_amount_string(&target_budget_str) {
                Ok(val) => Some(val),
                Err(msg) => {
                    self.set_status_message(format!("Error: Target budget - {}", msg), None);
                    return;
                }
            }
        };

        // Validate Hourly Rate
        let hourly_rate = if hourly_rate_str.is_empty() {
            None
        } else {
            match crate::validation::validate_amount_string(&hourly_rate_str) {
                Ok(val) => Some(val),
                Err(msg) => {
                    self.set_status_message(format!("Error: Hourly rate - {}", msg), None);
                    return;
                }
            }
        };

        // Validate Path
        if new_path_str.is_empty() {
            self.set_status_message("Error: Path cannot be empty.", None);
            return;
        }
        if new_database_path_str.is_empty() {
            self.set_status_message("Error: Database path cannot be empty.", None);
            return;
        }
        let new_path = PathBuf::from(&new_path_str);
        let new_database_path = PathBuf::from(&new_database_path_str);
        if !new_path.exists() {
            if let Err(e) = save_transactions(&self.transactions, &new_path) {
                self.set_status_message(
                    format!(
                        "Error creating transactions file '{}': {}. Check path and permissions.",
                        new_path.display(),
                        e
                    ),
                    None,
                );
                return;
            }
        }

        let seed_categories = if self.categories.is_empty() {
            load_categories().unwrap_or_default()
        } else {
            self.categories.clone()
        };
        if let Err(e) = Self::prepare_category_database_for_path_change(
            &self.database_path,
            &new_database_path,
            &seed_categories,
        ) {
            self.set_status_message(
                format!(
                    "Error preparing database '{}': {}. Check path and permissions.",
                    new_database_path.display(),
                    e
                ),
                None,
            );
            return;
        }

        // Save to Config
        let settings = AppSettings {
            data_file_path: Some(new_path_str.clone()),
            database_path: Some(new_database_path_str.clone()),
            target_budget,
            hourly_rate,
            show_hours: show_hours_val,
            fuzzy_search_mode: fuzzy_search_val,
            hide_help_bar: hide_help_bar_val,
        };
        if let Err(e) = save_settings(&settings) {
            self.set_status_message(format!("Error saving config file: {}", e), None);
            return;
        }

        // Reload Transactions
        self.data_file_path = new_path.clone();
        self.database_path = new_database_path.clone();
        let txs = match load_transactions(&self.data_file_path) {
            Ok(tx) => tx,
            Err(e) => {
                self.set_status_message(
                    format!(
                        "Error loading transactions from '{}': {}. Check file format and permissions.",
                        self.data_file_path.display(),
                        e
                    ),
                    None,
                );
                return;
            }
        };
        self.transactions = txs;
        if let Err(e) = self.reload_categories_from_store() {
            self.set_status_message(
                format!(
                    "Error loading categories from '{}': {}. Check database path and permissions.",
                    self.database_path.display(),
                    e
                ),
                None,
            );
            return;
        }

        self.exit_settings_mode();

        // Re-init app state (sorting, summaries)
        self.sort_transactions();
        self.filtered_indices = (0..self.transactions.len()).collect();
        if !self.filtered_indices.is_empty() {
            self.table_state.select(Some(0));
        } else {
            self.table_state.select(None);
        }
        self.calculate_monthly_summaries();
        self.calculate_category_summaries();

        self.set_status_message(
            format!(
                "Settings saved. Data: {} | Database: {}",
                self.data_file_path.display(),
                self.database_path.display()
            ),
            Some(Duration::seconds(3)),
        );
        self.target_budget = target_budget;
        self.hourly_rate = hourly_rate;
        self.show_hours = show_hours_val.unwrap_or(false);
        self.fuzzy_search_mode = fuzzy_search_val.unwrap_or(false);
        self.hide_help_bar = hide_help_bar_val.unwrap_or(false);
    }

    pub(crate) fn reset_settings_path_to_default(&mut self) {
        match Self::get_default_data_file_path() {
            Ok(default_path) => {
                let path_str = default_path.to_string_lossy().to_string();
                let clean_path = crate::validation::strip_path_quotes(&path_str);

                // Find index
                if let Some(idx) = self
                    .settings_state
                    .items
                    .iter()
                    .position(|i| i.key == "data_file_path")
                {
                    self.settings_state.items[idx].value = clean_path;
                    if self.settings_state.selected_index == idx {
                        self.settings_state.edit_cursor =
                            self.settings_state.items[idx].value.len();
                    }
                }

                self.set_status_message("Path reset to default. Press Enter to save.", None);
            }
            Err(e) => {
                let fallback_path = "transactions.csv";

                if let Some(idx) = self
                    .settings_state
                    .items
                    .iter()
                    .position(|i| i.key == "data_file_path")
                {
                    self.settings_state.items[idx].value = fallback_path.to_string();
                    if self.settings_state.selected_index == idx {
                        self.settings_state.edit_cursor =
                            self.settings_state.items[idx].value.len();
                    }
                }

                self.set_status_message(
                    format!(
                        "Error getting default path ({}). Reset to local '{}'. Press Enter to save.",
                        e, fallback_path
                    ),
                    None,
                );
            }
        }
    }

    pub(crate) fn reset_settings_database_path_to_default(&mut self) {
        let data_path_value = self
            .settings_state
            .get_value("data_file_path")
            .map(|value| crate::validation::strip_path_quotes(value))
            .filter(|value| !value.trim().is_empty());

        let default_path = match data_path_value {
            Some(path_str) => {
                Self::default_database_path_for_data_path(PathBuf::from(path_str).as_path())
            }
            None => Self::get_default_database_file_path()
                .unwrap_or_else(|_| PathBuf::from("budget.db")),
        };

        let clean_path =
            crate::validation::strip_path_quotes(default_path.to_string_lossy().as_ref());

        if let Some(idx) = self
            .settings_state
            .items
            .iter()
            .position(|i| i.key == "database_path")
        {
            self.settings_state.items[idx].value = clean_path;
            if self.settings_state.selected_index == idx {
                self.settings_state.edit_cursor = self.settings_state.items[idx].value.len();
            }
        }

        self.set_status_message(
            "Database path reset to default for the current data file. Press Enter to save.",
            None,
        );
    }

    pub(crate) fn activate_selected_setting(&mut self) {
        let selected_key = self
            .settings_state
            .items
            .get(self.settings_state.selected_index)
            .map(|item| item.key.clone());

        match selected_key.as_deref() {
            Some("manage_categories") => self.open_category_catalog(),
            _ => self.save_settings(),
        }
    }

    /// Generalized method to ensure a setting is visible or hidden based on a condition.
    /// This handles finding the correct position and maintaining list integrity.
    fn ensure_setting_visibility<F>(
        &mut self,
        target_key: &str,
        should_be_visible: bool,
        insert_after_key: &str,
        item_creator: F,
    ) where
        F: FnOnce() -> crate::app::settings_types::SettingItem,
    {
        let target_idx = self
            .settings_state
            .items
            .iter()
            .position(|i| i.key == target_key);

        if should_be_visible {
            if target_idx.is_none() {
                // Find where to insert
                if let Some(after_idx) = self
                    .settings_state
                    .items
                    .iter()
                    .position(|i| i.key == insert_after_key)
                {
                    let item = item_creator();
                    self.settings_state.items.insert(after_idx + 1, item);

                    // If we inserted before the selection, shift selection down
                    if self.settings_state.selected_index > after_idx {
                        self.settings_state.selected_index += 1;
                    }
                }
            }
        } else if let Some(idx) = target_idx {
            self.settings_state.items.remove(idx);

            // If we removed the item before or at the selection, shift selection up
            if self.settings_state.selected_index >= idx {
                self.settings_state.selected_index =
                    self.settings_state.selected_index.saturating_sub(1);
            }
        }
    }

    pub(crate) fn update_settings_visibility(&mut self) {
        // Rule 1: "show_hours" depends on "hourly_rate" having a value
        let hourly_rate_has_value = self
            .settings_state
            .get_value("hourly_rate")
            .map(|v| !v.trim().is_empty())
            .unwrap_or(false);

        self.ensure_setting_visibility("show_hours", hourly_rate_has_value, "hourly_rate", || {
            crate::app::settings_types::SettingItem {
                key: "show_hours".to_string(),
                label: "Show Costs in Hours".to_string(),
                value: " No ▶".to_string(), // Default to No
                setting_type: crate::app::settings_types::SettingType::Toggle,
                help: "Toggle to display transaction amounts as hours worked.".to_string(),
            }
        });
    }
}