adk-doc-audit 0.3.0

Documentation audit system for ADK-Rust that validates documentation against actual crate implementations
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
//! Audit orchestrator that coordinates all validation components.
//!
//! This module provides the main orchestration logic for running documentation audits.
//! It coordinates between the parser, analyzer, validator, and reporter components to
//! provide comprehensive documentation validation.

use crate::{
    AuditConfig, AuditError, AuditIssue, AuditReport, AuditSummary, CodeAnalyzer,
    DocumentationParser, ExampleValidator, FileAuditResult, IssueCategory, IssueSeverity,
    ReportGenerator, Result, SuggestionEngine, VersionValidator, reporter::AuditReportConfig,
};
use chrono::Utc;
use sha2::{Digest, Sha256};
use std::fs;
use std::path::{Path, PathBuf};
use std::time::Instant;
use tracing::{debug, error, info, instrument, warn};
use walkdir::WalkDir;

/// Main orchestrator that coordinates the audit process.
pub struct AuditOrchestrator {
    /// Configuration for the audit.
    config: AuditConfig,
    /// Documentation parser for extracting information from markdown files.
    parser: DocumentationParser,
    /// Code analyzer for validating API references.
    analyzer: CodeAnalyzer,
    /// Example validator for testing code compilation.
    validator: ExampleValidator,
    /// Version validator for checking version consistency.
    version_validator: VersionValidator,
    /// Suggestion engine for generating fix recommendations.
    _suggestion_engine: SuggestionEngine,
    /// Report generator for creating audit reports.
    _report_generator: ReportGenerator,
}

impl AuditOrchestrator {
    /// Create a new audit orchestrator with the given configuration.
    #[instrument(skip(config))]
    pub async fn new(config: AuditConfig) -> Result<Self> {
        info!("Initializing audit orchestrator");
        debug!("Configuration: {:?}", config);

        // Basic validation - check paths exist
        if !config.workspace_path.exists() {
            return Err(AuditError::WorkspaceNotFound { path: config.workspace_path.clone() });
        }

        // Initialize all components
        info!("Initializing documentation parser");
        let parser = DocumentationParser::new("0.1.0".to_string(), "1.85.0".to_string())?;

        info!("Initializing code analyzer");
        let analyzer = CodeAnalyzer::new(config.workspace_path.clone());

        info!("Initializing example validator");
        let validator =
            ExampleValidator::new("0.1.0".to_string(), config.workspace_path.clone()).await?;

        info!("Initializing version validator");
        let version_validator = VersionValidator::new(&config.workspace_path).await?;

        info!("Initializing suggestion engine");
        let suggestion_engine = SuggestionEngine::new_empty();

        info!("Initializing report generator");
        let report_generator = ReportGenerator::new(crate::reporter::OutputFormat::Console);

        info!("Audit orchestrator initialized successfully");

        Ok(Self {
            config,
            parser,
            analyzer,
            validator,
            version_validator,
            _suggestion_engine: suggestion_engine,
            _report_generator: report_generator,
        })
    }

    /// Run a full audit of all documentation files.
    #[instrument(skip(self))]
    pub async fn run_full_audit(&mut self) -> Result<AuditReport> {
        info!("Starting full documentation audit");
        let start_time = Instant::now();

        // Discover all documentation files
        let doc_files = self.discover_documentation_files().await?;
        info!("Found {} documentation files to audit", doc_files.len());

        // Process each documentation file
        let mut file_results = Vec::new();
        let mut all_issues = Vec::new();
        let all_recommendations = Vec::new();

        for doc_file in &doc_files {
            if self.should_skip_file(doc_file) {
                debug!("Skipping excluded file: {}", doc_file.display());
                continue;
            }

            info!("Processing file: {}", doc_file.display());

            match self.process_documentation_file(doc_file).await {
                Ok((file_result, mut issues, _recommendations)) => {
                    file_results.push(file_result);
                    all_issues.append(&mut issues);
                }
                Err(e) => {
                    error!("Failed to process file {}: {}", doc_file.display(), e);

                    // Create a failed file result
                    let file_result = FileAuditResult {
                        file_path: doc_file.clone(),
                        file_hash: self
                            .calculate_file_hash(doc_file)
                            .unwrap_or_else(|_| "error".to_string()),
                        last_modified: Utc::now(),
                        issues_count: 1,
                        issues: vec![self.create_processing_error_issue(doc_file, &e)],
                        passed: false,
                        audit_duration_ms: 0,
                    };

                    file_results.push(file_result);
                    all_issues.push(self.create_processing_error_issue(doc_file, &e));
                }
            }
        }

        // Create audit summary
        let summary = self.create_audit_summary(&file_results, &all_issues);

        let total_time = start_time.elapsed();
        info!("Full audit completed in {:?}", total_time);
        info!("Found {} total issues across {} files", all_issues.len(), file_results.len());

        // Generate the final report
        let report = AuditReport {
            summary,
            file_results,
            issues: all_issues,
            recommendations: all_recommendations,
            timestamp: Utc::now(),
            audit_config: AuditReportConfig::default(),
        };

        Ok(report)
    }

    /// Run an incremental audit on only the specified changed files.
    #[instrument(skip(self, changed_files))]
    pub async fn run_incremental_audit(
        &mut self,
        changed_files: &[PathBuf],
    ) -> Result<AuditReport> {
        info!("Starting incremental audit on {} files", changed_files.len());
        let start_time = Instant::now();

        // Filter to only documentation files that exist
        let mut doc_files = Vec::new();
        for file in changed_files {
            if self.is_documentation_file(file) && file.exists() {
                doc_files.push(file.clone());
            } else {
                debug!("Skipping non-documentation file: {}", file.display());
            }
        }

        if doc_files.is_empty() {
            info!("No documentation files to audit in changed files");
            return Ok(AuditReport {
                summary: AuditSummary {
                    total_files: 0,
                    files_with_issues: 0,
                    total_issues: 0,
                    critical_issues: 0,
                    warning_issues: 0,
                    info_issues: 0,
                    coverage_percentage: 100.0,
                    average_issues_per_file: 0.0,
                    most_common_issue: None,
                    problematic_files: Vec::new(),
                },
                file_results: Vec::new(),
                issues: Vec::new(),
                recommendations: Vec::new(),
                timestamp: Utc::now(),
                audit_config: AuditReportConfig::default(),
            });
        }

        info!("Processing {} documentation files", doc_files.len());

        // Process each changed documentation file
        let mut file_results = Vec::new();
        let mut all_issues = Vec::new();
        let all_recommendations = Vec::new();

        for doc_file in &doc_files {
            if self.should_skip_file(doc_file) {
                debug!("Skipping excluded file: {}", doc_file.display());
                continue;
            }

            info!("Processing changed file: {}", doc_file.display());

            match self.process_documentation_file(doc_file).await {
                Ok((file_result, mut issues, _recommendations)) => {
                    file_results.push(file_result);
                    all_issues.append(&mut issues);
                }
                Err(e) => {
                    error!("Failed to process file {}: {}", doc_file.display(), e);

                    // Create a failed file result
                    let file_result = FileAuditResult {
                        file_path: doc_file.clone(),
                        file_hash: self
                            .calculate_file_hash(doc_file)
                            .unwrap_or_else(|_| "error".to_string()),
                        last_modified: Utc::now(),
                        issues_count: 1,
                        issues: vec![self.create_processing_error_issue(doc_file, &e)],
                        passed: false,
                        audit_duration_ms: 0,
                    };

                    file_results.push(file_result);
                    all_issues.push(self.create_processing_error_issue(doc_file, &e));
                }
            }
        }

        // Create summary
        let summary = self.create_audit_summary(&file_results, &all_issues);

        let total_time = start_time.elapsed();
        info!("Incremental audit completed in {:?}", total_time);

        Ok(AuditReport {
            summary,
            file_results,
            issues: all_issues,
            recommendations: all_recommendations,
            timestamp: Utc::now(),
            audit_config: AuditReportConfig::default(),
        })
    }

    /// Validate a single documentation file.
    #[instrument(skip(self))]
    pub async fn validate_file(&mut self, file_path: &Path) -> Result<FileAuditResult> {
        info!("Validating single file: {}", file_path.display());

        if !file_path.exists() {
            return Err(AuditError::FileNotFound { path: file_path.to_path_buf() });
        }

        if !self.is_documentation_file(file_path) {
            return Err(AuditError::InvalidFileType {
                path: file_path.to_path_buf(),
                expected: "markdown documentation file".to_string(),
            });
        }

        // Process the single file
        match self.process_documentation_file(file_path).await {
            Ok((file_result, _issues, _recommendations)) => Ok(file_result),
            Err(e) => {
                error!("Failed to validate file {}: {}", file_path.display(), e);

                // Return a failed file result
                Ok(FileAuditResult {
                    file_path: file_path.to_path_buf(),
                    file_hash: self
                        .calculate_file_hash(file_path)
                        .unwrap_or_else(|_| "error".to_string()),
                    last_modified: Utc::now(),
                    issues_count: 1,
                    issues: vec![self.create_processing_error_issue(file_path, &e)],
                    passed: false,
                    audit_duration_ms: 0,
                })
            }
        }
    }

    /// Process a single documentation file through all validation stages.
    #[instrument(skip(self))]
    async fn process_documentation_file(
        &mut self,
        file_path: &Path,
    ) -> Result<(FileAuditResult, Vec<AuditIssue>, Vec<crate::Recommendation>)> {
        let file_start_time = Instant::now();
        debug!("Processing documentation file: {}", file_path.display());

        // Calculate file hash and metadata
        let file_hash = self.calculate_file_hash(file_path)?;
        let last_modified = self.get_file_modified_time(file_path)?;

        // Parse the documentation file
        debug!("Parsing documentation file");
        let parsed_doc = self.parser.parse_file(file_path).await?;

        let mut all_issues = Vec::new();
        let mut all_recommendations = Vec::new();

        // Stage 1: API Reference Validation
        debug!("Validating API references");
        for api_ref in &parsed_doc.api_references {
            match self.analyzer.validate_api_reference(api_ref).await {
                Ok(result) => {
                    if !result.success {
                        all_issues.push(AuditIssue {
                            id: format!("api-ref-{}", api_ref.item_path),
                            file_path: file_path.to_path_buf(),
                            line_number: Some(api_ref.line_number),
                            column_number: None,
                            severity: IssueSeverity::Warning,
                            category: IssueCategory::ApiMismatch,
                            message: format!(
                                "API reference '{}' not found in crate",
                                api_ref.item_path
                            ),
                            suggestion: Some(format!(
                                "Check if '{}' is correctly spelled and exported",
                                api_ref.item_path
                            )),
                            context: Some(api_ref.context.clone()),
                            code_snippet: None,
                            related_issues: Vec::new(),
                        });
                    }
                }
                Err(e) => {
                    debug!("Error validating API reference '{}': {}", api_ref.item_path, e);
                }
            }
        }

        // Stage 2: Code Example Validation
        debug!("Validating code examples");
        for example in &parsed_doc.code_examples {
            if example.is_runnable {
                match self.validator.validate_example(example).await {
                    Ok(result) => {
                        if !result.success {
                            all_issues.push(AuditIssue {
                                id: format!("example-{}", example.line_number),
                                file_path: file_path.to_path_buf(),
                                line_number: Some(example.line_number),
                                column_number: None,
                                severity: IssueSeverity::Critical,
                                category: IssueCategory::CompilationError,
                                message: "Code example does not compile".to_string(),
                                suggestion: result.suggestions.first().cloned(),
                                context: Some(example.content.clone()),
                                code_snippet: Some(example.content.clone()),
                                related_issues: Vec::new(),
                            });
                        }

                        // Check for warnings (potential async pattern issues)
                        for warning in &result.warnings {
                            all_issues.push(AuditIssue {
                                id: format!("async-{}", example.line_number),
                                file_path: file_path.to_path_buf(),
                                line_number: Some(example.line_number),
                                column_number: None,
                                severity: IssueSeverity::Warning,
                                category: IssueCategory::AsyncPatternError,
                                message: warning.clone(),
                                suggestion: Some(
                                    "Consider using proper async patterns".to_string(),
                                ),
                                context: Some(example.content.clone()),
                                code_snippet: Some(example.content.clone()),
                                related_issues: Vec::new(),
                            });
                        }
                    }
                    Err(e) => {
                        debug!("Error validating example at line {}: {}", example.line_number, e);
                    }
                }
            }
        }

        // Stage 3: Version Consistency Validation
        debug!("Validating version references");
        let version_config = crate::version::VersionValidationConfig::default();
        for version_ref in &parsed_doc.version_references {
            match self.version_validator.validate_version_reference(version_ref, &version_config) {
                Ok(result) => {
                    if !result.is_valid {
                        all_issues.push(AuditIssue {
                            id: format!("version-{}", version_ref.line_number),
                            file_path: file_path.to_path_buf(),
                            line_number: Some(version_ref.line_number),
                            column_number: None,
                            severity: IssueSeverity::Warning,
                            category: IssueCategory::VersionInconsistency,
                            message: format!(
                                "Version '{}' does not match workspace version",
                                version_ref.version
                            ),
                            suggestion: Some(
                                "Update version to match workspace Cargo.toml".to_string(),
                            ),
                            context: Some(version_ref.context.clone()),
                            code_snippet: None,
                            related_issues: Vec::new(),
                        });
                    }
                }
                Err(e) => {
                    debug!("Error validating version reference '{}': {}", version_ref.version, e);
                }
            }
        }

        // Stage 4: Internal Link Validation
        debug!("Validating internal links");
        for link in &parsed_doc.internal_links {
            if !self.validate_internal_link(link, file_path) {
                all_issues.push(AuditIssue {
                    id: format!("link-{}", link.line_number),
                    file_path: file_path.to_path_buf(),
                    line_number: Some(link.line_number),
                    column_number: None,
                    severity: IssueSeverity::Info,
                    category: IssueCategory::BrokenLink,
                    message: format!("Internal link '{}' may be broken", link.target),
                    suggestion: Some("Check if the target file or section exists".to_string()),
                    context: Some(link.text.clone()),
                    code_snippet: None,
                    related_issues: Vec::new(),
                });
            }
        }

        // Stage 5: Feature Flag Validation
        debug!("Validating feature flags");
        for feature in &parsed_doc.feature_mentions {
            let result = self.version_validator.validate_feature_flag(
                &feature.feature_name,
                feature.crate_name.as_deref().unwrap_or(""),
            );
            if !result.is_valid {
                all_issues.push(AuditIssue {
                    id: format!("feature-{}", feature.line_number),
                    file_path: file_path.to_path_buf(),
                    line_number: Some(feature.line_number),
                    column_number: None,
                    severity: IssueSeverity::Warning,
                    category: IssueCategory::InvalidFeatureFlag,
                    message: format!(
                        "Feature flag '{}' not found in any crate",
                        feature.feature_name
                    ),
                    suggestion: Some(
                        "Check if feature name is correct or add to Cargo.toml".to_string(),
                    ),
                    context: Some(feature.context.clone()),
                    code_snippet: None,
                    related_issues: Vec::new(),
                });
            }
        }

        // Generate suggestions for found issues (simplified for now)
        if !all_issues.is_empty() {
            debug!("Found {} issues, generating basic recommendations", all_issues.len());
            // For now, just create a simple recommendation
            all_recommendations.push(crate::Recommendation {
                id: "general-fix".to_string(),
                recommendation_type: crate::RecommendationType::FixIssue,
                priority: 3, // Medium priority
                title: "Fix Documentation Issues".to_string(),
                description: format!(
                    "Fix {} documentation issues found in {}",
                    all_issues.len(),
                    file_path.file_name().unwrap_or_default().to_string_lossy()
                ),
                affected_files: vec![file_path.to_path_buf()],
                estimated_effort_hours: Some(1.0),
                resolves_issues: all_issues.iter().map(|i| i.id.clone()).collect(),
            });
        }

        let processing_time = file_start_time.elapsed();

        // Create file audit result
        let file_result = FileAuditResult {
            file_path: file_path.to_path_buf(),
            file_hash,
            last_modified,
            issues_count: all_issues.len(),
            issues: all_issues.clone(),
            passed: all_issues.iter().all(|issue| issue.severity != IssueSeverity::Critical),
            audit_duration_ms: processing_time.as_millis() as u64,
        };

        debug!(
            "Completed processing file {} in {:?} with {} issues",
            file_path.display(),
            processing_time,
            all_issues.len()
        );

        Ok((file_result, all_issues, all_recommendations))
    }

    /// Validate an internal link within the documentation.
    fn validate_internal_link(&self, link: &crate::InternalLink, current_file: &Path) -> bool {
        // Simple validation - check if target file exists
        if link.target.starts_with("http://") || link.target.starts_with("https://") {
            return true; // External links are not validated here
        }

        // Handle relative paths
        let target_path = if link.target.starts_with('/') {
            // Absolute path from docs root
            self.config.docs_path.join(&link.target[1..])
        } else {
            // Relative path from current file
            current_file.parent().unwrap_or(&self.config.docs_path).join(&link.target)
        };

        target_path.exists()
    }

    /// Create a processing error issue for a file.
    fn create_processing_error_issue(&self, file_path: &Path, error: &AuditError) -> AuditIssue {
        AuditIssue {
            id: format!(
                "processing-error-{}",
                file_path.file_name().unwrap_or_default().to_string_lossy()
            ),
            file_path: file_path.to_path_buf(),
            line_number: None,
            column_number: None,
            severity: IssueSeverity::Critical,
            category: IssueCategory::ProcessingError,
            message: format!("Failed to process file: {}", error),
            suggestion: None,
            context: None,
            code_snippet: None,
            related_issues: Vec::new(),
        }
    }

    /// Discover all documentation files in the docs directory.
    async fn discover_documentation_files(&self) -> Result<Vec<PathBuf>> {
        let mut files = Vec::new();

        if !self.config.docs_path.exists() {
            warn!("Documentation directory does not exist: {}", self.config.docs_path.display());
            return Ok(files);
        }

        for entry in WalkDir::new(&self.config.docs_path)
            .follow_links(true)
            .into_iter()
            .filter_map(|e| e.ok())
        {
            let path = entry.path();
            if self.is_documentation_file(path) {
                files.push(path.to_path_buf());
            }
        }

        debug!("Discovered {} documentation files", files.len());
        Ok(files)
    }

    /// Check if a file is a documentation file (markdown).
    fn is_documentation_file(&self, path: &Path) -> bool {
        path.extension()
            .and_then(|ext| ext.to_str())
            .map(|ext| ext.eq_ignore_ascii_case("md") || ext.eq_ignore_ascii_case("markdown"))
            .unwrap_or(false)
    }

    /// Check if a file should be skipped based on exclusion patterns.
    fn should_skip_file(&self, path: &Path) -> bool {
        let path_str = path.to_string_lossy();

        for pattern in &self.config.excluded_files {
            if glob_match::glob_match(pattern, &path_str) {
                return true;
            }
        }

        false
    }

    /// Create audit summary from file results and issues.
    fn create_audit_summary(
        &self,
        file_results: &[FileAuditResult],
        issues: &[AuditIssue],
    ) -> AuditSummary {
        let total_files = file_results.len();
        let files_with_issues = file_results.iter().filter(|r| !r.issues.is_empty()).count();
        let total_issues = issues.len();

        let critical_issues =
            issues.iter().filter(|i| i.severity == IssueSeverity::Critical).count();
        let warning_issues = issues.iter().filter(|i| i.severity == IssueSeverity::Warning).count();
        let info_issues = issues.iter().filter(|i| i.severity == IssueSeverity::Info).count();

        let coverage_percentage = if total_files > 0 {
            ((total_files - files_with_issues) as f64 / total_files as f64) * 100.0
        } else {
            100.0
        };

        let average_issues_per_file =
            if total_files > 0 { total_issues as f64 / total_files as f64 } else { 0.0 };

        AuditSummary {
            total_files,
            files_with_issues,
            total_issues,
            critical_issues,
            warning_issues,
            info_issues,
            coverage_percentage,
            average_issues_per_file,
            most_common_issue: None,
            problematic_files: Vec::new(),
        }
    }

    /// Calculate SHA256 hash of a file for change detection.
    fn calculate_file_hash(&self, file_path: &Path) -> Result<String> {
        let content = fs::read(file_path).map_err(|e| AuditError::IoError {
            path: file_path.to_path_buf(),
            details: e.to_string(),
        })?;

        let mut hasher = Sha256::new();
        hasher.update(&content);
        let hash = hasher.finalize();
        Ok(format!("{:x}", hash))
    }

    /// Get the last modified time of a file.
    fn get_file_modified_time(&self, file_path: &Path) -> Result<chrono::DateTime<Utc>> {
        let metadata = fs::metadata(file_path).map_err(|e| AuditError::IoError {
            path: file_path.to_path_buf(),
            details: e.to_string(),
        })?;

        let modified = metadata.modified().map_err(|e| AuditError::IoError {
            path: file_path.to_path_buf(),
            details: e.to_string(),
        })?;

        Ok(chrono::DateTime::from(modified))
    }
}

// Simple glob matching implementation
mod glob_match {
    pub fn glob_match(pattern: &str, text: &str) -> bool {
        // Simple implementation - in a real system you'd use a proper glob library
        if pattern.contains('*') {
            // Handle ** patterns (recursive directory matching)
            if pattern.contains("**") {
                let pattern = pattern.replace("**", "*");
                return glob_match_simple(&pattern, text);
            } else {
                return glob_match_simple(pattern, text);
            }
        }

        pattern == text
    }

    fn glob_match_simple(pattern: &str, text: &str) -> bool {
        let parts: Vec<&str> = pattern.split('*').collect();

        if parts.len() == 1 {
            // No wildcards
            return pattern == text;
        }

        if parts.len() == 2 {
            // Single wildcard
            let prefix = parts[0];
            let suffix = parts[1];
            return text.starts_with(prefix)
                && text.ends_with(suffix)
                && text.len() >= prefix.len() + suffix.len();
        }

        // Multiple wildcards - more complex matching
        let mut text_pos = 0;

        for (i, part) in parts.iter().enumerate() {
            if part.is_empty() {
                continue;
            }

            if i == 0 {
                // First part must match at the beginning
                if !text[text_pos..].starts_with(part) {
                    return false;
                }
                text_pos += part.len();
            } else if i == parts.len() - 1 {
                // Last part must match at the end
                return text[text_pos..].ends_with(part);
            } else {
                // Middle parts can match anywhere after current position
                if let Some(pos) = text[text_pos..].find(part) {
                    text_pos += pos + part.len();
                } else {
                    return false;
                }
            }
        }

        true
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::fs;
    use tempfile::TempDir;

    async fn create_test_orchestrator() -> (AuditOrchestrator, TempDir) {
        let temp_dir = TempDir::new().unwrap();
        let workspace_path = temp_dir.path().to_path_buf();
        let docs_path = workspace_path.join("docs");

        // Create basic directory structure
        fs::create_dir_all(&docs_path).unwrap();

        // Create a simple Cargo.toml
        let cargo_toml = r#"
[package]
name = "test-crate"
version = "0.1.0"
edition = "2021"
"#;
        fs::write(workspace_path.join("Cargo.toml"), cargo_toml).unwrap();

        let config = AuditConfig::builder()
            .workspace_path(&workspace_path)
            .docs_path(&docs_path)
            .build()
            .unwrap();

        let orchestrator = AuditOrchestrator::new(config).await.unwrap();
        (orchestrator, temp_dir)
    }

    #[tokio::test]
    async fn test_orchestrator_creation() {
        let (_orchestrator, _temp_dir) = create_test_orchestrator().await;
        // If we get here, orchestrator was created successfully
    }

    #[tokio::test]
    async fn test_discover_documentation_files() {
        let (orchestrator, temp_dir) = create_test_orchestrator().await;

        // Create some test files
        let docs_path = temp_dir.path().join("docs");
        fs::write(docs_path.join("test1.md"), "# Test 1").unwrap();
        fs::write(docs_path.join("test2.markdown"), "# Test 2").unwrap();
        fs::write(docs_path.join("not_docs.txt"), "Not docs").unwrap();

        let files = orchestrator.discover_documentation_files().await.unwrap();

        assert_eq!(files.len(), 2);
        assert!(files.iter().any(|f| f.file_name().unwrap() == "test1.md"));
        assert!(files.iter().any(|f| f.file_name().unwrap() == "test2.markdown"));
    }

    #[tokio::test]
    async fn test_is_documentation_file() {
        let (orchestrator, _temp_dir) = create_test_orchestrator().await;

        assert!(orchestrator.is_documentation_file(Path::new("test.md")));
        assert!(orchestrator.is_documentation_file(Path::new("test.markdown")));
        assert!(orchestrator.is_documentation_file(Path::new("test.MD")));
        assert!(!orchestrator.is_documentation_file(Path::new("test.txt")));
        assert!(!orchestrator.is_documentation_file(Path::new("test.rs")));
    }

    #[tokio::test]
    async fn test_should_skip_file() {
        let temp_dir = TempDir::new().unwrap();
        let workspace_path = temp_dir.path().to_path_buf();
        let docs_path = workspace_path.join("docs");
        fs::create_dir_all(&docs_path).unwrap();

        // Create a simple Cargo.toml
        let cargo_toml = r#"
[package]
name = "test-crate"
version = "0.1.0"
edition = "2021"
"#;
        fs::write(workspace_path.join("Cargo.toml"), cargo_toml).unwrap();

        let config = AuditConfig::builder()
            .workspace_path(&workspace_path)
            .docs_path(&docs_path)
            .exclude_files(vec!["**/internal/**".to_string(), "draft_*.md".to_string()])
            .build()
            .unwrap();

        let orchestrator = AuditOrchestrator::new(config).await.unwrap();

        assert!(orchestrator.should_skip_file(Path::new("docs/internal/secret.md")));
        assert!(orchestrator.should_skip_file(Path::new("draft_feature.md")));
        assert!(!orchestrator.should_skip_file(Path::new("docs/public.md")));
    }

    #[tokio::test]
    async fn test_empty_incremental_audit() {
        let (mut orchestrator, _temp_dir) = create_test_orchestrator().await;

        let result = orchestrator.run_incremental_audit(&[]).await.unwrap();

        assert_eq!(result.summary.total_files, 0);
        assert_eq!(result.summary.total_issues, 0);
        assert_eq!(result.file_results.len(), 0);
    }
}