prompthive 0.2.8

Open source prompt manager for developers. Terminal-native, sub-15ms operations, works with any AI tool.
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
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
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
// Core prompt management commands - extracted from main.rs

use crate::PromptMetadata;
use crate::{
    clean, edit, HistoryEntry, HistoryTracker, IoOptions, MatchResult, Matcher, Prompt, Storage,
};
#[cfg(feature = "compose")]
use crate::{parse_prompt_list, Composer};
use anyhow::{Context, Result};
use chrono;
use colored::*;
use fuzzy_matcher::{skim::SkimMatcherV2, FuzzyMatcher};
use is_terminal::IsTerminal;
use std::env;
use std::fs;
use std::path::PathBuf;
use std::process::Command;
use std::time::Instant;

// Core commands like use, new, edit, show, delete, ls, find, rename

#[allow(dead_code)]
pub fn handle_use(
    storage: &Storage,
    name: &str,
    input: Option<&str>,
    edit: bool,
    io_options: &IoOptions,
    with_directives: Option<&str>,
    start: Instant,
) -> Result<()> {
    let history_tracker = HistoryTracker::new(storage.base_dir().to_path_buf());
    let command = format!("use {}", name);
    let input_text = input.unwrap_or("").to_string();
    use std::io::Read;

    // Use fast resolution path that avoids loading all prompts
    let resolved_name = match storage.resolve_prompt_fast(name) {
        Ok(name) => name,
        Err(_) => {
            // Fall back to full fuzzy matching only if fast path fails
            let prompt_names = storage.list_prompts()?;
            let mut prompts = Vec::new();

            for prompt_name in &prompt_names {
                if let Ok((metadata, _)) = storage.read_prompt(prompt_name) {
                    prompts.push(Prompt {
                        name: prompt_name.clone(),
                        short_code: Matcher::generate_short_code(
                            prompt_name,
                            &prompts
                                .iter()
                                .map(|p: &Prompt| p.short_code.clone())
                                .collect::<Vec<_>>(),
                        ),
                        description: metadata.description,
                        version: metadata.version,
                        created_at: metadata.created_at,
                        updated_at: metadata.updated_at,
                        git_hash: metadata.git_hash,
                    });
                }
            }

            // Find matching prompt
            let matcher = Matcher::new(prompts);
            match matcher.find(name) {
                MatchResult::Exact(prompt) => prompt.name,
                _ => {
                    // Record failed history
                    let entry = HistoryEntry::new(
                        command,
                        &input_text,
                        "",
                        false,
                        start.elapsed().as_millis(),
                    );
                    let _ = history_tracker.record(entry);
                    
                    // Use the error helper for better error messages
                    use crate::error_help;
                    let available_prompts = storage.list_prompts().unwrap_or_default();
                    eprintln!("{}", error_help::format_prompt_not_found(name, &available_prompts));
                    std::process::exit(1);
                }
            }
        }
    };

    // Read the prompt content
    let (_, mut body) = storage.read_prompt(&resolved_name)?;

    // Handle --with directive files
    if let Some(directives) = with_directives {
        let mut directive_content = String::new();
        let directive_files: Vec<&str> = directives.split(',').map(|s| s.trim()).collect();

        for directive_file in directive_files {
            // Check if it's a file path or a prompt name
            if directive_file.starts_with('~')
                || directive_file.starts_with('/')
                || directive_file.contains('.')
            {
                // It's a file path - expand and read
                let expanded_path = shellexpand::tilde(directive_file);
                let path = PathBuf::from(expanded_path.as_ref());

                if path.exists() {
                    match fs::read_to_string(&path) {
                        Ok(content) => {
                            directive_content.push_str(&content);
                            directive_content.push_str("\n\n");
                        }
                        Err(e) => {
                            eprintln!(
                                "Warning: Failed to read directive file '{}': {}",
                                directive_file, e
                            );
                        }
                    }
                } else {
                    eprintln!("Warning: Directive file '{}' not found", directive_file);
                }
            } else {
                // It's a prompt name - try to read from storage
                match storage.read_prompt(directive_file) {
                    Ok((_, content)) => {
                        directive_content.push_str(&content);
                        directive_content.push_str("\n\n");
                    }
                    Err(_) => {
                        eprintln!("Warning: Prompt '{}' not found", directive_file);
                    }
                }
            }
        }

        // Prepend directive content to the body
        if !directive_content.is_empty() {
            body = format!("{}{}", directive_content, body);
        }
    }

    // Get input from argument or stdin
    let input_content = if let Some(provided_input) = input {
        provided_input.to_string()
    } else if !std::io::stdin().is_terminal() {
        let mut stdin_content = String::new();
        std::io::stdin().read_to_string(&mut stdin_content)?;
        stdin_content.trim().to_string()
    } else {
        String::new()
    };

    // Process template variables and input
    let mut composer = Composer::new(storage.clone());
    body = composer
        .template_processor()
        .process(&body, &input_content)?;

    // Handle --edit flag
    if edit {
        body = edit::edit_content(&body)?;
    }

    // Apply unified I/O using IoOptions with prompt name for sync
    io_options.apply_unified_io_with_prompt(storage, &body, "Executed prompt result", Some(&resolved_name), start)?;

    // Record successful history
    let entry = HistoryEntry::new(
        command,
        &input_text,
        &body,
        true,
        start.elapsed().as_millis(),
    );
    let _ = history_tracker.record(entry); // Don't fail if history fails

    Ok(())
}

#[allow(dead_code)]
pub fn handle_new(
    storage: &Storage,
    arg1: &str,
    arg2: Option<&str>,
    edit: bool,
    clean: bool,
    sync: Option<&str>,  // Deprecated, use io_options.file instead
    io_options: &IoOptions,
    start: Instant,
) -> Result<()> {
    // Check for stdin content first
    let mut stdin_content = if !std::io::stdin().is_terminal() {
        use std::io::Read;
        let mut content = String::new();
        std::io::stdin().read_to_string(&mut content)?;
        content.trim().to_string()
    } else {
        String::new()
    };

    // Clean TUI artifacts if requested
    if clean && !stdin_content.is_empty() {
        stdin_content = clean::clean_text(&stdin_content);
    }

    // Smart detection for flexible argument order
    let is_arg1_content = arg1.contains(' ')
        || arg1.contains('{')  // Template variable
        || arg1.contains('?')  // Question
        || arg1.contains('\n') // Multiline
        || arg1.len() > 40; // Long text

    let (prompt_name, prompt_content) = match arg2 {
        Some(arg2_value) => {
            // Two arguments provided - figure out which is which
            let is_arg2_content = arg2_value.contains(' ')
                || arg2_value.contains('{')
                || arg2_value.contains('?')
                || arg2_value.len() > 40;

            match (is_arg1_content, is_arg2_content) {
                (true, false) => (arg2_value.to_string(), arg1.to_string()), // "content" name
                (false, true) => (arg1.to_string(), arg2_value.to_string()), // name "content"
                (false, false) => (arg1.to_string(), arg2_value.to_string()), // name name (second is content)
                (true, true) => {
                    // Both look like content - use first as content, generate name
                    (generate_prompt_name(arg1), arg1.to_string())
                }
            }
        }
        None => {
            // Single argument - check stdin first, then use smart detection
            if !stdin_content.is_empty() {
                (arg1.to_string(), stdin_content.clone())
            } else if is_arg1_content {
                (generate_prompt_name(arg1), arg1.to_string())
            } else {
                (arg1.to_string(), String::new())
            }
        }
    };

    let prompt_path = storage.prompt_path(&prompt_name);

    if prompt_path.exists() {
        eprintln!("Error: Prompt '{}' already exists", prompt_name);
        std::process::exit(1);
    }

    // Create prompt with metadata
    let metadata = PromptMetadata {
        id: prompt_name.clone(),
        description: if prompt_content.is_empty() {
            format!("Description for {}", prompt_name)
        } else {
            // Use first line or 50 chars as description
            prompt_content
                .lines()
                .next()
                .unwrap_or(&prompt_content)
                .chars()
                .take(50)
                .collect::<String>()
                .trim()
                .to_string()
        },
        tags: None,
        created_at: Some(chrono::Utc::now().to_rfc3339()),
        updated_at: None,
        version: None,
        git_hash: None,
        parent_version: None,
    };

    let body = if prompt_content.is_empty() {
        format!("# {}\n\nYour prompt here...", prompt_name)
    } else {
        format!("# {}\n\n{}", prompt_name, prompt_content)
    };

    storage.write_prompt(&prompt_name, &metadata, &body)?;

    if !io_options.quiet {
        // Linear-style success message - clean and minimal
        println!(
            "✓ Created {} ({}ms)",
            prompt_name,
            start.elapsed().as_millis()
        );
    }

    // If content provided, apply unified I/O with prompt name for sync
    if !prompt_content.is_empty() {
        io_options.apply_unified_io_with_prompt(storage, &prompt_content, "Created prompt content", Some(&prompt_name), start)?;
    } else if edit {
        // Open in editor if --edit flag
        let editor = env::var("EDITOR").unwrap_or_else(|_| "vim".to_string());
        println!("Opening in {}...", editor.dimmed());
        let prompt_path = storage.prompt_path(&prompt_name);

        Command::new(&editor)
            .arg(&prompt_path)
            .status()
            .context("Failed to launch editor")?;
    }
    // Note: Editor is only opened when --edit flag is explicitly provided

    // Handle file sync functionality (via -f or deprecated --sync)
    let sync_path = io_options.file.as_deref().or(sync);
    if let Some(file_path) = sync_path {
        use super::SimpleSyncManager;
        let sync_manager = SimpleSyncManager::new(storage.clone())?;
        
        let local_path = if file_path.is_empty() {
            // Smart default: ./prompt-name.md
            let file_name = if prompt_name.starts_with('@') {
                // Strip team prefix: @team/api-design -> api-design.md
                prompt_name.split('/').last().unwrap_or(&prompt_name)
            } else {
                &prompt_name
            };
            
            // Convert to kebab-case for filename
            let kebab_name = file_name.replace('_', "-").to_lowercase();
            std::env::current_dir()?.join(format!("{}.md", kebab_name))
        } else {
            // Use provided path
            if std::path::Path::new(file_path).is_absolute() {
                std::path::PathBuf::from(file_path)
            } else {
                std::env::current_dir()?.join(file_path)
            }
        };
        
        match sync_manager.sync_prompt(&prompt_name, Some(local_path.clone())) {
            Ok(created_path) => {
                if !io_options.quiet {
                    println!("🔄 Created bidirectional sync:");
                    println!("   📁 Local file: {:?}", created_path);
                    println!("   📦 PromptHive: {}", prompt_name);
                }
            }
            Err(e) => {
                if !io_options.quiet {
                    println!("⚠️  Failed to create sync: {}", e);
                    println!("💡 Create sync manually with: ph sync sync-file {:?} --name {}", local_path, prompt_name);
                }
            }
        }
    }

    Ok(())
}

#[allow(dead_code)]
pub fn handle_edit(storage: &Storage, name: &str, start: Instant) -> Result<()> {
    // Use fuzzy matching to resolve the prompt name
    let resolved_name = resolve_prompt_name(storage, name)?;
    let prompt_path = storage.prompt_path(&resolved_name);

    // Check if we can write to the file before launching editor
    #[cfg(unix)]
    {
        use std::os::unix::fs::PermissionsExt;
        if prompt_path.exists() {
            let metadata = fs::metadata(&prompt_path)?;
            let permissions = metadata.permissions();
            let mode = permissions.mode();
            
            // Check if file is writable by user
            if mode & 0o200 == 0 {
                use crate::error_help;
                let error_msg = error_help::format_permission_error(&prompt_path.display().to_string(), "edit");
                return Err(anyhow::anyhow!("{}", error_msg));
            }
        }
    }

    // Get editor from environment or config
    let editor = if cfg!(test) {
        // In test mode, always use echo to avoid hanging
        "echo".to_string()
    } else {
        env::var("EDITOR").unwrap_or_else(|_| "vim".to_string())
    };

    // Linear-style editor launch message
    println!(
        "✓ Opening in {} ({}ms)",
        editor,
        start.elapsed().as_millis()
    );

    // Launch editor
    Command::new(&editor)
        .arg(&prompt_path)
        .status()
        .with_context(|| format!("Failed to launch editor '{}'", editor))?;

    Ok(())
}

#[allow(dead_code)]
pub fn handle_show(
    storage: &Storage,
    name: &str,
    edit: bool,
    io_options: &IoOptions,
    start: Instant,
) -> Result<()> {
    // Get all prompts
    let prompt_names = storage.list_prompts()?;
    let mut prompts = Vec::new();

    for prompt_name in &prompt_names {
        if let Ok((metadata, _)) = storage.read_prompt(prompt_name) {
            prompts.push(Prompt {
                name: prompt_name.clone(),
                short_code: Matcher::generate_short_code(
                    prompt_name,
                    &prompts
                        .iter()
                        .map(|p: &Prompt| p.short_code.clone())
                        .collect::<Vec<_>>(),
                ),
                description: metadata.description,
                version: metadata.version,
                created_at: metadata.created_at,
                updated_at: metadata.updated_at,
                git_hash: metadata.git_hash,
            });
        }
    }

    // Find matching prompt
    let matcher = Matcher::new(prompts);
    let result = matcher.find(name);

    match result {
        MatchResult::Exact(prompt) => {
            // Read and display the prompt content
            let (metadata, body) = storage.read_prompt(&prompt.name)?;

            let content = body;

            // If edit flag is set, open in editor before applying I/O
            if edit {
                let prompt_path = storage.prompt_path(&prompt.name);
                let editor = env::var("EDITOR").unwrap_or_else(|_| "vim".to_string());
                if !io_options.quiet {
                    println!("Opening in {}...", editor.dimmed());
                }
                Command::new(&editor)
                    .arg(&prompt_path)
                    .status()
                    .context("Failed to launch editor")?;
                    
                // Re-read the content after editing
                let (metadata, body) = storage.read_prompt(&prompt.name)?;
                io_options.apply_display_io(&body, &prompt.name, &metadata.description, start)?;
            } else {
                io_options.apply_display_io(&content, &prompt.name, &metadata.description, start)?;
            }
        }
        _ => {
            result.display();
            std::process::exit(1);
        }
    }

    Ok(())
}

#[allow(dead_code)]
pub fn handle_delete(storage: &Storage, name: &str, start: Instant) -> Result<()> {
    // Use fuzzy matching to resolve the prompt name
    let resolved_name = resolve_prompt_name(storage, name)?;
    let prompt_path = storage.prompt_path(&resolved_name);

    // Read the prompt to show description
    let (metadata, body) = storage.read_prompt(&resolved_name)?;

    // Show prompt details for confirmation
    println!("\n{}", "About to delete:".yellow().bold());
    println!("  {} - {}", resolved_name.bold(), metadata.description);

    // Show first few lines of content
    let preview = body.lines().take(3).collect::<Vec<_>>().join("\n  ");
    if !preview.trim().is_empty() {
        println!("  {}", preview.dimmed());
        if body.lines().count() > 3 {
            println!("  {}", "...".dimmed());
        }
    }

    println!("\n{}", format!("Delete '{}'? [y/N] ", resolved_name).red());

    let mut input = String::new();
    std::io::stdin().read_line(&mut input)?;

    if input.trim().to_lowercase() != "y" {
        println!("Deletion cancelled");
        return Ok(());
    }

    // Delete the file
    fs::remove_file(&prompt_path)?;

    // Linear-style deletion confirmation
    println!(
        "✓ Deleted {} ({}ms)",
        resolved_name,
        start.elapsed().as_millis()
    );

    Ok(())
}

#[allow(dead_code)]
pub fn handle_ls(storage: &Storage, io_options: &IoOptions, start: Instant) -> Result<()> {
    let prompts = storage.list_prompts()?;

    // Build content string for I/O operations
    let mut content = String::new();

    if prompts.is_empty() {
        content = "No prompts yet. Create one with: ph new <name>".to_string();
    } else {
        for prompt_name in &prompts {
            if let Ok((metadata, _)) = storage.read_prompt(prompt_name) {
                content.push_str(&format!("{:<20} - {}\n", prompt_name, metadata.description));
            }
        }
        content = content.trim_end().to_string(); // Remove trailing newline
    }

    // Apply unified I/O
    io_options.apply_unified_io(storage, &content, "Prompt list", start)?;

    // Display to terminal if not quiet and TTY
    if !io_options.quiet && std::io::stdout().is_terminal() {
        println!(
            "📋 {} ({}ms)\n",
            "Your prompts:".green(),
            start.elapsed().as_millis()
        );

        if prompts.is_empty() {
            println!(
                "  No prompts yet. Create one with: {} new <name>",
                "ph".bold()
            );
        } else {
            for prompt_name in prompts {
                if let Ok((metadata, _)) = storage.read_prompt(&prompt_name) {
                    println!("  {:<20} - {}", prompt_name.bold(), metadata.description);
                }
            }
        }
    }

    Ok(())
}

#[allow(dead_code)]
pub fn handle_find(
    storage: &Storage,
    query: &str,
    io_options: &IoOptions,
    start: Instant,
) -> Result<()> {
    let prompt_names = storage.list_prompts()?;
    let mut prompts = Vec::new();

    // Build prompt list with metadata
    for prompt_name in &prompt_names {
        if let Ok((metadata, _)) = storage.read_prompt(prompt_name) {
            prompts.push(Prompt {
                name: prompt_name.clone(),
                short_code: Matcher::generate_short_code(
                    prompt_name,
                    &prompts
                        .iter()
                        .map(|p: &Prompt| p.short_code.clone())
                        .collect::<Vec<_>>(),
                ),
                description: metadata.description,
                version: metadata.version,
                created_at: metadata.created_at,
                updated_at: metadata.updated_at,
                git_hash: metadata.git_hash,
            });
        }
    }

    // Use fuzzy matcher for search
    let fuzzy = SkimMatcherV2::default();
    let mut matches: Vec<_> = prompts
        .iter()
        .filter_map(|prompt| {
            // Match against name or description
            let name_score = fuzzy.fuzzy_match(&prompt.name, query);
            let desc_score = fuzzy.fuzzy_match(&prompt.description, query);

            // Take the best score
            let best_score = match (name_score, desc_score) {
                (Some(n), Some(d)) => Some(n.max(d)),
                (Some(n), None) => Some(n),
                (None, Some(d)) => Some(d),
                (None, None) => None,
            };

            best_score.map(|score| (prompt, score))
        })
        .collect();

    // Sort by score (highest first)
    matches.sort_by(|a, b| b.1.cmp(&a.1));

    // Build content string for I/O operations
    let content = if matches.is_empty() {
        format!("No prompts found matching '{}'\n", query)
    } else {
        matches
            .iter()
            .map(|(prompt, _)| format!("{:<20} - {}", prompt.name, prompt.description))
            .collect::<Vec<_>>()
            .join("\n")
    };

    // Apply unified I/O
    io_options.apply_unified_io(storage, &content, "Search results", start)?;

    // Display to terminal if not quiet and TTY
    if !io_options.quiet && std::io::stdout().is_terminal() {
        println!(
            "🔍 {} '{}' ({}ms)\n",
            "Searching for".green(),
            query.bold(),
            start.elapsed().as_millis()
        );

        if matches.is_empty() {
            println!("  No prompts found matching '{}'", query);
        } else {
            for (prompt, _) in matches {
                println!("  {:<20} - {}", prompt.name.bold(), prompt.description);
            }
        }
    }

    Ok(())
}

#[allow(dead_code)]
pub fn handle_rename(
    storage: &Storage,
    old_name: &str,
    new_name: &str,
    start: Instant,
) -> Result<()> {
    // Resolve old name with fuzzy matching
    let resolved_old = resolve_prompt_name(storage, old_name)?;

    // Check if new name already exists
    if storage.prompt_exists(new_name) {
        eprintln!("Error: Prompt '{}' already exists", new_name);
        std::process::exit(1);
    }

    // Read the old prompt
    let (metadata, body) = storage.read_prompt(&resolved_old)?;

    // Write to new location
    storage.write_prompt(new_name, &metadata, &body)?;

    // Delete old prompt
    storage.delete_prompt(&resolved_old)?;

    // Linear-style rename confirmation
    println!(
        "✓ Renamed {}{} ({}ms)",
        resolved_old,
        new_name,
        start.elapsed().as_millis()
    );

    Ok(())
}

// Helper function to generate prompt name from content
#[allow(dead_code)]
fn generate_prompt_name(content: &str) -> String {
    let stop_words = [
        "the", "a", "an", "and", "or", "but", "in", "on", "at", "to", "for", "of", "with", "by",
        "from", "as", "is", "was", "are", "were",
    ];

    let words: Vec<&str> = content
        .split_whitespace()
        .take(7) // Take first 7 words
        .filter(|w| !stop_words.contains(&w.to_lowercase().as_str()))
        .collect();

    let name = words
        .join("-")
        .to_lowercase()
        .chars()
        .filter(|c| c.is_alphanumeric() || *c == '-')
        .collect::<String>();

    // Truncate to reasonable length
    name.chars().take(30).collect()
}

// Helper function to resolve a prompt name using fuzzy matching
#[allow(dead_code)]
pub fn resolve_prompt_name(storage: &Storage, query: &str) -> Result<String> {
    // Check if query contains bank syntax (bank/prompt)
    if query.contains('/') {
        let parts: Vec<&str> = query.splitn(2, '/').collect();
        if parts.len() == 2 {
            let bank = parts[0];
            let prompt = parts[1];

            // First try exact match
            let bank_prompt = format!("{}/{}", bank, prompt);
            if storage.prompt_exists(&bank_prompt) {
                return Ok(bank_prompt);
            }

            // Then try fuzzy matching within the bank
            let bank_prompts = storage.list_bank_prompts(bank)?;
            if !bank_prompts.is_empty() {
                let fuzzy = SkimMatcherV2::default();
                let mut best_match = None;
                let mut best_score = 0;

                for bank_prompt_name in &bank_prompts {
                    // Extract just the prompt name part for matching
                    let prompt_part = bank_prompt_name
                        .split('/')
                        .next_back()
                        .unwrap_or(bank_prompt_name);
                    if let Some(score) = fuzzy.fuzzy_match(prompt_part, prompt) {
                        if score > best_score {
                            best_score = score;
                            best_match = Some(bank_prompt_name.clone());
                        }
                    }
                }

                if let Some(matched) = best_match {
                    return Ok(matched);
                }
            }
        }
    }

    // Regular prompt resolution (no bank specified)
    let prompt_names = storage.list_prompts()?;
    let mut prompts = Vec::new();

    for prompt_name in &prompt_names {
        if let Ok((metadata, _)) = storage.read_prompt(prompt_name) {
            prompts.push(Prompt {
                name: prompt_name.clone(),
                short_code: Matcher::generate_short_code(
                    prompt_name,
                    &prompts
                        .iter()
                        .map(|p: &Prompt| p.short_code.clone())
                        .collect::<Vec<_>>(),
                ),
                description: metadata.description,
                version: metadata.version,
                created_at: metadata.created_at,
                updated_at: metadata.updated_at,
                git_hash: metadata.git_hash,
            });
        }
    }

    let matcher = Matcher::new(prompts);
    match matcher.find(query) {
        MatchResult::Exact(prompt) => Ok(prompt.name),
        MatchResult::Multiple(suggestions) => {
            eprintln!("Error: Multiple matches. Did you mean:");
            for prompt in suggestions {
                eprintln!(
                    "  {:<12} ({}) - {}",
                    prompt.name.bold(),
                    prompt.short_code.dimmed(),
                    prompt.description
                );
            }
            std::process::exit(1);
        }
        MatchResult::None => {
            eprintln!("Error: No prompt found matching '{}'", query);
            std::process::exit(1);
        }
    }
}

#[cfg(feature = "import")]
#[allow(dead_code)]
#[allow(clippy::too_many_arguments)]
pub fn handle_import(
    storage: &Storage,
    path: &str,
    custom_name: Option<&str>,
    force: bool,
    version: bool,
    skip: bool,
    update: bool,
    start: Instant,
) -> Result<()> {
    let importer = crate::Importer::new(storage.clone());
    let result =
        importer.import_from_path_enhanced(path, custom_name, force, version, skip, update)?;

    result.display();
    println!(
        "\n⏱️  {} ({}ms)",
        result.summary().green(),
        start.elapsed().as_millis()
    );

    Ok(())
}

#[allow(dead_code)]
pub fn handle_compose(
    storage: &Storage,
    prompts: &str,
    input: Option<&str>,
    edit: bool,
    io_options: &IoOptions,
    start: Instant,
) -> Result<()> {
    let prompt_names = parse_prompt_list(prompts);

    if prompt_names.is_empty() {
        eprintln!("Error: No prompts specified");
        std::process::exit(1);
    }

    let composer = Composer::new(storage.clone());
    let result = composer.compose_and_return(&prompt_names, input.map(|s| s.to_string()), edit)?;

    // Apply unified I/O using IoOptions
    io_options.apply_unified_io(storage, &result, "Composed prompt result", start)?;

    // Show composition summary if not quiet
    if !io_options.quiet {
        println!(
            "🔗 {} {} ({}ms)",
            "Composed".green(),
            prompts.bold(),
            start.elapsed().as_millis()
        );
    }

    Ok(())
}