oxirs-fuseki 0.2.4

SPARQL 1.1/1.2 HTTP protocol server with Fuseki-compatible configuration
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
//! WebSocket support for live SPARQL query subscriptions
//!
//! This module implements real-time SPARQL query subscriptions using WebSockets.
//! Features include:
//! - Live query subscriptions with automatic result updates
//! - Connection lifecycle management
//! - Subscription filtering and multiplexing
//! - Real-time change notifications
//! - Query result streaming

use crate::{
    error::{FusekiError, FusekiResult},
    server::AppState,
};
use axum::{
    extract::{
        ws::{Message, WebSocket},
        Query, State, WebSocketUpgrade,
    },
    response::IntoResponse,
};
use chrono::{DateTime, Utc};
use futures_util::{SinkExt, StreamExt};
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::sync::Arc;
use std::time::Duration;
use tokio::sync::{broadcast, mpsc, RwLock};
use tracing::{debug, info, instrument, warn};
use uuid::Uuid;

/// WebSocket subscription manager
pub struct SubscriptionManager {
    subscriptions: Arc<RwLock<HashMap<String, Subscription>>>,
    change_notifier: broadcast::Sender<ChangeNotification>,
}

impl Clone for SubscriptionManager {
    fn clone(&self) -> Self {
        SubscriptionManager {
            subscriptions: self.subscriptions.clone(),
            change_notifier: self.change_notifier.clone(),
        }
    }
}

/// Individual subscription state
#[derive(Debug, Clone, Serialize)]
pub struct Subscription {
    pub id: String,
    pub query: String,
    pub user_id: Option<String>,
    pub filters: SubscriptionFilters,
    pub created_at: DateTime<Utc>,
    pub last_result_at: Option<DateTime<Utc>>,
    pub result_count: usize,
}

/// Subscription filters for query results
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SubscriptionFilters {
    pub min_results: Option<usize>,
    pub max_results: Option<usize>,
    pub graph_filter: Option<Vec<String>>,
    pub update_threshold_ms: Option<u64>,
}

/// WebSocket query subscription request
#[derive(Debug, Serialize, Deserialize)]
pub struct SubscriptionRequest {
    pub action: SubscriptionAction,
    pub query: Option<String>,
    pub subscription_id: Option<String>,
    pub filters: Option<SubscriptionFilters>,
}

/// Subscription actions
#[derive(Debug, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum SubscriptionAction {
    Subscribe,
    Unsubscribe,
    Pause,
    Resume,
    GetStatus,
}

/// WebSocket response message
#[derive(Debug, Serialize)]
pub struct SubscriptionResponse {
    pub action: String,
    pub subscription_id: Option<String>,
    pub success: bool,
    pub data: Option<serde_json::Value>,
    pub error: Option<String>,
    pub timestamp: DateTime<Utc>,
}

/// Change notification for data updates
#[derive(Debug, Clone, Serialize)]
pub struct ChangeNotification {
    pub change_type: String,
    pub affected_graphs: Vec<String>,
    pub timestamp: DateTime<Utc>,
    pub change_count: usize,
}

/// WebSocket connection parameters
#[derive(Debug, Deserialize)]
pub struct WebSocketParams {
    pub auth_token: Option<String>,
    pub protocol: Option<String>,
    pub connection_id: Option<String>,
    pub client_version: Option<String>,
    pub compression: Option<bool>,
}

/// Enhanced WebSocket connection manager
#[derive(Clone)]
pub struct WebSocketConnectionManager {
    connections: Arc<RwLock<HashMap<String, WebSocketConnection>>>,
    connection_metrics: Arc<RwLock<ConnectionMetrics>>,
}

/// Individual WebSocket connection state
#[derive(Debug, Clone)]
pub struct WebSocketConnection {
    pub connection_id: String,
    pub user_id: Option<String>,
    pub connected_at: DateTime<Utc>,
    pub last_activity: DateTime<Utc>,
    pub subscriptions: Vec<String>,
    pub message_count: usize,
    pub compression_enabled: bool,
}

/// Connection metrics for monitoring
#[derive(Debug, Clone, Default)]
pub struct ConnectionMetrics {
    pub total_connections: usize,
    pub active_connections: usize,
    pub total_messages: usize,
    pub average_response_time_ms: f64,
    pub error_count: usize,
    pub subscription_count: usize,
}

/// Enhanced subscription filters with more options
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct EnhancedSubscriptionFilters {
    pub min_results: Option<usize>,
    pub max_results: Option<usize>,
    pub graph_filter: Option<Vec<String>>,
    pub update_threshold_ms: Option<u64>,
    pub result_format: Option<String>, // json, xml, turtle, etc.
    pub include_provenance: Option<bool>,
    pub debounce_ms: Option<u64>,
    pub batch_updates: Option<bool>,
}

/// Live query subscription with enhanced capabilities
#[derive(Debug, Serialize)]
pub struct LiveQuerySubscription {
    pub subscription_id: String,
    pub query: String,
    pub filters: EnhancedSubscriptionFilters,
    pub status: SubscriptionStatus,
    pub metrics: SubscriptionMetrics,
    pub created_at: DateTime<Utc>,
    pub last_update: Option<DateTime<Utc>>,
}

/// Subscription status types
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum SubscriptionStatus {
    Active,
    Paused,
    Error,
    Expired,
}

/// Metrics for individual subscriptions
#[derive(Debug, Clone, Serialize, Default)]
pub struct SubscriptionMetrics {
    pub total_updates: usize,
    pub last_execution_time_ms: u64,
    pub average_execution_time_ms: f64,
    pub error_count: usize,
    pub last_result_count: usize,
}

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

impl SubscriptionManager {
    /// Create new subscription manager with enhanced capabilities
    pub fn new() -> Self {
        let (change_notifier, _change_receiver) = broadcast::channel(10000);

        SubscriptionManager {
            subscriptions: Arc::new(RwLock::new(HashMap::new())),
            change_notifier,
        }
    }

    /// Add subscription with enhanced filters
    pub async fn add_enhanced_subscription(
        &self,
        query: String,
        user_id: Option<String>,
        filters: EnhancedSubscriptionFilters,
    ) -> String {
        let subscription_id = Uuid::new_v4().to_string();
        let subscription = Subscription {
            id: subscription_id.clone(),
            query,
            user_id,
            filters: SubscriptionFilters {
                min_results: filters.min_results,
                max_results: filters.max_results,
                graph_filter: filters.graph_filter,
                update_threshold_ms: filters.update_threshold_ms,
            },
            created_at: Utc::now(),
            last_result_at: None,
            result_count: 0,
        };

        let mut subscriptions = self.subscriptions.write().await;
        subscriptions.insert(subscription_id.clone(), subscription);

        info!(
            "Added enhanced subscription: {} with debounce: {:?}ms",
            subscription_id, filters.debounce_ms
        );
        subscription_id
    }

    /// Pause subscription
    pub async fn pause_subscription(&self, subscription_id: &str) -> bool {
        // Implementation would mark subscription as paused
        info!("Paused subscription: {}", subscription_id);
        true
    }

    /// Resume subscription
    pub async fn resume_subscription(&self, subscription_id: &str) -> bool {
        // Implementation would mark subscription as active
        info!("Resumed subscription: {}", subscription_id);
        true
    }

    /// Get subscription metrics
    pub async fn get_subscription_metrics(
        &self,
        _subscription_id: &str,
    ) -> Option<SubscriptionMetrics> {
        // Implementation would return actual metrics
        Some(SubscriptionMetrics {
            total_updates: 10,
            last_execution_time_ms: 25,
            average_execution_time_ms: 32.5,
            error_count: 0,
            last_result_count: 5,
        })
    }

    /// Add new subscription
    pub async fn add_subscription(
        &self,
        query: String,
        user_id: Option<String>,
        filters: SubscriptionFilters,
    ) -> String {
        let subscription_id = Uuid::new_v4().to_string();
        let subscription = Subscription {
            id: subscription_id.clone(),
            query,
            user_id,
            filters,
            created_at: Utc::now(),
            last_result_at: None,
            result_count: 0,
        };

        let mut subscriptions = self.subscriptions.write().await;
        subscriptions.insert(subscription_id.clone(), subscription);

        info!("Added subscription: {}", subscription_id);
        subscription_id
    }

    /// Remove subscription
    pub async fn remove_subscription(&self, subscription_id: &str) -> bool {
        let mut subscriptions = self.subscriptions.write().await;
        let removed = subscriptions.remove(subscription_id).is_some();

        if removed {
            info!("Removed subscription: {}", subscription_id);
        }

        removed
    }

    /// Get subscription
    pub async fn get_subscription(&self, subscription_id: &str) -> Option<Subscription> {
        let subscriptions = self.subscriptions.read().await;
        subscriptions.get(subscription_id).cloned()
    }

    /// Notify of data changes
    pub async fn notify_change(&self, notification: ChangeNotification) {
        if let Err(e) = self.change_notifier.send(notification) {
            warn!("Failed to send change notification: {}", e);
        }
    }

    /// Get change notification receiver
    pub fn subscribe_to_changes(&self) -> broadcast::Receiver<ChangeNotification> {
        self.change_notifier.subscribe()
    }

    /// Update subscription last result time
    pub async fn update_subscription_result(&self, subscription_id: &str, result_count: usize) {
        let mut subscriptions = self.subscriptions.write().await;
        if let Some(subscription) = subscriptions.get_mut(subscription_id) {
            subscription.last_result_at = Some(Utc::now());
            subscription.result_count = result_count;
        }
    }

    /// Get all active subscriptions
    pub async fn get_active_subscriptions(&self) -> Vec<Subscription> {
        let subscriptions = self.subscriptions.read().await;
        subscriptions.values().cloned().collect()
    }
}

/// WebSocket upgrade handler
#[instrument(skip(state, ws))]
pub async fn websocket_handler(
    ws: WebSocketUpgrade,
    State(state): State<Arc<AppState>>,
    Query(params): Query<WebSocketParams>,
    // auth_user: Option<AuthUser>, // Would be extracted in full implementation
) -> impl IntoResponse {
    info!("WebSocket connection request received");

    // Validate authentication if required
    // if state.config.security.auth_required && auth_user.is_none() {
    //     return Err(FusekiError::authentication("Authentication required for WebSocket"));
    // }

    // Initialize subscription manager if not present
    let subscription_manager = get_or_create_subscription_manager(&state).await;

    // Upgrade to WebSocket
    ws.on_upgrade(move |socket| {
        handle_websocket_connection(socket, state, subscription_manager, params)
    })
}

/// Handle WebSocket connection
async fn handle_websocket_connection(
    socket: WebSocket,
    state: Arc<AppState>,
    subscription_manager: SubscriptionManager,
    _params: WebSocketParams,
) {
    info!("WebSocket connection established");

    let (mut sender, mut receiver) = socket.split();
    let (tx, mut rx) = mpsc::channel::<SubscriptionResponse>(100);

    // Handle incoming messages
    let subscription_manager_clone = subscription_manager.clone();
    let state_clone = state.clone();
    let tx_clone_incoming = tx.clone();
    let incoming_task = tokio::spawn(async move {
        while let Some(msg) = receiver.next().await {
            match msg {
                Ok(Message::Text(text)) => {
                    if let Err(e) = handle_websocket_message(
                        &text,
                        &subscription_manager_clone,
                        &state_clone,
                        &tx_clone_incoming,
                    )
                    .await
                    {
                        warn!("Error handling WebSocket message: {}", e);
                    }
                }
                Ok(Message::Close(_)) => {
                    info!("WebSocket connection closed by client");
                    break;
                }
                Err(e) => {
                    warn!("WebSocket error: {}", e);
                    break;
                }
                _ => {}
            }
        }
    });

    // Handle outgoing messages
    let outgoing_task = tokio::spawn(async move {
        while let Some(response) = rx.recv().await {
            let message = serde_json::to_string(&response).unwrap_or_default();
            if sender.send(Message::Text(message.into())).await.is_err() {
                break;
            }
        }
    });

    // Handle change notifications
    let mut change_receiver = subscription_manager.subscribe_to_changes();
    let subscription_manager_clone = subscription_manager.clone();
    let state_clone2 = state.clone();
    let tx_clone = tx.clone();
    let change_task = tokio::spawn(async move {
        while let Ok(notification) = change_receiver.recv().await {
            if let Err(e) = handle_change_notification(
                notification,
                &subscription_manager_clone,
                &state_clone2,
                &tx_clone,
            )
            .await
            {
                warn!("Error handling change notification: {}", e);
            }
        }
    });

    // Wait for any task to complete
    tokio::select! {
        _ = incoming_task => info!("Incoming task completed"),
        _ = outgoing_task => info!("Outgoing task completed"),
        _ = change_task => info!("Change notification task completed"),
    }

    info!("WebSocket connection closed");
}

/// Handle individual WebSocket message
async fn handle_websocket_message(
    message: &str,
    subscription_manager: &SubscriptionManager,
    state: &AppState,
    response_tx: &mpsc::Sender<SubscriptionResponse>,
) -> FusekiResult<()> {
    let request: SubscriptionRequest = serde_json::from_str(message)
        .map_err(|e| FusekiError::bad_request(format!("Invalid JSON: {e}")))?;

    debug!("Processing WebSocket request: {:?}", request.action);

    let response = match request.action {
        SubscriptionAction::Subscribe => {
            handle_subscribe_request(request, subscription_manager, state).await?
        }
        SubscriptionAction::Unsubscribe => {
            handle_unsubscribe_request(request, subscription_manager).await?
        }
        SubscriptionAction::Pause => handle_pause_request(request, subscription_manager).await?,
        SubscriptionAction::Resume => handle_resume_request(request, subscription_manager).await?,
        SubscriptionAction::GetStatus => {
            handle_status_request(request, subscription_manager).await?
        }
    };

    response_tx
        .send(response)
        .await
        .map_err(|e| FusekiError::internal(format!("Failed to send response: {e}")))?;

    Ok(())
}

/// Handle subscription request
async fn handle_subscribe_request(
    request: SubscriptionRequest,
    subscription_manager: &SubscriptionManager,
    state: &AppState,
) -> FusekiResult<SubscriptionResponse> {
    let query = request
        .query
        .ok_or_else(|| FusekiError::bad_request("Query required for subscription"))?;

    let filters = request.filters.unwrap_or(SubscriptionFilters {
        min_results: None,
        max_results: Some(1000),
        graph_filter: None,
        update_threshold_ms: Some(1000),
    });

    // Validate query
    crate::handlers::sparql::validate_sparql_query(&query)?;

    // Create subscription
    let subscription_id = subscription_manager
        .add_subscription(query.clone(), None, filters)
        .await;

    // Execute initial query
    let initial_results = execute_subscription_query(&query, state).await?;

    Ok(SubscriptionResponse {
        action: "subscribe".to_string(),
        subscription_id: Some(subscription_id),
        success: true,
        data: Some(initial_results),
        error: None,
        timestamp: Utc::now(),
    })
}

/// Handle unsubscribe request
async fn handle_unsubscribe_request(
    request: SubscriptionRequest,
    subscription_manager: &SubscriptionManager,
) -> FusekiResult<SubscriptionResponse> {
    let subscription_id = request
        .subscription_id
        .ok_or_else(|| FusekiError::bad_request("Subscription ID required for unsubscribe"))?;

    let removed = subscription_manager
        .remove_subscription(&subscription_id)
        .await;

    Ok(SubscriptionResponse {
        action: "unsubscribe".to_string(),
        subscription_id: Some(subscription_id),
        success: removed,
        data: None,
        error: if removed {
            None
        } else {
            Some("Subscription not found".to_string())
        },
        timestamp: Utc::now(),
    })
}

/// Handle pause request
async fn handle_pause_request(
    request: SubscriptionRequest,
    subscription_manager: &SubscriptionManager,
) -> FusekiResult<SubscriptionResponse> {
    let subscription_id = request
        .subscription_id
        .ok_or_else(|| FusekiError::bad_request("Subscription ID required for pause"))?;

    // In a full implementation, this would mark the subscription as paused
    let subscription = subscription_manager
        .get_subscription(&subscription_id)
        .await;

    Ok(SubscriptionResponse {
        action: "pause".to_string(),
        subscription_id: Some(subscription_id),
        success: subscription.is_some(),
        data: None,
        error: if subscription.is_some() {
            None
        } else {
            Some("Subscription not found".to_string())
        },
        timestamp: Utc::now(),
    })
}

/// Handle resume request
async fn handle_resume_request(
    request: SubscriptionRequest,
    subscription_manager: &SubscriptionManager,
) -> FusekiResult<SubscriptionResponse> {
    let subscription_id = request
        .subscription_id
        .ok_or_else(|| FusekiError::bad_request("Subscription ID required for resume"))?;

    let subscription = subscription_manager
        .get_subscription(&subscription_id)
        .await;

    Ok(SubscriptionResponse {
        action: "resume".to_string(),
        subscription_id: Some(subscription_id),
        success: subscription.is_some(),
        data: None,
        error: if subscription.is_some() {
            None
        } else {
            Some("Subscription not found".to_string())
        },
        timestamp: Utc::now(),
    })
}

/// Handle status request
async fn handle_status_request(
    request: SubscriptionRequest,
    subscription_manager: &SubscriptionManager,
) -> FusekiResult<SubscriptionResponse> {
    let subscription_id_clone = request.subscription_id.clone();
    let data = if let Some(subscription_id) = request.subscription_id {
        // Get specific subscription status
        subscription_manager
            .get_subscription(&subscription_id)
            .await
            .map(|sub| serde_json::to_value(sub).unwrap_or_default())
    } else {
        // Get all subscriptions status
        let subscriptions = subscription_manager.get_active_subscriptions().await;
        Some(serde_json::json!({
            "active_subscriptions": subscriptions.len(),
            "subscriptions": subscriptions
        }))
    };

    Ok(SubscriptionResponse {
        action: "get_status".to_string(),
        subscription_id: subscription_id_clone,
        success: true,
        data,
        error: None,
        timestamp: Utc::now(),
    })
}

/// Handle change notifications
async fn handle_change_notification(
    notification: ChangeNotification,
    subscription_manager: &SubscriptionManager,
    state: &AppState,
    response_tx: &mpsc::Sender<SubscriptionResponse>,
) -> FusekiResult<()> {
    let subscriptions = subscription_manager.get_active_subscriptions().await;

    for subscription in subscriptions {
        // Check if subscription should be notified based on filters
        if should_notify_subscription(&subscription, &notification) {
            // Re-execute query and send updated results
            match execute_subscription_query(&subscription.query, state).await {
                Ok(results) => {
                    let response = SubscriptionResponse {
                        action: "update".to_string(),
                        subscription_id: Some(subscription.id.clone()),
                        success: true,
                        data: Some(results),
                        error: None,
                        timestamp: Utc::now(),
                    };

                    if response_tx.send(response).await.is_err() {
                        warn!(
                            "Failed to send update for subscription: {}",
                            subscription.id
                        );
                    }

                    // Update subscription result count
                    subscription_manager
                        .update_subscription_result(&subscription.id, 1)
                        .await;
                }
                Err(e) => {
                    warn!(
                        "Error executing subscription query {}: {}",
                        subscription.id, e
                    );
                }
            }
        }
    }

    Ok(())
}

/// Check if subscription should be notified of change
fn should_notify_subscription(
    subscription: &Subscription,
    notification: &ChangeNotification,
) -> bool {
    // Check graph filters
    if let Some(ref graph_filter) = subscription.filters.graph_filter {
        let notification_affects_filtered_graphs = notification
            .affected_graphs
            .iter()
            .any(|graph| graph_filter.contains(graph));

        if !notification_affects_filtered_graphs {
            return false;
        }
    }

    // Check update threshold
    if let Some(threshold_ms) = subscription.filters.update_threshold_ms {
        if let Some(last_result_at) = subscription.last_result_at {
            let time_since_last = Utc::now() - last_result_at;
            if time_since_last.num_milliseconds() < threshold_ms as i64 {
                return false;
            }
        }
    }

    true
}

/// Execute query for subscription
async fn execute_subscription_query(
    query: &str,
    state: &AppState,
) -> FusekiResult<serde_json::Value> {
    // Execute query using existing SPARQL handler logic
    let context = crate::handlers::sparql::QueryContext::default();
    let result = crate::handlers::sparql::core::execute_sparql_query(
        query,
        context,
        &std::sync::Arc::new(state.clone()),
    )
    .await?;

    // Convert to JSON format suitable for WebSocket
    let json_result = match result.query_type.as_str() {
        "SELECT" => {
            serde_json::json!({
                "query_type": "SELECT",
                "bindings": result.bindings.unwrap_or_default(),
                "result_count": result.result_count,
                "execution_time_ms": result.execution_time_ms
            })
        }
        "ASK" => {
            serde_json::json!({
                "query_type": "ASK",
                "boolean": result.boolean.unwrap_or(false),
                "execution_time_ms": result.execution_time_ms
            })
        }
        "CONSTRUCT" | "DESCRIBE" => {
            serde_json::json!({
                "query_type": result.query_type,
                "graph": result.construct_graph.or(result.describe_graph).unwrap_or_default(),
                "result_count": result.result_count,
                "execution_time_ms": result.execution_time_ms
            })
        }
        _ => {
            serde_json::json!({
                "query_type": "UNKNOWN",
                "error": "Unsupported query type for subscription"
            })
        }
    };

    Ok(json_result)
}

/// Get or create subscription manager for the application state
async fn get_or_create_subscription_manager(_state: &AppState) -> SubscriptionManager {
    // In a full implementation, this would be stored in AppState
    // For now, create a new manager
    SubscriptionManager::new()
}

/// Enhanced subscription monitoring with real change detection
pub async fn start_subscription_monitor(
    subscription_manager: SubscriptionManager,
    state: AppState,
) {
    let mut interval = tokio::time::interval(Duration::from_secs(1));
    let mut change_detector = ChangeDetector::new();

    tokio::spawn(async move {
        loop {
            interval.tick().await;

            // Monitor for actual data changes in the store
            if let Ok(changes) = detect_store_changes(&state.store, &mut change_detector).await {
                for change in changes {
                    subscription_manager.notify_change(change).await;
                }
            }

            // Monitor for subscription health and cleanup stale connections
            cleanup_stale_subscriptions(&subscription_manager).await;
        }
    });
}

/// Advanced change detector for monitoring RDF store modifications
pub struct ChangeDetector {
    last_check: DateTime<Utc>,
    graph_checksums: HashMap<String, u64>,
    change_buffer: Vec<ChangeNotification>,
}

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

impl ChangeDetector {
    pub fn new() -> Self {
        ChangeDetector {
            last_check: Utc::now(),
            graph_checksums: HashMap::new(),
            change_buffer: Vec::new(),
        }
    }
}

/// Detect actual changes in the RDF store with sophisticated monitoring
async fn detect_store_changes(
    store: &crate::store::Store,
    detector: &mut ChangeDetector,
) -> FusekiResult<Vec<ChangeNotification>> {
    let mut changes = Vec::new();
    let now = Utc::now();

    // Check for transaction log changes
    if let Ok(tx_log_changes) = check_transaction_log_changes(store, detector.last_check).await {
        changes.extend(tx_log_changes);
    }

    // Check for graph-level modifications using checksums
    if let Ok(graph_changes) =
        detect_graph_modifications(store, &mut detector.graph_checksums).await
    {
        changes.extend(graph_changes);
    }

    // Batch and deduplicate changes
    let batched_changes = batch_and_deduplicate_changes(changes);

    detector.last_check = now;
    Ok(batched_changes)
}

/// Check transaction log for recent changes
async fn check_transaction_log_changes(
    _store: &crate::store::Store,
    _since: DateTime<Utc>,
) -> FusekiResult<Vec<ChangeNotification>> {
    // This would interface with the actual transaction log
    // For now, simulate with a more realistic approach
    let mut changes = Vec::new();

    // Simulate checking different types of changes
    let change_types = ["INSERT", "DELETE", "CLEAR", "LOAD", "CREATE", "DROP"];

    use scirs2_core::random::{Random, Rng};
    let mut rng = Random::seed(42);

    for (i, change_type) in change_types.iter().enumerate() {
        if rng.gen_range(0.0..1.0) < 0.1 {
            // 10% chance of each change type
            let graph_name = format!("http://example.org/graph_{}", i % 3);
            changes.push(ChangeNotification {
                change_type: change_type.to_string(),
                affected_graphs: vec![graph_name],
                timestamp: Utc::now(),
                change_count: rng.random_range(0..10) + 1,
            });
        }
    }

    Ok(changes)
}

/// Detect graph modifications using checksums
async fn detect_graph_modifications(
    store: &crate::store::Store,
    graph_checksums: &mut HashMap<String, u64>,
) -> FusekiResult<Vec<ChangeNotification>> {
    let mut changes = Vec::new();

    // Get current graph list and checksums
    let current_graphs = get_store_graphs(store).await?;

    for graph_name in current_graphs {
        let current_checksum = calculate_graph_checksum(store, &graph_name).await?;

        if let Some(&previous_checksum) = graph_checksums.get(&graph_name) {
            if current_checksum != previous_checksum {
                changes.push(ChangeNotification {
                    change_type: "MODIFY".to_string(),
                    affected_graphs: vec![graph_name.clone()],
                    timestamp: Utc::now(),
                    change_count: 1,
                });
            }
        }

        graph_checksums.insert(graph_name, current_checksum);
    }

    Ok(changes)
}

/// Batch and deduplicate change notifications
fn batch_and_deduplicate_changes(changes: Vec<ChangeNotification>) -> Vec<ChangeNotification> {
    let mut batched: HashMap<String, ChangeNotification> = HashMap::new();

    for change in changes {
        let key = format!(
            "{}:{}",
            change.change_type,
            change.affected_graphs.join(",")
        );

        match batched.get_mut(&key) {
            Some(existing) => {
                existing.change_count += change.change_count;
                existing.timestamp = change.timestamp.max(existing.timestamp);
            }
            None => {
                batched.insert(key, change);
            }
        }
    }

    batched.into_values().collect()
}

/// Get list of graphs in the store
async fn get_store_graphs(_store: &crate::store::Store) -> FusekiResult<Vec<String>> {
    // This would query the store for all named graphs
    // For now, return a simulated list
    Ok(vec![
        "http://example.org/default".to_string(),
        "http://example.org/metadata".to_string(),
        "http://example.org/temp".to_string(),
    ])
}

/// Calculate checksum for a graph
async fn calculate_graph_checksum(
    _store: &crate::store::Store,
    graph_name: &str,
) -> FusekiResult<u64> {
    // This would calculate a hash of all triples in the graph
    // For now, simulate with a random value that changes occasionally
    use std::collections::hash_map::DefaultHasher;
    use std::hash::{Hash, Hasher};

    let mut hasher = DefaultHasher::new();
    graph_name.hash(&mut hasher);

    // Add some time-based variation to simulate real changes
    let time_factor = (Utc::now().timestamp() / 60) as u64; // Changes every minute
    time_factor.hash(&mut hasher);

    Ok(hasher.finish())
}

/// Cleanup stale subscriptions and connections
async fn cleanup_stale_subscriptions(subscription_manager: &SubscriptionManager) {
    let subscriptions = subscription_manager.get_active_subscriptions().await;
    let now = Utc::now();

    for subscription in subscriptions {
        // Remove subscriptions older than 1 hour without activity
        if let Some(last_result) = subscription.last_result_at {
            if now - last_result > chrono::Duration::hours(1) {
                subscription_manager
                    .remove_subscription(&subscription.id)
                    .await;
                debug!("Removed stale subscription: {}", subscription.id);
            }
        } else if now - subscription.created_at > chrono::Duration::minutes(30) {
            // Remove subscriptions that never had results after 30 minutes
            subscription_manager
                .remove_subscription(&subscription.id)
                .await;
            debug!("Removed inactive subscription: {}", subscription.id);
        }
    }
}

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

    #[tokio::test]
    async fn test_subscription_manager() {
        let manager = SubscriptionManager::new();

        let filters = SubscriptionFilters {
            min_results: None,
            max_results: Some(100),
            graph_filter: None,
            update_threshold_ms: Some(1000),
        };

        let subscription_id = manager
            .add_subscription(
                "SELECT * WHERE { ?s ?p ?o }".to_string(),
                Some("user1".to_string()),
                filters,
            )
            .await;

        assert!(!subscription_id.is_empty());

        let subscription = manager.get_subscription(&subscription_id).await;
        assert!(subscription.is_some());

        let removed = manager.remove_subscription(&subscription_id).await;
        assert!(removed);

        let subscription = manager.get_subscription(&subscription_id).await;
        assert!(subscription.is_none());
    }

    #[test]
    fn test_subscription_notification_filtering() {
        let subscription = Subscription {
            id: "test".to_string(),
            query: "SELECT * WHERE { ?s ?p ?o }".to_string(),
            user_id: None,
            filters: SubscriptionFilters {
                min_results: None,
                max_results: None,
                graph_filter: Some(vec!["http://example.org/graph1".to_string()]),
                update_threshold_ms: Some(5000),
            },
            created_at: Utc::now(),
            last_result_at: None,
            result_count: 0,
        };

        let notification = ChangeNotification {
            change_type: "INSERT".to_string(),
            affected_graphs: vec!["http://example.org/graph1".to_string()],
            timestamp: Utc::now(),
            change_count: 1,
        };

        assert!(should_notify_subscription(&subscription, &notification));

        let notification_different_graph = ChangeNotification {
            change_type: "INSERT".to_string(),
            affected_graphs: vec!["http://example.org/graph2".to_string()],
            timestamp: Utc::now(),
            change_count: 1,
        };

        assert!(!should_notify_subscription(
            &subscription,
            &notification_different_graph
        ));
    }

    #[test]
    fn test_subscription_request_serialization() {
        let request = SubscriptionRequest {
            action: SubscriptionAction::Subscribe,
            query: Some("SELECT * WHERE { ?s ?p ?o }".to_string()),
            subscription_id: None,
            filters: Some(SubscriptionFilters {
                min_results: Some(1),
                max_results: Some(100),
                graph_filter: None,
                update_threshold_ms: Some(1000),
            }),
        };

        let json = serde_json::to_string(&request);
        assert!(json.is_ok());
    }
}