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
pub mod actions;
pub mod mapping;
pub mod operations;
pub mod state;
use crate::app::mapping::*;
use crate::error::*;
use crate::models::*;
use crate::services::*;
use anyhow::Result;
use metis_core::{domain::documents::types::DocumentType, Initiative, Strategy, Task};
pub struct App {
// Core application state
pub core_state: state::CoreAppState,
// UI state
pub ui_state: state::UiState,
// Selection state
pub selection_state: state::SelectionState,
// Error handler
pub error_handler: ErrorHandler,
// Services
pub workspace_service: WorkspaceService,
pub document_service: Option<DocumentService>,
pub sync_service: Option<SyncService>,
pub transition_service: Option<TransitionService>,
}
impl Default for App {
fn default() -> Self {
Self::new()
}
}
impl App {
pub fn new() -> Self {
Self {
core_state: state::CoreAppState::new(),
ui_state: state::UiState::new(),
selection_state: state::SelectionState::new(),
error_handler: ErrorHandler::new(),
workspace_service: WorkspaceService::new(),
document_service: None,
sync_service: None,
transition_service: None,
}
}
pub async fn initialize(&mut self) -> Result<()> {
// 1. Check if we're in a metis workspace
match self.workspace_service.check_workspace().await {
Ok(Some(workspace_dir)) => {
self.core_state.set_workspace(workspace_dir.clone());
// Initialize services
self.document_service = Some(DocumentService::new(workspace_dir.clone()));
self.sync_service = Some(SyncService::new(workspace_dir.clone()));
self.transition_service = Some(TransitionService::new(workspace_dir));
// 2. Perform database synchronization
if let Some(sync_service) = &self.sync_service {
match sync_service.sync_database().await {
Ok(_) => {
self.core_state.set_sync_complete();
// 3. Load flight level configuration
self.load_flight_config().await?;
// 4. Load documents into boards
self.load_documents().await?;
}
Err(e) => {
self.error_handler.handle_error(AppError::from(e));
}
}
}
}
Ok(None) => {
self.error_handler
.handle_error(AppError::WorkspaceError("No workspace found".to_string()));
}
Err(e) => {
self.error_handler.handle_error(AppError::from(e));
}
}
Ok(())
}
pub fn is_ready(&self) -> bool {
self.core_state.is_ready()
}
pub fn get_current_board(&self) -> &KanbanBoard {
self.ui_state.get_current_board()
}
// Convenience methods for accessing state
pub fn app_state(&self) -> &AppState {
&self.ui_state.app_state
}
pub fn error_message(&self) -> Option<String> {
self.ui_state
.message_state
.get_current_message()
.map(|msg| msg.content.clone())
}
pub fn get_selected_item(&self) -> Option<&KanbanItem> {
let current_board = self.ui_state.current_board;
let (col_idx, item_idx) = self.selection_state.get_current_selection(current_board);
let board = self.ui_state.get_current_board();
if col_idx < board.columns.len() && item_idx < board.columns[col_idx].items.len() {
Some(&board.columns[col_idx].items[item_idx])
} else {
None
}
}
pub fn view_selected_ticket(&mut self) {
let current_board = self.ui_state.current_board;
let selection = self.selection_state.get_current_selection(current_board);
self.ui_state.viewing_ticket = Some((current_board, selection.0, selection.1));
// Go directly to edit mode instead of view mode
self.start_content_editing();
}
pub fn get_viewed_ticket(&self) -> Option<&KanbanItem> {
if let Some((board_type, col_idx, item_idx)) = self.ui_state.viewing_ticket {
let board = match board_type {
BoardType::Strategy => &self.ui_state.strategy_board,
BoardType::Initiative => &self.ui_state.initiative_board,
BoardType::Task => &self.ui_state.task_board,
BoardType::Adr => &self.ui_state.adr_board,
BoardType::Backlog => &self.ui_state.backlog_board,
};
if col_idx < board.columns.len() && item_idx < board.columns[col_idx].items.len() {
Some(&board.columns[col_idx].items[item_idx])
} else {
None
}
} else {
None
}
}
// Input handling
pub fn handle_key_event(&mut self, key: crossterm::event::KeyEvent) {
use tui_input::backend::crossterm::EventHandler;
self.ui_state
.input_title
.handle_event(&crossterm::event::Event::Key(key));
}
pub async fn load_flight_config(&mut self) -> Result<()> {
if let Some(workspace_dir) = &self.core_state.workspace_dir {
// Create database connection and load configuration
// workspace_dir is the .metis directory, so we need metis.db directly
let db_path = workspace_dir.join("metis.db");
if let Ok(db) = metis_core::Database::new(db_path.to_str().unwrap()) {
if let Ok(mut config_repo) = db.configuration_repository() {
match config_repo.get_flight_level_config() {
Ok(config) => {
self.core_state.set_flight_config(config);
// Ensure the current board is valid for the new configuration
self.ui_state
.ensure_valid_board(&self.core_state.flight_config);
}
Err(e) => {
// Log error but continue with default configuration
eprintln!("Warning: Failed to load flight level configuration: {}", e);
eprintln!("Using default (full) configuration");
// Ensure the current board is valid for the default configuration
self.ui_state
.ensure_valid_board(&self.core_state.flight_config);
}
}
} else {
eprintln!("Warning: Failed to create configuration repository, using default configuration");
// Ensure the current board is valid for the default configuration
self.ui_state
.ensure_valid_board(&self.core_state.flight_config);
}
} else {
eprintln!("Warning: Failed to connect to database, using default configuration");
// Ensure the current board is valid for the default configuration
self.ui_state
.ensure_valid_board(&self.core_state.flight_config);
}
}
Ok(())
}
pub async fn load_documents(&mut self) -> Result<()> {
if let Some(document_service) = &self.document_service {
// Clear all boards before loading new documents
for column in &mut self.ui_state.strategy_board.columns {
column.items.clear();
}
for column in &mut self.ui_state.initiative_board.columns {
column.items.clear();
}
for column in &mut self.ui_state.task_board.columns {
column.items.clear();
}
for column in &mut self.ui_state.adr_board.columns {
column.items.clear();
}
for column in &mut self.ui_state.backlog_board.columns {
column.items.clear();
}
// Reset selection state to avoid referencing non-existent items
self.selection_state.strategy_selection = (0, 0);
self.selection_state.initiative_selection = (0, 0);
self.selection_state.task_selection = (0, 0);
self.selection_state.adr_selection = (0, 0);
self.selection_state.backlog_selection = (0, 0);
let mut documents = document_service.load_documents_from_database().await?;
// Sort documents by type first, then by appropriate criteria
documents.sort_by(|a, b| {
use std::cmp::Ordering;
// Helper function to get document type order
let type_order = |doc_type: &DocumentType| -> u8 {
match doc_type {
DocumentType::Vision => 0,
DocumentType::Strategy => 1,
DocumentType::Initiative => 2,
DocumentType::Task => 3,
DocumentType::Adr => 4,
}
};
// First compare by document type
let a_type_order = type_order(&a.document_type);
let b_type_order = type_order(&b.document_type);
match a_type_order.cmp(&b_type_order) {
Ordering::Equal => {
// Same document type, use type-specific sorting
match (&a.document_type, &b.document_type) {
(DocumentType::Adr, DocumentType::Adr) => {
// For ADRs, extract number from ID and sort numerically
let a_num = extract_adr_number(&a.id);
let b_num = extract_adr_number(&b.id);
a_num.cmp(&b_num)
}
_ => a.title.cmp(&b.title), // Other documents sort by title
}
}
other => other, // Different types, use type ordering
}
});
// Clear existing boards
self.ui_state.strategy_board = KanbanBoard::create_strategy_board();
self.ui_state.initiative_board = KanbanBoard::create_initiative_board();
self.ui_state.task_board = KanbanBoard::create_task_board();
self.ui_state.adr_board = KanbanBoard::create_adr_board();
self.ui_state.backlog_board = KanbanBoard::create_backlog_board();
// Load documents into appropriate boards
for doc in documents {
match doc.document_type {
DocumentType::Strategy => {
if let Ok(strategy) =
Strategy::from_file(std::path::Path::new(&doc.filepath)).await
{
let column_index = get_strategy_column_index(&strategy);
let item = KanbanItem {
document: DocumentObject::Strategy(strategy),
prelude: doc.title.clone(),
risk_complexity: None,
file_path: doc.filepath,
};
if column_index < self.ui_state.strategy_board.columns.len() {
self.ui_state.strategy_board.columns[column_index]
.items
.push(item);
}
}
}
DocumentType::Initiative => {
if let Ok(initiative) =
Initiative::from_file(std::path::Path::new(&doc.filepath)).await
{
let column_index = get_initiative_column_index(&initiative);
let item = KanbanItem {
document: DocumentObject::Initiative(initiative),
prelude: doc.title.clone(),
risk_complexity: None,
file_path: doc.filepath,
};
if column_index < self.ui_state.initiative_board.columns.len() {
self.ui_state.initiative_board.columns[column_index]
.items
.push(item);
}
}
}
DocumentType::Task => {
if let Ok(task) = Task::from_file(std::path::Path::new(&doc.filepath)).await
{
use metis_core::{domain::documents::types::Phase, Document};
// Check if this is a backlog item (only Phase::Backlog)
let is_backlog = task.phase() == Ok(Phase::Backlog);
if is_backlog {
// Place in backlog board
let column_index = get_backlog_column_index(&task);
let item = KanbanItem {
document: DocumentObject::Task(task),
prelude: doc.title.clone(),
risk_complexity: None,
file_path: doc.filepath,
};
if column_index < self.ui_state.backlog_board.columns.len() {
self.ui_state.backlog_board.columns[column_index]
.items
.push(item);
}
} else {
// Place in regular task board
let column_index = get_task_column_index(&task);
let item = KanbanItem {
document: DocumentObject::Task(task),
prelude: doc.title.clone(),
risk_complexity: None,
file_path: doc.filepath,
};
if column_index < self.ui_state.task_board.columns.len() {
self.ui_state.task_board.columns[column_index]
.items
.push(item);
}
}
}
}
DocumentType::Adr => {
if let Ok(adr) =
metis_core::Adr::from_file(std::path::Path::new(&doc.filepath)).await
{
let column_index = get_adr_column_index(&adr);
let item = KanbanItem {
document: DocumentObject::Adr(adr),
prelude: doc.title.clone(),
risk_complexity: None,
file_path: doc.filepath,
};
if column_index < self.ui_state.adr_board.columns.len() {
self.ui_state.adr_board.columns[column_index]
.items
.push(item);
}
}
}
_ => {
// Skip other document types for now
}
}
}
}
Ok(())
}
}