debtmap 0.17.0

Code complexity and technical debt analyzer
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
use crate::core::AnalysisResults;
use crate::priority::{DebtType, UnifiedAnalysis, UnifiedDebtItem};
use serde::{Deserialize, Serialize};
use std::collections::HashMap;

// Health Dashboard Components
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct HealthDashboard {
    pub overall_health: HealthStatus,
    pub trend: TrendIndicator,
    pub velocity_impact: VelocityImpact,
    pub risk_level: RiskLevel,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum HealthStatus {
    Good(u8),         // 80-100%
    ModerateRisk(u8), // 60-79%
    HighRisk(u8),     // 40-59%
    Critical(u8),     // 0-39%
}

impl HealthStatus {
    pub fn from_score(score: u32) -> Self {
        let score_u8 = score.min(100) as u8;
        match score {
            80..=100 => HealthStatus::Good(score_u8),
            60..=79 => HealthStatus::ModerateRisk(score_u8),
            40..=59 => HealthStatus::HighRisk(score_u8),
            _ => HealthStatus::Critical(score_u8),
        }
    }

    pub fn as_string(&self) -> String {
        match self {
            HealthStatus::Good(score) => format!("{}% [GOOD] Good", score),
            HealthStatus::ModerateRisk(score) => format!("{}% [WARN] Moderate Risk", score),
            HealthStatus::HighRisk(score) => format!("{}% [HIGH] High Risk", score),
            HealthStatus::Critical(score) => format!("{}% [CRIT] Critical", score),
        }
    }

    pub fn interpretation(&self) -> &'static str {
        match self {
            HealthStatus::Good(_) => "Minimal technical debt, sustainable velocity",
            HealthStatus::ModerateRisk(_) => "Some debt accumulation, watch for trends",
            HealthStatus::HighRisk(_) => "Significant debt impact, allocate debt reduction",
            HealthStatus::Critical(_) => "Major architectural issues, immediate action needed",
        }
    }
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum TrendIndicator {
    Improving,
    Stable,
    Declining,
}

impl TrendIndicator {
    pub fn as_emoji(&self) -> &'static str {
        match self {
            TrendIndicator::Improving => "/\\",
            TrendIndicator::Stable => "--",
            TrendIndicator::Declining => "\\/",
        }
    }

    pub fn as_string(&self) -> &'static str {
        match self {
            TrendIndicator::Improving => "Improving",
            TrendIndicator::Stable => "Stable",
            TrendIndicator::Declining => "Declining",
        }
    }
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct VelocityImpact {
    pub slowdown_percentage: f64,
    pub description: String,
}

impl VelocityImpact {
    pub fn from_debt_analysis(debt_count: usize, avg_complexity: f64) -> Self {
        // Empirical formula based on industry studies
        let complexity_factor = (avg_complexity / 10.0).min(2.0);
        let debt_factor = (debt_count as f64 / 50.0).min(2.0);
        let slowdown = (complexity_factor * 0.15 + debt_factor * 0.10) * 100.0;

        let description = match slowdown {
            x if x < 5.0 => "Minimal impact on delivery speed",
            x if x < 15.0 => "Moderate slowdown in feature delivery",
            x if x < 30.0 => "Significant impact on development velocity",
            _ => "Severe velocity reduction, impacting deadlines",
        };

        VelocityImpact {
            slowdown_percentage: slowdown,
            description: description.to_string(),
        }
    }
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum RiskLevel {
    Low,
    Moderate,
    High,
    Critical,
}

impl RiskLevel {
    pub fn from_metrics(health_score: u32, debt_count: usize, avg_complexity: f64) -> Self {
        if health_score >= 80 && debt_count < 20 && avg_complexity < 10.0 {
            RiskLevel::Low
        } else if health_score >= 60 && debt_count < 50 && avg_complexity < 15.0 {
            RiskLevel::Moderate
        } else if health_score >= 40 && debt_count < 100 && avg_complexity < 20.0 {
            RiskLevel::High
        } else {
            RiskLevel::Critical
        }
    }

    pub fn as_string(&self) -> &'static str {
        match self {
            RiskLevel::Low => "Low Risk",
            RiskLevel::Moderate => "Moderate Risk",
            RiskLevel::High => "High Risk",
            RiskLevel::Critical => "Critical Risk",
        }
    }
}

// Quick Wins Analysis
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct QuickWins {
    pub count: usize,
    pub total_effort_hours: u32,
    pub expected_impact: ImpactSummary,
    pub recommendations: Vec<String>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ImpactSummary {
    pub health_improvement: f64,
    pub complexity_reduction: f64,
    pub coverage_increase: f64,
}

pub fn identify_quick_wins(items: &[UnifiedDebtItem]) -> QuickWins {
    let quick_items: Vec<_> = items
        .iter()
        .filter(|item| estimate_effort_hours(item) <= 8)
        .collect();

    let total_effort: u32 = quick_items
        .iter()
        .map(|item| estimate_effort_hours(item))
        .sum();

    let expected_impact = calculate_batch_impact(&quick_items);

    let mut recommendations = Vec::new();

    // Group quick wins by type for better recommendations
    let mut by_type: HashMap<String, Vec<&UnifiedDebtItem>> = HashMap::new();
    for item in &quick_items {
        let type_key = match &item.debt_type {
            DebtType::TestingGap { .. } => "Testing",
            DebtType::ComplexityHotspot { .. } => "Complexity",
            DebtType::DeadCode { .. } => "Dead Code",
            DebtType::Duplication { .. } => "Duplication",
            DebtType::Risk { .. } => "Risk",
            DebtType::GodObject { .. } => "God Object",
            DebtType::TestComplexityHotspot { .. } => "Test Complexity",
            DebtType::TestTodo { .. } => "Test Todo",
            DebtType::TestDuplication { .. } => "Test Duplication",
            DebtType::ErrorSwallowing { .. } => "Error Handling",
            DebtType::AllocationInefficiency { .. } => "Allocation",
            DebtType::StringConcatenation { .. } => "String Operations",
            DebtType::NestedLoops { .. } => "Nested Loops",
            DebtType::BlockingIO { .. } => "Blocking IO",
            DebtType::SuboptimalDataStructure { .. } => "Data Structure",
            DebtType::FeatureEnvy { .. } => "Feature Envy",
            DebtType::PrimitiveObsession { .. } => "Primitive Obsession",
            DebtType::MagicValues { .. } => "Magic Values",
            DebtType::AssertionComplexity { .. } => "Complex Assertions",
            DebtType::FlakyTestPattern { .. } => "Flaky Tests",
            DebtType::AsyncMisuse { .. } => "Async Issues",
            DebtType::ResourceLeak { .. } => "Resource Leaks",
            DebtType::CollectionInefficiency { .. } => "Collection Issues",
            // Type organization (Spec 187)
            DebtType::ScatteredType { .. } => "Scattered Types",
            DebtType::OrphanedFunctions { .. } => "Orphaned Functions",
            DebtType::UtilitiesSprawl { .. } => "Utilities Sprawl",
            // Default for legacy variants
            _ => "Other",
        };
        by_type.entry(type_key.to_string()).or_default().push(item);
    }

    // Generate specific recommendations
    if let Some(testing_items) = by_type.get("Testing") {
        recommendations.push(format!(
            "Add tests for {} untested critical functions (est. {} hours)",
            testing_items.len(),
            testing_items
                .iter()
                .map(|i| estimate_effort_hours(i))
                .sum::<u32>()
        ));
    }

    if let Some(dead_code_items) = by_type.get("Dead Code") {
        recommendations.push(format!(
            "Remove {} unused functions to reduce maintenance burden",
            dead_code_items.len()
        ));
    }

    if let Some(duplication_items) = by_type.get("Duplication") {
        recommendations.push(format!(
            "Extract {} duplicated code blocks into shared functions",
            duplication_items.len()
        ));
    }

    // Limit to top 3 recommendations
    recommendations.truncate(3);

    QuickWins {
        count: quick_items.len(),
        total_effort_hours: total_effort,
        expected_impact,
        recommendations,
    }
}

fn calculate_batch_impact(items: &[&UnifiedDebtItem]) -> ImpactSummary {
    let total_complexity_reduction: f64 = items
        .iter()
        .map(|item| item.expected_impact.complexity_reduction)
        .sum();

    let total_coverage_improvement: f64 = items
        .iter()
        .map(|item| item.expected_impact.coverage_improvement)
        .sum();

    // Estimate health improvement based on fixes
    let health_improvement = (items.len() as f64 * 0.5).min(10.0);

    ImpactSummary {
        health_improvement,
        complexity_reduction: total_complexity_reduction,
        coverage_increase: total_coverage_improvement * 100.0,
    }
}

// Strategic Priorities
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct StrategicPriority {
    pub title: String,
    pub description: String,
    pub effort_estimate: EffortEstimate,
    pub business_impact: String,
    pub blocking_factor: f64,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum EffortEstimate {
    Hours(u32),
    Days(u32),
    Sprints(u32),
}

impl EffortEstimate {
    pub fn as_string(&self) -> String {
        match self {
            EffortEstimate::Hours(h) => format!("{} hours", h),
            EffortEstimate::Days(d) => format!("{} days", d),
            EffortEstimate::Sprints(s) => format!("{} sprints", s),
        }
    }
}

pub fn identify_strategic_priorities(
    items: &[UnifiedDebtItem],
    limit: usize,
) -> Vec<StrategicPriority> {
    let mut priorities = Vec::new();

    for item in items.iter().take(limit) {
        let effort_hours = estimate_effort_hours(item);
        let effort_estimate = if effort_hours <= 8 {
            EffortEstimate::Hours(effort_hours)
        } else if effort_hours <= 40 {
            EffortEstimate::Days(effort_hours / 8)
        } else {
            EffortEstimate::Sprints(effort_hours / 80)
        };

        let business_impact = generate_business_impact(item);

        // Calculate blocking factor based on dependencies
        let blocking_factor = calculate_blocking_factor(item);

        priorities.push(StrategicPriority {
            title: item.recommendation.primary_action.clone(),
            description: format!(
                "In {}: {}",
                item.location.file.display(),
                item.location.function
            ),
            effort_estimate,
            business_impact,
            blocking_factor,
        });
    }

    priorities
}

fn generate_business_impact(item: &UnifiedDebtItem) -> String {
    match &item.debt_type {
        DebtType::TestingGap { coverage, .. } => {
            format!(
                "Reduces production bug risk by ~{}%, improves deployment confidence",
                ((1.0 - coverage) * 30.0) as u32
            )
        }
        DebtType::ComplexityHotspot { cyclomatic, .. } => {
            format!(
                "Reduces feature development time by ~{}%, lowers bug introduction rate",
                (cyclomatic / 2).min(30)
            )
        }
        DebtType::GodObject {
            responsibilities, ..
        } => {
            format!(
                "Unblocks parallel development on {} related features, reduces merge conflicts",
                responsibilities / 3
            )
        }
        DebtType::DeadCode { .. } => {
            "Reduces maintenance overhead, improves code clarity".to_string()
        }
        DebtType::Duplication { instances, .. } => {
            format!(
                "Eliminates {} duplicate maintenance points, reduces inconsistency risk",
                instances
            )
        }
        DebtType::Risk { risk_score, .. } => {
            format!(
                "Reduces technical risk by {:.0}%, improves system stability",
                risk_score * 10.0
            )
        }
        DebtType::BlockingIO { operation, .. } => {
            format!(
                "Improves responsiveness by eliminating blocking {}",
                operation
            )
        }
        DebtType::NestedLoops {
            complexity_estimate,
            ..
        } => {
            format!(
                "Reduces algorithmic complexity ({}), improves performance",
                complexity_estimate
            )
        }
        DebtType::SuboptimalDataStructure {
            recommended_type, ..
        } => {
            format!("Improves performance by using {}", recommended_type)
        }
        DebtType::FeatureEnvy { external_class, .. } => {
            format!(
                "Improves cohesion, reduces coupling with {}",
                external_class
            )
        }
        DebtType::TestComplexityHotspot { .. } => {
            "Simplifies test maintenance, reduces test execution time".to_string()
        }
        DebtType::TestDuplication { .. } => {
            "Reduces test maintenance burden, improves consistency".to_string()
        }
        DebtType::ErrorSwallowing { .. } => {
            "Improves error visibility, aids debugging and monitoring".to_string()
        }
        DebtType::AllocationInefficiency { .. } => {
            "Reduces memory pressure, improves performance".to_string()
        }
        DebtType::StringConcatenation { .. } => {
            "Improves string building performance, reduces allocations".to_string()
        }
        DebtType::PrimitiveObsession { .. } => {
            "Improves type safety, reduces validation errors".to_string()
        }
        DebtType::MagicValues { .. } => {
            "Improves code readability, reduces maintenance errors".to_string()
        }
        DebtType::AssertionComplexity { .. } => {
            "Simplifies test understanding, improves test reliability".to_string()
        }
        DebtType::FlakyTestPattern { .. } => {
            "Improves test reliability, reduces false failures".to_string()
        }
        DebtType::AsyncMisuse { .. } => {
            "Prevents concurrency bugs, improves async performance".to_string()
        }
        DebtType::ResourceLeak { .. } => {
            "Prevents resource exhaustion, improves system stability".to_string()
        }
        DebtType::CollectionInefficiency { .. } => {
            "Improves collection performance, reduces memory usage".to_string()
        }
        // Type organization (Spec 187)
        DebtType::ScatteredType { type_name, .. } => {
            format!(
                "Consolidates {} behavior, improves discoverability and maintainability",
                type_name
            )
        }
        DebtType::OrphanedFunctions { target_type, .. } => {
            format!(
                "Improves {} API discoverability, enables better IDE autocomplete",
                target_type
            )
        }
        DebtType::UtilitiesSprawl { .. } => {
            "Improves module cohesion, reduces cognitive load when navigating codebase".to_string()
        }
        _ => "Improves overall codebase health and maintainability".to_string(),
    }
}

fn calculate_blocking_factor(item: &UnifiedDebtItem) -> f64 {
    // Higher downstream dependencies = higher blocking factor
    let dependency_factor = (item.downstream_dependencies as f64 / 10.0).min(1.0);

    // Higher complexity = more blocking
    let complexity_factor = (item.cyclomatic_complexity as f64 / 20.0).min(1.0);

    // Combined blocking factor
    (dependency_factor * 0.6 + complexity_factor * 0.4) * 10.0
}

// Team Guidance
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TeamGuidance {
    pub recommended_debt_allocation: u8,
    pub focus_areas: Vec<String>,
    pub process_improvements: Vec<String>,
}

impl TeamGuidance {
    pub fn from_analysis(health_score: u32, items: &[UnifiedDebtItem]) -> Self {
        let recommended_debt_allocation = match health_score {
            80..=100 => 5, // 5-10%
            60..=79 => 15, // 15-20%
            40..=59 => 25, // 25-30%
            _ => 40,       // 40-50%
        };

        let mut focus_areas = Vec::new();
        let mut process_improvements = Vec::new();

        // Analyze debt types to determine focus areas
        let mut type_counts: HashMap<&str, usize> = HashMap::new();
        for item in items {
            let type_key = match &item.debt_type {
                DebtType::TestingGap { .. } => "Testing",
                DebtType::ComplexityHotspot { .. } => "Complexity",
                DebtType::GodObject { .. } => "Architecture",
                _ => "Other",
            };
            *type_counts.entry(type_key).or_default() += 1;
        }

        // Generate focus areas based on debt distribution
        if type_counts.get("Testing").unwrap_or(&0) > &10 {
            focus_areas.push("Improve test coverage for critical paths".to_string());
            process_improvements.push("Implement test-first development practices".to_string());
        }

        if type_counts.get("Complexity").unwrap_or(&0) > &5 {
            focus_areas.push("Refactor complex functions into smaller units".to_string());
            process_improvements.push("Enforce complexity limits in code reviews".to_string());
        }

        if type_counts.get("Architecture").unwrap_or(&0) > &3 {
            focus_areas.push("Refactor god objects and improve module boundaries".to_string());
            process_improvements.push("Regular architecture review sessions".to_string());
        }

        TeamGuidance {
            recommended_debt_allocation,
            focus_areas,
            process_improvements,
        }
    }
}

// Success Metrics
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SuccessMetrics {
    pub target_health_score: u8,
    pub target_coverage: f64,
    pub target_complexity_reduction: f64,
    pub timeline: String,
}

impl SuccessMetrics {
    pub fn from_current_state(health_score: u32, avg_complexity: f64) -> Self {
        let target_health_score = match health_score {
            80..=100 => 90,
            60..=79 => 80,
            40..=59 => 65,
            _ => 50,
        };

        let target_complexity_reduction = if avg_complexity > 15.0 {
            30.0
        } else if avg_complexity > 10.0 {
            20.0
        } else {
            10.0
        };

        let timeline = match health_score {
            80..=100 => "Next sprint",
            60..=79 => "Next 2 sprints",
            40..=59 => "Next quarter",
            _ => "Next 2 quarters",
        };

        SuccessMetrics {
            target_health_score,
            target_coverage: 0.8,
            target_complexity_reduction,
            timeline: timeline.to_string(),
        }
    }
}

// Complete Executive Summary
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ExecutiveSummary {
    pub health_dashboard: HealthDashboard,
    pub quick_wins: QuickWins,
    pub strategic_priorities: Vec<StrategicPriority>,
    pub team_guidance: TeamGuidance,
    pub success_metrics: SuccessMetrics,
}

pub fn generate_executive_summary(
    results: &AnalysisResults,
    unified_analysis: Option<&UnifiedAnalysis>,
    health_score: u32,
    avg_complexity: f64,
) -> ExecutiveSummary {
    let debt_count = results.technical_debt.items.len();

    // Generate health dashboard
    let health_dashboard = HealthDashboard {
        overall_health: HealthStatus::from_score(health_score),
        trend: TrendIndicator::Stable, // Would need historical data for real trend
        velocity_impact: VelocityImpact::from_debt_analysis(debt_count, avg_complexity),
        risk_level: RiskLevel::from_metrics(health_score, debt_count, avg_complexity),
    };

    // Identify quick wins and strategic priorities
    let (quick_wins, strategic_priorities, team_guidance) = if let Some(analysis) = unified_analysis
    {
        let items: Vec<UnifiedDebtItem> = analysis.items.iter().cloned().collect();
        (
            identify_quick_wins(&items),
            identify_strategic_priorities(&items, 3),
            TeamGuidance::from_analysis(health_score, &items),
        )
    } else {
        (
            QuickWins {
                count: 0,
                total_effort_hours: 0,
                expected_impact: ImpactSummary {
                    health_improvement: 0.0,
                    complexity_reduction: 0.0,
                    coverage_increase: 0.0,
                },
                recommendations: vec![],
            },
            vec![],
            TeamGuidance {
                recommended_debt_allocation: 10,
                focus_areas: vec![],
                process_improvements: vec![],
            },
        )
    };

    let success_metrics = SuccessMetrics::from_current_state(health_score, avg_complexity);

    ExecutiveSummary {
        health_dashboard,
        quick_wins,
        strategic_priorities,
        team_guidance,
        success_metrics,
    }
}

// Helper function to estimate effort
pub fn estimate_effort_hours(item: &UnifiedDebtItem) -> u32 {
    match &item.debt_type {
        DebtType::TestingGap { cyclomatic, .. } => {
            // Testing effort scales with complexity
            (cyclomatic / 2).clamp(2, 16)
        }
        DebtType::ComplexityHotspot {
            cyclomatic,
            cognitive,
            ..
        } => {
            // Refactoring effort based on both metrics
            ((cyclomatic + cognitive) / 3).clamp(4, 40)
        }
        DebtType::DeadCode { .. } => {
            // Dead code removal is usually quick
            1
        }
        DebtType::Duplication { instances, .. } => {
            // Extraction effort scales with instances
            (instances * 2).min(24)
        }
        DebtType::GodObject {
            responsibilities, ..
        } => {
            // God object refactoring is complex
            (responsibilities * 4).clamp(16, 80)
        }
        DebtType::BlockingIO { .. } => {
            // Async refactoring
            12
        }
        DebtType::NestedLoops { depth, .. } => {
            // Complexity scales with depth
            (depth * 4).min(24)
        }
        DebtType::SuboptimalDataStructure { .. } => {
            // Data structure refactoring
            8
        }
        DebtType::FeatureEnvy { .. } => {
            // Moving methods between classes
            6
        }
        DebtType::TestComplexityHotspot { .. } => {
            // Test refactoring
            4
        }
        DebtType::TestDuplication { instances, .. } => {
            // Test deduplication
            (*instances).min(12)
        }
        DebtType::TestTodo { .. } => {
            // Completing TODO test
            2
        }
        DebtType::ErrorSwallowing { .. } => {
            // Error handling fix
            3
        }
        DebtType::AllocationInefficiency { .. } => {
            // Memory optimization
            6
        }
        DebtType::StringConcatenation { .. } => {
            // String builder refactor
            3
        }
        DebtType::PrimitiveObsession { .. } => {
            // Type creation
            8
        }
        DebtType::MagicValues { .. } => {
            // Constants extraction
            2
        }
        DebtType::AssertionComplexity { .. } => {
            // Assertion simplification
            3
        }
        DebtType::FlakyTestPattern { .. } => {
            // Test stabilization
            6
        }
        DebtType::AsyncMisuse { .. } => {
            // Async refactoring
            10
        }
        DebtType::ResourceLeak { .. } => {
            // Resource management fix
            5
        }
        DebtType::CollectionInefficiency { .. } => {
            // Collection optimization
            4
        }
        // Type organization (Spec 187)
        DebtType::ScatteredType { file_count, .. } => {
            // Consolidating scattered methods scales with file count
            (file_count * 2).clamp(3, 10) as u32
        }
        DebtType::OrphanedFunctions { function_count, .. } => {
            // Converting functions to methods is straightforward
            (function_count / 4).clamp(1, 3) as u32
        }
        DebtType::UtilitiesSprawl { distinct_types, .. } => {
            // Breaking up utilities module is moderate effort
            (distinct_types * 2).clamp(5, 10) as u32
        }
        _ => 8, // Default estimate
    }
}

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

    use crate::priority::{
        ActionableRecommendation, FunctionRole, ImpactMetrics, Location, UnifiedScore,
    };
    use std::path::PathBuf;

    fn create_test_debt_item(debt_type: DebtType, complexity: u32) -> UnifiedDebtItem {
        UnifiedDebtItem {
            location: Location {
                file: PathBuf::from("test.rs"),
                function: "test_function".to_string(),
                line: 10,
            },
            debt_type,
            recommendation: ActionableRecommendation {
                primary_action: "Test action".to_string(),
                rationale: "Test rationale".to_string(),
                implementation_steps: vec![],
                related_items: vec![],
                steps: None,
                estimated_effort_hours: None,
            },
            expected_impact: ImpactMetrics {
                risk_reduction: 0.5,
                complexity_reduction: 0.3,
                coverage_improvement: 0.2,
                lines_reduction: 10,
            },
            unified_score: UnifiedScore {
                final_score: 5.0,
                base_score: None,
                exponential_factor: None,
                risk_boost: None,
                pre_adjustment_score: None,
                adjustment_applied: None,
                coverage_factor: 1.0,
                complexity_factor: 1.0,
                dependency_factor: 1.0,
                role_multiplier: 1.0,
                purity_factor: None,
                refactorability_factor: None,
                pattern_factor: None,
                // Spec 260: Score transparency fields
                debt_adjustment: None,
                pre_normalization_score: None,
                structural_multiplier: Some(1.0),
                has_coverage_data: false,
                contextual_risk_multiplier: None,
                pre_contextual_score: None,
                debt_type_multiplier: None,
            },
            upstream_dependencies: 0,
            downstream_dependencies: 0,
            cyclomatic_complexity: complexity,
            cognitive_complexity: complexity - 2,
            nesting_depth: 3,
            function_length: 50,
            function_role: FunctionRole::PureLogic,
            transitive_coverage: None,
            file_context: None,
            upstream_callers: vec![],
            downstream_callees: vec![],
            upstream_production_callers: vec![],
            upstream_test_callers: vec![],
            production_blast_radius: 0,
            is_pure: None,
            purity_confidence: None,
            purity_level: None,
            god_object_indicators: None,
            tier: None,
            function_context: None,
            context_confidence: None,
            contextual_recommendation: None,
            pattern_analysis: None,
            context_multiplier: None,
            context_type: None,
            language_specific: None, // spec 190
            detected_pattern: None,
            contextual_risk: None, // spec 203
            file_line_count: None,
            responsibility_category: None,
            error_swallowing_count: None,
            error_swallowing_patterns: None,
            entropy_analysis: None,
            context_suggestion: None,
        }
    }

    #[test]
    fn test_health_status_classification() {
        assert!(matches!(
            HealthStatus::from_score(90),
            HealthStatus::Good(_)
        ));
        assert!(matches!(
            HealthStatus::from_score(70),
            HealthStatus::ModerateRisk(_)
        ));
        assert!(matches!(
            HealthStatus::from_score(50),
            HealthStatus::HighRisk(_)
        ));
        assert!(matches!(
            HealthStatus::from_score(30),
            HealthStatus::Critical(_)
        ));
    }

    #[test]
    fn test_velocity_impact_calculation() {
        let impact = VelocityImpact::from_debt_analysis(10, 5.0);
        assert!(impact.slowdown_percentage < 10.0);

        let high_impact = VelocityImpact::from_debt_analysis(100, 20.0);
        assert!(high_impact.slowdown_percentage > 20.0);
    }

    #[test]
    fn test_quick_wins_identification() {
        let items = vec![
            create_test_debt_item(
                DebtType::DeadCode {
                    visibility: crate::priority::FunctionVisibility::Private,
                    cyclomatic: 5,
                    cognitive: 3,
                    usage_hints: vec![],
                },
                5,
            ),
            create_test_debt_item(
                DebtType::TestingGap {
                    coverage: 0.0,
                    cyclomatic: 10,
                    cognitive: 8,
                },
                10,
            ),
            create_test_debt_item(
                DebtType::ComplexityHotspot {
                    cyclomatic: 30,
                    cognitive: 25,
                },
                30,
            ),
        ];

        let quick_wins = identify_quick_wins(&items);
        assert!(quick_wins.count > 0);
        assert!(quick_wins.total_effort_hours <= quick_wins.count as u32 * 8);
    }

    #[test]
    fn test_effort_estimation() {
        let test_gap = create_test_debt_item(
            DebtType::TestingGap {
                coverage: 0.0,
                cyclomatic: 10,
                cognitive: 8,
            },
            10,
        );
        assert!(estimate_effort_hours(&test_gap) <= 16);

        let dead_code = create_test_debt_item(
            DebtType::DeadCode {
                visibility: crate::priority::FunctionVisibility::Private,
                cyclomatic: 5,
                cognitive: 3,
                usage_hints: vec![],
            },
            5,
        );
        assert_eq!(estimate_effort_hours(&dead_code), 1);
    }

    #[test]
    fn test_team_guidance_generation() {
        let items = vec![
            create_test_debt_item(
                DebtType::TestingGap {
                    coverage: 0.0,
                    cyclomatic: 10,
                    cognitive: 8,
                },
                10,
            );
            15
        ];

        let guidance = TeamGuidance::from_analysis(75, &items);
        assert_eq!(guidance.recommended_debt_allocation, 15);
        assert!(!guidance.focus_areas.is_empty());
    }

    #[test]
    fn test_success_metrics_generation() {
        let metrics = SuccessMetrics::from_current_state(65, 12.0);
        assert_eq!(metrics.target_health_score, 80);
        assert_eq!(metrics.target_coverage, 0.8);
        assert_eq!(metrics.target_complexity_reduction, 20.0);
    }
}