envelope-cli 0.2.6

Terminal-based zero-based budgeting application
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
//! Bulk categorize dialog
//!
//! Apply category to multiple selected transactions

use ratatui::{
    layout::{Constraint, Direction, Layout, Rect},
    style::{Color, Modifier, Style},
    text::{Line, Span},
    widgets::{Block, Borders, Clear, List, ListItem, ListState, Paragraph},
    Frame,
};

use crate::models::CategoryId;
use crate::services::CategoryService;
use crate::tui::app::App;
use crate::tui::layout::centered_rect;

/// State for the bulk categorize dialog
#[derive(Debug, Clone, Default)]
pub struct BulkCategorizeState {
    /// Selected category
    pub selected_category: Option<CategoryId>,
    /// Index in the category list
    pub category_list_index: usize,
    /// Search/filter input
    pub search_input: String,
    /// Search cursor position
    pub search_cursor: usize,
    /// Error message
    pub error_message: Option<String>,
    /// Success message
    pub success_message: Option<String>,
}

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

    /// Reset the state
    pub fn reset(&mut self) {
        *self = Self::default();
    }

    /// Clear error message
    pub fn clear_error(&mut self) {
        self.error_message = None;
    }

    /// Set error message
    pub fn set_error(&mut self, msg: impl Into<String>) {
        self.error_message = Some(msg.into());
        self.success_message = None;
    }

    /// Set success message
    pub fn set_success(&mut self, msg: impl Into<String>) {
        self.success_message = Some(msg.into());
        self.error_message = None;
    }

    /// Insert character at cursor
    pub fn insert_char(&mut self, c: char) {
        self.search_input.insert(self.search_cursor, c);
        self.search_cursor += 1;
        // Reset selection when typing
        self.category_list_index = 0;
    }

    /// Delete character before cursor
    pub fn backspace(&mut self) {
        if self.search_cursor > 0 {
            self.search_cursor -= 1;
            self.search_input.remove(self.search_cursor);
            // Reset selection when typing
            self.category_list_index = 0;
        }
    }

    /// Clear search
    pub fn clear_search(&mut self) {
        self.search_input.clear();
        self.search_cursor = 0;
        self.category_list_index = 0;
    }
}

/// Render the bulk categorize dialog
pub fn render(frame: &mut Frame, app: &mut App) {
    let area = centered_rect(55, 60, frame.area());

    // Clear the background
    frame.render_widget(Clear, area);

    let count = app.selected_transactions.len();

    let block = Block::default()
        .title(format!(
            " Categorize {} Transaction{} ",
            count,
            if count == 1 { "" } else { "s" }
        ))
        .title_style(
            Style::default()
                .fg(Color::Cyan)
                .add_modifier(Modifier::BOLD),
        )
        .borders(Borders::ALL)
        .border_style(Style::default().fg(Color::Cyan));

    frame.render_widget(block, area);

    // Inner area
    let inner = Rect {
        x: area.x + 2,
        y: area.y + 1,
        width: area.width.saturating_sub(4),
        height: area.height.saturating_sub(2),
    };

    // Layout
    let chunks = Layout::default()
        .direction(Direction::Vertical)
        .constraints([
            Constraint::Length(1), // Search label
            Constraint::Length(1), // Search input
            Constraint::Length(1), // Spacer
            Constraint::Min(6),    // Category list
            Constraint::Length(1), // Spacer
            Constraint::Length(1), // Error/success
            Constraint::Length(1), // Hints
        ])
        .split(inner);

    // Get categories
    let category_service = CategoryService::new(app.storage);
    let all_categories = category_service.list_categories().unwrap_or_default();

    // Filter categories by search
    let search = app.bulk_categorize_state.search_input.to_lowercase();
    let filtered_categories: Vec<_> = all_categories
        .iter()
        .filter(|c| search.is_empty() || c.name.to_lowercase().contains(&search))
        .collect();

    // Search input
    render_search_field(
        frame,
        &app.bulk_categorize_state.search_input,
        app.bulk_categorize_state.search_cursor,
        chunks[0],
        chunks[1],
    );

    // Category list
    render_category_list(
        frame,
        &filtered_categories,
        app.bulk_categorize_state.selected_category,
        app.bulk_categorize_state.category_list_index,
        chunks[3],
    );

    // Error/success message
    if let Some(ref error) = app.bulk_categorize_state.error_message {
        let error_line = Line::from(Span::styled(
            error.as_str(),
            Style::default().fg(Color::Red),
        ));
        frame.render_widget(Paragraph::new(error_line), chunks[5]);
    } else if let Some(ref success) = app.bulk_categorize_state.success_message {
        let success_line = Line::from(Span::styled(
            success.as_str(),
            Style::default().fg(Color::Green),
        ));
        frame.render_widget(Paragraph::new(success_line), chunks[5]);
    }

    // Hints
    let hints = Line::from(vec![
        Span::styled("[↑↓]", Style::default().fg(Color::Yellow)),
        Span::raw(" Select  "),
        Span::styled("[Enter]", Style::default().fg(Color::Green)),
        Span::raw(" Apply  "),
        Span::styled("[Esc]", Style::default().fg(Color::Red)),
        Span::raw(" Cancel"),
    ]);
    frame.render_widget(Paragraph::new(hints), chunks[6]);
}

/// Render the search field
fn render_search_field(
    frame: &mut Frame,
    search: &str,
    cursor: usize,
    label_area: Rect,
    input_area: Rect,
) {
    // Label
    let label = Line::from(Span::styled(
        "Search categories:",
        Style::default().fg(Color::Cyan),
    ));
    frame.render_widget(Paragraph::new(label), label_area);

    // Input with cursor
    let mut spans = vec![Span::raw("  ")];

    let cursor_pos = cursor.min(search.len());
    let (before, after) = search.split_at(cursor_pos);

    spans.push(Span::styled(
        before.to_string(),
        Style::default().fg(Color::White),
    ));

    let cursor_char = after.chars().next().unwrap_or(' ');
    spans.push(Span::styled(
        cursor_char.to_string(),
        Style::default().fg(Color::Black).bg(Color::Cyan),
    ));

    if after.len() > 1 {
        spans.push(Span::styled(
            after[1..].to_string(),
            Style::default().fg(Color::White),
        ));
    }

    if search.is_empty() {
        spans.push(Span::styled(
            " (type to filter)",
            Style::default().fg(Color::Yellow),
        ));
    }

    frame.render_widget(Paragraph::new(Line::from(spans)), input_area);
}

/// Render the category list
fn render_category_list(
    frame: &mut Frame,
    categories: &[&crate::models::Category],
    selected: Option<CategoryId>,
    list_index: usize,
    area: Rect,
) {
    if categories.is_empty() {
        let text =
            Paragraph::new("No matching categories").style(Style::default().fg(Color::Yellow));
        frame.render_widget(text, area);
        return;
    }

    let items: Vec<ListItem> = categories
        .iter()
        .map(|cat| {
            let style = if Some(cat.id) == selected {
                Style::default().fg(Color::Green)
            } else {
                Style::default().fg(Color::White)
            };
            ListItem::new(Line::from(Span::styled(format!("  {}", cat.name), style)))
        })
        .collect();

    let list = List::new(items)
        .highlight_style(
            Style::default()
                .bg(Color::DarkGray)
                .add_modifier(Modifier::BOLD),
        )
        .highlight_symbol("â–¶ ");

    let mut state = ListState::default();
    state.select(Some(list_index.min(categories.len().saturating_sub(1))));

    frame.render_stateful_widget(list, area, &mut state);
}

/// Handle key events for the bulk categorize dialog
pub fn handle_key(app: &mut App, key: crossterm::event::KeyEvent) -> bool {
    use crossterm::event::KeyCode;

    // Get filtered categories for index bounds
    let category_service = CategoryService::new(app.storage);
    let all_categories = category_service.list_categories().unwrap_or_default();
    let search = app.bulk_categorize_state.search_input.to_lowercase();
    let filtered: Vec<_> = all_categories
        .iter()
        .filter(|c| search.is_empty() || c.name.to_lowercase().contains(&search))
        .collect();
    let cat_count = filtered.len();

    match key.code {
        KeyCode::Esc => {
            app.bulk_categorize_state.reset();
            app.close_dialog();
            return true;
        }

        KeyCode::Enter => {
            // Apply the selected category
            if cat_count > 0 {
                let idx = app
                    .bulk_categorize_state
                    .category_list_index
                    .min(cat_count.saturating_sub(1));
                if let Some(cat) = filtered.get(idx) {
                    execute_bulk_categorize(app, cat.id);
                }
            } else {
                app.bulk_categorize_state.set_error("No category selected");
            }
            return true;
        }

        KeyCode::Up | KeyCode::Char('k') => {
            if app.bulk_categorize_state.category_list_index > 0 {
                app.bulk_categorize_state.category_list_index -= 1;
            }
            return true;
        }

        KeyCode::Down | KeyCode::Char('j') => {
            if app.bulk_categorize_state.category_list_index < cat_count.saturating_sub(1) {
                app.bulk_categorize_state.category_list_index += 1;
            }
            return true;
        }

        KeyCode::Char(c) => {
            app.bulk_categorize_state.clear_error();
            app.bulk_categorize_state.insert_char(c);
            return true;
        }

        KeyCode::Backspace => {
            app.bulk_categorize_state.clear_error();
            app.bulk_categorize_state.backspace();
            return true;
        }

        KeyCode::Delete => {
            app.bulk_categorize_state.clear_search();
            return true;
        }

        _ => {}
    }

    false
}

/// Execute the bulk categorize operation
fn execute_bulk_categorize(app: &mut App, category_id: CategoryId) {
    let transaction_ids = app.selected_transactions.clone();

    if transaction_ids.is_empty() {
        app.bulk_categorize_state
            .set_error("No transactions selected");
        return;
    }

    let mut success_count = 0;
    let mut error_count = 0;

    for txn_id in &transaction_ids {
        match app.storage.transactions.get(*txn_id) {
            Ok(Some(mut txn)) => {
                // Skip transfers (they shouldn't be categorized)
                if txn.is_transfer() {
                    continue;
                }

                // Update category
                txn.category_id = Some(category_id);
                txn.updated_at = chrono::Utc::now();

                if app.storage.transactions.upsert(txn).is_ok() {
                    success_count += 1;
                } else {
                    error_count += 1;
                }
            }
            _ => {
                error_count += 1;
            }
        }
    }

    // Save all changes
    if let Err(e) = app.storage.transactions.save() {
        app.bulk_categorize_state
            .set_error(format!("Failed to save: {}", e));
        return;
    }

    // Get category name for message
    let category_service = CategoryService::new(app.storage);
    let category_name = category_service
        .get_category(category_id)
        .ok()
        .flatten()
        .map(|c| c.name)
        .unwrap_or_else(|| "Unknown".into());

    // Clear selections
    app.selected_transactions.clear();
    app.multi_select_mode = false;

    // Close dialog and show status
    if error_count > 0 {
        app.set_status(format!(
            "Categorized {} transactions as '{}' ({} errors)",
            success_count, category_name, error_count
        ));
    } else {
        app.set_status(format!(
            "Categorized {} transaction{} as '{}'",
            success_count,
            if success_count == 1 { "" } else { "s" },
            category_name
        ));
    }

    app.bulk_categorize_state.reset();
    app.close_dialog();
}