webpage_quality_analyzer 1.0.2

High-performance webpage quality analyzer with 115 comprehensive metrics - Rust library with WASM, C++, and Python bindings
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
/// Profile Compiler for Phase 3 - Profile-Aware Scoring Engine
/// Pre-compiles profile configurations into optimized scoring rules for maximum performance
use crate::config::enhanced_models::*;
use crate::constants::*;
use crate::metrics::definitions::{
    BonusCondition, LinearScorer, MetricCategory, MetricDefinition, MetricScorer, ThresholdSet,
    METRIC_REGISTRY,
};
use crate::metrics::scoring_functions::WordCountScorer;
use std::collections::HashMap;
use std::sync::Arc;

/// Compilation errors that occur during profile compilation
#[derive(Debug, thiserror::Error)]
pub enum CompilationError {
    #[error("Unknown metric referenced in profile: {0}")]
    UnknownMetric(String),

    #[error("Invalid metric configuration for {metric}: {reason}")]
    InvalidMetricConfig { metric: String, reason: String },

    #[error("Category weight validation failed: {0}")]
    CategoryWeights(String),

    #[error("Penalty compilation failed: {0}")]
    PenaltyCompilation(String),

    #[error("Bonus compilation failed: {0}")]
    BonusCompilation(String),

    #[error("Profile validation failed: {0}")]
    ProfileValidation(String),
}

/// Compiled profile optimized for fast scoring operations
/// Uses Arc<> for shared immutable data to avoid expensive cloning
#[derive(Debug, Clone)]
pub struct CompiledProfile {
    pub profile_name: String,
    pub compiled_rules: HashMap<String, CompiledMetricRule>,
    pub category_weights: HashMap<MetricCategory, f32>,
    pub global_penalties: Vec<CompiledPenalty>,
    pub global_bonuses: Vec<CompiledBonus>,
    pub quality_bands: Arc<QualityBandConfig>,
    pub content_expectations: Arc<ContentExpectations>,
}

impl Default for CompiledProfile {
    fn default() -> Self {
        Self {
            profile_name: DEFAULT_PROFILE.to_string(),
            compiled_rules: HashMap::new(),
            category_weights: HashMap::new(),
            global_penalties: Vec::new(),
            global_bonuses: Vec::new(),
            quality_bands: Arc::new(QualityBandConfig::default()),
            content_expectations: Arc::new(ContentExpectations::default()),
        }
    }
}

/// Compiled metric rule with optimized scorer
#[derive(Clone)]
pub struct CompiledMetricRule {
    pub metric_name: String,
    pub weight: f32,
    pub enabled: bool,
    pub scorer: Arc<dyn MetricScorer>,
    pub thresholds: ThresholdSet,
    pub penalty_multiplier: f32,
    pub bonus_conditions: Vec<BonusCondition>,
    pub category: MetricCategory,
}

// Manual Debug implementation
impl std::fmt::Debug for CompiledMetricRule {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("CompiledMetricRule")
            .field("metric_name", &self.metric_name)
            .field("weight", &self.weight)
            .field("enabled", &self.enabled)
            .field("scorer", &"<MetricScorer>")
            .field("thresholds", &self.thresholds)
            .field("penalty_multiplier", &self.penalty_multiplier)
            .field("bonus_conditions", &self.bonus_conditions)
            .field("category", &self.category)
            .finish()
    }
}

/// Compiled penalty condition for fast evaluation
#[derive(Debug, Clone)]
pub struct CompiledPenalty {
    pub penalty_id: String,
    pub trigger_condition: PenaltyTrigger,
    pub penalty_type: PenaltyType,
    pub description: String,
    pub enabled: bool,
}

#[derive(Debug, Clone)]
pub enum PenaltyTrigger {
    MetricBelow {
        metric: String,
        threshold: f32,
    },
    MetricAbove {
        metric: String,
        threshold: f32,
    },
    MetricEquals {
        metric: String,
        value: f32,
    },
    MetricMissing {
        metric: String,
    },
    ContentDeficiency {
        deficiency_type: String,
        severity: f32,
    },
    MultipleConditions {
        conditions: Vec<PenaltyTrigger>,
        operator: LogicalOperator,
    },
}

#[derive(Debug, Clone)]
pub enum PenaltyType {
    FixedPoints { points: f32 },
    Multiplier { factor: f32 },
    Percentage { percent: f32 },
}

/// Compiled bonus condition for performance optimization
#[derive(Debug, Clone)]
pub struct CompiledBonus {
    pub bonus_id: String,
    pub trigger_condition: BonusTrigger,
    pub bonus_points: f32,
    pub description: String,
    pub enabled: bool,
}

#[derive(Debug, Clone)]
pub enum BonusTrigger {
    MetricExcellence {
        metric: String,
        threshold: f32,
    },
    ContentQuality {
        quality_type: String,
        requirement: String,
    },
    SynergyBonus {
        metrics: Vec<String>,
        combined_threshold: f32,
    },
}

#[derive(Debug, Clone)]
pub enum LogicalOperator {
    And,
    Or,
}

/// Profile compiler implementation
pub struct ProfileCompiler {
    metric_registry: &'static crate::metrics::definitions::MetricRegistry,
}

impl ProfileCompiler {
    /// Create a new profile compiler
    pub fn new() -> Self {
        Self {
            metric_registry: &METRIC_REGISTRY,
        }
    }

    /// Compile an enhanced profile into optimized scoring rules
    pub fn compile_profile(
        &self,
        profile: &EnhancedScoringProfile,
    ) -> Result<CompiledProfile, CompilationError> {
        // Validate profile before compilation
        self.validate_profile_for_compilation(profile)?;

        // Compile metric rules
        let compiled_rules = self.compile_metric_rules(profile)?;

        // Extract and validate category weights
        let category_weights = self.extract_category_weights(&profile.category_weights)?;

        // Compile global penalties
        let global_penalties = self.compile_penalties(&profile.penalties)?;

        // Compile global bonuses
        let global_bonuses = self.compile_bonuses(&profile.bonuses)?;

        Ok(CompiledProfile {
            profile_name: profile.metadata.name.clone(),
            compiled_rules,
            category_weights,
            global_penalties,
            global_bonuses,
            quality_bands: Arc::new(profile.quality_bands.clone()),
            content_expectations: Arc::new(profile.content_expectations.clone()),
        })
    }

    /// Validate profile structure before compilation
    fn validate_profile_for_compilation(
        &self,
        profile: &EnhancedScoringProfile,
    ) -> Result<(), CompilationError> {
        // Check that all metric overrides reference valid metrics
        for metric_name in profile.metric_overrides.keys() {
            if self
                .metric_registry
                .get_metric_definition(metric_name)
                .is_none()
            {
                return Err(CompilationError::UnknownMetric(metric_name.clone()));
            }
        }

        // Validate category weights sum approximately to 1.0 (BUG FIX: Stricter tolerance)
        let weight_sum: f32 = profile.category_weights.values().sum();
        const TOLERANCE: f32 = 0.001;  // ✅ Stricter tolerance (0.1% instead of 10%)
        
        if (weight_sum - 1.0).abs() > TOLERANCE {
            return Err(CompilationError::CategoryWeights(format!(
                "Category weights must sum to 1.0 (got {:.6}). Please adjust weights. Current weights: {:?}",
                weight_sum,
                profile.category_weights
            )));
        }

        Ok(())
    }

    /// Compile metric rules with optimized scorers
    fn compile_metric_rules(
        &self,
        profile: &EnhancedScoringProfile,
    ) -> Result<HashMap<String, CompiledMetricRule>, CompilationError> {
        let mut compiled_rules = HashMap::new();
        let profile_name = &profile.metadata.name;  // ✅ Get profile name for threshold lookup

        // Get all available metrics from registry
        for metric_def in self.metric_registry.all_metrics() {
            let metric_name = metric_def.field_name;

            // Check if there's a profile-specific override
            let metric_config =
                if let Some(override_config) = profile.metric_overrides.get(metric_name) {
                    override_config.clone()
                } else {
                    // Use default configuration from metric definition
                    MetricOverride {
                        weight: metric_def.default_weight,
                        enabled: metric_def.default_enabled,
                        thresholds: None,
                        penalty_multiplier: 1.0,
                        bonus_conditions: Vec::new(),
                        scoring_function_override: None,
                    }
                };

            // Skip disabled metrics
            if !metric_config.enabled {
                continue;
            }

            // Determine thresholds (use override or default)
            let thresholds = metric_config
                .thresholds
                .clone()
                .unwrap_or_else(|| self.get_default_thresholds_for_metric(metric_def, profile_name));

            // Create appropriate scorer
            let scorer = self.create_scorer_for_metric(metric_def, &metric_config)?;

            let compiled_rule = CompiledMetricRule {
                metric_name: metric_name.to_string(),
                weight: metric_config.weight,
                enabled: metric_config.enabled,
                scorer,
                thresholds,
                penalty_multiplier: metric_config.penalty_multiplier,
                bonus_conditions: metric_config.bonus_conditions,
                category: metric_def.category.clone(),
            };

            compiled_rules.insert(metric_name.to_string(), compiled_rule);
        }

        Ok(compiled_rules)
    }

    /// Create appropriate scorer for a specific metric
    fn create_scorer_for_metric(
        &self,
        metric_def: &MetricDefinition,
        config: &MetricOverride,
    ) -> Result<Arc<dyn MetricScorer>, CompilationError> {
        // Use custom scoring function if specified
        if let Some(scoring_override) = &config.scoring_function_override {
            return self.create_custom_scorer(scoring_override, metric_def);
        }

        // Create scorer based on metric's default scoring function
        match &metric_def.scoring_function {
            crate::metrics::definitions::ScoringFunction::Linear {
                min_value,
                max_value,
                reverse_scoring,
            } => Ok(Arc::new(LinearScorer {
                min_value: *min_value,
                max_value: *max_value,
                reverse_scoring: *reverse_scoring,
            })),
            crate::metrics::definitions::ScoringFunction::CustomFunction { function_name } => {
                match function_name.as_str() {
                    "word_count" => Ok(Arc::new(WordCountScorer)),
                    _ => Ok(Arc::new(LinearScorer {
                        min_value: 0.0,
                        max_value: 100.0,
                        reverse_scoring: false,
                    })),
                }
            }
            _ => {
                // Default to linear scorer for other types
                Ok(Arc::new(LinearScorer {
                    min_value: 0.0,
                    max_value: 100.0,
                    reverse_scoring: false,
                }))
            }
        }
    }

    /// Create custom scorer based on override
    fn create_custom_scorer(
        &self,
        scoring_override: &ScoringFunctionOverride,
        _metric_def: &MetricDefinition,
    ) -> Result<Arc<dyn MetricScorer>, CompilationError> {
        match scoring_override {
            ScoringFunctionOverride::Linear {
                min_value,
                max_value,
                reverse_scoring,
            } => Ok(Arc::new(LinearScorer {
                min_value: *min_value,
                max_value: *max_value,
                reverse_scoring: *reverse_scoring,
            })),
            ScoringFunctionOverride::CustomFunction { function_name } => {
                match function_name.as_str() {
                    "word_count" => Ok(Arc::new(WordCountScorer)),
                    _ => Err(CompilationError::InvalidMetricConfig {
                        metric: UNKNOWN_PROFILE.to_string(),
                        reason: format!("Unknown custom function: {}", function_name),
                    }),
                }
            }
            _ => {
                // Default fallback
                Ok(Arc::new(LinearScorer {
                    min_value: 0.0,
                    max_value: 100.0,
                    reverse_scoring: false,
                }))
            }
        }
    }

    /// Get default thresholds for a metric
    fn get_default_thresholds_for_metric(&self, metric_def: &MetricDefinition, profile_name: &str) -> ThresholdSet {
        // ✅ FIX: Normalize profile name to snake_case for lookup
        let normalized_name = profile_name
            .to_lowercase()
            .replace(" ", "_");
        
        // ✅ FIX: Check if metric has profile-specific configurations first
        if let Some(profile_config) = metric_def.profile_configurations.get(&normalized_name) {
            #[cfg(debug_assertions)]
            {
                eprintln!("✅ Using profile-specific thresholds for '{}' in profile '{}' (normalized: '{}'): {:?}", 
                    metric_def.field_name, profile_name, normalized_name, profile_config.thresholds);
            }
            return profile_config.thresholds.clone();
        }
        
        // Also try the original name in case it's already normalized
        if let Some(profile_config) = metric_def.profile_configurations.get(profile_name) {
            #[cfg(debug_assertions)]
            {
                eprintln!("✅ Using profile-specific thresholds for '{}' in profile '{}': {:?}", 
                    metric_def.field_name, profile_name, profile_config.thresholds);
            }
            return profile_config.thresholds.clone();
        }
        
        #[cfg(debug_assertions)]
        {
            if !metric_def.profile_configurations.is_empty() {
                eprintln!("⚠️  Metric '{}' has profile configs but not for '{}' (normalized: '{}'). Available profiles: {:?}", 
                    metric_def.field_name, profile_name, normalized_name, metric_def.profile_configurations.keys().collect::<Vec<_>>());
            }
        }
        
        // Use metric-specific defaults or generic defaults
        match metric_def.field_name {
            "word_count" => ThresholdSet {
                excellent: 1000.0,
                good: 500.0,
                fair: 200.0,
                poor: 50.0,
            },
            "readability_fk" => ThresholdSet {
                excellent: 8.0,
                good: 12.0,
                fair: 16.0,
                poor: 20.0,
            },
            _ => ThresholdSet::default(),
        }
    }

    /// Extract and normalize category weights
    fn extract_category_weights(
        &self,
        weights: &HashMap<String, f32>,
    ) -> Result<HashMap<MetricCategory, f32>, CompilationError> {
        let mut category_weights = HashMap::new();

        for (category_name, weight) in weights {
            let category = self.parse_category_name(category_name)?;
            category_weights.insert(category, *weight);
        }

        // Ensure all categories have weights (default to 0.0 for missing)
        for category in MetricCategory::all() {
            category_weights.entry(category).or_insert(0.0);
        }

        Ok(category_weights)
    }

    /// Parse category name string to MetricCategory enum
    fn parse_category_name(&self, category_name: &str) -> Result<MetricCategory, CompilationError> {
        match category_name.to_lowercase().as_str() {
            "content" => Ok(MetricCategory::Content),
            "structure" => Ok(MetricCategory::Structure),
            "media" => Ok(MetricCategory::Media),
            "seo" => Ok(MetricCategory::Seo),
            "links" => Ok(MetricCategory::Links),
            "technical" => Ok(MetricCategory::Technical),
            "accessibility" => Ok(MetricCategory::Accessibility),
            "mobile" => Ok(MetricCategory::Mobile),
            "authority" => Ok(MetricCategory::Authority),
            "language" => Ok(MetricCategory::Language),
            "forms" => Ok(MetricCategory::Forms),
            "structureddata" => Ok(MetricCategory::StructuredData),
            "branding" => Ok(MetricCategory::Branding),
            "userexperience" => Ok(MetricCategory::UserExperience),
            "business" => Ok(MetricCategory::Business),
            "internationalization" => Ok(MetricCategory::Internationalization),
            "performance" => Ok(MetricCategory::Performance),
            "security" => Ok(MetricCategory::Security),
            "analytics" => Ok(MetricCategory::Analytics),
            "errorhandling" => Ok(MetricCategory::ErrorHandling),
            _ => Err(CompilationError::CategoryWeights(format!(
                "Unknown category: {}",
                category_name
            ))),
        }
    }

    /// Compile penalty configurations
    fn compile_penalties(
        &self,
        penalties: &PenaltyConfig,
    ) -> Result<Vec<CompiledPenalty>, CompilationError> {
        let mut compiled_penalties = Vec::new();

        // Compile severe penalties
        for (penalty_id, penalty) in &penalties.severe_penalties {
            let compiled = self.compile_single_penalty(penalty_id, penalty)?;
            compiled_penalties.push(compiled);
        }

        // Compile moderate penalties
        for (penalty_id, penalty) in &penalties.moderate_penalties {
            let compiled = self.compile_single_penalty(penalty_id, penalty)?;
            compiled_penalties.push(compiled);
        }

        // Compile light penalties
        for (penalty_id, penalty) in &penalties.light_penalties {
            let compiled = self.compile_single_penalty(penalty_id, penalty)?;
            compiled_penalties.push(compiled);
        }

        Ok(compiled_penalties)
    }

    /// Compile a single penalty configuration
    fn compile_single_penalty(
        &self,
        penalty_id: &str,
        penalty: &GlobalPenalty,
    ) -> Result<CompiledPenalty, CompilationError> {
        // Convert from enhanced_models::PenaltyTrigger to profile_compiler::PenaltyTrigger
        let trigger_condition = self.convert_penalty_trigger(&penalty.trigger_condition)?;
        // Convert from enhanced_models::PenaltyType to profile_compiler::PenaltyType
        let penalty_type = self.convert_penalty_type(&penalty.penalty_type)?;

        Ok(CompiledPenalty {
            penalty_id: penalty_id.to_string(),
            trigger_condition,
            penalty_type,
            description: penalty.description.clone(),
            enabled: penalty.enabled,
        })
    }

    /// Convert penalty trigger from enhanced_models type to compiler type
    fn convert_penalty_trigger(
        &self,
        trigger: &crate::config::enhanced_models::PenaltyTrigger,
    ) -> Result<PenaltyTrigger, CompilationError> {
        use crate::config::enhanced_models::PenaltyTrigger as EMPenalty;
        match trigger {
            EMPenalty::MetricBelow { metric, threshold } => Ok(PenaltyTrigger::MetricBelow {
                metric: metric.clone(),
                threshold: *threshold,
            }),
            EMPenalty::MetricAbove { metric, threshold } => Ok(PenaltyTrigger::MetricAbove {
                metric: metric.clone(),
                threshold: *threshold,
            }),
            EMPenalty::MetricEquals { metric, value } => Ok(PenaltyTrigger::MetricEquals {
                metric: metric.clone(),
                value: *value,
            }),
            EMPenalty::MetricMissing { metric } => Ok(PenaltyTrigger::MetricMissing {
                metric: metric.clone(),
            }),
            EMPenalty::ContentDeficiency {
                deficiency_type,
                severity,
            } => Ok(PenaltyTrigger::ContentDeficiency {
                deficiency_type: deficiency_type.clone(),
                severity: *severity,
            }),
            EMPenalty::MultipleConditions {
                conditions,
                operator,
            } => {
                let converted_conditions: Result<Vec<_>, _> = conditions
                    .iter()
                    .map(|c| self.convert_penalty_trigger(c))
                    .collect();
                Ok(PenaltyTrigger::MultipleConditions {
                    conditions: converted_conditions?,
                    operator: match operator {
                        crate::config::enhanced_models::LogicalOperator::And => {
                            LogicalOperator::And
                        }
                        crate::config::enhanced_models::LogicalOperator::Or => LogicalOperator::Or,
                    },
                })
            }
            EMPenalty::MetricThreshold {
                metric_name,
                operator,
                threshold,
            } => {
                // Map to appropriate simple variant based on operator
                match operator {
                    crate::config::enhanced_models::ComparisonOperator::LessThan => {
                        Ok(PenaltyTrigger::MetricBelow {
                            metric: metric_name.clone(),
                            threshold: *threshold,
                        })
                    }
                    crate::config::enhanced_models::ComparisonOperator::GreaterThan => {
                        Ok(PenaltyTrigger::MetricAbove {
                            metric: metric_name.clone(),
                            threshold: *threshold,
                        })
                    }
                    crate::config::enhanced_models::ComparisonOperator::Equals => {
                        Ok(PenaltyTrigger::MetricEquals {
                            metric: metric_name.clone(),
                            value: *threshold,
                        })
                    }
                    _ => {
                        // For other operators, default to MetricBelow
                        Ok(PenaltyTrigger::MetricBelow {
                            metric: metric_name.clone(),
                            threshold: *threshold,
                        })
                    }
                }
            }
        }
    }

    /// Convert penalty type from enhanced_models type to compiler type
    fn convert_penalty_type(
        &self,
        penalty_type: &crate::config::enhanced_models::PenaltyType,
    ) -> Result<PenaltyType, CompilationError> {
        use crate::config::enhanced_models::PenaltyType as EMPenaltyType;
        match penalty_type {
            EMPenaltyType::FixedPoints { points } => {
                Ok(PenaltyType::FixedPoints { points: *points })
            }
            EMPenaltyType::Multiplier { factor } => Ok(PenaltyType::Multiplier { factor: *factor }),
            EMPenaltyType::CategoryPenalty {
                category: _,
                multiplier,
            } => {
                // Convert to Percentage for now
                Ok(PenaltyType::Percentage {
                    percent: *multiplier * 100.0,
                })
            }
        }
    }

    /// Compile bonus configurations
    fn compile_bonuses(
        &self,
        bonuses: &BonusConfig,
    ) -> Result<Vec<CompiledBonus>, CompilationError> {
        let mut compiled_bonuses = Vec::new();

        // Compile excellence bonuses
        for (bonus_id, bonus) in &bonuses.excellence_bonuses {
            let compiled = self.compile_single_bonus(bonus_id, bonus)?;
            compiled_bonuses.push(compiled);
        }

        // Compile achievement bonuses
        for (bonus_id, bonus) in &bonuses.achievement_bonuses {
            let compiled = self.compile_single_bonus(bonus_id, bonus)?;
            compiled_bonuses.push(compiled);
        }

        // Compile synergy bonuses
        for (bonus_id, bonus) in &bonuses.synergy_bonuses {
            let compiled = self.compile_single_bonus(bonus_id, bonus)?;
            compiled_bonuses.push(compiled);
        }

        Ok(compiled_bonuses)
    }

    /// Compile a single bonus configuration
    fn compile_single_bonus(
        &self,
        bonus_id: &str,
        bonus: &GlobalBonus,
    ) -> Result<CompiledBonus, CompilationError> {
        let trigger_condition = self.convert_bonus_trigger(&bonus.trigger_condition)?;

        Ok(CompiledBonus {
            bonus_id: bonus_id.to_string(),
            trigger_condition,
            bonus_points: bonus.bonus_points,
            description: bonus.description.clone(),
            enabled: bonus.enabled,
        })
    }

    /// Convert bonus trigger from enhanced_models type to compiler type
    fn convert_bonus_trigger(
        &self,
        trigger: &crate::config::enhanced_models::BonusTrigger,
    ) -> Result<BonusTrigger, CompilationError> {
        use crate::config::enhanced_models::BonusTrigger as EMBonus;
        match trigger {
            EMBonus::MetricExcellence { metric, threshold } => Ok(BonusTrigger::MetricExcellence {
                metric: metric.clone(),
                threshold: *threshold,
            }),
            EMBonus::ContentQuality {
                quality_type,
                requirement,
            } => Ok(BonusTrigger::ContentQuality {
                quality_type: quality_type.clone(),
                requirement: requirement.clone(),
            }),
            EMBonus::SynergyBonus {
                metrics,
                combined_threshold,
            } => Ok(BonusTrigger::SynergyBonus {
                metrics: metrics.clone(),
                combined_threshold: *combined_threshold,
            }),
            EMBonus::MetricThreshold {
                metric_name,
                operator: _,
                threshold,
            } => {
                // Map to MetricExcellence for simplicity
                Ok(BonusTrigger::MetricExcellence {
                    metric: metric_name.clone(),
                    threshold: *threshold,
                })
            }
            EMBonus::MultipleMetricsGood { metrics, threshold } => {
                // Map to SynergyBonus
                Ok(BonusTrigger::SynergyBonus {
                    metrics: metrics.clone(),
                    combined_threshold: *threshold,
                })
            }
            EMBonus::CategoryExcellence {
                category,
                threshold,
            } => {
                // Map to ContentQuality with category information
                Ok(BonusTrigger::ContentQuality {
                    quality_type: QUALITY_CATEGORY_EXCELLENCE.to_string(),
                    requirement: format!("{} >= {}", category, threshold),
                })
            }
        }
    }
}

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