linthis 0.19.3

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
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
// 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.

//! AI-assisted fix suggestion generation.

use serde::{Deserialize, Serialize};
use std::path::Path;
use std::time::Instant;

use super::context::{extract_context, CodeContext, ContextOptions};
use super::prompts::{IssueCategory, PromptBuilder, PromptVariables};
use super::provider::{AiProvider, AiProviderTrait};
use crate::utils::types::{LintIssue, Severity};

/// Options for suggestion generation
#[derive(Debug, Clone)]
pub struct SuggestionOptions {
    /// Context extraction options
    pub context_options: ContextOptions,
    /// Maximum suggestions per issue
    pub max_suggestions: usize,
    /// Include explanation with fix
    pub include_explanation: bool,
    /// Include confidence score
    pub include_confidence: bool,
    /// Batch size for parallel processing
    pub batch_size: usize,
    /// Skip if issue already has a suggestion
    pub skip_with_suggestion: bool,
}

impl Default for SuggestionOptions {
    fn default() -> Self {
        Self {
            context_options: ContextOptions::default(),
            max_suggestions: 3,
            include_explanation: true,
            include_confidence: true,
            batch_size: 5,
            skip_with_suggestion: true,
        }
    }
}

/// A single fix suggestion
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct FixSuggestion {
    /// The suggested fix code
    pub code: String,
    /// Explanation of the fix
    pub explanation: Option<String>,
    /// Confidence score (0.0 - 1.0)
    pub confidence: Option<f32>,
    /// Whether this is a complete replacement or partial
    pub is_complete: bool,
    /// Start line for the fix
    pub start_line: usize,
    /// End line for the fix
    pub end_line: usize,
    /// Language of the code
    pub language: String,
}

impl FixSuggestion {
    pub fn new(code: String, start_line: usize, end_line: usize, language: &str) -> Self {
        Self {
            code,
            explanation: None,
            confidence: None,
            is_complete: true,
            start_line,
            end_line,
            language: language.to_string(),
        }
    }

    pub fn with_explanation(mut self, explanation: &str) -> Self {
        self.explanation = Some(explanation.to_string());
        self
    }

    pub fn with_confidence(mut self, confidence: f32) -> Self {
        self.confidence = Some(confidence.clamp(0.0, 1.0));
        self
    }
}

/// Result of suggestion generation for a single issue
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SuggestionResult {
    /// The original issue code
    pub issue_code: String,
    /// File path
    pub file_path: String,
    /// Line number
    pub line_number: usize,
    /// Issue message
    pub message: String,
    /// Generated suggestions
    pub suggestions: Vec<FixSuggestion>,
    /// Error if generation failed
    pub error: Option<String>,
    /// Generation time in milliseconds
    pub generation_time_ms: u64,
    /// Code context used
    pub context: Option<CodeContext>,
}

impl SuggestionResult {
    pub fn success(
        issue_code: &str,
        file_path: &str,
        line_number: usize,
        message: &str,
        suggestions: Vec<FixSuggestion>,
        generation_time_ms: u64,
    ) -> Self {
        Self {
            issue_code: issue_code.to_string(),
            file_path: file_path.to_string(),
            line_number,
            message: message.to_string(),
            suggestions,
            error: None,
            generation_time_ms,
            context: None,
        }
    }

    pub fn failure(
        issue_code: &str,
        file_path: &str,
        line_number: usize,
        message: &str,
        error: &str,
    ) -> Self {
        Self {
            issue_code: issue_code.to_string(),
            file_path: file_path.to_string(),
            line_number,
            message: message.to_string(),
            suggestions: Vec::new(),
            error: Some(error.to_string()),
            generation_time_ms: 0,
            context: None,
        }
    }

    pub fn with_context(mut self, context: CodeContext) -> Self {
        self.context = Some(context);
        self
    }

    pub fn is_success(&self) -> bool {
        self.error.is_none() && !self.suggestions.is_empty()
    }
}

/// Full suggestions report for multiple issues
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SuggestionsReport {
    /// Individual suggestion results
    pub results: Vec<SuggestionResult>,
    /// Total issues processed
    pub total_issues: usize,
    /// Successfully generated suggestions
    pub successful: usize,
    /// Failed generations
    pub failed: usize,
    /// Total processing time in milliseconds
    pub total_time_ms: u64,
    /// AI provider used
    pub provider: String,
    /// Model used
    pub model: String,
}

impl SuggestionsReport {
    pub fn new(provider: &str, model: &str) -> Self {
        Self {
            results: Vec::new(),
            total_issues: 0,
            successful: 0,
            failed: 0,
            total_time_ms: 0,
            provider: provider.to_string(),
            model: model.to_string(),
        }
    }

    pub fn add_result(&mut self, result: SuggestionResult) {
        self.total_issues += 1;
        if result.is_success() {
            self.successful += 1;
        } else {
            self.failed += 1;
        }
        self.results.push(result);
    }

    pub fn success_rate(&self) -> f64 {
        if self.total_issues == 0 {
            0.0
        } else {
            self.successful as f64 / self.total_issues as f64
        }
    }
}

/// Main AI suggester
pub struct AiSuggester {
    provider: AiProvider,
    prompt_builder: PromptBuilder,
}

impl AiSuggester {
    /// Create a new suggester with the given provider
    pub fn with_provider(provider: AiProvider) -> Self {
        Self {
            provider,
            prompt_builder: PromptBuilder::new(),
        }
    }

    /// Create suggester from environment variables
    pub fn from_env() -> Self {
        Self::with_provider(AiProvider::default())
    }

    /// Check if the suggester is available
    pub fn is_available(&self) -> bool {
        self.provider.is_available()
    }

    /// Get the provider name
    pub fn provider_name(&self) -> &str {
        self.provider.name()
    }

    /// Get the model name
    pub fn model_name(&self) -> &str {
        &self.provider.config().model
    }

    /// Generate fix suggestion for a single issue
    pub fn suggest_fix(
        &self,
        issue: &LintIssue,
        source_code: &str,
        options: &SuggestionOptions,
    ) -> SuggestionResult {
        let start = Instant::now();

        let file_path_str = issue.file_path.to_string_lossy().to_string();
        let issue_code = issue.code.as_deref().unwrap_or("UNKNOWN");

        // Note: skip_with_suggestion is now disabled by default for AI fix
        // because the linter suggestion is human-readable text, not actual code
        if options.skip_with_suggestion && issue.suggestion.is_some() {
            return SuggestionResult::success(
                issue_code,
                &file_path_str,
                issue.line,
                &issue.message,
                vec![],
                0,
            );
        }

        // Extract code context
        let context = match extract_context_from_source(
            &file_path_str,
            source_code,
            issue.line,
            &options.context_options,
        ) {
            Ok(ctx) => ctx,
            Err(e) => {
                return SuggestionResult::failure(
                    issue_code,
                    &file_path_str,
                    issue.line,
                    &issue.message,
                    &format!("Failed to extract context: {}", e),
                );
            }
        };

        // Determine issue category
        let category = categorize_issue(issue);

        // Build prompt variables
        let vars = PromptVariables::new()
            .with_language(&context.language)
            .with_file_path(&context.file_path)
            .with_line_number(context.line_number as u32)
            .with_issue_message(&issue.message)
            .with_rule_id(issue_code)
            .with_code_context(&context.full_snippet)
            .with_issue_line(&context.issue_lines);

        let vars = if let Some(ref imports) = context.imports {
            vars.with_imports(imports)
        } else {
            vars
        };

        let vars = if let Some(ref scope) = context.scope {
            vars.with_scope(scope)
        } else {
            vars
        };

        // Build and send prompt
        let (system_prompt, user_prompt) = self.prompt_builder.build_prompt(category, &vars);

        let response = match self.provider.complete(&user_prompt, Some(&system_prompt)) {
            Ok(r) => r,
            Err(e) => {
                return SuggestionResult::failure(
                    issue_code,
                    &file_path_str,
                    issue.line,
                    &issue.message,
                    &e,
                );
            }
        };

        // Parse response into suggestions
        let suggestions = parse_ai_response(&response, &context.language, issue.line);

        let elapsed = start.elapsed().as_millis() as u64;

        let mut result = SuggestionResult::success(
            issue_code,
            &file_path_str,
            issue.line,
            &issue.message,
            suggestions,
            elapsed,
        );

        result = result.with_context(context);

        result
    }

    /// Generate suggestions for multiple issues
    pub fn suggest_fixes(
        &self,
        issues: &[LintIssue],
        source_codes: &std::collections::HashMap<String, String>,
        options: &SuggestionOptions,
    ) -> SuggestionsReport {
        let start = Instant::now();
        let mut report = SuggestionsReport::new(self.provider_name(), self.model_name());

        for issue in issues {
            let file_path_str = issue.file_path.to_string_lossy().to_string();
            let issue_code = issue.code.as_deref().unwrap_or("UNKNOWN");

            let source = match source_codes.get(&file_path_str) {
                Some(s) => s,
                None => {
                    report.add_result(SuggestionResult::failure(
                        issue_code,
                        &file_path_str,
                        issue.line,
                        &issue.message,
                        "Source code not found",
                    ));
                    continue;
                }
            };

            let result = self.suggest_fix(issue, source, options);
            report.add_result(result);
        }

        report.total_time_ms = start.elapsed().as_millis() as u64;

        report
    }

    /// Suggest fix for a file at a specific line
    pub fn suggest_fix_for_file(
        &self,
        file_path: &Path,
        line_number: usize,
        message: &str,
        rule_id: &str,
        options: &SuggestionOptions,
    ) -> SuggestionResult {
        let start = Instant::now();
        let file_path_str = file_path.to_string_lossy().to_string();

        // Extract context from file
        let context = match extract_context(file_path, line_number as u32, &options.context_options)
        {
            Ok(ctx) => ctx,
            Err(e) => {
                return SuggestionResult::failure(
                    rule_id,
                    &file_path_str,
                    line_number,
                    message,
                    &e,
                );
            }
        };

        // Determine category from rule_id pattern
        let category = categorize_from_rule_id(rule_id);

        // Build prompt
        let vars = PromptVariables::new()
            .with_language(&context.language)
            .with_file_path(&context.file_path)
            .with_line_number(line_number as u32)
            .with_issue_message(message)
            .with_rule_id(rule_id)
            .with_code_context(&context.full_snippet)
            .with_issue_line(&context.issue_lines);

        let (system_prompt, user_prompt) = self.prompt_builder.build_prompt(category, &vars);

        let response = match self.provider.complete(&user_prompt, Some(&system_prompt)) {
            Ok(r) => r,
            Err(e) => {
                return SuggestionResult::failure(
                    rule_id,
                    &file_path_str,
                    line_number,
                    message,
                    &e,
                );
            }
        };

        let suggestions = parse_ai_response(&response, &context.language, line_number);
        let elapsed = start.elapsed().as_millis() as u64;

        SuggestionResult::success(
            rule_id,
            &file_path_str,
            line_number,
            message,
            suggestions,
            elapsed,
        )
        .with_context(context)
    }
}

/// Extract context from source code string
fn extract_context_from_source(
    file_path: &str,
    source: &str,
    line_number: usize,
    options: &ContextOptions,
) -> Result<CodeContext, String> {
    let lines: Vec<&str> = source.lines().collect();
    let total_lines = lines.len();

    if total_lines == 0 {
        return Err("File is empty".to_string());
    }

    // Handle line 0 as file-level issue (use beginning of file)
    let effective_line = if line_number == 0 { 1 } else { line_number };

    if effective_line > total_lines {
        return Err(format!(
            "Line number {} out of range (file has {} lines)",
            line_number, total_lines
        ));
    }

    let idx = effective_line - 1;
    let language = detect_language_from_path(file_path);

    let mut context = CodeContext::new(file_path, &language, effective_line as u32);

    // For file-level issues (line 0), show first N lines as context
    if line_number == 0 {
        let context_lines = (options.lines_before + options.lines_after + 1).min(total_lines);
        context.issue_lines = "(file-level issue)".to_string();
        context.before = String::new();
        context.after = lines[..context_lines].join("\n");
        context.full_snippet = format!(">>> File-level issue <<<\n{}", context.after);
        return Ok(context);
    }

    // Extract issue line
    context.issue_lines = lines[idx].to_string();

    // Extract before context
    let start = idx.saturating_sub(options.lines_before);
    if start < idx {
        context.before = lines[start..idx].join("\n");
    }

    // Extract after context
    let end = (idx + 1 + options.lines_after).min(total_lines);
    if idx + 1 < end {
        context.after = lines[idx + 1..end].join("\n");
    }

    // Build full snippet
    context.full_snippet = format!(
        "{}\n>>> {} <<<\n{}",
        context.before, context.issue_lines, context.after
    );

    Ok(context)
}

/// Detect language from file path
fn detect_language_from_path(path: &str) -> String {
    let path = std::path::Path::new(path);
    path.extension()
        .and_then(|e| e.to_str())
        .map(|ext| match ext {
            "rs" => "rust",
            "py" | "pyw" => "python",
            "js" | "jsx" | "mjs" => "javascript",
            "ts" | "tsx" | "mts" => "typescript",
            "go" => "go",
            "java" => "java",
            "cpp" | "cc" | "cxx" | "c" | "h" | "hpp" => "cpp",
            "swift" => "swift",
            "kt" | "kts" => "kotlin",
            "rb" => "ruby",
            "php" => "php",
            _ => ext,
        })
        .unwrap_or("unknown")
        .to_string()
}

/// Categorize issue based on lint issue properties
fn categorize_issue(issue: &LintIssue) -> IssueCategory {
    // Try to categorize based on code patterns
    let code_lower = issue.code.as_deref().unwrap_or("").to_lowercase();
    let msg_lower = issue.message.to_lowercase();

    if code_lower.contains("security")
        || code_lower.contains("vuln")
        || msg_lower.contains("security")
        || msg_lower.contains("vulnerability")
        || msg_lower.contains("injection")
        || msg_lower.contains("xss")
    {
        return IssueCategory::Security;
    }

    if code_lower.contains("perf")
        || msg_lower.contains("performance")
        || msg_lower.contains("slow")
        || msg_lower.contains("optimize")
    {
        return IssueCategory::Performance;
    }

    if code_lower.contains("complex")
        || msg_lower.contains("complexity")
        || msg_lower.contains("cyclomatic")
        || msg_lower.contains("cognitive")
    {
        return IssueCategory::Complexity;
    }

    if code_lower.contains("style")
        || code_lower.contains("format")
        || msg_lower.contains("naming")
        || msg_lower.contains("indent")
    {
        return IssueCategory::Style;
    }

    if code_lower.contains("deprecated") || msg_lower.contains("deprecated") {
        return IssueCategory::Deprecation;
    }

    if code_lower.contains("type")
        || msg_lower.contains("type mismatch")
        || msg_lower.contains("type error")
    {
        return IssueCategory::Type;
    }

    if code_lower.contains("doc")
        || msg_lower.contains("documentation")
        || msg_lower.contains("missing doc")
    {
        return IssueCategory::Documentation;
    }

    // Check severity for potential bugs
    if issue.severity == Severity::Error {
        return IssueCategory::Bug;
    }

    IssueCategory::General
}

/// Categorize from rule ID patterns
fn categorize_from_rule_id(rule_id: &str) -> IssueCategory {
    let rule_lower = rule_id.to_lowercase();

    // Common linter rule patterns
    if rule_lower.starts_with("sec") || rule_lower.contains("security") {
        IssueCategory::Security
    } else if rule_lower.starts_with("perf") || rule_lower.contains("performance") {
        IssueCategory::Performance
    } else if rule_lower.contains("complex") {
        IssueCategory::Complexity
    } else if rule_lower.starts_with("style") || rule_lower.starts_with("fmt") {
        IssueCategory::Style
    } else if rule_lower.contains("deprecated") {
        IssueCategory::Deprecation
    } else if rule_lower.starts_with("type") {
        IssueCategory::Type
    } else if rule_lower.starts_with("doc") {
        IssueCategory::Documentation
    } else {
        IssueCategory::General
    }
}

/// Parse AI response into fix suggestions
fn parse_ai_response(response: &str, language: &str, line_number: usize) -> Vec<FixSuggestion> {
    let mut suggestions = Vec::new();

    // Try unified diff format first (preferred)
    if let Some(diff_suggestions) = parse_unified_diff(response, language) {
        suggestions.extend(diff_suggestions);
    }

    // Fall back to code blocks if no diff found
    if suggestions.is_empty() {
        // Extract code blocks from the response
        let code_block_pattern = format!("```{}\\s*\\n([\\s\\S]*?)\\n```", language);
        let generic_block_pattern = r"```\s*\n([\s\S]*?)\n```";

        // Try language-specific blocks first
        if let Ok(re) = regex::Regex::new(&code_block_pattern) {
            for cap in re.captures_iter(response) {
                if let Some(code) = cap.get(1) {
                    let code_str = code.as_str().trim();
                    // Skip if it's a diff block we already tried to parse
                    if !code_str.starts_with("@@") && !code_str.starts_with("---") {
                        // Limit code block size - if AI returned too much, extract only relevant lines
                        let limited_code = limit_code_block_size(code_str, 5);
                        let line_count = limited_code.lines().count();
                        let suggestion = FixSuggestion::new(
                            limited_code,
                            line_number,
                            line_number + line_count.saturating_sub(1),
                            language,
                        );
                        suggestions.push(suggestion);
                    }
                }
            }
        }

        // If no language-specific blocks found, try generic blocks
        if suggestions.is_empty() {
            if let Ok(re) = regex::Regex::new(generic_block_pattern) {
                for cap in re.captures_iter(response) {
                    if let Some(code) = cap.get(1) {
                        let code_str = code.as_str().trim();
                        // Skip diff blocks and empty blocks
                        if !code_str.is_empty()
                            && !code_str.starts_with("@@")
                            && !code_str.starts_with("---")
                        {
                            let limited_code = limit_code_block_size(code_str, 5);
                            let line_count = limited_code.lines().count();
                            let suggestion = FixSuggestion::new(
                                limited_code,
                                line_number,
                                line_number + line_count.saturating_sub(1),
                                language,
                            );
                            suggestions.push(suggestion);
                        }
                    }
                }
            }
        }
    }

    // If still no code blocks, treat the whole response as code if it looks like code
    if suggestions.is_empty() && looks_like_code(response) {
        let suggestion = FixSuggestion::new(
            response.trim().to_string(),
            line_number,
            line_number,
            language,
        );
        suggestions.push(suggestion);
    }

    // Extract explanations if present
    let explanation = extract_explanation(response);
    if let Some(exp) = explanation {
        for suggestion in &mut suggestions {
            suggestion.explanation = Some(exp.clone());
        }
    }

    suggestions
}

/// Parse unified diff format from AI response
fn parse_unified_diff(response: &str, language: &str) -> Option<Vec<FixSuggestion>> {
    // Look for diff code blocks
    let diff_pattern = r"```diff\s*\n([\s\S]*?)\n```";

    let re = regex::Regex::new(diff_pattern).ok()?;
    let mut suggestions = Vec::new();

    for cap in re.captures_iter(response) {
        if let Some(diff_content) = cap.get(1) {
            if let Some(suggestion) = parse_diff_hunk(diff_content.as_str(), language) {
                suggestions.push(suggestion);
            }
        }
    }

    // Also try to find inline diff (not in code block)
    if suggestions.is_empty() {
        if let Some(suggestion) = parse_diff_hunk(response, language) {
            suggestions.push(suggestion);
        }
    }

    if suggestions.is_empty() {
        None
    } else {
        Some(suggestions)
    }
}

/// Parse a single diff hunk and extract the fix
fn parse_diff_hunk(diff: &str, language: &str) -> Option<FixSuggestion> {
    // Parse the @@ -start,count +start,count @@ header
    let hunk_pattern = r"@@\s*-(\d+)(?:,(\d+))?\s+\+(\d+)(?:,(\d+))?\s*@@";
    let re = regex::Regex::new(hunk_pattern).ok()?;

    let cap = re.captures(diff)?;
    let old_start: usize = cap.get(1)?.as_str().parse().ok()?;
    let _old_count: usize = cap.get(2).map_or(1, |m| m.as_str().parse().unwrap_or(1));
    let _new_start: usize = cap.get(3)?.as_str().parse().ok()?;
    let _new_count: usize = cap.get(4).map_or(1, |m| m.as_str().parse().unwrap_or(1));

    // Find the position after the @@ line
    let hunk_end = cap.get(0)?.end();
    let diff_body = &diff[hunk_end..];

    // Parse diff lines - only extract actual changes (+ and - lines)
    // Skip context lines (lines starting with space) as they confuse the replacement
    let mut added_lines: Vec<String> = Vec::new();
    let mut removed_lines: Vec<String> = Vec::new();
    let mut first_change_offset: Option<usize> = None;
    let mut context_before = 0;

    for line in diff_body.lines() {
        if line.starts_with('+') && !line.starts_with("+++") {
            // Added line - remove the '+' prefix
            if first_change_offset.is_none() {
                first_change_offset = Some(context_before);
            }
            added_lines.push(line[1..].to_string());
        } else if line.starts_with('-') && !line.starts_with("---") {
            // Removed line
            if first_change_offset.is_none() {
                first_change_offset = Some(context_before);
            }
            removed_lines.push(line[1..].to_string());
        } else if line.starts_with(' ') {
            // Context line - count them before first change
            if first_change_offset.is_none() {
                context_before += 1;
            }
            // Don't add context lines to added_lines
        }
        // Ignore other lines (@@, \, empty, etc.)
    }

    if added_lines.is_empty() {
        return None;
    }

    // Calculate actual start line (accounting for context before)
    let actual_start = old_start + first_change_offset.unwrap_or(0);
    let removed_count = removed_lines.len();

    // Only use the added lines that correspond to the removed lines
    // If AI returned way more lines than removed, it likely returned extra context
    let fix_code = if removed_count > 0 && added_lines.len() > removed_count * 3 {
        // AI returned too much content - try to extract just the relevant portion
        // Take only the first N lines where N is reasonable based on removed count
        let take_count = (removed_count * 2).max(3).min(added_lines.len());
        added_lines[..take_count].join("\n")
    } else {
        added_lines.join("\n")
    };

    // End line is based on how many lines were actually removed
    let end_line = if removed_count > 0 {
        actual_start + removed_count - 1
    } else {
        actual_start
    };

    Some(FixSuggestion::new(
        fix_code,
        actual_start,
        end_line,
        language,
    ))
}

/// Limit code block size to avoid applying entire file as a fix
/// If the code block has more than max_lines, try to extract only the relevant portion
fn limit_code_block_size(code: &str, max_lines: usize) -> String {
    let lines: Vec<&str> = code.lines().collect();

    if lines.len() <= max_lines {
        return code.to_string();
    }

    // If code is too long, it's likely the AI returned too much
    // Try to find the actual changed portion by looking for common patterns

    // For simple cases, just take the first few lines
    // This is a heuristic - the AI should really return minimal diffs
    lines[..max_lines].join("\n")
}

/// Check if text looks like code
fn looks_like_code(text: &str) -> bool {
    let code_indicators = [
        "fn ",
        "let ",
        "const ",
        "var ",
        "function ",
        "def ",
        "class ",
        "if ",
        "for ",
        "while ",
        "return ",
        "import ",
        "from ",
        "use ",
        "{",
        "}",
        "(",
        ")",
        ";",
        "=>",
        "->",
    ];

    let trimmed = text.trim();
    code_indicators.iter().any(|&ind| trimmed.contains(ind))
}

/// Extract explanation from response
fn extract_explanation(response: &str) -> Option<String> {
    // Look for common explanation patterns
    let patterns = [
        "Note:",
        "Explanation:",
        "Security note:",
        "Bug fix note:",
        "This fix",
        "The change",
    ];

    for pattern in &patterns {
        if let Some(pos) = response.find(pattern) {
            let rest = &response[pos..];
            // Get text until next code block or end
            if let Some(end) = rest.find("```") {
                return Some(rest[..end].trim().to_string());
            } else {
                return Some(rest.trim().to_string());
            }
        }
    }

    None
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::path::PathBuf;

    #[test]
    fn test_suggestion_options_default() {
        let options = SuggestionOptions::default();
        assert_eq!(options.max_suggestions, 3);
        assert!(options.include_explanation);
        assert!(options.skip_with_suggestion);
    }

    #[test]
    fn test_fix_suggestion() {
        let suggestion = FixSuggestion::new("let x = 5;".to_string(), 10, 10, "rust")
            .with_explanation("Fixed unused variable")
            .with_confidence(0.9);

        assert_eq!(suggestion.start_line, 10);
        assert_eq!(suggestion.confidence, Some(0.9));
        assert!(suggestion.explanation.is_some());
    }

    #[test]
    fn test_suggestion_result() {
        let result =
            SuggestionResult::success("W0001", "src/main.rs", 10, "unused variable", vec![], 100);

        assert!(!result.is_success()); // No suggestions

        let result_with_suggestions = SuggestionResult::success(
            "W0001",
            "src/main.rs",
            10,
            "unused variable",
            vec![FixSuggestion::new(
                "let _x = 5;".to_string(),
                10,
                10,
                "rust",
            )],
            100,
        );

        assert!(result_with_suggestions.is_success());
    }

    #[test]
    fn test_suggestions_report() {
        let mut report = SuggestionsReport::new("Mock", "mock-model");

        let success = SuggestionResult::success(
            "W0001",
            "test.rs",
            1,
            "test",
            vec![FixSuggestion::new("code".to_string(), 1, 1, "rust")],
            10,
        );

        let failure = SuggestionResult::failure("W0002", "test.rs", 2, "test", "error");

        report.add_result(success);
        report.add_result(failure);

        assert_eq!(report.total_issues, 2);
        assert_eq!(report.successful, 1);
        assert_eq!(report.failed, 1);
        assert_eq!(report.success_rate(), 0.5);
    }

    #[test]
    fn test_parse_ai_response() {
        let response = r#"
Here's the fix:
```rust
let _x = 5;
```
Note: Added underscore prefix to indicate intentionally unused variable.
"#;

        let suggestions = parse_ai_response(response, "rust", 10);

        assert_eq!(suggestions.len(), 1);
        assert!(suggestions[0].code.contains("let _x"));
        assert!(suggestions[0].explanation.is_some());
    }

    #[test]
    fn test_parse_unified_diff() {
        let response = r#"
Here's the fix:
```diff
@@ -5,1 +5,1 @@
-    let x = 5;
+    let _x = 5;
```
Note: Added underscore prefix.
"#;

        let suggestions = parse_ai_response(response, "rust", 5);

        assert_eq!(suggestions.len(), 1);
        assert_eq!(suggestions[0].start_line, 5);
        assert_eq!(suggestions[0].end_line, 5);
        assert!(suggestions[0].code.contains("let _x"));
    }

    #[test]
    fn test_parse_unified_diff_multiline() {
        let response = r#"
```diff
@@ -10,4 +10,3 @@
 context before
-    old line 1
-    old line 2
+    new line
 context after
```
"#;

        let suggestions = parse_ai_response(response, "python", 10);

        assert_eq!(suggestions.len(), 1);
        // Start line is 11 (10 + 1 context line before the change)
        assert_eq!(suggestions[0].start_line, 11);
        // End line is 12 (start + 2 removed lines - 1)
        assert_eq!(suggestions[0].end_line, 12);
    }

    #[test]
    fn test_parse_diff_with_context() {
        let response = r#"
```diff
@@ -4,3 +4,3 @@
     let y = 10;
-    let x = 5;
+    let _x = 5;
     println!("{}", y);
```
"#;

        let suggestions = parse_ai_response(response, "rust", 5);

        assert_eq!(suggestions.len(), 1);
        // The fix should include the replaced line
        assert!(suggestions[0].code.contains("let _x"));
    }

    #[test]
    fn test_categorize_issue() {
        let mut issue = LintIssue {
            file_path: PathBuf::from("test.rs"),
            line: 1,
            column: None,
            severity: Severity::Warning,
            message: "SQL injection vulnerability detected".to_string(),
            code: Some("SEC001".to_string()),
            source: Some("linthis".to_string()),
            suggestion: None,
            language: None,
            code_line: None,
            context_before: Vec::new(),
            context_after: Vec::new(),
        };

        assert_eq!(categorize_issue(&issue), IssueCategory::Security);

        issue.message = "High cyclomatic complexity".to_string();
        issue.code = Some("COMPLEX001".to_string());
        assert_eq!(categorize_issue(&issue), IssueCategory::Complexity);
    }

    #[test]
    fn test_detect_language() {
        assert_eq!(detect_language_from_path("src/main.rs"), "rust");
        assert_eq!(detect_language_from_path("src/app.py"), "python");
        assert_eq!(detect_language_from_path("src/index.ts"), "typescript");
    }

    #[test]
    fn test_ai_suggester_mock() {
        use super::super::provider::AiProviderConfig;

        let provider = AiProvider::new(AiProviderConfig::mock());
        let suggester = AiSuggester::with_provider(provider);

        assert!(suggester.is_available());
        assert_eq!(suggester.provider_name(), "Mock");
    }
}