graphrag-core 0.2.0

Core portable library for GraphRAG - works on native and WASM
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
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
//! Main ROGRAG processor implementation
//!
//! Orchestrates the complete ROGRAG pipeline including query decomposition,
//! dual-level retrieval, and robust response generation.

#[cfg(feature = "rograg")]
use crate::core::KnowledgeGraph;
#[cfg(feature = "rograg")]
use crate::Result;

use crate::rograg::{
    DecompositionResult, FuzzyMatchResult, FuzzyMatcher, HybridQueryDecomposer, IntentClassifier,
    IntentResult, LogicFormResult, LogicFormRetriever, QueryDecomposer, QueryValidator,
    StreamingResponseBuilder,
};

use crate::rograg::quality_metrics::QualityMetrics;
#[cfg(feature = "rograg")]
use serde::{Deserialize, Serialize};
#[cfg(feature = "rograg")]
use std::sync::Arc;
#[cfg(feature = "rograg")]
use std::time::{Duration, Instant};
#[cfg(feature = "rograg")]
use thiserror::Error;

/// Error types for ROGRAG processing.
#[cfg(feature = "rograg")]
#[derive(Error, Debug)]
pub enum ProcessingError {
    /// Query processing exceeded the configured timeout.
    ///
    /// Occurs when query processing takes longer than `max_processing_time`.
    #[error("Query processing timeout after {duration:?}")]
    Timeout {
        /// Duration elapsed before timeout.
        duration: Duration,
    },

    /// All retrieval strategies failed to produce results.
    ///
    /// Occurs when both logic form and fuzzy matching fail.
    #[error("All retrieval strategies failed: {reason}")]
    AllStrategiesFailed {
        /// Reason why strategies failed.
        reason: String,
    },

    /// Query is malformed, empty, or otherwise invalid.
    ///
    /// Occurs during query validation before processing begins.
    #[error("Invalid query: {reason}")]
    InvalidQuery {
        /// Description of what makes the query invalid.
        reason: String,
    },

    /// ROGRAG configuration is invalid or incomplete.
    ///
    /// Occurs when configuration parameters are outside valid ranges.
    #[error("Configuration error: {reason}")]
    ConfigurationError {
        /// Description of the configuration issue.
        reason: String,
    },

    /// Component initialization failed during processor setup.
    ///
    /// Occurs when decomposer, matcher, or classifier fails to initialize.
    #[error("Component initialization failed: {component}: {reason}")]
    InitializationFailed {
        /// Name of the component that failed.
        component: String,
        /// Reason for initialization failure.
        reason: String,
    },
}

/// ROGRAG processor implementation
#[cfg(feature = "rograg")]
pub struct RogragProcessor {
    decomposer: Arc<dyn QueryDecomposer>,
    fuzzy_matcher: Arc<FuzzyMatcher>,
    intent_classifier: Arc<IntentClassifier>,
    logic_form_retriever: Arc<LogicFormRetriever>,
    streaming_builder: Arc<StreamingResponseBuilder>,
    validator: Arc<QueryValidator>,
    quality_metrics: Arc<std::sync::Mutex<QualityMetrics>>,
    config: RogragConfig,
}

/// Builder for RogragProcessor
#[cfg(feature = "rograg")]
pub struct RogragProcessorBuilder {
    decomposer: Option<Arc<dyn QueryDecomposer>>,
    fuzzy_matcher: Option<Arc<FuzzyMatcher>>,
    intent_classifier: Option<Arc<IntentClassifier>>,
    logic_form_retriever: Option<Arc<LogicFormRetriever>>,
    streaming_builder: Option<Arc<StreamingResponseBuilder>>,
    validator: Option<Arc<QueryValidator>>,
    quality_metrics: Option<Arc<std::sync::Mutex<QualityMetrics>>>,
    config: RogragConfig,
}

/// Processing context for a single query
#[cfg(feature = "rograg")]
#[derive(Debug)]
struct ProcessingContext {
    #[allow(dead_code)]
    query: String,
    start_time: Instant,
    decomposition_time: Option<Duration>,
    retrieval_time: Option<Duration>,
    response_time: Option<Duration>,
    fallback_count: usize,
    errors_encountered: Vec<String>,
}

#[cfg(feature = "rograg")]
impl ProcessingContext {
    fn new(query: String) -> Self {
        Self {
            query,
            start_time: Instant::now(),
            decomposition_time: None,
            retrieval_time: None,
            response_time: None,
            fallback_count: 0,
            errors_encountered: Vec::new(),
        }
    }

    fn record_decomposition_time(&mut self, duration: Duration) {
        self.decomposition_time = Some(duration);
    }

    fn record_retrieval_time(&mut self, duration: Duration) {
        self.retrieval_time = Some(duration);
    }

    fn record_response_time(&mut self, duration: Duration) {
        self.response_time = Some(duration);
    }

    fn increment_fallback(&mut self) {
        self.fallback_count += 1;
    }

    fn add_error(&mut self, error: String) {
        self.errors_encountered.push(error);
    }

    fn total_time(&self) -> Duration {
        self.start_time.elapsed()
    }

    fn to_processing_stats(&self) -> ProcessingStats {
        ProcessingStats {
            total_time_ms: self.total_time().as_millis() as u64,
            decomposition_time_ms: self
                .decomposition_time
                .map(|d| d.as_millis() as u64)
                .unwrap_or(0),
            retrieval_time_ms: self
                .retrieval_time
                .map(|d| d.as_millis() as u64)
                .unwrap_or(0),
            synthesis_time_ms: self
                .response_time
                .map(|d| d.as_millis() as u64)
                .unwrap_or(0),
            intent_classification_time_ms: 0,
            subqueries_processed: 0, // Will be set by caller
            fallback_used: self.fallback_count > 0,
            validation_time_ms: 0,
        }
    }
}

#[cfg(feature = "rograg")]
impl Default for RogragProcessorBuilder {
    fn default() -> Self {
        Self::new()
    }
}

impl RogragProcessorBuilder {
    /// Create a new builder
    pub fn new() -> Self {
        Self {
            decomposer: None,
            fuzzy_matcher: None,
            intent_classifier: None,
            logic_form_retriever: None,
            streaming_builder: None,
            validator: None,
            quality_metrics: None,
            config: RogragConfig::default(),
        }
    }

    /// Set custom decomposer
    pub fn with_decomposer(mut self, decomposer: Arc<dyn QueryDecomposer>) -> Self {
        self.decomposer = Some(decomposer);
        self
    }

    /// Set custom fuzzy matcher
    pub fn with_fuzzy_matcher(mut self, fuzzy_matcher: Arc<FuzzyMatcher>) -> Self {
        self.fuzzy_matcher = Some(fuzzy_matcher);
        self
    }

    /// Set custom intent classifier
    pub fn with_intent_classifier(mut self, intent_classifier: Arc<IntentClassifier>) -> Self {
        self.intent_classifier = Some(intent_classifier);
        self
    }

    /// Set custom logic form retriever
    pub fn with_logic_form_retriever(
        mut self,
        logic_form_retriever: Arc<LogicFormRetriever>,
    ) -> Self {
        self.logic_form_retriever = Some(logic_form_retriever);
        self
    }

    /// Set custom streaming builder
    pub fn with_streaming_builder(
        mut self,
        streaming_builder: Arc<StreamingResponseBuilder>,
    ) -> Self {
        self.streaming_builder = Some(streaming_builder);
        self
    }

    /// Set custom validator
    pub fn with_validator(mut self, validator: Arc<QueryValidator>) -> Self {
        self.validator = Some(validator);
        self
    }

    /// Set custom quality metrics
    pub fn with_quality_metrics(
        mut self,
        quality_metrics: Arc<std::sync::Mutex<QualityMetrics>>,
    ) -> Self {
        self.quality_metrics = Some(quality_metrics);
        self
    }

    /// Set configuration
    pub fn with_config(mut self, config: RogragConfig) -> Self {
        self.config = config;
        self
    }

    /// Build the processor
    pub fn build(self) -> Result<RogragProcessor> {
        // Use provided components or create defaults — propagate construction errors.
        let decomposer = match self.decomposer {
            Some(d) => d,
            None => Arc::new(HybridQueryDecomposer::new()?),
        };

        let fuzzy_matcher = self
            .fuzzy_matcher
            .unwrap_or_else(|| Arc::new(FuzzyMatcher::new()));

        let intent_classifier = match self.intent_classifier {
            Some(c) => c,
            None => Arc::new(IntentClassifier::new()?),
        };

        let logic_form_retriever = self
            .logic_form_retriever
            .unwrap_or_else(|| Arc::new(LogicFormRetriever::new()));

        let streaming_builder = self
            .streaming_builder
            .unwrap_or_else(|| Arc::new(StreamingResponseBuilder::new()));

        let validator = self
            .validator
            .unwrap_or_else(|| Arc::new(QueryValidator::new()));

        let quality_metrics = self
            .quality_metrics
            .unwrap_or_else(|| Arc::new(std::sync::Mutex::new(QualityMetrics::new())));

        Ok(RogragProcessor {
            decomposer,
            fuzzy_matcher,
            intent_classifier,
            logic_form_retriever,
            streaming_builder,
            validator,
            quality_metrics,
            config: self.config,
        })
    }
}

#[cfg(feature = "rograg")]
impl RogragProcessor {
    /// Create a new ROGRAG processor with default configuration
    pub fn new() -> Result<Self> {
        RogragProcessorBuilder::new().build()
    }

    /// Create a new ROGRAG processor with custom configuration
    pub fn with_config(config: RogragConfig) -> Result<Self> {
        RogragProcessorBuilder::new().with_config(config).build()
    }

    /// Get a builder for custom configuration
    pub fn builder() -> RogragProcessorBuilder {
        RogragProcessorBuilder::new()
    }

    /// Process a query using the complete ROGRAG pipeline
    pub async fn process_query(
        &self,
        query: &str,
        graph: &KnowledgeGraph,
    ) -> Result<RogragResponse> {
        let mut context = ProcessingContext::new(query.to_string());

        // Process query (timeout handling would require tokio feature)
        let result = self
            .process_query_internal(query, graph, &mut context)
            .await;

        // Record metrics if tracking is enabled
        if self.config.quality_tracking {
            if let Ok(ref response) = result {
                if let Some(decomposition_result) = self.get_decomposition_for_metrics(query).await
                {
                    if let Ok(mut metrics) = self.quality_metrics.lock() {
                        let _ = metrics.record_query(
                            query,
                            &decomposition_result,
                            response,
                            context.total_time(),
                        );
                    }
                }
            }
        }

        result
    }

    /// Internal query processing implementation
    async fn process_query_internal(
        &self,
        query: &str,
        graph: &KnowledgeGraph,
        context: &mut ProcessingContext,
    ) -> Result<RogragResponse> {
        // Step 1: Validate query
        let query_validation = self.validator.validate_query(query)?;
        if !query_validation.is_valid {
            return Ok(RogragResponse::refusal(
                query_validation
                    .issues
                    .first()
                    .map(|issue| issue.description.clone())
                    .unwrap_or_else(|| "Query validation failed".to_string()),
                "Invalid query".to_string(),
            ));
        }

        // Step 2: Classify intent
        let intent_result = self.intent_classifier.classify(query)?;
        if intent_result.should_refuse {
            return Ok(RogragResponse::refusal(
                intent_result
                    .refusal_reason
                    .unwrap_or_else(|| "Query cannot be answered safely".to_string()),
                "Safety refusal".to_string(),
            ));
        }

        // Step 3: Query decomposition
        let decomposition_start = Instant::now();
        let decomposition_result = self.decompose_query_with_fallback(query, context).await?;
        context.record_decomposition_time(decomposition_start.elapsed());

        // Step 4: Dual-level retrieval
        let retrieval_start = Instant::now();
        let subquery_results = self
            .execute_dual_level_retrieval(&decomposition_result, graph, context)
            .await?;
        context.record_retrieval_time(retrieval_start.elapsed());

        // Step 5: Response generation
        let response_start = Instant::now();
        let mut response = self
            .generate_response(query, subquery_results, intent_result, context)
            .await?;
        context.record_response_time(response_start.elapsed());

        // Step 6: Response validation and enhancement
        response = self.validator.validate_response(&response)?;

        // Step 7: Update processing statistics
        response.processing_stats = context.to_processing_stats();
        response.processing_stats.subqueries_processed = decomposition_result.subqueries.len();

        Ok(response)
    }

    /// Decompose query with fallback handling
    async fn decompose_query_with_fallback(
        &self,
        query: &str,
        context: &mut ProcessingContext,
    ) -> Result<DecompositionResult> {
        match self.decomposer.decompose(query).await {
            Ok(result) => Ok(result),
            Err(error) if self.config.enable_fallbacks => {
                context.add_error(format!("Decomposition failed: {error}"));
                context.increment_fallback();

                // Fallback to single query
                Ok(DecompositionResult::single_query(query.to_string()))
            },
            Err(error) => Err(error),
        }
    }

    /// Execute dual-level retrieval for all subqueries
    async fn execute_dual_level_retrieval(
        &self,
        decomposition_result: &DecompositionResult,
        graph: &KnowledgeGraph,
        context: &mut ProcessingContext,
    ) -> Result<Vec<SubqueryResult>> {
        let mut all_results = Vec::new();

        for subquery in &decomposition_result.subqueries {
            let subquery_result = self
                .process_single_subquery(&subquery.text, graph, context)
                .await;

            match subquery_result {
                Ok(result) => all_results.push(result),
                Err(error) if self.config.enable_fallbacks => {
                    context.add_error(format!("Subquery '{}' failed: {}", subquery.text, error));
                    context.increment_fallback();

                    // Create fallback result
                    all_results.push(SubqueryResult {
                        subquery: subquery.text.clone(),
                        result_type: SubqueryResultType::Fallback,
                        confidence: 0.1,
                        content: "Unable to process this part of the query".to_string(),
                        sources: vec![],
                    });
                },
                Err(error) => return Err(error),
            }
        }

        if all_results.is_empty() {
            return Err(ProcessingError::AllStrategiesFailed {
                reason: "No subqueries could be processed".to_string(),
            }
            .into());
        }

        Ok(all_results)
    }

    /// Process a single subquery using dual-level retrieval
    async fn process_single_subquery(
        &self,
        subquery: &str,
        graph: &KnowledgeGraph,
        context: &mut ProcessingContext,
    ) -> Result<SubqueryResult> {
        // Level 1: Try logic form retrieval
        match self.logic_form_retriever.retrieve(subquery, graph).await {
            Ok(logic_result) => {
                return Ok(SubqueryResult::logic_form(
                    subquery.to_string(),
                    logic_result,
                ));
            },
            Err(error) if self.config.enable_fallbacks => {
                context.add_error(format!(
                    "Logic form retrieval failed for '{subquery}': {error}"
                ));
            },
            Err(error) => return Err(error),
        }

        // Level 2: Fallback to fuzzy matching
        let fuzzy_result = self
            .fuzzy_matcher
            .match_query(subquery, graph)
            .map_err(|error| {
                context.add_error(format!("Fuzzy matching failed for '{subquery}': {error}"));
                error
            })?;

        context.increment_fallback();
        Ok(SubqueryResult::fuzzy_match(
            subquery.to_string(),
            fuzzy_result,
        ))
    }

    /// Generate response from subquery results
    async fn generate_response(
        &self,
        query: &str,
        subquery_results: Vec<SubqueryResult>,
        intent_result: IntentResult,
        _context: &mut ProcessingContext,
    ) -> Result<RogragResponse> {
        if self.config.response_streaming {
            self.streaming_builder
                .build_streaming_response(query.to_string(), subquery_results, intent_result)
                .await
        } else {
            self.streaming_builder
                .build_complete_response(query.to_string(), subquery_results, intent_result)
                .await
        }
    }

    /// Get decomposition result for metrics (helper method)
    async fn get_decomposition_for_metrics(&self, query: &str) -> Option<DecompositionResult> {
        self.decomposer.decompose(query).await.ok()
    }

    /// Get quality metrics
    pub fn get_quality_metrics(&self) -> Result<QualityMetrics> {
        self.quality_metrics
            .lock()
            .map(|metrics| metrics.clone())
            .map_err(|_| {
                ProcessingError::ConfigurationError {
                    reason: "Failed to access quality metrics".to_string(),
                }
                .into()
            })
    }

    /// Get current configuration
    pub fn get_config(&self) -> &RogragConfig {
        &self.config
    }

    /// Update configuration
    pub fn update_config(&mut self, config: RogragConfig) {
        self.config = config;
    }

    /// Check system health
    pub async fn health_check(&self) -> HealthCheckResult {
        let mut issues = Vec::new();
        let mut overall_health = HealthStatus::Healthy;

        // Check decomposer
        if !self.decomposer.can_decompose("test query") {
            issues.push("Query decomposer may not be functioning properly".to_string());
            overall_health = HealthStatus::Degraded;
        }

        // Check quality metrics
        if let Ok(metrics) = self.quality_metrics.lock() {
            let stats = metrics.get_performance_statistics();
            if stats.error_rate > 0.1 {
                issues.push(format!("High error rate: {:.1}%", stats.error_rate * 100.0));
                overall_health = HealthStatus::Unhealthy;
            }
            if stats.avg_processing_time_ms > 10000.0 {
                issues.push(format!(
                    "Slow processing: {:.0}ms average",
                    stats.avg_processing_time_ms
                ));
                if overall_health == HealthStatus::Healthy {
                    overall_health = HealthStatus::Degraded;
                }
            }
        } else {
            issues.push("Cannot access quality metrics".to_string());
            overall_health = HealthStatus::Degraded;
        }

        HealthCheckResult {
            status: overall_health,
            issues,
            timestamp: std::time::SystemTime::now()
                .duration_since(std::time::UNIX_EPOCH)
                .expect("system clock before UNIX epoch")
                .as_secs(),
        }
    }

    /// Get system statistics
    pub fn get_system_statistics(&self) -> SystemStatistics {
        let performance_stats = self
            .quality_metrics
            .lock()
            .map(|metrics| metrics.get_performance_statistics().clone())
            .unwrap_or_default();

        SystemStatistics {
            total_queries_processed: performance_stats.total_queries,
            average_processing_time_ms: performance_stats.avg_processing_time_ms,
            current_throughput_qps: performance_stats.throughput_qps,
            error_rate: performance_stats.error_rate,
            fallback_rate: performance_stats.fallback_rate,
            average_quality_score: performance_stats.avg_quality_score,
            decomposition_success_rate: if performance_stats.total_queries > 0 {
                performance_stats.successful_decompositions as f64
                    / performance_stats.total_queries as f64
            } else {
                0.0
            },
        }
    }

    /// Process multiple queries in batch
    pub async fn batch_process(
        &self,
        queries: &[&str],
        graph: &KnowledgeGraph,
    ) -> Vec<Result<RogragResponse>> {
        let mut results = Vec::with_capacity(queries.len());

        for query in queries {
            let result = self.process_query(query, graph).await;
            results.push(result);
        }

        results
    }

    /// Process multiple queries concurrently
    pub async fn concurrent_batch_process(
        &self,
        queries: &[&str],
        graph: &KnowledgeGraph,
        max_concurrent: usize,
    ) -> Vec<Result<RogragResponse>> {
        use futures::stream::{self, StreamExt};

        stream::iter(queries)
            .map(|query| async move { self.process_query(query, graph).await })
            .buffer_unordered(max_concurrent)
            .collect()
            .await
    }
}

/// Health check result for system monitoring.
///
/// Provides current system health status with any detected issues.
#[cfg(feature = "rograg")]
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct HealthCheckResult {
    /// Current health status of the system.
    pub status: HealthStatus,

    /// List of detected issues or warnings.
    pub issues: Vec<String>,

    /// Unix timestamp when health check was performed.
    pub timestamp: u64,
}

/// Health status levels for system monitoring.
#[cfg(feature = "rograg")]
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub enum HealthStatus {
    /// System is operating normally.
    Healthy,

    /// System is operational but with degraded performance or warnings.
    Degraded,

    /// System has critical issues requiring attention.
    Unhealthy,
}

/// System statistics for performance monitoring.
///
/// Aggregated metrics across all processed queries for system analysis.
#[cfg(feature = "rograg")]
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SystemStatistics {
    /// Total number of queries processed since system start.
    pub total_queries_processed: usize,

    /// Average processing time per query in milliseconds.
    pub average_processing_time_ms: f64,

    /// Current throughput in queries per second.
    pub current_throughput_qps: f64,

    /// Error rate as a fraction (0.0 to 1.0).
    pub error_rate: f64,

    /// Rate of fallback usage as a fraction (0.0 to 1.0).
    pub fallback_rate: f64,

    /// Average quality score across all responses (0.0 to 1.0).
    pub average_quality_score: f64,

    /// Success rate of query decomposition (0.0 to 1.0).
    pub decomposition_success_rate: f64,
}

#[cfg(feature = "rograg")]
impl Default for SystemStatistics {
    fn default() -> Self {
        Self {
            total_queries_processed: 0,
            average_processing_time_ms: 0.0,
            current_throughput_qps: 0.0,
            error_rate: 0.0,
            fallback_rate: 0.0,
            average_quality_score: 0.0,
            decomposition_success_rate: 0.0,
        }
    }
}

/// Configuration for ROGRAG processing behavior.
///
/// Controls all aspects of the ROGRAG pipeline including feature toggles,
/// thresholds, and performance limits.
#[cfg(feature = "rograg")]
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct RogragConfig {
    /// Maximum time allowed for processing a single query.
    pub max_processing_time: Duration,

    /// Whether to classify query intent before processing.
    pub enable_intent_classification: bool,

    /// Whether to decompose complex queries into subqueries.
    pub enable_query_decomposition: bool,

    /// Whether to attempt logic form retrieval.
    pub enable_logic_form_retrieval: bool,

    /// Whether to use fuzzy matching as a fallback.
    pub enable_fuzzy_matching: bool,

    /// Confidence threshold for triggering fallback strategies (0.0 to 1.0).
    pub fallback_threshold: f32,

    /// Minimum acceptable quality score for responses (0.0 to 1.0).
    pub quality_threshold: f32,

    /// Maximum number of subqueries to generate per query.
    pub max_subqueries: usize,

    /// Whether to enable streaming response generation.
    pub response_streaming: bool,

    /// Whether to track quality metrics for processed queries.
    pub quality_tracking: bool,

    /// Whether to enable automatic fallback on failures.
    pub enable_fallbacks: bool,
}

#[cfg(feature = "rograg")]
impl Default for RogragConfig {
    fn default() -> Self {
        Self {
            max_processing_time: Duration::from_secs(30),
            enable_intent_classification: true,
            enable_query_decomposition: true,
            enable_logic_form_retrieval: true,
            enable_fuzzy_matching: true,
            fallback_threshold: 0.6,
            quality_threshold: 0.7,
            max_subqueries: 5,
            response_streaming: true,
            quality_tracking: true,
            enable_fallbacks: true,
        }
    }
}

/// Response from ROGRAG processing.
///
/// Contains the generated answer, confidence scores, sources, and processing metadata.
#[cfg(feature = "rograg")]
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct RogragResponse {
    /// The original query that was processed.
    pub query: String,

    /// Generated response content.
    pub content: String,

    /// Overall confidence in the response (0.0 to 1.0).
    pub confidence: f32,

    /// Source entity/chunk IDs used to generate the response.
    pub sources: Vec<String>,

    /// Intent classification result for the query.
    pub intent_result: IntentResult,

    /// Results from processing individual subqueries.
    pub subquery_results: Vec<SubqueryResult>,

    /// Processing performance statistics.
    pub processing_stats: ProcessingStats,

    /// Whether this is a refusal to answer.
    pub is_refusal: bool,

    /// Whether response was generated with streaming.
    pub is_streaming: bool,
}

#[cfg(feature = "rograg")]
impl RogragResponse {
    /// Create a refusal response.
    ///
    /// Used when the system determines a query should not be answered.
    ///
    /// # Arguments
    ///
    /// * `query` - The original query being refused
    /// * `reason` - Explanation for the refusal
    pub fn refusal(query: String, reason: String) -> Self {
        Self {
            query,
            content: format!("Unable to provide an answer: {reason}"),
            confidence: 0.0,
            sources: vec![],
            intent_result: IntentResult::default(),
            subquery_results: vec![],
            processing_stats: ProcessingStats::default(),
            is_refusal: true,
            is_streaming: false,
        }
    }
}

/// Result from processing a single subquery.
///
/// Contains the answer, confidence, and metadata for one decomposed subquery.
#[cfg(feature = "rograg")]
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SubqueryResult {
    /// The subquery text that was processed.
    pub subquery: String,

    /// Generated content for this subquery.
    pub content: String,

    /// Confidence in this subquery result (0.0 to 1.0).
    pub confidence: f32,

    /// Source IDs used for this subquery.
    pub sources: Vec<String>,

    /// Type of retrieval strategy used.
    pub result_type: SubqueryResultType,
}

#[cfg(feature = "rograg")]
impl SubqueryResult {
    /// Create a subquery result from logic form retrieval.
    ///
    /// # Arguments
    ///
    /// * `subquery` - The subquery text
    /// * `logic_result` - Result from logic form execution
    pub fn logic_form(subquery: String, logic_result: LogicFormResult) -> Self {
        Self {
            subquery,
            content: logic_result.answer,
            confidence: logic_result.confidence,
            sources: logic_result.sources,
            result_type: SubqueryResultType::LogicForm,
        }
    }

    /// Create a subquery result from fuzzy matching.
    ///
    /// # Arguments
    ///
    /// * `subquery` - The subquery text
    /// * `fuzzy_result` - Result from fuzzy matching
    pub fn fuzzy_match(subquery: String, fuzzy_result: FuzzyMatchResult) -> Self {
        Self {
            subquery,
            content: fuzzy_result.content,
            confidence: fuzzy_result.confidence,
            sources: fuzzy_result.sources,
            result_type: SubqueryResultType::FuzzyMatch,
        }
    }
}

/// Type of retrieval strategy used for subquery processing.
///
/// Indicates which retrieval method successfully answered the subquery.
#[cfg(feature = "rograg")]
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub enum SubqueryResultType {
    /// Result from logic form-based structured retrieval.
    LogicForm,

    /// Result from fuzzy matching fallback.
    FuzzyMatch,

    /// Generic fallback when other strategies failed.
    Fallback,
}

/// Processing statistics for a query.
///
/// Detailed timing breakdown and metrics for performance analysis.
#[cfg(feature = "rograg")]
#[derive(Debug, Clone, Serialize, Deserialize, Default)]
pub struct ProcessingStats {
    /// Total processing time in milliseconds.
    pub total_time_ms: u64,

    /// Time spent on query decomposition in milliseconds.
    pub decomposition_time_ms: u64,

    /// Time spent on intent classification in milliseconds.
    pub intent_classification_time_ms: u64,

    /// Time spent on retrieval operations in milliseconds.
    pub retrieval_time_ms: u64,

    /// Time spent on response synthesis in milliseconds.
    pub synthesis_time_ms: u64,

    /// Time spent on validation in milliseconds.
    pub validation_time_ms: u64,

    /// Number of subqueries that were processed.
    pub subqueries_processed: usize,

    /// Whether fallback strategies were used.
    pub fallback_used: bool,
}

#[cfg(feature = "rograg")]
#[cfg(test)]
mod tests {
    use super::*;
    use crate::core::{Entity, EntityId, KnowledgeGraph};

    #[cfg(feature = "rograg")]
    fn create_test_graph() -> KnowledgeGraph {
        let mut graph = KnowledgeGraph::new();

        let entity = Entity {
            id: EntityId::new("entity_1".to_string()),
            name: "Entity Name".to_string(),
            entity_type: "ENTITY".to_string(),
            confidence: 0.9,
            mentions: vec![],
            embedding: None,
            first_mentioned: None,
            last_mentioned: None,
            temporal_validity: None,
        };

        graph.add_entity(entity).unwrap();
        graph
    }

    #[cfg(feature = "rograg")]
    #[cfg(feature = "rograg")]
    #[tokio::test]
    async fn test_processor_with_config() {
        let config = RogragConfig {
            max_subqueries: 3,
            quality_threshold: 0.8,
            ..Default::default()
        };

        let processor = RogragProcessor::with_config(config);
        assert!(processor.is_ok());
    }

    #[cfg(feature = "rograg")]
    #[tokio::test]
    async fn test_query_processing() {
        let processor = RogragProcessor::new().unwrap();
        let graph = create_test_graph();

        let result = processor
            .process_query("What is Entity Name?", &graph)
            .await;
        assert!(result.is_ok());

        let response = result.unwrap();
        assert!(!response.content.is_empty());
        assert!(response.confidence >= 0.0);
    }

    #[cfg(feature = "rograg")]
    #[tokio::test]
    async fn test_batch_processing() {
        let processor = RogragProcessor::new().unwrap();
        let graph = create_test_graph();

        let queries = vec!["What is Entity Name?", "Who is the entity?"];
        let results = processor.batch_process(&queries, &graph).await;

        assert_eq!(results.len(), 2);
        assert!(results.iter().all(|r| r.is_ok()));
    }

    #[cfg(feature = "rograg")]
    #[tokio::test]
    async fn test_health_check() {
        let processor = RogragProcessor::new().unwrap();
        let health = processor.health_check().await;

        // Should be healthy for a new processor
        assert!(matches!(
            health.status,
            HealthStatus::Healthy | HealthStatus::Degraded
        ));
    }

    #[cfg(feature = "rograg")]
    #[test]
    fn test_system_statistics() {
        let processor = RogragProcessor::new().unwrap();
        let stats = processor.get_system_statistics();

        assert_eq!(stats.total_queries_processed, 0);
        assert_eq!(stats.average_processing_time_ms, 0.0);
    }

    #[cfg(feature = "rograg")]
    #[test]
    fn test_builder_pattern() {
        let processor = RogragProcessor::builder()
            .with_config(RogragConfig {
                max_subqueries: 10,
                ..Default::default()
            })
            .build();

        assert!(processor.is_ok());
        assert_eq!(processor.unwrap().get_config().max_subqueries, 10);
    }

    #[cfg(feature = "rograg")]
    #[tokio::test]
    async fn test_concurrent_batch_processing() {
        let processor = RogragProcessor::new().unwrap();
        let graph = create_test_graph();

        let queries = vec![
            "What is Entity Name?",
            "Who is the entity?",
            "Tell me about entities",
        ];
        let results = processor
            .concurrent_batch_process(&queries, &graph, 2)
            .await;

        assert_eq!(results.len(), 3);
        assert!(results.iter().all(|r| r.is_ok()));
    }
}