linthis 0.20.0

A fast, cross-platform multi-language linter and formatter
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
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
// Copyright 2024 zhlinh and linthis Project Authors. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found at
//
// https://opensource.org/license/MIT
//
// The above copyright notice and this permission
// notice shall be included in all copies or
// substantial portions of the Software.

//! Fix subcommand handler for reviewing and fixing lint issues.
//!
//! This module handles the `fix` subcommand, supporting:
//! - Loading results from previous runs
//! - Running check/format first then fixing
//! - Interactive review mode
//! - AI-powered fix suggestions (batch and single-file modes)

use colored::Colorize;
use std::fs;
use std::path::{Path, PathBuf};
use std::process::ExitCode;

use crate::cli::backup::{collect_files_from_issues, create_backup, handle_list_backups};
use crate::cli::helpers::{find_latest_result_file, resolve_ai_provider};
use crate::cli::recheck::{
    print_recheck_footer, print_recheck_header, print_recheck_summary, recheck_modified_files,
};
use linthis::ai::{AiProvider, AiProviderConfig, AiProviderKind, AiSuggester, SuggestionOptions};
use linthis::config::Config;
use linthis::interactive::{run_ai_fix_all, run_interactive, AiFixConfig};
use linthis::utils::types::LintIssue;

/// Options for the fix subcommand
pub struct FixCommandOptions {
    /// Source of lint results: "last" or a file path
    pub source: String,
    /// Run lint check first
    pub check: bool,
    /// Run format only first
    pub format_only: bool,
    /// Enable AI mode
    pub ai: bool,
    /// AI provider name
    pub provider: Option<String>,
    /// AI model name
    pub model: Option<String>,
    /// Extra arguments passed to the AI provider CLI
    pub provider_args: Option<String>,
    /// Max suggestions per issue
    pub max_suggestions: usize,
    /// Auto-apply suggestions
    pub accept_all: bool,
    /// Number of parallel jobs (0 = sequential)
    pub jobs: usize,
    /// Target specific file (for single-file AI mode)
    pub file: Option<PathBuf>,
    /// Target specific line
    pub line: Option<u32>,
    /// Issue message for context
    pub message: Option<String>,
    /// Rule ID for context
    pub rule: Option<String>,
    /// Output format (human, json, diff)
    pub output: String,
    /// Include code context in output
    pub with_context: bool,
    /// Verbose output
    pub verbose: bool,
    /// Quiet mode
    pub quiet: bool,
    /// Undo last fix (restore from backup)
    pub undo: bool,
    /// List available backups
    pub list_backups: bool,
}

/// Handle the fix subcommand
pub fn handle_fix_command(mut options: FixCommandOptions) -> ExitCode {
    // Handle --list-backups
    if options.list_backups {
        return handle_list_backups("linthis fix");
    }

    // Handle --undo (redirects to unified undo with fix filter)
    if options.undo {
        let filter = if options.source == "last" {
            "fix".to_string()
        } else {
            options.source.clone()
        };
        return super::backup::handle_undo_filtered(&filter);
    }

    // Load config for AI settings
    let project_root = linthis::utils::get_project_root();
    let config = Config::load_merged(&project_root);

    // Interactive provider selection when --ai without --provider
    if options.ai
        && options.provider.is_none()
        && std::env::var("LINTHIS_AI_PROVIDER").is_err()
        && config.ai.provider.is_none()
        && std::io::IsTerminal::is_terminal(&std::io::stdin())
    {
        if let Some(provider) = super::helpers::select_ai_provider_interactive() {
            options.provider = Some(provider);
        } else {
            println!("Fix cancelled");
            return ExitCode::SUCCESS;
        }
    }

    // If --check or --format-only is specified, run lint first
    if options.check || options.format_only {
        return handle_fix_with_lint(&options, &config);
    }

    // If single file mode (--ai with -i/--include and --line)
    if options.ai && options.file.is_some() && options.line.is_some() {
        return handle_single_file_ai_fix(&options, &config);
    }

    // Load from result file and fix
    handle_fix_from_result(&options, &config)
}

/// Handle fix with running lint first
fn handle_fix_with_lint(options: &FixCommandOptions, config: &Config) -> ExitCode {
    use linthis::{run, RunMode, RunOptions};

    let mode = if options.format_only {
        RunMode::FormatOnly
    } else {
        RunMode::CheckOnly
    };

    if !options.quiet {
        println!(
            "{} Running {} first...",
            "→".cyan(),
            if options.format_only {
                "format"
            } else {
                "check"
            }
        );
    }

    // Run lint/format
    let run_options = RunOptions {
        paths: vec![PathBuf::from(".")],
        mode,
        languages: vec![],
        exclude_patterns: vec![],
        verbose: options.verbose,
        quiet: options.quiet,
        plugins: vec![],
        no_cache: false,
        config_resolver: None,
        tool_install_mode: linthis::ToolInstallMode::Prompt,
        hook_event: None,
    };

    match run(&run_options) {
        Ok(result) => {
            if result.issues.is_empty() {
                if !options.quiet {
                    println!("{}", "No issues found.".green());
                }
                return ExitCode::SUCCESS;
            }

            if !options.quiet {
                println!(
                    "  Found {} issue{}\n",
                    result.issues.len(),
                    if result.issues.len() == 1 { "" } else { "s" }
                );
            }

            // Create backup before making changes
            let files_to_backup = collect_files_from_issues(&result.issues);
            let _backup_id = create_backup(&files_to_backup, "linthis fix -c", options.quiet);
            if !options.quiet && !files_to_backup.is_empty() {
                println!();
            }

            // For AI mode with accept_all, use iterative fix loop
            if options.ai && options.accept_all {
                return run_ai_fix_loop(options, config, result);
            }

            // Enter fix mode
            let (modified_files, fixed_count) = if options.ai {
                let provider =
                    resolve_ai_provider(options.provider.as_deref(), config.ai.provider.as_deref());
                let ai_config = AiFixConfig::with_provider(&provider)
                    .with_model(options.model.clone())
                    .with_accept_all(options.accept_all)
                    .with_verbose(options.verbose)
                    .with_parallel(options.jobs);

                let ai_result = run_ai_fix_all(&result, &ai_config);
                (ai_result.modified_files, ai_result.applied)
            } else {
                let interactive_result = run_interactive(&result);
                let count = interactive_result.edited + interactive_result.ignored;
                (interactive_result.modified_files, count)
            };

            // Recheck modified files
            if !modified_files.is_empty() {
                print_recheck_header();
                let recheck_result = recheck_modified_files(
                    &modified_files,
                    &result.issues,
                    options.quiet,
                    options.verbose,
                );
                print_recheck_summary(&recheck_result, fixed_count);
                print_recheck_footer();
            }

            ExitCode::from(result.exit_code as u8)
        }
        Err(e) => {
            eprintln!("{}: {}", "Error".red(), e);
            ExitCode::from(2)
        }
    }
}

/// Maximum number of AI fix iterations to prevent infinite loops
const MAX_AI_FIX_ITERATIONS: usize = 100;

/// Handle fix by loading from result file
/// Resolve the result file path from the source option.
fn resolve_result_path(source: &str) -> Result<PathBuf, ExitCode> {
    let path = if source == "last" {
        match find_latest_result_file() {
            Some(p) => p,
            None => {
                let project_root = linthis::utils::get_project_root();
                let result_dir = project_root.join(".linthis").join("result");
                eprintln!(
                    "{}: No result files found in {}",
                    "Error".red(),
                    result_dir.display()
                );
                eprintln!(
                    "  Run {} first to generate a result file.",
                    "linthis -c".cyan()
                );
                return Err(ExitCode::from(1));
            }
        }
    } else {
        PathBuf::from(source)
    };

    if !path.exists() {
        eprintln!(
            "{}: Result file not found: {}",
            "Error".red(),
            path.display()
        );
        return Err(ExitCode::from(1));
    }
    Ok(path)
}

/// Count issues by type (lint, security, complexity) and print summary.
fn print_issue_type_summary(issues: &[LintIssue]) {
    let lint_count = issues
        .iter()
        .filter(|i| {
            !i.source.as_deref().unwrap_or("").starts_with("security/")
                && i.source.as_deref() != Some("linthis-complexity")
        })
        .count();
    let sec_count = issues
        .iter()
        .filter(|i| i.source.as_deref().unwrap_or("").starts_with("security/"))
        .count();
    let cx_count = issues
        .iter()
        .filter(|i| i.source.as_deref() == Some("linthis-complexity"))
        .count();

    let mut parts = Vec::new();
    if lint_count > 0 {
        parts.push(format!("{} lint", lint_count));
    }
    if sec_count > 0 {
        parts.push(format!("{} security", sec_count));
    }
    if cx_count > 0 {
        parts.push(format!("{} complexity", cx_count));
    }
    println!(
        "  Found {} issue{} from previous run ({})\n",
        issues.len(),
        if issues.len() == 1 { "" } else { "s" },
        parts.join(", "),
    );
}

/// Run fix logic (AI or interactive) and recheck modified files.
fn run_fix_and_recheck(
    options: &FixCommandOptions,
    config: &Config,
    result: &linthis::utils::types::RunResult,
) {
    // Create backup before making changes
    let files_to_backup = collect_files_from_issues(&result.issues);
    let _backup_id = create_backup(&files_to_backup, "linthis fix", options.quiet);
    if !options.quiet && !files_to_backup.is_empty() {
        println!();
    }

    let (modified_files, fixed_count) = if options.ai {
        let provider =
            resolve_ai_provider(options.provider.as_deref(), config.ai.provider.as_deref());
        let ai_config = AiFixConfig::with_provider(&provider)
            .with_model(options.model.clone())
            .with_accept_all(options.accept_all)
            .with_verbose(options.verbose)
            .with_parallel(options.jobs);

        let ai_result = run_ai_fix_all(result, &ai_config);
        (ai_result.modified_files, ai_result.applied)
    } else {
        let interactive_result = run_interactive(result);
        let count = interactive_result.edited + interactive_result.ignored;
        (interactive_result.modified_files, count)
    };

    if !modified_files.is_empty() {
        print_recheck_header();
        let recheck_result = recheck_modified_files(
            &modified_files,
            &result.issues,
            options.quiet,
            options.verbose,
        );
        print_recheck_summary(&recheck_result, fixed_count);
        print_recheck_footer();
    }
}

fn handle_fix_from_result(options: &FixCommandOptions, config: &Config) -> ExitCode {
    let path = match resolve_result_path(&options.source) {
        Ok(p) => p,
        Err(code) => return code,
    };

    if !options.quiet {
        println!("{} Loading results from: {}", "→".cyan(), path.display());
    }

    match linthis::reports::load_result_from_file(&path) {
        Some(mut result) => {
            result.merge_all_check_issues();

            if result.issues.is_empty() {
                if !options.quiet {
                    println!("{}", "No issues in the saved result.".green());
                }
                return ExitCode::SUCCESS;
            }

            if !options.quiet {
                print_issue_type_summary(&result.issues);
            }

            if options.ai && options.accept_all {
                return run_ai_fix_loop(options, config, result);
            }

            run_fix_and_recheck(options, config, &result);
            ExitCode::from(result.exit_code as u8)
        }
        None => {
            eprintln!(
                "{}: Failed to load result file: {}",
                "Error".red(),
                path.display()
            );
            eprintln!("  Make sure the file is a valid JSON result file.");
            eprintln!(
                "  Run {} first to generate a result file.",
                "linthis -c".cyan()
            );
            ExitCode::from(2)
        }
    }
}

/// Run AI fix in a loop until no issues remain or max iterations reached
/// Build an `AiFixConfig` from the command options and resolved provider.
fn build_ai_fix_config(
    options: &FixCommandOptions,
    config: &Config,
    accept_all: bool,
) -> AiFixConfig {
    let provider = resolve_ai_provider(options.provider.as_deref(), config.ai.provider.as_deref());
    AiFixConfig::with_provider(&provider)
        .with_model(options.model.clone())
        .with_accept_all(accept_all)
        .with_verbose(options.verbose)
        .with_parallel(options.jobs)
}

/// Re-run lint checks on modified files and return the new result.
///
/// Returns `Some(result)` on success or `None` on recheck failure.
fn recheck_modified_paths(
    modified_files: &std::collections::HashSet<PathBuf>,
    verbose: bool,
    quiet: bool,
) -> Option<linthis::utils::types::RunResult> {
    use linthis::{run, RunMode, RunOptions};

    let modified_paths: Vec<PathBuf> = modified_files.iter().cloned().collect();
    if modified_paths.is_empty() {
        return None;
    }

    if !quiet {
        println!(
            "\n{} Re-checking {} modified file{}...",
            "→".cyan(),
            modified_paths.len(),
            if modified_paths.len() == 1 { "" } else { "s" }
        );
    }

    let run_options = RunOptions {
        paths: modified_paths,
        mode: RunMode::CheckOnly,
        languages: vec![],
        exclude_patterns: vec![],
        verbose,
        quiet: true,
        plugins: vec![],
        no_cache: true,
        config_resolver: None,
        tool_install_mode: linthis::ToolInstallMode::Disabled,
        hook_event: None,
    };

    match run(&run_options) {
        Ok(result) => Some(result),
        Err(e) => {
            eprintln!("{}: Re-check failed: {}", "Error".red(), e);
            None
        }
    }
}

/// Print the final summary for an AI fix loop.
fn print_ai_fix_summary(
    current_result: &linthis::utils::types::RunResult,
    iteration: usize,
    total_fixed: usize,
    elapsed: std::time::Duration,
) {
    let elapsed_str = format_duration(elapsed);
    println!("\n{}", "─".repeat(50));
    println!(
        "{} AI Fix completed after {} iteration{}",
        "→".cyan(),
        iteration,
        if iteration == 1 { "" } else { "s" }
    );
    println!("  Total fixes applied: {}", total_fixed.to_string().cyan());
    println!("  Total time: {}", elapsed_str.cyan());

    if !current_result.issues.is_empty() {
        println!(
            "  {} remaining issue{}",
            current_result.issues.len().to_string().yellow(),
            if current_result.issues.len() == 1 {
                ""
            } else {
                "s"
            }
        );
        println!(
            "\n  Run {} to see remaining issues",
            "linthis report show".cyan()
        );
    }

    println!("\n  To undo: {}", "linthis fix --undo".cyan());
}

/// Print the iteration header showing the current iteration number and
/// remaining issue count.
fn print_iteration_header(iteration: usize, issue_count: usize) {
    println!(
        "\n{} AI Fix Iteration {} / {}",
        "→".cyan().bold(),
        iteration,
        MAX_AI_FIX_ITERATIONS
    );
    println!(
        "  {} issue{} to fix",
        issue_count,
        if issue_count == 1 { "" } else { "s" }
    );
}

/// Print the "all issues fixed" success banner.
fn print_all_fixed_banner(iteration: usize, total_fixed: usize, elapsed: std::time::Duration) {
    let elapsed_str = format_duration(elapsed);
    println!(
        "\n{} All issues fixed after {} iteration{}!",
        "✓".green().bold(),
        iteration,
        if iteration == 1 { "" } else { "s" }
    );
    println!("  Total fixes applied: {}", total_fixed.to_string().cyan());
    println!("  Total time: {}", elapsed_str.cyan());
}

/// Outcome of a single AI fix iteration, used to decide whether the loop
/// should continue.
enum IterationOutcome {
    /// All issues have been resolved.
    AllFixed,
    /// Some issues remain; continue with the updated result.
    Continue(Box<linthis::utils::types::RunResult>),
    /// No further progress can be made; stop the loop.
    Stop,
}

/// Execute one iteration of the AI fix loop.  Returns an
/// `IterationOutcome` that tells the caller what to do next.
fn run_single_ai_iteration(
    current_result: &linthis::utils::types::RunResult,
    options: &FixCommandOptions,
    config: &Config,
    iteration: usize,
) -> (usize, IterationOutcome) {
    let ai_config = build_ai_fix_config(options, config, true);
    let ai_result = run_ai_fix_all(current_result, &ai_config);
    let applied = ai_result.applied;

    if ai_result.modified_files.is_empty() {
        if !options.quiet {
            println!("  {} No files modified in this iteration", "âš ".yellow());
        }
        return (applied, IterationOutcome::Stop);
    }

    if !options.quiet {
        println!(
            "  {} Applied {} fix{}",
            "✓".green(),
            applied,
            if applied == 1 { "" } else { "es" }
        );
    }

    if iteration >= MAX_AI_FIX_ITERATIONS {
        if !options.quiet {
            println!(
                "\n{} Reached maximum iterations ({})",
                "âš ".yellow(),
                MAX_AI_FIX_ITERATIONS
            );
        }
        return (applied, IterationOutcome::Stop);
    }

    match recheck_modified_paths(&ai_result.modified_files, options.verbose, options.quiet) {
        Some(result) if result.issues.is_empty() => (applied, IterationOutcome::AllFixed),
        Some(result) => {
            if !options.quiet {
                println!(
                    "  {} remaining issue{}",
                    result.issues.len(),
                    if result.issues.len() == 1 { "" } else { "s" }
                );
            }
            (applied, IterationOutcome::Continue(Box::new(result)))
        }
        None => (applied, IterationOutcome::Stop),
    }
}

fn run_ai_fix_loop(
    options: &FixCommandOptions,
    config: &Config,
    initial_result: linthis::utils::types::RunResult,
) -> ExitCode {
    let files_to_backup = collect_files_from_issues(&initial_result.issues);
    let _backup_id = create_backup(&files_to_backup, "AI fix with --yes", options.quiet);

    if !options.quiet {
        println!();
    }

    let mut current_result = initial_result;
    let mut iteration = 0;
    let mut total_fixed = 0;
    let start_time = std::time::Instant::now();

    loop {
        iteration += 1;

        if !options.quiet {
            print_iteration_header(iteration, current_result.issues.len());
        }

        let (applied, outcome) =
            run_single_ai_iteration(&current_result, options, config, iteration);
        total_fixed += applied;

        match outcome {
            IterationOutcome::AllFixed => {
                if !options.quiet {
                    print_all_fixed_banner(iteration, total_fixed, start_time.elapsed());
                }
                return ExitCode::SUCCESS;
            }
            IterationOutcome::Continue(result) => {
                current_result = *result;
            }
            IterationOutcome::Stop => break,
        }
    }

    if !options.quiet {
        print_ai_fix_summary(
            &current_result,
            iteration,
            total_fixed,
            start_time.elapsed(),
        );
    }

    if current_result.issues.is_empty() {
        ExitCode::SUCCESS
    } else {
        ExitCode::from(1)
    }
}

/// Format a duration into a human-readable string (e.g. "3m 25s")
fn format_duration(duration: std::time::Duration) -> String {
    let total_secs = duration.as_secs();
    if total_secs >= 60 {
        format!("{}m {}s", total_secs / 60, total_secs % 60)
    } else {
        format!("{}s", total_secs)
    }
}

/// Handle single file AI fix mode
/// Build an `AiProviderConfig` from the provider kind.
fn build_provider_config(provider_kind: &AiProviderKind) -> AiProviderConfig {
    match provider_kind {
        AiProviderKind::Claude => AiProviderConfig::claude(),
        AiProviderKind::ClaudeCli => AiProviderConfig::claude_cli(),
        AiProviderKind::CodeBuddy => AiProviderConfig::codebuddy(),
        AiProviderKind::CodeBuddyCli => AiProviderConfig::codebuddy_cli(),
        AiProviderKind::OpenAi => AiProviderConfig::openai(),
        AiProviderKind::CodexCli => AiProviderConfig::codex_cli(),
        AiProviderKind::Gemini => AiProviderConfig::gemini(),
        AiProviderKind::GeminiCli => AiProviderConfig::gemini_cli(),
        AiProviderKind::Local => AiProviderConfig::local(),
        AiProviderKind::Custom(name) => AiProviderConfig {
            kind: AiProviderKind::Custom(name.clone()),
            ..AiProviderConfig::default()
        },
        AiProviderKind::Mock => AiProviderConfig::mock(),
    }
}

/// Resolve the API key for a given provider kind from environment variables.
fn resolve_api_key(provider_kind: &AiProviderKind) -> Option<String> {
    match provider_kind {
        AiProviderKind::Claude => std::env::var("ANTHROPIC_AUTH_TOKEN")
            .or_else(|_| std::env::var("ANTHROPIC_API_KEY"))
            .ok(),
        AiProviderKind::CodeBuddy => std::env::var("CODEBUDDY_API_KEY").ok(),
        AiProviderKind::OpenAi | AiProviderKind::CodexCli => std::env::var("OPENAI_API_KEY").ok(),
        AiProviderKind::Gemini => std::env::var("GEMINI_API_KEY")
            .or_else(|_| std::env::var("GOOGLE_API_KEY"))
            .ok(),
        _ => None,
    }
}

/// Resolve the endpoint override for a given provider kind from environment variables.
fn resolve_endpoint(provider_kind: &AiProviderKind) -> Option<String> {
    match provider_kind {
        AiProviderKind::Claude => std::env::var("ANTHROPIC_BASE_URL").ok(),
        AiProviderKind::CodeBuddy => std::env::var("CODEBUDDY_BASE_URL").ok(),
        _ => None,
    }
}

/// Create an `AiSuggester` from the command options and app config.
fn create_suggester(
    options: &FixCommandOptions,
    app_config: &Config,
) -> (AiSuggester, AiProviderKind) {
    let provider_str = resolve_ai_provider(
        options.provider.as_deref(),
        app_config.ai.provider.as_deref(),
    );
    let provider_kind: AiProviderKind = provider_str.parse().unwrap_or_default();

    let mut prov_config = build_provider_config(&provider_kind);
    if let Some(ref model) = options.model {
        prov_config.model = model.clone();
    }
    if let Some(ref pa) = options.provider_args {
        prov_config.extra_args = pa.split_whitespace().map(|s| s.to_string()).collect();
    }
    prov_config.api_key = resolve_api_key(&provider_kind);
    prov_config.endpoint = resolve_endpoint(&provider_kind);

    let provider = AiProvider::new(prov_config);
    (AiSuggester::with_provider(provider), provider_kind)
}

/// Print a help message when the AI provider is unavailable.
fn print_provider_unavailable_hint(provider_kind: &AiProviderKind) {
    match provider_kind {
        AiProviderKind::Claude => {
            eprintln!("Set ANTHROPIC_AUTH_TOKEN or ANTHROPIC_API_KEY environment variable");
        }
        AiProviderKind::ClaudeCli => {
            eprintln!("Install Claude CLI (claude command must be available)");
        }
        AiProviderKind::OpenAi => {
            eprintln!("Set OPENAI_API_KEY environment variable");
        }
        AiProviderKind::CodexCli => {
            eprintln!("Install Codex CLI (npm install -g @openai/codex)");
        }
        AiProviderKind::Gemini => {
            eprintln!("Set GEMINI_API_KEY or GOOGLE_API_KEY environment variable");
        }
        AiProviderKind::GeminiCli => {
            eprintln!("Install Gemini CLI (npm install -g @google/gemini-cli)");
        }
        AiProviderKind::Local => {
            eprintln!("Set LINTHIS_AI_ENDPOINT environment variable");
        }
        _ => {}
    }
}

/// Try to auto-apply the first suggestion from a successful result.
fn try_auto_apply(
    result: &linthis::ai::SuggestionResult,
    file_path: &Path,
    line_number: u32,
    message: &str,
    rule_id: &str,
    accept_all: bool,
) -> Option<ExitCode> {
    if !accept_all || result.suggestions.is_empty() {
        return None;
    }

    let suggestion = result.suggestions.first()?;
    let issue = LintIssue {
        file_path: file_path.to_path_buf(),
        line: line_number as usize,
        column: None,
        severity: linthis::utils::types::Severity::Error,
        message: message.to_string(),
        code: Some(rule_id.to_string()),
        source: Some("ai-fix".to_string()),
        language: None,
        suggestion: None,
        code_line: None,
        context_before: vec![],
        context_after: vec![],
    };

    if apply_suggestion(&issue, suggestion) {
        println!("{} Applied suggestion!", "✓".green());
        None
    } else {
        eprintln!("{} Failed to apply suggestion.", "✗".red());
        Some(ExitCode::FAILURE)
    }
}

fn handle_single_file_ai_fix(options: &FixCommandOptions, config: &Config) -> ExitCode {
    let file_path = options.file.as_ref().unwrap();
    let line_number = options.line.unwrap();

    let (suggester, provider_kind) = create_suggester(options, config);

    if !suggester.is_available() {
        eprintln!(
            "{}: AI provider {} is not available",
            "Error".red(),
            suggester.provider_name()
        );
        print_provider_unavailable_hint(&provider_kind);
        return ExitCode::FAILURE;
    }

    if options.verbose {
        println!(
            "Using AI provider: {} ({})",
            suggester.provider_name(),
            suggester.model_name()
        );
    }

    let suggestion_options = SuggestionOptions {
        max_suggestions: options.max_suggestions,
        include_explanation: true,
        include_confidence: true,
        ..Default::default()
    };

    let message = options.message.as_deref().unwrap_or("Issue at this line");
    let rule_id = options.rule.as_deref().unwrap_or("UNKNOWN");

    if options.verbose {
        println!(
            "Generating suggestions for {}:{}",
            file_path.display(),
            line_number
        );
    }

    let result = suggester.suggest_fix_for_file(
        file_path,
        line_number as usize,
        message,
        rule_id,
        &suggestion_options,
    );

    format_single_result(&result, &options.output, options.with_context);

    if result.is_success() {
        if let Some(code) = try_auto_apply(
            &result,
            file_path,
            line_number,
            message,
            rule_id,
            options.accept_all,
        ) {
            return code;
        }
        ExitCode::SUCCESS
    } else {
        ExitCode::FAILURE
    }
}

/// Format a single suggestion result
fn format_single_result(result: &linthis::ai::SuggestionResult, format: &str, with_context: bool) {
    match format {
        "json" => {
            let json = serde_json::to_string_pretty(result).unwrap_or_default();
            println!("{}", json);
        }
        "diff" => {
            if !result.suggestions.is_empty() {
                println!("--- a/{}", result.file_path);
                println!("+++ b/{}", result.file_path);
                for suggestion in &result.suggestions {
                    println!(
                        "@@ -{},{} +{},{} @@",
                        result.line_number, 1, result.line_number, 1
                    );
                    println!(
                        "-{}",
                        result
                            .context
                            .as_ref()
                            .map(|c| c.issue_lines.as_str())
                            .unwrap_or("")
                    );
                    println!("+{}", suggestion.code.lines().next().unwrap_or(""));
                }
            }
        }
        _ => {
            // Human-readable format
            println!("{}:{}", result.file_path, result.line_number);
            println!("  Issue: {}", result.message);

            if let Some(ref err) = result.error {
                println!("  {}: {}", "Error".red(), err);
                return;
            }

            if with_context {
                if let Some(ref ctx) = result.context {
                    println!("  Context:");
                    println!("  ```{}", ctx.language);
                    for line in ctx.full_snippet.lines().take(10) {
                        println!("  {}", line);
                    }
                    println!("  ```");
                }
            }

            if result.suggestions.is_empty() {
                println!("  {}", "No suggestions generated.".yellow());
            } else {
                for (idx, suggestion) in result.suggestions.iter().enumerate() {
                    println!(
                        "  {} {}:",
                        format!("[{}]", idx + 1).cyan(),
                        "Suggestion".bold()
                    );
                    println!("  ```{}", suggestion.language);
                    for line in suggestion.code.lines() {
                        println!("    {}", line.green());
                    }
                    println!("  ```");

                    if let Some(ref exp) = suggestion.explanation {
                        println!("  Explanation: {}", exp);
                    }
                    if let Some(conf) = suggestion.confidence {
                        println!("  Confidence: {:.0}%", conf * 100.0);
                    }
                }
            }
            println!();
        }
    }
}

/// Apply a suggestion to a file
fn apply_suggestion(issue: &LintIssue, suggestion: &linthis::ai::FixSuggestion) -> bool {
    let content = match fs::read_to_string(&issue.file_path) {
        Ok(c) => c,
        Err(_) => return false,
    };

    let lines: Vec<&str> = content.lines().collect();
    let line_idx = issue.line.saturating_sub(1);

    if line_idx >= lines.len() {
        return false;
    }

    // Build new content
    let mut new_lines: Vec<String> = lines.iter().map(|s| s.to_string()).collect();

    // Get suggestion lines
    let suggestion_lines: Vec<&str> = suggestion.code.lines().collect();
    if suggestion_lines.is_empty() {
        return false;
    }

    // Determine if this is a single-line or multi-line replacement
    let replacement_end = suggestion.end_line.max(issue.line);
    let lines_to_replace = replacement_end - issue.line + 1;

    // Remove old lines and insert new ones
    let remove_count = lines_to_replace.min(new_lines.len() - line_idx);
    for _ in 0..remove_count {
        if line_idx < new_lines.len() {
            new_lines.remove(line_idx);
        }
    }

    // Insert suggestion lines
    for (i, line) in suggestion_lines.iter().enumerate() {
        new_lines.insert(line_idx + i, line.to_string());
    }

    // Write back
    let new_content = new_lines.join("\n");

    // Preserve trailing newline if original had one
    let final_content = if content.ends_with('\n') && !new_content.ends_with('\n') {
        format!("{}\n", new_content)
    } else {
        new_content
    };

    fs::write(&issue.file_path, final_content).is_ok()
}

// ============================================================================
// Backup and Restore Functions (delegated to crate::cli::backup)
// ============================================================================

// Backup functions (create_backup, handle_list_backups, handle_undo, collect_files_from_issues)
// are now in crate::cli::backup and re-exported via mod.rs