widget_intelligence 2.0.0

A Rust library for intelligent Kyma widget suggestion and learning
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
use bincode::{Decode, Encode};
use serde::{Deserialize, Serialize};
use std::collections::hash_map::DefaultHasher;
use std::collections::HashMap;
use std::hash::{Hash, Hasher};
use std::time::{SystemTime, UNIX_EPOCH};
use strsim::jaro_winkler;

/// Type alias for filtered widget description from JSON
pub type FilteredWidgetDescription = HashMap<String, serde_json::Value>;

/// Represents a widget with its properties and normalized current value (0.0-1.0 or -1.0-1.0)
#[derive(Debug, Clone, Encode, Decode, Serialize, Deserialize)]
pub struct Widget {
    pub label: Option<String>,
    pub minimum: Option<f64>,
    pub maximum: Option<f64>,
    pub is_generated: Option<bool>,
    pub display_type: Option<String>,
    pub current_value: Option<f64>,
    pub event_id: Option<u64>,
    pub values: Vec<f64>,
}

impl Widget {
    /// Creates a simplified widget with only label, event_id, and values
    pub fn simplified(label: Option<String>, event_id: Option<u64>, values: Vec<f64>) -> Self {
        let current_value = if !values.is_empty() {
            Some(values[0])
        } else {
            None
        };

        Self {
            label,
            event_id,
            values: values.clone(),
            minimum: None,
            maximum: None,
            is_generated: None,
            display_type: None,
            current_value,
        }
    }

    /// Gets the values vector, including the current_value if available
    pub fn get_values(&self) -> Vec<f64> {
        let mut result = self.values.clone();
        if let Some(current) = self.current_value {
            if !result.contains(&current) {
                result.push(current);
            }
        }
        result
    }
}

/// Represents a widget value with metadata
#[derive(Debug, Clone, Encode, Decode, Serialize, Deserialize)]
pub struct WidgetValue {
    pub widget_id: String,
    pub label: Option<String>,
    pub value: f64,
    pub confidence: f64,
}

/// Represents a preset collection of widget values
#[derive(Debug, Clone, Encode, Decode, Serialize, Deserialize)]
pub struct Preset {
    pub name: String,
    pub description: Option<String>,
    pub widget_values: Vec<WidgetValue>,
    pub created_by: Option<String>,
    pub usage_count: u32,
    pub last_used: u64,
}

/// Features extracted from a widget for similarity calculation
/// value_patterns stores normalized values (0.0-1.0 or -1.0-1.0) from observed widgets
#[derive(Debug, Clone, Encode, Decode, Serialize, Deserialize)]
pub struct WidgetFeatures {
    pub label_tokens: Vec<String>,
    pub min_value: f64,
    pub max_value: f64,
    pub range: f64,
    pub is_generated: f64,
    pub display_type_hash: u64,
    pub value_patterns: Vec<f64>,
    pub normalized_position: f64,
}

impl Default for WidgetFeatures {
    fn default() -> Self {
        Self {
            label_tokens: Vec::new(),
            min_value: 0.0,
            max_value: 100.0,
            range: 100.0,
            is_generated: 0.0,
            display_type_hash: 0,
            value_patterns: Vec::new(),
            normalized_position: 0.0,
        }
    }
}

/// Statistical information about widget values
#[derive(Debug, Clone, Encode, Decode, Serialize, Deserialize)]
pub struct ValueStats {
    pub common_values: Vec<f64>,
    pub frequency_map: HashMap<String, u32>,
    pub mean: f64,
    pub std_dev: f64,
    pub percentiles: Vec<f64>,
}

/// A stored widget record with features and usage statistics
#[derive(Debug, Clone, Encode, Decode, Serialize, Deserialize)]
pub struct WidgetRecord {
    pub id: u64,
    pub widget: Widget,
    pub features: WidgetFeatures,
    pub frequency: u32,
    pub last_seen: u64,
    pub value_stats: Option<ValueStats>,
}

impl From<FilteredWidgetDescription> for WidgetRecord {
    fn from(filtered: FilteredWidgetDescription) -> Self {
        // Helper function to extract string values from JSON
        fn extract_string(map: &HashMap<String, serde_json::Value>, key: &str) -> Option<String> {
            map.get(key).and_then(|v| v.as_str().map(|s| s.to_string()))
        }

        // Helper function to extract f64 values from JSON
        fn extract_f64(map: &HashMap<String, serde_json::Value>, key: &str) -> Option<f64> {
            map.get(key).and_then(|v| v.as_f64())
        }

        // Helper function to extract bool values from JSON
        fn extract_bool(map: &HashMap<String, serde_json::Value>, key: &str) -> Option<bool> {
            map.get(key).and_then(|v| v.as_bool())
        }

        // Helper function to extract u64 values from JSON
        fn extract_u64(map: &HashMap<String, serde_json::Value>, key: &str) -> Option<u64> {
            map.get(key).and_then(|v| v.as_u64())
        }

        // Extract widget data from the filtered description
        let current_value = extract_f64(&filtered, "current_value");
        let event_id = extract_u64(&filtered, "concreteEventID");

        let widget = Widget {
            label: extract_string(&filtered, "label"),
            minimum: extract_f64(&filtered, "minimum"),
            maximum: extract_f64(&filtered, "maximum"),
            current_value,
            is_generated: extract_bool(&filtered, "isGenerated"),
            display_type: extract_string(&filtered, "displayType"),
            event_id,
            values: if let Some(val) = current_value { vec![val] } else { Vec::new() },
        };

        // Create basic features from the widget data
        let label_tokens = if let Some(ref label) = widget.label {
            label
                .to_lowercase()
                .split_whitespace()
                .map(|s| s.chars().filter(|c| c.is_alphanumeric()).collect())
                .filter(|s: &String| !s.is_empty())
                .collect()
        } else {
            Vec::new()
        };

        let min_value = widget.minimum.unwrap_or(0.0);
        let max_value = widget.maximum.unwrap_or(1.0);
        let range = max_value - min_value;

        // Calculate display type hash
        let display_type_hash = if let Some(ref display_type) = widget.display_type {
            let mut hasher = std::collections::hash_map::DefaultHasher::new();
            std::hash::Hash::hash(display_type, &mut hasher);
            std::hash::Hasher::finish(&hasher)
        } else {
            0
        };

        let features = WidgetFeatures {
            label_tokens,
            min_value,
            max_value,
            range,
            is_generated: if widget.is_generated.unwrap_or(false) {
                1.0
            } else {
                0.0
            },
            display_type_hash,
            value_patterns: if let Some(current) = widget.current_value {
                vec![current]
            } else {
                Vec::new()
            },
            normalized_position: widget.current_value.unwrap_or(0.5)
        };

        // Get current timestamp
        let current_time = SystemTime::now()
            .duration_since(UNIX_EPOCH)
            .unwrap_or_else(|_| std::time::Duration::from_secs(0))
            .as_secs();

        // Extract ID from concreteEventID if available, otherwise use 0
        let id = extract_u64(&filtered, "concreteEventID").unwrap_or(0);

        WidgetRecord {
            id,
            widget,
            features,
            frequency: 1,
            last_seen: current_time,
            value_stats: None,
        }
    }
}

/// A suggestion for a widget value with confidence and reasoning
/// All suggested values are normalized (0.0-1.0 or -1.0-1.0)
#[derive(Debug, Clone, Encode, Decode, Serialize, Deserialize)]
pub struct Suggestion {
    pub widget: Widget,
    pub confidence: f64,
    pub reason: String,
    pub suggested_value: Option<f64>,
    pub value_confidence: f64,
    pub alternative_values: Vec<f64>,
}

/// The main engine for widget suggestions and learning
pub struct WidgetSuggestionEngine {
    pub records: Vec<WidgetRecord>,
    pub presets: Vec<Preset>,
    pub display_types: HashMap<String, u64>,
    pub next_id: u64,
}

impl WidgetSuggestionEngine {
    pub fn new() -> Self {
        Self {
            records: Vec::new(),
            presets: Vec::new(),
            display_types: HashMap::new(),
            next_id: 1,
        }
    }

    pub fn store_widget(&mut self, widget: Widget) {
        let current_time = SystemTime::now()
            .duration_since(UNIX_EPOCH)
            .unwrap()
            .as_secs();

        // Extract features
        let features = self.extract_features(&widget);

        // First, check if we have an exact match by event_id
        if let Some(event_id) = widget.event_id {
            for i in 0..self.records.len() {
                if self.records[i].widget.event_id == Some(event_id) {
                    // Update existing record with the same event_id
                    self.records[i].frequency += 1;
                    self.records[i].last_seen = current_time;

                    // Update label if new one is provided
                    if widget.label.is_some() && self.records[i].widget.label.is_none() {
                        self.records[i].widget.label = widget.label.clone();
                    }

                    // Add new values to the existing values vector
                    for &value in &widget.values {
                        if !self.records[i].widget.values.contains(&value) {
                            self.records[i].widget.values.push(value);
                            // Also add to feature's value_patterns for backward compatibility
                            self.records[i].features.value_patterns.push(value);
                        }
                    }

                    return;
                }
            }
        }

        // Next, check if we have an exact match by label
        if let Some(label) = &widget.label {
            for i in 0..self.records.len() {
                if let Some(record_label) = &self.records[i].widget.label {
                    if record_label == label {
                        // Update existing record with the same label
                        self.records[i].frequency += 1;
                        self.records[i].last_seen = current_time;

                        // Update event_id if new one is provided
                        if widget.event_id.is_some() && self.records[i].widget.event_id.is_none() {
                            self.records[i].widget.event_id = widget.event_id;
                        }

                        // Add new values to the existing values vector
                        for &value in &widget.values {
                            if !self.records[i].widget.values.contains(&value) {
                                self.records[i].widget.values.push(value);
                                // Also add to feature's value_patterns for backward compatibility
                                self.records[i].features.value_patterns.push(value);
                            }
                        }

                        return;
                    }
                }
            }
        }

        // Finally, check for similar widgets
        let mut found_similar = false;

        for i in 0..self.records.len() {
            let similarity = self.calculate_similarity(&features, &self.records[i].features);

            if similarity > 0.85 {
                self.records[i].frequency += 1;
                self.records[i].last_seen = current_time;

                // Update widget if new one has more complete information
                if widget.label.is_some() && self.records[i].widget.label.is_none() {
                    self.records[i].widget.label = widget.label.clone();
                }

                if widget.event_id.is_some() && self.records[i].widget.event_id.is_none() {
                    self.records[i].widget.event_id = widget.event_id;
                }

                // Add new values to the existing values vector
                for &value in &widget.values {
                    if !self.records[i].widget.values.contains(&value) {
                        self.records[i].widget.values.push(value);
                        // Also add to feature's value_patterns for backward compatibility
                        self.records[i].features.value_patterns.push(value);
                    }
                }

                found_similar = true;
                break;
            }
        }

        if !found_similar {
            let record = WidgetRecord {
                id: self.next_id,
                widget,
                features,
                frequency: 1,
                last_seen: current_time,
                value_stats: None,
            };
            self.records.push(record);
            self.next_id += 1;
        }
    }

    pub fn store_preset(&mut self, preset: Preset) {
        // Store or update preset
        if let Some(existing) = self.presets.iter_mut().find(|p| p.name == preset.name) {
            existing.usage_count += 1;
            existing.last_used = preset.last_used;
            existing.widget_values = preset.widget_values;
            existing.description = preset.description;
        } else {
            self.presets.push(preset);
        }
    }

    pub fn get_suggestions(
        &self,
        partial_widget: &Widget,
        max_suggestions: usize,
    ) -> Vec<Suggestion> {
        // If the partial widget has an event_id, use that for suggestions
        if let Some(event_id) = partial_widget.event_id {
            return self.get_suggestions_by_event_id(event_id, max_suggestions);
        }

        let features = self.extract_features_partial(partial_widget);
        let mut suggestions = Vec::new();

        // First, try to find widgets with matching label
        if let Some(label) = &partial_widget.label {
            for record in &self.records {
                if let Some(record_label) = &record.widget.label {
                    if record_label == label {
                        let (suggested_value, value_confidence, alternative_values) =
                            self.suggest_values_from_vector(&record.widget);

                        let reason = format!(
                            "Exact label match for '{}' (frequency: {})",
                            label,
                            record.frequency
                        );

                        suggestions.push(Suggestion {
                            widget: record.widget.clone(),
                            confidence: 1.0,  // Highest confidence for exact matches
                            reason,
                            suggested_value,
                            value_confidence,
                            alternative_values,
                        });
                    }
                }
            }
        }

        // If we don't have enough suggestions from exact matches, add similar widgets
        if suggestions.len() < max_suggestions {
            for record in &self.records {
                // Skip records we've already included
                if suggestions.iter().any(|s| s.widget.label == record.widget.label) {
                    continue;
                }

                let similarity = self.calculate_similarity(&features, &record.features);

                if similarity > 0.3 {
                    let (suggested_value, value_confidence, alternative_values) =
                        self.suggest_values_from_vector(&record.widget);

                    let reason = format!(
                        "Similar to {} (similarity: {:.2}, frequency: {})",
                        record.widget.label.as_deref().unwrap_or("unnamed widget"),
                        similarity,
                        record.frequency
                    );

                    suggestions.push(Suggestion {
                        widget: record.widget.clone(),
                        confidence: similarity,
                        reason,
                        suggested_value,
                        value_confidence,
                        alternative_values,
                    });
                }
            }
        }

        suggestions.sort_by(|a, b| b.confidence.partial_cmp(&a.confidence).unwrap());
        suggestions.truncate(max_suggestions);
        suggestions
    }

    pub fn get_suggestions_by_event_id(
        &self,
        event_id: u64,
        max_suggestions: usize,
    ) -> Vec<Suggestion> {
        // Find records with matching event ID
        let matching_records: Vec<&WidgetRecord> = self.records.iter()
            .filter(|r| r.widget.event_id == Some(event_id) || r.id == event_id)
            .collect();

        if matching_records.is_empty() {
            // If no exact match, fall back to regular suggestions
            if let Some(first_record) = self.records.first() {
                return self.get_suggestions(&first_record.widget, max_suggestions);
            } else {
                return Vec::new();
            }
        }

        let mut suggestions = Vec::new();

        // First, process exact matches
        for &record in &matching_records {
            // For exact event ID matches, use the observed values directly
            let (suggested_value, value_confidence, alternative_values) =
                self.suggest_values_from_vector(&record.widget);

            let reason = format!(
                "Exact match for event ID {} ({})",
                event_id,
                record.widget.label.as_deref().unwrap_or("unnamed widget")
            );

            suggestions.push(Suggestion {
                widget: record.widget.clone(),
                confidence: 1.0,  // Highest confidence for exact matches
                reason,
                suggested_value,
                value_confidence,
                alternative_values,
            });
        }

        // If we don't have enough suggestions from exact matches, add similar widgets
        if suggestions.len() < max_suggestions {
            // Use the first matching record as a template for finding similar widgets
            if let Some(&template) = matching_records.first() {
                let features = &template.features;

                for record in &self.records {
                    // Skip records we've already included
                    if record.widget.event_id == Some(event_id) || record.id == event_id {
                        continue;
                    }

                    let similarity = self.calculate_similarity(features, &record.features);

                    if similarity > 0.5 {  // Higher threshold for event ID-based suggestions
                        let (suggested_value, value_confidence, alternative_values) =
                            self.suggest_values_from_vector(&record.widget);

                        let reason = format!(
                            "Similar to event ID {} ({}) with similarity {:.2}",
                            event_id,
                            template.widget.label.as_deref().unwrap_or("unnamed widget"),
                            similarity
                        );

                        suggestions.push(Suggestion {
                            widget: record.widget.clone(),
                            confidence: similarity,
                            reason,
                            suggested_value,
                            value_confidence,
                            alternative_values,
                        });
                    }
                }
            }
        }

        suggestions.sort_by(|a, b| b.confidence.partial_cmp(&a.confidence).unwrap());
        suggestions.truncate(max_suggestions);
        suggestions
    }

    /// Suggest values based on the widget's values vector
    fn suggest_values_from_vector(&self, widget: &Widget) -> (Option<f64>, f64, Vec<f64>) {
        let values = widget.get_values();

        if values.is_empty() {
            return (None, 0.3, vec![0.5, 0.3, 0.7]);  // Default fallback
        }

        // Calculate confidence based on number of observed values
        let confidence = match values.len() {
            0 => 0.3,
            1..=2 => 0.5,
            3..=5 => 0.7,
            _ => 0.9,
        };

        // Find the most common value
        let mut value_counts: HashMap<String, u32> = HashMap::new();
        for &val in &values {
            let key = format!("{:.4}", val);
            *value_counts.entry(key).or_insert(0) += 1;
        }

        let mut most_common_value = values[0];
        let mut max_count = 1;

        for (val_str, count) in value_counts.iter() {
            if *count > max_count {
                if let Ok(val) = val_str.parse::<f64>() {
                    most_common_value = val;
                    max_count = *count;
                }
            }
        }

        // Return the most common value and all unique values
        let mut unique_values = values.clone();
        unique_values.sort_by(|a, b| a.partial_cmp(b).unwrap());
        unique_values.dedup();

        (Some(most_common_value), confidence, unique_values)
    }

    pub fn get_preset_insights(&self, widget: &Widget) -> Option<String> {
        for preset in &self.presets {
            for widget_value in &preset.widget_values {
                if let Some(label) = &widget.label {
                    if let Some(preset_label) = &widget_value.label {
                        if jaro_winkler(label, preset_label) > 0.8 {
                            return Some(format!(
                                "This widget is often set to {} in the '{}' preset",
                                widget_value.value, preset.name
                            ));
                        }
                    }
                }
            }
        }
        None
    }

    pub fn get_stats(&self) -> HashMap<String, usize> {
        let mut stats = HashMap::new();
        stats.insert("total_widgets".to_string(), self.records.len());
        stats.insert("total_presets".to_string(), self.presets.len());
        stats.insert("display_types".to_string(), self.display_types.len());
        stats
    }

    fn extract_features(&mut self, widget: &Widget) -> WidgetFeatures {
        let label_tokens = if let Some(label) = &widget.label {
            self.tokenize_label(label)
        } else {
            Vec::new()
        };

        let min_value = widget.minimum.unwrap_or(0.0);
        let max_value = widget.maximum.unwrap_or(100.0);
        let range = max_value - min_value;

        let display_type_hash = if let Some(display_type) = &widget.display_type {
            let mut hasher = DefaultHasher::new();
            display_type.hash(&mut hasher);
            let hash = hasher.finish();

            // Store display type for future reference
            self.display_types.insert(display_type.clone(), hash);
            hash
        } else {
            0
        };

        let is_generated = if widget.is_generated.unwrap_or(false) {
            1.0
        } else {
            0.0
        };

        let mut value_patterns = self.extract_value_patterns(&label_tokens, &widget.display_type);

        // Add the normalized current_value to value_patterns if available
        if let Some(current) = widget.current_value {
            value_patterns.push(current);
        }

        // current_value is already normalized, use it directly
        let normalized_position = widget.current_value.unwrap_or(0.5);

        WidgetFeatures {
            label_tokens,
            min_value,
            max_value,
            range,
            is_generated,
            display_type_hash,
            value_patterns,
            normalized_position,
        }
    }

    fn extract_features_partial(&self, widget: &Widget) -> WidgetFeatures {
        let label_tokens = if let Some(label) = &widget.label {
            self.tokenize_label(label)
        } else {
            Vec::new()
        };

        let min_value = widget.minimum.unwrap_or(0.0);
        let max_value = widget.maximum.unwrap_or(100.0);
        let range = max_value - min_value;

        let display_type_hash = if let Some(display_type) = &widget.display_type {
            let mut hasher = DefaultHasher::new();
            display_type.hash(&mut hasher);
            hasher.finish()
        } else {
            0
        };

        let is_generated = if widget.is_generated.unwrap_or(false) {
            1.0
        } else {
            0.0
        };

        let mut value_patterns = self.extract_value_patterns(&label_tokens, &widget.display_type);

        // Add the normalized current_value to value_patterns if available
        if let Some(current) = widget.current_value {
            value_patterns.push(current);
        }

        // current_value is already normalized, use it directly
        let normalized_position = widget.current_value.unwrap_or(0.5);

        WidgetFeatures {
            label_tokens,
            min_value,
            max_value,
            range,
            is_generated,
            display_type_hash,
            value_patterns,
            normalized_position,
        }
    }

    fn tokenize_label(&self, label: &str) -> Vec<String> {
        label
            .to_lowercase()
            .split_whitespace()
            .filter(|word| !word.is_empty())
            .map(|word| word.to_string())
            .collect()
    }

    fn calculate_similarity(&self, features1: &WidgetFeatures, features2: &WidgetFeatures) -> f64 {
        let label_similarity =
            self.calculate_label_similarity(&features1.label_tokens, &features2.label_tokens);
        let range_similarity = self.calculate_range_similarity(features1, features2);
        let display_type_similarity = if features1.display_type_hash == features2.display_type_hash
            && features1.display_type_hash != 0
        {
            1.0
        } else {
            0.0
        };
        let generated_similarity = 1.0 - (features1.is_generated - features2.is_generated).abs();

        // Weighted combination
        let similarity = (label_similarity * 0.4)
            + (range_similarity * 0.3)
            + (display_type_similarity * 0.2)
            + (generated_similarity * 0.1);

        similarity.clamp(0.0, 1.0)
    }

    fn calculate_label_similarity(&self, tokens1: &[String], tokens2: &[String]) -> f64 {
        if tokens1.is_empty() || tokens2.is_empty() {
            return if tokens1.is_empty() && tokens2.is_empty() {
                1.0
            } else {
                0.0
            };
        }

        let mut total_similarity = 0.0;
        let mut matches = 0;

        for token1 in tokens1 {
            let mut best_match = 0.0;
            for token2 in tokens2 {
                let similarity = jaro_winkler(token1, token2);
                if similarity > best_match {
                    best_match = similarity;
                }
            }
            if best_match > 0.7 {
                total_similarity += best_match;
                matches += 1;
            }
        }

        if matches > 0 {
            total_similarity / matches as f64
        } else {
            0.0
        }
    }

    fn calculate_range_similarity(
        &self,
        features1: &WidgetFeatures,
        features2: &WidgetFeatures,
    ) -> f64 {
        let min_diff = (features1.min_value - features2.min_value).abs();
        let max_diff = (features1.max_value - features2.max_value).abs();
        let range_diff = (features1.range - features2.range).abs();

        let max_range = features1.range.max(features2.range);
        if max_range == 0.0 {
            return 1.0;
        }

        let normalized_diff = (min_diff + max_diff + range_diff) / (3.0 * max_range);
        1.0 - normalized_diff.min(1.0)
    }

    fn extract_value_patterns(
        &self,
        label_tokens: &[String],
        _display_type: &Option<String>,
    ) -> Vec<f64> {
        let mut patterns = Vec::new();

        // Common value patterns based on label tokens
        for token in label_tokens {
            match token.as_str() {
                "volume" | "level" | "gain" => patterns.push(0.75),
                "bass" | "low" => patterns.push(0.6),
                "treble" | "high" => patterns.push(0.7),
                "mid" | "middle" => patterns.push(0.5),
                "pan" => patterns.push(0.5),
                "reverb" | "delay" => patterns.push(0.3),
                _ => {}
            }
        }

        if patterns.is_empty() {
            patterns.push(0.5); // Default middle value
        }

        patterns
    }

}

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

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

    #[test]
    fn test_filtered_widget_description_conversion() {
        let mut filtered = FilteredWidgetDescription::new();
        filtered.insert(
            "concreteEventID".to_string(),
            serde_json::Value::Number(serde_json::Number::from(42)),
        );
        filtered.insert(
            "label".to_string(),
            serde_json::Value::String("Master Volume".to_string()),
        );
        filtered.insert(
            "minimum".to_string(),
            serde_json::Value::Number(serde_json::Number::from_f64(0.0).unwrap()),
        );
        filtered.insert(
            "maximum".to_string(),
            serde_json::Value::Number(serde_json::Number::from_f64(127.0).unwrap()),
        );
        filtered.insert(
            "current_value".to_string(),
            serde_json::Value::Number(serde_json::Number::from_f64(0.75).unwrap()),
        );
        filtered.insert(
            "displayType".to_string(),
            serde_json::Value::String("slider".to_string()),
        );
        filtered.insert("isGenerated".to_string(), serde_json::Value::Bool(false));

        // Test the conversion using the From trait - this is the idiomatic way
        let widget_record: WidgetRecord = filtered.into();

        assert_eq!(widget_record.id, 42);
        assert_eq!(
            widget_record.widget.label,
            Some("Master Volume".to_string())
        );
        assert_eq!(widget_record.widget.minimum, Some(0.0));
        assert_eq!(widget_record.widget.maximum, Some(127.0));
        assert_eq!(
            widget_record.widget.display_type,
            Some("slider".to_string())
        );
        assert_eq!(widget_record.widget.is_generated, Some(false));
        assert_eq!(widget_record.frequency, 1);
        assert_eq!(
            widget_record.features.label_tokens,
            vec!["master", "volume"]
        );
        assert_eq!(widget_record.features.min_value, 0.0);
        assert_eq!(widget_record.features.max_value, 127.0);
        assert_eq!(widget_record.features.range, 127.0);
        assert_eq!(widget_record.features.is_generated, 0.0);
        assert!(widget_record.features.display_type_hash != 0);
        assert_eq!(widget_record.widget.current_value, Some(0.75));
        // Check that current_value is added to value_patterns
        assert!(widget_record.features.value_patterns.contains(&0.75));
    }

    #[test]
    fn test_conversion_with_missing_fields() {
        // Test conversion when some fields are missing
        let mut filtered = FilteredWidgetDescription::new();
        filtered.insert(
            "concreteEventID".to_string(),
            serde_json::Value::Number(serde_json::Number::from(100)),
        );
        filtered.insert(
            "label".to_string(),
            serde_json::Value::String("Test Control".to_string()),
        );
        // Note: missing minimum, maximum, displayType, isGenerated

        let widget_record: WidgetRecord = filtered.into();

        assert_eq!(widget_record.id, 100);
        assert_eq!(widget_record.widget.label, Some("Test Control".to_string()));
        assert_eq!(widget_record.widget.minimum, None);
        assert_eq!(widget_record.widget.maximum, None);
        assert_eq!(widget_record.widget.display_type, None);
        assert_eq!(widget_record.widget.is_generated, None);
        assert_eq!(widget_record.features.min_value, 0.0); // Default value
        assert_eq!(widget_record.features.max_value, 1.0); // Default value
        assert_eq!(widget_record.features.range, 1.0);
        assert_eq!(widget_record.features.display_type_hash, 0);
    }

    #[test]
    fn test_normalized_value_accumulation() {
        let mut engine = WidgetSuggestionEngine::new();

        // Create a widget with normalized value
        let widget1 = Widget {
            label: Some("Volume".to_string()),
            minimum: Some(0.0),
            maximum: Some(100.0),
            current_value: Some(0.7), // Normalized value
            is_generated: Some(false),
            display_type: Some("slider".to_string()),
            event_id: None,
            values: vec![0.7],
        };

        // Store first widget
        engine.store_widget(widget1.clone());

        // Store similar widget with different normalized value
        let mut widget2 = widget1.clone();
        widget2.current_value = Some(0.8);
        engine.store_widget(widget2);

        // Store another similar widget
        let mut widget3 = widget1.clone();
        widget3.current_value = Some(0.75);
        engine.store_widget(widget3);

        // Check that values are accumulated in value_patterns
        assert_eq!(engine.records.len(), 1); // Should be merged as similar
        assert_eq!(engine.records[0].frequency, 3);

        let patterns = &engine.records[0].features.value_patterns;
        assert!(patterns.contains(&0.7));
        assert!(patterns.contains(&0.8));
        assert!(patterns.contains(&0.75));
        // Also contains default pattern from extract_value_patterns
        assert!(patterns.len() >= 3);
    }
}