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
//! Tag management command for task organization and categorization.
//!
//! Provides comprehensive tag management functionality, enabling users to create, organize, and utilize tags for better task categorization.
//!
//! ## Features
//!
//! - **Tag CRUD Operations**: Create, read, update, and delete tag definitions
//! - **Color Coding**: Visual organization with customizable tag colors
//! - **Task Association**: Link tags to tasks for categorization
//! - **Filtering**: Find tasks by tag assignments
//! - **Auto-Creation**: Automatically create tags when assigned to tasks
//!
//! ## Usage
//!
//! ```bash
//! # List all tags
//! kasl tag list
//!
//! # Create new tag with color
//! kasl tag create --name "urgent" --color "red"
//!
//! # Delete tag
//! kasl tag delete "old-tag"
//!
//! # Show tag usage statistics
//! kasl tag stats
//! ```
use crate::;
use Result;
use ;
use ;
/// Command-line arguments for tag management operations.
///
/// The tag command uses subcommands to organize different tag management
/// operations, providing a clean and intuitive interface for users to
/// manage their tag library and task associations.
/// Available tag management operations.
///
/// Each subcommand provides specific functionality for tag lifecycle
/// management and task association operations, supporting both direct
/// command-line usage and interactive workflows.
/// Executes tag management operations based on the specified subcommand.
///
/// This function serves as the main dispatcher for tag operations, routing
/// to appropriate handlers based on user input. When no subcommand is provided,
/// it enters interactive mode for operation selection.
///
/// ## Operation Routing
///
/// - **Create**: Tag creation with validation and color assignment
/// - **List**: Formatted display of all available tags
/// - **Edit**: Interactive or direct tag modification
/// - **Delete**: Safe tag removal with usage impact analysis
/// - **Tasks**: Display tasks filtered by tag assignment
/// - **Interactive**: Menu-driven operation selection when no subcommand given
///
/// ## Error Handling
///
/// Each operation includes appropriate error handling for:
/// - Tag not found scenarios
/// - Database connectivity issues
/// - Validation failures
/// - User input errors
/// - Concurrent modification conflicts
///
/// # Arguments
///
/// * `args` - Parsed command-line arguments containing operation specification
///
/// # Returns
///
/// Returns `Ok(())` on successful operation completion, or an error if
/// the requested operation fails due to validation, database, or user input issues.
///
/// # Examples
///
/// ```bash
/// # Create a new urgent tag with red color
/// kasl tag create urgent --color red
///
/// # List all available tags
/// kasl tag list
///
/// # Edit a tag's properties
/// kasl tag edit urgent
///
/// # Show all tasks tagged as "backend"
/// kasl tag tasks backend
///
/// # Interactive mode
/// kasl tag
/// ```
pub async
/// Handles tag creation with validation and uniqueness checking.
///
/// This function manages the complete tag creation workflow:
/// 1. **Uniqueness Validation**: Ensures tag name doesn't already exist
/// 2. **Tag Creation**: Creates new tag with specified name and optional color
/// 3. **Database Storage**: Saves the new tag with proper error handling
/// 4. **User Feedback**: Provides confirmation of successful creation
///
/// ## Tag Properties
///
/// New tags are created with these properties:
/// - **Name**: Unique identifier provided by user
/// - **Color**: Optional visual indicator (defaults to system-assigned if not provided)
/// - **Creation Date**: Automatically set to current timestamp
///
/// ## Validation Rules
///
/// - Tag names must be unique across the entire tag library
/// - Tag names cannot be empty or contain only whitespace
/// - Color values are optional and accept standard color names or hex codes
///
/// # Arguments
///
/// * `name` - Unique name for the new tag
/// * `color` - Optional color specification for visual organization
/// Displays all available tags in a formatted table.
///
/// This function retrieves all tags from the database and presents them
/// in a user-friendly table format showing all relevant properties.
/// The display helps users understand their available tags and their
/// configurations for effective task categorization.
///
/// ## Display Format
///
/// The table includes these columns:
/// - **ID**: Database identifier for the tag
/// - **Name**: The tag's unique name
/// - **Color**: Visual color indicator (if assigned)
///
/// ## Empty State Handling
///
/// When no tags exist, the function provides helpful guidance about
/// creating the first tag rather than displaying an empty table.
/// Handles tag editing with flexible identifier support.
///
/// This function provides comprehensive tag editing capabilities:
/// 1. **Tag Resolution**: Finds tag by name or ID
/// 2. **Current State Display**: Shows existing tag properties
/// 3. **Interactive Editing**: Prompts for new values with current values as defaults
/// 4. **Validation**: Ensures edited values meet tag requirements
/// 5. **Database Update**: Saves changes with proper error handling
///
/// ## Identifier Resolution
///
/// The function accepts flexible tag identification:
/// - **Numeric Input**: Treated as database ID
/// - **String Input**: Treated as tag name
/// - **Automatic Detection**: Parses input to determine type
///
/// ## Editing Interface
///
/// For each editable property, the interface:
/// - Shows the current value as the default
/// - Allows the user to accept current value or enter new one
/// - Validates new values according to tag rules
/// - Provides clear feedback about validation errors
///
/// # Arguments
///
/// * `tag_identifier` - Tag name or ID string for flexible tag identification
/// Handles safe tag deletion with usage impact analysis.
///
/// This function manages the tag deletion process with comprehensive
/// safety measures to prevent accidental data loss and inform users
/// about the impact of deletion:
/// 1. **Tag Resolution**: Finds tag by name or ID
/// 2. **Usage Analysis**: Counts how many tasks currently use the tag
/// 3. **Impact Communication**: Informs user about affected tasks
/// 4. **Confirmation Prompt**: Requires explicit user confirmation
/// 5. **Safe Deletion**: Removes tag and updates task associations
///
/// ## Safety Features
///
/// - Confirms tag exists before showing deletion prompt
/// - Analyzes and reports impact on existing tasks
/// - Uses different confirmation messages based on usage
/// - Defaults to "No" for safety
/// - Provides escape opportunity before actual deletion
/// - Clear feedback about cancellation vs. completion
///
/// ## Usage Impact
///
/// The function provides different confirmation prompts based on tag usage:
/// - **Unused Tags**: Simple confirmation for deletion
/// - **Used Tags**: Enhanced warning showing number of affected tasks
/// - **Heavily Used Tags**: Additional emphasis on impact scope
///
/// # Arguments
///
/// * `tag_identifier` - Tag name or ID string for flexible tag identification
/// Displays all tasks associated with a specific tag.
///
/// This function provides filtered task viewing based on tag assignment,
/// allowing users to see all work items within a particular category.
/// It's useful for project management and understanding work distribution
/// across different areas.
///
/// ## Display Features
///
/// - **Tag Validation**: Ensures the specified tag exists
/// - **Task Filtering**: Shows only tasks with the specified tag
/// - **Standard Format**: Uses the same task display format as other commands
/// - **Empty State Handling**: Provides helpful message when no tasks found
///
/// ## Use Cases
///
/// This functionality supports various workflows:
/// - **Project Review**: See all tasks for a specific project
/// - **Priority Management**: View all urgent or high-priority tasks
/// - **Sprint Planning**: Review tasks by type or area
/// - **Progress Tracking**: Monitor completion within categories
///
/// # Arguments
///
/// * `tag_name` - Name of the tag to filter tasks by
async
/// Handles interactive tag management when no subcommand is provided.
///
/// This function provides a menu-driven interface for users who prefer
/// interactive operation over command-line arguments. It presents all
/// available tag operations in an easy-to-navigate menu format.
///
/// ## Interactive Menu
///
/// The menu presents these options:
/// 1. **Create tag**: Launches tag creation workflow with prompts for name and color
/// 2. **List tags**: Shows all available tags in formatted table
/// 3. **Edit tag**: Tag selection interface followed by editing prompts
/// 4. **Delete tag**: Tag selection interface with safety confirmations
///
/// Each menu option delegates to the appropriate specialized handler
/// function, ensuring consistent behavior between interactive and
/// command-line usage.
///
/// ## Tag Selection Interface
///
/// For edit and delete operations, the interactive mode provides:
/// - **Tag List Display**: Shows all available tags for selection
/// - **Empty State Handling**: Graceful handling when no tags exist
/// - **User-Friendly Selection**: Clear presentation of tag options
/// - **Operation Cancellation**: Easy way to exit without changes
///
/// ## User Experience
///
/// The interactive mode is designed for:
/// - Users new to the command-line interface
/// - Occasional tag management tasks
/// - Exploration of available tag operations
/// - Situations where remembering exact command syntax is inconvenient