infiniloom-engine 0.6.3

High-performance repository context engine for LLMs - AST parsing, token counting, and secret detection
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
//! License detection for compliance scanning
//!
//! This module detects open-source licenses in codebases, particularly
//! focusing on copyleft licenses (GPL, AGPL, LGPL) that may require
//! special handling in enterprise environments.
//!
//! # Compliance Use Cases
//!
//! - **Enterprise Code Audit**: Identify copyleft code before embedding
//! - **Legal Review**: Flag files requiring license compliance
//! - **CI/CD Gates**: Fail builds containing prohibited licenses
//!
//! # Supported Licenses
//!
//! | License | Risk Level | Notes |
//! |---------|------------|-------|
//! | GPL-3.0 | High | Strong copyleft, viral |
//! | GPL-2.0 | High | Strong copyleft |
//! | AGPL-3.0 | Critical | Network copyleft |
//! | LGPL-3.0 | Medium | Weak copyleft |
//! | LGPL-2.1 | Medium | Weak copyleft |
//! | MIT | Low | Permissive |
//! | Apache-2.0 | Low | Permissive |
//! | BSD-3-Clause | Low | Permissive |
//! | Unlicensed | Unknown | No license detected |
//!
//! # Example
//!
//! ```rust,ignore
//! use infiniloom_engine::license::{LicenseScanner, LicenseRisk};
//!
//! let scanner = LicenseScanner::new();
//!
//! // Scan a file
//! if let Some(finding) = scanner.scan_file(Path::new("lib/crypto.rs")) {
//!     if finding.license.risk() >= LicenseRisk::High {
//!         println!("Warning: {} contains {}", finding.file, finding.license.name());
//!     }
//! }
//!
//! // Scan entire repository
//! let findings = scanner.scan_repository(repo_path)?;
//! let copyleft_files: Vec<_> = findings
//!     .iter()
//!     .filter(|f| f.license.is_copyleft())
//!     .collect();
//! ```

use std::path::Path;

use serde::{Deserialize, Serialize};

/// Detected license types
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(rename_all = "kebab-case")]
pub enum License {
    // === Strong Copyleft ===
    /// GNU General Public License v3.0
    Gpl3,
    /// GNU General Public License v2.0
    Gpl2,
    /// GNU Affero General Public License v3.0 (network copyleft)
    Agpl3,

    // === Weak Copyleft ===
    /// GNU Lesser General Public License v3.0
    Lgpl3,
    /// GNU Lesser General Public License v2.1
    Lgpl21,
    /// Mozilla Public License 2.0
    Mpl2,
    /// Eclipse Public License 2.0
    Epl2,

    // === Permissive ===
    /// MIT License
    Mit,
    /// Apache License 2.0
    Apache2,
    /// BSD 3-Clause "New" License
    Bsd3Clause,
    /// BSD 2-Clause "Simplified" License
    Bsd2Clause,
    /// ISC License
    Isc,
    /// The Unlicense (public domain)
    Unlicense,
    /// Creative Commons Zero v1.0 Universal
    Cc0,
    /// Do What The Fuck You Want To Public License
    Wtfpl,

    // === Proprietary/Restricted ===
    /// Proprietary/Commercial license
    Proprietary,

    // === Unknown ===
    /// Unknown license
    Unknown,
}

impl License {
    /// Get the SPDX identifier for this license
    pub fn spdx_id(&self) -> &'static str {
        match self {
            Self::Gpl3 => "GPL-3.0-only",
            Self::Gpl2 => "GPL-2.0-only",
            Self::Agpl3 => "AGPL-3.0-only",
            Self::Lgpl3 => "LGPL-3.0-only",
            Self::Lgpl21 => "LGPL-2.1-only",
            Self::Mpl2 => "MPL-2.0",
            Self::Epl2 => "EPL-2.0",
            Self::Mit => "MIT",
            Self::Apache2 => "Apache-2.0",
            Self::Bsd3Clause => "BSD-3-Clause",
            Self::Bsd2Clause => "BSD-2-Clause",
            Self::Isc => "ISC",
            Self::Unlicense => "Unlicense",
            Self::Cc0 => "CC0-1.0",
            Self::Wtfpl => "WTFPL",
            Self::Proprietary => "PROPRIETARY",
            Self::Unknown => "UNKNOWN",
        }
    }

    /// Get human-readable name
    pub fn name(&self) -> &'static str {
        match self {
            Self::Gpl3 => "GNU General Public License v3.0",
            Self::Gpl2 => "GNU General Public License v2.0",
            Self::Agpl3 => "GNU Affero General Public License v3.0",
            Self::Lgpl3 => "GNU Lesser General Public License v3.0",
            Self::Lgpl21 => "GNU Lesser General Public License v2.1",
            Self::Mpl2 => "Mozilla Public License 2.0",
            Self::Epl2 => "Eclipse Public License 2.0",
            Self::Mit => "MIT License",
            Self::Apache2 => "Apache License 2.0",
            Self::Bsd3Clause => "BSD 3-Clause License",
            Self::Bsd2Clause => "BSD 2-Clause License",
            Self::Isc => "ISC License",
            Self::Unlicense => "The Unlicense",
            Self::Cc0 => "Creative Commons Zero v1.0",
            Self::Wtfpl => "WTFPL",
            Self::Proprietary => "Proprietary License",
            Self::Unknown => "Unknown License",
        }
    }

    /// Get the risk level for this license
    pub fn risk(&self) -> LicenseRisk {
        match self {
            Self::Agpl3 => LicenseRisk::Critical,
            Self::Gpl3 | Self::Gpl2 => LicenseRisk::High,
            Self::Lgpl3 | Self::Lgpl21 | Self::Mpl2 | Self::Epl2 => LicenseRisk::Medium,
            Self::Mit
            | Self::Apache2
            | Self::Bsd3Clause
            | Self::Bsd2Clause
            | Self::Isc
            | Self::Unlicense
            | Self::Cc0
            | Self::Wtfpl => LicenseRisk::Low,
            Self::Proprietary => LicenseRisk::High,
            Self::Unknown => LicenseRisk::Unknown,
        }
    }

    /// Check if this is a copyleft license
    pub fn is_copyleft(&self) -> bool {
        matches!(
            self,
            Self::Gpl3
                | Self::Gpl2
                | Self::Agpl3
                | Self::Lgpl3
                | Self::Lgpl21
                | Self::Mpl2
                | Self::Epl2
        )
    }

    /// Check if this is a strong (viral) copyleft license
    pub fn is_strong_copyleft(&self) -> bool {
        matches!(self, Self::Gpl3 | Self::Gpl2 | Self::Agpl3)
    }

    /// Check if this is a permissive license
    pub fn is_permissive(&self) -> bool {
        matches!(
            self,
            Self::Mit
                | Self::Apache2
                | Self::Bsd3Clause
                | Self::Bsd2Clause
                | Self::Isc
                | Self::Unlicense
                | Self::Cc0
                | Self::Wtfpl
        )
    }
}

/// License risk levels for compliance
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum LicenseRisk {
    /// Unknown risk (no license detected)
    Unknown,
    /// Low risk (permissive licenses)
    Low,
    /// Medium risk (weak copyleft)
    Medium,
    /// High risk (strong copyleft, proprietary)
    High,
    /// Critical risk (AGPL - network copyleft)
    Critical,
}

impl LicenseRisk {
    /// Get string representation
    pub fn as_str(&self) -> &'static str {
        match self {
            Self::Unknown => "unknown",
            Self::Low => "low",
            Self::Medium => "medium",
            Self::High => "high",
            Self::Critical => "critical",
        }
    }
}

/// A license detection result
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct LicenseFinding {
    /// File where license was found
    pub file: String,

    /// Detected license
    pub license: License,

    /// Line number where license indicator was found
    pub line: u32,

    /// Confidence score (0.0 - 1.0)
    pub confidence: f32,

    /// Text snippet that matched
    pub matched_text: String,
}

/// Configuration for license scanning
#[derive(Debug, Clone)]
pub struct LicenseScanConfig {
    /// Minimum confidence threshold (0.0 - 1.0)
    pub min_confidence: f32,

    /// Risk level threshold (only report licenses >= this level)
    pub min_risk: LicenseRisk,

    /// Scan LICENSE/COPYING files
    pub scan_license_files: bool,

    /// Scan source code headers
    pub scan_headers: bool,

    /// Maximum lines to scan per file (for headers)
    pub max_header_lines: usize,
}

impl Default for LicenseScanConfig {
    fn default() -> Self {
        Self {
            min_confidence: 0.7,
            min_risk: LicenseRisk::Unknown,
            scan_license_files: true,
            scan_headers: true,
            max_header_lines: 50,
        }
    }
}

/// License scanner for detecting licenses in codebases
pub struct LicenseScanner {
    config: LicenseScanConfig,
}

impl Default for LicenseScanner {
    fn default() -> Self {
        Self::new()
    }
}

impl LicenseScanner {
    /// Create a new license scanner with default config
    pub fn new() -> Self {
        Self { config: LicenseScanConfig::default() }
    }

    /// Create with custom configuration
    pub fn with_config(config: LicenseScanConfig) -> Self {
        Self { config }
    }

    /// Scan file content for license indicators
    pub fn scan(&self, content: &str, file_path: &str) -> Vec<LicenseFinding> {
        let mut findings = Vec::new();

        // Check if this is a license file
        let is_license_file = self.is_license_file(file_path);

        if is_license_file && self.config.scan_license_files {
            if let Some(finding) = self.scan_license_file(content, file_path) {
                findings.push(finding);
            }
        }

        if self.config.scan_headers {
            findings.extend(self.scan_headers(content, file_path));
        }

        // Filter by confidence and risk
        findings
            .into_iter()
            .filter(|f| {
                f.confidence >= self.config.min_confidence
                    && f.license.risk() >= self.config.min_risk
            })
            .collect()
    }

    /// Check if a file is a license file
    fn is_license_file(&self, file_path: &str) -> bool {
        let path = Path::new(file_path);
        let file_name = path
            .file_name()
            .and_then(|n| n.to_str())
            .map(|s| s.to_uppercase())
            .unwrap_or_default();

        matches!(
            file_name.as_str(),
            "LICENSE"
                | "LICENSE.MD"
                | "LICENSE.TXT"
                | "LICENCE"
                | "LICENCE.MD"
                | "LICENCE.TXT"
                | "COPYING"
                | "COPYING.MD"
                | "COPYING.TXT"
                | "LICENSE-MIT"
                | "LICENSE-APACHE"
                | "LICENSE.MIT"
                | "LICENSE.APACHE"
        )
    }

    /// Scan a LICENSE/COPYING file
    fn scan_license_file(&self, content: &str, file_path: &str) -> Option<LicenseFinding> {
        let content_lower = content.to_lowercase();

        // Check for specific license texts (in order of specificity)
        let detections: Vec<(License, f32, &str)> = vec![
            // AGPL (must check before GPL due to substring match)
            (License::Agpl3, 0.95, "gnu affero general public license"),
            (License::Agpl3, 0.9, "agpl-3.0"),
            (License::Agpl3, 0.85, "agpl version 3"),
            // LGPL (must check before GPL)
            (License::Lgpl3, 0.95, "gnu lesser general public license version 3"),
            (License::Lgpl3, 0.9, "lgpl-3.0"),
            (License::Lgpl21, 0.95, "gnu lesser general public license version 2.1"),
            (License::Lgpl21, 0.9, "lgpl-2.1"),
            (License::Lgpl21, 0.9, "lgpl version 2.1"),
            // GPL
            (License::Gpl3, 0.95, "gnu general public license version 3"),
            // Canonical GPL3 header: "GNU GENERAL PUBLIC LICENSE\nVersion 3, 29 June 2007"
            (License::Gpl3, 0.95, "version 3, 29 june 2007"),
            (License::Gpl3, 0.9, "gpl-3.0"),
            (License::Gpl3, 0.85, "gplv3"),
            (License::Gpl2, 0.95, "gnu general public license version 2"),
            // Canonical GPL2 header: "GNU GENERAL PUBLIC LICENSE\nVersion 2, June 1991"
            (License::Gpl2, 0.95, "version 2, june 1991"),
            (License::Gpl2, 0.9, "gpl-2.0"),
            (License::Gpl2, 0.85, "gplv2"),
            // MPL
            (License::Mpl2, 0.95, "mozilla public license version 2.0"),
            (License::Mpl2, 0.9, "mpl-2.0"),
            // EPL
            (License::Epl2, 0.95, "eclipse public license - v 2.0"),
            (License::Epl2, 0.9, "epl-2.0"),
            // Apache
            (License::Apache2, 0.95, "apache license, version 2.0"),
            (License::Apache2, 0.95, "apache license version 2.0"),
            (License::Apache2, 0.9, "apache-2.0"),
            (License::Apache2, 0.85, "licensed under the apache license"),
            // MIT
            (License::Mit, 0.95, "mit license"),
            (License::Mit, 0.9, "permission is hereby granted, free of charge"),
            (License::Mit, 0.85, "the software is provided \"as is\", without warranty"),
            // BSD
            (License::Bsd3Clause, 0.95, "3-clause bsd license"),
            (License::Bsd3Clause, 0.9, "bsd-3-clause"),
            (License::Bsd3Clause, 0.85, "redistributions of source code must retain"),
            (License::Bsd2Clause, 0.95, "2-clause bsd license"),
            (License::Bsd2Clause, 0.9, "bsd-2-clause"),
            // ISC
            (License::Isc, 0.95, "isc license"),
            (License::Isc, 0.9, "permission to use, copy, modify, and/or distribute"),
            // Unlicense
            (License::Unlicense, 0.95, "this is free and unencumbered software"),
            (License::Unlicense, 0.9, "unlicense"),
            // CC0
            (License::Cc0, 0.95, "cc0 1.0 universal"),
            (License::Cc0, 0.9, "creative commons zero"),
            // WTFPL
            (License::Wtfpl, 0.95, "do what the fuck you want to public license"),
            (License::Wtfpl, 0.9, "wtfpl"),
        ];

        for (license, confidence, pattern) in detections {
            if content_lower.contains(pattern) {
                // Find the line number
                let line = content_lower
                    .lines()
                    .enumerate()
                    .find(|(_, l)| l.contains(pattern))
                    .map_or(1, |(i, _)| (i + 1) as u32);

                return Some(LicenseFinding {
                    file: file_path.to_owned(),
                    license,
                    line,
                    confidence,
                    matched_text: pattern.to_owned(),
                });
            }
        }

        None
    }

    /// Scan source code headers for SPDX identifiers and license comments
    fn scan_headers(&self, content: &str, file_path: &str) -> Vec<LicenseFinding> {
        let mut findings = Vec::new();
        let lines: Vec<&str> = content.lines().take(self.config.max_header_lines).collect();

        for (line_num, line) in lines.iter().enumerate() {
            let line_lower = line.to_lowercase();

            // Check for SPDX license identifiers
            if let Some(finding) = self.check_spdx_identifier(&line_lower, file_path, line_num + 1)
            {
                findings.push(finding);
                continue;
            }

            // Check for license comments
            if let Some(finding) = self.check_license_comment(&line_lower, file_path, line_num + 1)
            {
                findings.push(finding);
            }
        }

        findings
    }

    /// Check for SPDX license identifiers
    fn check_spdx_identifier(
        &self,
        line: &str,
        file_path: &str,
        line_num: usize,
    ) -> Option<LicenseFinding> {
        // Pattern: SPDX-License-Identifier: <license>
        if !line.contains("spdx-license-identifier") {
            return None;
        }

        let spdx_mappings: Vec<(&str, License)> = vec![
            ("agpl-3.0", License::Agpl3),
            ("gpl-3.0", License::Gpl3),
            ("gpl-2.0", License::Gpl2),
            ("lgpl-3.0", License::Lgpl3),
            ("lgpl-2.1", License::Lgpl21),
            ("mpl-2.0", License::Mpl2),
            ("epl-2.0", License::Epl2),
            ("apache-2.0", License::Apache2),
            ("mit", License::Mit),
            ("bsd-3-clause", License::Bsd3Clause),
            ("bsd-2-clause", License::Bsd2Clause),
            ("isc", License::Isc),
            ("unlicense", License::Unlicense),
            ("cc0-1.0", License::Cc0),
        ];

        for (spdx_id, license) in spdx_mappings {
            if line.contains(spdx_id) {
                return Some(LicenseFinding {
                    file: file_path.to_owned(),
                    license,
                    line: line_num as u32,
                    confidence: 0.99, // SPDX identifiers are very reliable
                    matched_text: format!("SPDX-License-Identifier: {}", spdx_id),
                });
            }
        }

        None
    }

    /// Check for license mentions in comments
    fn check_license_comment(
        &self,
        line: &str,
        file_path: &str,
        line_num: usize,
    ) -> Option<LicenseFinding> {
        // Must be in a comment
        if !line.contains("//")
            && !line.contains("/*")
            && !line.contains('*')
            && !line.contains('#')
        {
            return None;
        }

        let comment_patterns: Vec<(&str, License, f32)> = vec![
            // High confidence patterns
            ("licensed under agpl", License::Agpl3, 0.85),
            ("licensed under gpl", License::Gpl3, 0.8),
            ("licensed under lgpl", License::Lgpl3, 0.8),
            ("licensed under the mit license", License::Mit, 0.85),
            ("licensed under apache", License::Apache2, 0.85),
            // Medium confidence patterns
            ("this file is part of", License::Unknown, 0.5), // Often followed by license
            ("copyright", License::Unknown, 0.3),
        ];

        for (pattern, license, confidence) in comment_patterns {
            if line.contains(pattern) && license != License::Unknown {
                return Some(LicenseFinding {
                    file: file_path.to_owned(),
                    license,
                    line: line_num as u32,
                    confidence,
                    matched_text: pattern.to_owned(),
                });
            }
        }

        None
    }

    /// Scan a file path for license information
    pub fn scan_file(&self, path: &Path) -> Result<Vec<LicenseFinding>, std::io::Error> {
        let content = std::fs::read_to_string(path)?;
        let file_path = path.to_string_lossy();
        Ok(self.scan(&content, &file_path))
    }

    /// Scan a repository for license information
    pub fn scan_repository(&self, repo_path: &Path) -> Result<Vec<LicenseFinding>, std::io::Error> {
        use ignore::WalkBuilder;

        let mut all_findings = Vec::new();

        let walker = WalkBuilder::new(repo_path)
            .hidden(false)
            .git_ignore(true)
            .build();

        for entry in walker.flatten() {
            let path = entry.path();

            if !path.is_file() {
                continue;
            }

            // Scan license files
            if self.is_license_file(&path.to_string_lossy()) {
                if let Ok(findings) = self.scan_file(path) {
                    all_findings.extend(findings);
                }
                continue;
            }

            // Scan source file headers if configured
            if self.config.scan_headers {
                let ext = path.extension().and_then(|e| e.to_str()).unwrap_or("");
                let is_source = matches!(
                    ext,
                    "rs" | "py" | "js" | "ts" | "go" | "c" | "cpp" | "h" | "java" | "rb" | "php"
                );

                if is_source {
                    if let Ok(findings) = self.scan_file(path) {
                        all_findings.extend(findings);
                    }
                }
            }
        }

        // Deduplicate by file and license
        all_findings.sort_by(|a, b| {
            a.file
                .cmp(&b.file)
                .then_with(|| a.license.spdx_id().cmp(b.license.spdx_id()))
        });
        all_findings.dedup_by(|a, b| a.file == b.file && a.license == b.license);

        Ok(all_findings)
    }

    /// Get a summary of license findings
    pub fn summarize(findings: &[LicenseFinding]) -> LicenseSummary {
        let mut summary = LicenseSummary::default();

        for finding in findings {
            match finding.license.risk() {
                LicenseRisk::Critical => summary.critical_count += 1,
                LicenseRisk::High => summary.high_count += 1,
                LicenseRisk::Medium => summary.medium_count += 1,
                LicenseRisk::Low => summary.low_count += 1,
                LicenseRisk::Unknown => summary.unknown_count += 1,
            }

            if finding.license.is_copyleft() {
                summary.copyleft_files.push(finding.file.clone());
            }

            // Track unique licenses
            if !summary.licenses.contains(&finding.license) {
                summary.licenses.push(finding.license);
            }
        }

        summary.copyleft_files.sort();
        summary.copyleft_files.dedup();

        summary
    }
}

/// Summary of license findings
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct LicenseSummary {
    /// Count of critical risk licenses (AGPL)
    pub critical_count: usize,

    /// Count of high risk licenses (GPL, proprietary)
    pub high_count: usize,

    /// Count of medium risk licenses (LGPL, MPL)
    pub medium_count: usize,

    /// Count of low risk licenses (MIT, Apache, BSD)
    pub low_count: usize,

    /// Count of unknown licenses
    pub unknown_count: usize,

    /// Files containing copyleft licenses
    pub copyleft_files: Vec<String>,

    /// Unique licenses found
    pub licenses: Vec<License>,
}

impl LicenseSummary {
    /// Check if any copyleft licenses were found
    pub fn has_copyleft(&self) -> bool {
        !self.copyleft_files.is_empty()
    }

    /// Check if any high-risk licenses were found
    pub fn has_high_risk(&self) -> bool {
        self.critical_count > 0 || self.high_count > 0
    }

    /// Get total number of findings
    pub fn total(&self) -> usize {
        self.critical_count
            + self.high_count
            + self.medium_count
            + self.low_count
            + self.unknown_count
    }
}

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

    #[test]
    fn test_license_risk_levels() {
        assert_eq!(License::Agpl3.risk(), LicenseRisk::Critical);
        assert_eq!(License::Gpl3.risk(), LicenseRisk::High);
        assert_eq!(License::Lgpl3.risk(), LicenseRisk::Medium);
        assert_eq!(License::Mit.risk(), LicenseRisk::Low);
        assert_eq!(License::Unknown.risk(), LicenseRisk::Unknown);
    }

    #[test]
    fn test_copyleft_detection() {
        assert!(License::Gpl3.is_copyleft());
        assert!(License::Agpl3.is_copyleft());
        assert!(License::Lgpl3.is_copyleft());
        assert!(!License::Mit.is_copyleft());
        assert!(!License::Apache2.is_copyleft());
    }

    #[test]
    fn test_strong_copyleft() {
        assert!(License::Gpl3.is_strong_copyleft());
        assert!(License::Agpl3.is_strong_copyleft());
        assert!(!License::Lgpl3.is_strong_copyleft());
        assert!(!License::Mit.is_strong_copyleft());
    }

    #[test]
    fn test_scan_mit_license() {
        let scanner = LicenseScanner::new();
        let content = r#"
MIT License

Copyright (c) 2024 Example Corp

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software.
"#;

        let findings = scanner.scan(content, "LICENSE");
        assert_eq!(findings.len(), 1);
        assert_eq!(findings[0].license, License::Mit);
        assert!(findings[0].confidence >= 0.9);
    }

    #[test]
    fn test_scan_gpl3_license() {
        let scanner = LicenseScanner::new();
        let content = r#"
GNU GENERAL PUBLIC LICENSE
Version 3, 29 June 2007

Copyright (C) 2007 Free Software Foundation, Inc.
"#;

        let findings = scanner.scan(content, "COPYING");
        assert_eq!(findings.len(), 1);
        assert_eq!(findings[0].license, License::Gpl3);
    }

    #[test]
    fn test_scan_spdx_identifier() {
        let scanner = LicenseScanner::new();
        let content = r#"
// SPDX-License-Identifier: Apache-2.0

fn main() {
    println!("Hello, world!");
}
"#;

        let findings = scanner.scan(content, "src/main.rs");
        assert_eq!(findings.len(), 1);
        assert_eq!(findings[0].license, License::Apache2);
        assert!(findings[0].confidence >= 0.95);
    }

    #[test]
    fn test_scan_agpl_in_header() {
        let scanner = LicenseScanner::new();
        let content = r#"
# Licensed under AGPL-3.0
# Copyright 2024 Example Corp

def main():
    pass
"#;

        let findings = scanner.scan(content, "main.py");
        assert!(!findings.is_empty());
        assert!(findings.iter().any(|f| f.license == License::Agpl3));
    }

    #[test]
    fn test_license_summary() {
        let findings = vec![
            LicenseFinding {
                file: "lib/a.rs".to_owned(),
                license: License::Gpl3,
                line: 1,
                confidence: 0.95,
                matched_text: "gpl-3.0".to_owned(),
            },
            LicenseFinding {
                file: "lib/b.rs".to_owned(),
                license: License::Mit,
                line: 1,
                confidence: 0.9,
                matched_text: "mit".to_owned(),
            },
            LicenseFinding {
                file: "lib/c.rs".to_owned(),
                license: License::Agpl3,
                line: 1,
                confidence: 0.95,
                matched_text: "agpl-3.0".to_owned(),
            },
        ];

        let summary = LicenseScanner::summarize(&findings);

        assert_eq!(summary.critical_count, 1);
        assert_eq!(summary.high_count, 1);
        assert_eq!(summary.low_count, 1);
        assert!(summary.has_copyleft());
        assert!(summary.has_high_risk());
        assert_eq!(summary.copyleft_files.len(), 2);
    }

    #[test]
    fn test_is_license_file() {
        let scanner = LicenseScanner::new();

        assert!(scanner.is_license_file("LICENSE"));
        assert!(scanner.is_license_file("LICENSE.md"));
        assert!(scanner.is_license_file("COPYING"));
        assert!(scanner.is_license_file("LICENSE-MIT"));
        assert!(!scanner.is_license_file("src/main.rs"));
        assert!(!scanner.is_license_file("README.md"));
    }

    #[test]
    fn test_risk_ordering() {
        assert!(LicenseRisk::Critical > LicenseRisk::High);
        assert!(LicenseRisk::High > LicenseRisk::Medium);
        assert!(LicenseRisk::Medium > LicenseRisk::Low);
        assert!(LicenseRisk::Low > LicenseRisk::Unknown);
    }

    #[test]
    fn test_spdx_ids() {
        assert_eq!(License::Gpl3.spdx_id(), "GPL-3.0-only");
        assert_eq!(License::Mit.spdx_id(), "MIT");
        assert_eq!(License::Apache2.spdx_id(), "Apache-2.0");
    }
}