clasp-config-defra 4.5.0

DefraDB-backed CLASP configuration storage with P2P sync and version history
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
//! Main DefraDB configuration store.

use clasp_journal_defra::DefraClient;
use tracing::{debug, warn};
use uuid::Uuid;

use crate::convert;
use crate::error::{ConfigDefraError, Result};
use crate::schema;
use crate::types::*;

/// DefraDB-backed configuration store for CLASP.
///
/// Provides CRUD operations for router, connection, bridge, and rule
/// configs, plus full-config snapshots for versioning. All data is
/// stored in DefraDB and automatically synced across peers via
/// Merkle CRDTs.
pub struct DefraConfigStore {
    client: DefraClient,
    /// Optional secp256k1 identity for ACP-protected mutations.
    /// When set, all mutations include an Authorization header.
    identity: Option<String>,
}

impl DefraConfigStore {
    /// Create a new config store, provisioning all schemas.
    ///
    /// Schemas are provisioned idempotently -- calling this multiple
    /// times against the same DefraDB instance is safe.
    pub async fn new(defra_url: &str) -> Result<Self> {
        let client = DefraClient::new(defra_url);

        // Provision all config schemas
        for sdl in schema::ALL_SCHEMAS {
            client
                .add_schema(sdl)
                .await
                .map_err(|e| ConfigDefraError::Schema(e.to_string()))?;
        }

        debug!("Config schemas provisioned");
        Ok(Self {
            client,
            identity: None,
        })
    }

    /// Set the secp256k1 identity for ACP-protected operations.
    ///
    /// When set, all mutations and queries include this identity in the
    /// Authorization header. Documents created with an identity are
    /// private to the creator by default (ACP enforcement).
    pub fn set_identity(&mut self, identity: String) {
        self.identity = Some(identity);
    }

    /// Clear the ACP identity. Subsequent operations are unauthenticated.
    pub fn clear_identity(&mut self) {
        self.identity = None;
    }

    /// Create a config store with ACP-protected schemas.
    ///
    /// Registers the CLASP ACP policy with DefraDB and provisions
    /// the @policy-annotated schemas. The identity must be a secp256k1
    /// hex private key for DefraDB ACP authentication.
    ///
    /// Schemas are provisioned idempotently.
    pub async fn new_with_acp(defra_url: &str, identity: &str) -> Result<Self> {
        let client = DefraClient::new(defra_url);

        // Register the ACP policy
        let policy_id = client
            .add_policy(crate::policy::CLASP_ACP_POLICY, identity)
            .await
            .map_err(|e| ConfigDefraError::Schema(format!("policy registration failed: {e}")))?;

        debug!(policy_id = %policy_id, "ACP policy registered");

        // Provision ACP-enabled schemas with the policy ID substituted
        for schema_template in crate::policy::ALL_SCHEMAS_ACP {
            let sdl = crate::policy::resolve_policy_id(schema_template, &policy_id);
            client
                .add_schema(&sdl)
                .await
                .map_err(|e| ConfigDefraError::Schema(e.to_string()))?;
        }

        debug!("ACP-enabled config schemas provisioned");
        Ok(Self {
            client,
            identity: Some(identity.to_string()),
        })
    }

    // -- Router configs -------------------------------------------------------

    /// Save a router config (upsert: creates or updates by configId).
    pub async fn save_router(&self, config: &RouterConfig) -> Result<()> {
        if let Some(doc_id) = self
            .find_doc_id("ClaspRouterConfig", &config.config_id)
            .await?
        {
            let mutation = convert::router_to_update_mutation(&doc_id, config);
            self.execute_mutation(&mutation).await?;
            debug!(config_id = %config.config_id, "Updated router config");
        } else {
            let mutation = convert::router_to_create_mutation(config);
            self.execute_mutation(&mutation).await?;
            debug!(config_id = %config.config_id, "Created router config");
        }
        Ok(())
    }

    /// Get a router config by configId.
    pub async fn get_router(&self, config_id: &str) -> Result<Option<RouterConfig>> {
        let query = format!(
            r#"query {{
                ClaspRouterConfig(filter: {{configId: {{_eq: "{id}"}}}}) {{
                    {fields}
                }}
            }}"#,
            id = gql_escape(config_id),
            fields = ROUTER_FIELDS,
        );

        let data = self.execute_query(&query).await?;
        let docs = extract_array(&data, "ClaspRouterConfig");
        match docs.first() {
            Some(doc) => Ok(Some(convert::router_from_doc(doc)?)),
            None => Ok(None),
        }
    }

    /// List all router configs.
    pub async fn list_routers(&self) -> Result<Vec<RouterConfig>> {
        let query = format!(
            r#"query {{
                ClaspRouterConfig {{
                    {fields}
                }}
            }}"#,
            fields = ROUTER_FIELDS,
        );

        let data = self.execute_query(&query).await?;
        let docs = extract_array(&data, "ClaspRouterConfig");
        docs.iter().map(|d| convert::router_from_doc(d)).collect()
    }

    /// List router configs owned by the given owner.
    pub async fn list_routers_by_owner(&self, owner: &str) -> Result<Vec<RouterConfig>> {
        let query = format!(
            r#"query {{
                ClaspRouterConfig(filter: {{owner: {{_eq: "{owner}"}}}}) {{
                    {fields}
                }}
            }}"#,
            owner = gql_escape(owner),
            fields = ROUTER_FIELDS,
        );

        let data = self.execute_query(&query).await?;
        let docs = extract_array(&data, "ClaspRouterConfig");
        docs.iter().map(|d| convert::router_from_doc(d)).collect()
    }

    /// Delete a router config by configId. Returns true if found and deleted.
    pub async fn delete_router(&self, config_id: &str) -> Result<bool> {
        self.delete_by_config_id("ClaspRouterConfig", config_id)
            .await
    }

    // -- Connection configs ---------------------------------------------------

    /// Save a connection config (upsert).
    pub async fn save_connection(&self, config: &ConnectionConfig) -> Result<()> {
        if let Some(doc_id) = self
            .find_doc_id("ClaspConnectionConfig", &config.config_id)
            .await?
        {
            let mutation = convert::connection_to_update_mutation(&doc_id, config);
            self.execute_mutation(&mutation).await?;
            debug!(config_id = %config.config_id, "Updated connection config");
        } else {
            let mutation = convert::connection_to_create_mutation(config);
            self.execute_mutation(&mutation).await?;
            debug!(config_id = %config.config_id, "Created connection config");
        }
        Ok(())
    }

    /// Get a connection config by configId.
    pub async fn get_connection(&self, config_id: &str) -> Result<Option<ConnectionConfig>> {
        let query = format!(
            r#"query {{
                ClaspConnectionConfig(filter: {{configId: {{_eq: "{id}"}}}}) {{
                    {fields}
                }}
            }}"#,
            id = gql_escape(config_id),
            fields = CONNECTION_FIELDS,
        );

        let data = self.execute_query(&query).await?;
        let docs = extract_array(&data, "ClaspConnectionConfig");
        match docs.first() {
            Some(doc) => Ok(Some(convert::connection_from_doc(doc)?)),
            None => Ok(None),
        }
    }

    /// List all connection configs.
    pub async fn list_connections(&self) -> Result<Vec<ConnectionConfig>> {
        let query = format!(
            r#"query {{
                ClaspConnectionConfig {{
                    {fields}
                }}
            }}"#,
            fields = CONNECTION_FIELDS,
        );

        let data = self.execute_query(&query).await?;
        let docs = extract_array(&data, "ClaspConnectionConfig");
        docs.iter()
            .map(|d| convert::connection_from_doc(d))
            .collect()
    }

    /// Delete a connection config by configId.
    pub async fn delete_connection(&self, config_id: &str) -> Result<bool> {
        self.delete_by_config_id("ClaspConnectionConfig", config_id)
            .await
    }

    // -- Bridge configs -------------------------------------------------------

    /// Save a bridge config (upsert).
    pub async fn save_bridge(&self, config: &BridgeConfig) -> Result<()> {
        if let Some(doc_id) = self
            .find_doc_id("ClaspBridgeConfig", &config.config_id)
            .await?
        {
            let mutation = convert::bridge_to_update_mutation(&doc_id, config);
            self.execute_mutation(&mutation).await?;
            debug!(config_id = %config.config_id, "Updated bridge config");
        } else {
            let mutation = convert::bridge_to_create_mutation(config);
            self.execute_mutation(&mutation).await?;
            debug!(config_id = %config.config_id, "Created bridge config");
        }
        Ok(())
    }

    /// Get a bridge config by configId.
    pub async fn get_bridge(&self, config_id: &str) -> Result<Option<BridgeConfig>> {
        let query = format!(
            r#"query {{
                ClaspBridgeConfig(filter: {{configId: {{_eq: "{id}"}}}}) {{
                    {fields}
                }}
            }}"#,
            id = gql_escape(config_id),
            fields = BRIDGE_FIELDS,
        );

        let data = self.execute_query(&query).await?;
        let docs = extract_array(&data, "ClaspBridgeConfig");
        match docs.first() {
            Some(doc) => Ok(Some(convert::bridge_from_doc(doc)?)),
            None => Ok(None),
        }
    }

    /// List all bridge configs.
    pub async fn list_bridges(&self) -> Result<Vec<BridgeConfig>> {
        let query = format!(
            r#"query {{
                ClaspBridgeConfig {{
                    {fields}
                }}
            }}"#,
            fields = BRIDGE_FIELDS,
        );

        let data = self.execute_query(&query).await?;
        let docs = extract_array(&data, "ClaspBridgeConfig");
        docs.iter().map(|d| convert::bridge_from_doc(d)).collect()
    }

    /// Delete a bridge config by configId.
    pub async fn delete_bridge(&self, config_id: &str) -> Result<bool> {
        self.delete_by_config_id("ClaspBridgeConfig", config_id)
            .await
    }

    // -- Rule configs ---------------------------------------------------------

    /// Save a rule config (upsert).
    pub async fn save_rule(&self, config: &RuleConfig) -> Result<()> {
        if let Some(doc_id) = self
            .find_doc_id("ClaspRuleConfig", &config.config_id)
            .await?
        {
            let mutation = convert::rule_to_update_mutation(&doc_id, config);
            self.execute_mutation(&mutation).await?;
            debug!(config_id = %config.config_id, "Updated rule config");
        } else {
            let mutation = convert::rule_to_create_mutation(config);
            self.execute_mutation(&mutation).await?;
            debug!(config_id = %config.config_id, "Created rule config");
        }
        Ok(())
    }

    /// Get a rule config by configId.
    pub async fn get_rule(&self, config_id: &str) -> Result<Option<RuleConfig>> {
        let query = format!(
            r#"query {{
                ClaspRuleConfig(filter: {{configId: {{_eq: "{id}"}}}}) {{
                    {fields}
                }}
            }}"#,
            id = gql_escape(config_id),
            fields = RULE_FIELDS,
        );

        let data = self.execute_query(&query).await?;
        let docs = extract_array(&data, "ClaspRuleConfig");
        match docs.first() {
            Some(doc) => Ok(Some(convert::rule_from_doc(doc)?)),
            None => Ok(None),
        }
    }

    /// List all rule configs.
    pub async fn list_rules(&self) -> Result<Vec<RuleConfig>> {
        let query = format!(
            r#"query {{
                ClaspRuleConfig {{
                    {fields}
                }}
            }}"#,
            fields = RULE_FIELDS,
        );

        let data = self.execute_query(&query).await?;
        let docs = extract_array(&data, "ClaspRuleConfig");
        docs.iter().map(|d| convert::rule_from_doc(d)).collect()
    }

    /// Delete a rule config by configId.
    pub async fn delete_rule(&self, config_id: &str) -> Result<bool> {
        self.delete_by_config_id("ClaspRuleConfig", config_id).await
    }

    // -- Snapshots ------------------------------------------------------------

    /// Save a full configuration snapshot.
    pub async fn save_snapshot(&self, snapshot: &ConfigSnapshot) -> Result<()> {
        let mutation = convert::snapshot_to_create_mutation(snapshot)?;
        self.execute_mutation(&mutation).await?;
        debug!(snapshot_id = %snapshot.snapshot_id, "Saved config snapshot");
        Ok(())
    }

    /// Get a snapshot by snapshotId.
    pub async fn get_snapshot(&self, snapshot_id: &str) -> Result<Option<ConfigSnapshot>> {
        let query = format!(
            r#"query {{
                ClaspConfigSnapshot(filter: {{snapshotId: {{_eq: "{id}"}}}}) {{
                    {fields}
                }}
            }}"#,
            id = gql_escape(snapshot_id),
            fields = SNAPSHOT_FIELDS,
        );

        let data = self.execute_query(&query).await?;
        let docs = extract_array(&data, "ClaspConfigSnapshot");
        match docs.first() {
            Some(doc) => Ok(Some(convert::snapshot_from_doc(doc)?)),
            None => Ok(None),
        }
    }

    /// List all snapshots, ordered by createdAt descending.
    pub async fn list_snapshots(&self) -> Result<Vec<ConfigSnapshot>> {
        let query = format!(
            r#"query {{
                ClaspConfigSnapshot(order: {{createdAt: DESC}}) {{
                    {fields}
                }}
            }}"#,
            fields = SNAPSHOT_FIELDS,
        );

        let data = self.execute_query(&query).await?;
        let docs = extract_array(&data, "ClaspConfigSnapshot");
        docs.iter().map(|d| convert::snapshot_from_doc(d)).collect()
    }

    /// Get the most recent snapshot.
    pub async fn latest_snapshot(&self) -> Result<Option<ConfigSnapshot>> {
        let query = format!(
            r#"query {{
                ClaspConfigSnapshot(order: {{createdAt: DESC}}, limit: 1) {{
                    {fields}
                }}
            }}"#,
            fields = SNAPSHOT_FIELDS,
        );

        let data = self.execute_query(&query).await?;
        let docs = extract_array(&data, "ClaspConfigSnapshot");
        match docs.first() {
            Some(doc) => Ok(Some(convert::snapshot_from_doc(doc)?)),
            None => Ok(None),
        }
    }

    // -- Import/Export --------------------------------------------------------

    /// Import a JSON config string (compatible with the bridge app's file
    /// format) and store it as individual configs plus a snapshot.
    ///
    /// Returns the snapshot that was created from the import.
    pub async fn import_json(&self, json: &str, owner: &str) -> Result<ConfigSnapshot> {
        let snapshot: ConfigSnapshot = serde_json::from_str(json)
            .map_err(|e| ConfigDefraError::Deserialization(format!("invalid config JSON: {e}")))?;

        // Save individual configs
        for router in &snapshot.routers {
            self.save_router(router).await?;
        }
        for conn in &snapshot.connections {
            self.save_connection(conn).await?;
        }
        for bridge in &snapshot.bridges {
            self.save_bridge(bridge).await?;
        }
        for rule in &snapshot.rules {
            self.save_rule(rule).await?;
        }

        // Create a snapshot record for the import
        let import_snapshot = ConfigSnapshot {
            snapshot_id: Uuid::new_v4().to_string(),
            name: format!("Import: {}", snapshot.name),
            description: format!("Imported from JSON by {owner}"),
            routers: snapshot.routers,
            connections: snapshot.connections,
            bridges: snapshot.bridges,
            rules: snapshot.rules,
            owner: owner.into(),
            created_at: chrono::Utc::now().timestamp() as u64,
        };

        self.save_snapshot(&import_snapshot).await?;
        debug!(owner = %owner, "Imported config from JSON");
        Ok(import_snapshot)
    }

    /// Export the current config state as a JSON string compatible with
    /// the bridge app's file format.
    pub async fn export_json(&self) -> Result<String> {
        let routers = self.list_routers().await?;
        let connections = self.list_connections().await?;
        let bridges = self.list_bridges().await?;
        let rules = self.list_rules().await?;

        let snapshot = ConfigSnapshot {
            snapshot_id: Uuid::new_v4().to_string(),
            name: "Export".into(),
            description: "Exported from DefraDB config store".into(),
            routers,
            connections,
            bridges,
            rules,
            owner: String::new(),
            created_at: chrono::Utc::now().timestamp() as u64,
        };

        let json = serde_json::to_string_pretty(&snapshot)?;
        Ok(json)
    }

    // -- History --------------------------------------------------------------

    /// Query DefraDB's commit history for a specific config document.
    ///
    /// Uses DefraDB's `_commits` introspection to retrieve the Merkle
    /// DAG history for the document identified by `config_id` in the
    /// given `collection`.
    pub async fn config_history(
        &self,
        config_id: &str,
        collection: &str,
    ) -> Result<Vec<serde_json::Value>> {
        Self::validate_collection(collection)?;

        // First find the docID for this configId
        let doc_id = self.find_doc_id(collection, config_id).await?;
        let doc_id = match doc_id {
            Some(id) => id,
            None => return Ok(Vec::new()),
        };

        let query = format!(
            r#"query {{
                commits(docID: "{doc_id}", order: {{height: DESC}}) {{
                    cid
                    height
                    delta
                    links {{
                        cid
                        name
                    }}
                }}
            }}"#,
            doc_id = gql_escape(&doc_id),
        );

        let data = self.execute_query(&query).await?;
        let commits = extract_array(&data, "commits");
        Ok(commits)
    }

    // -- Internal helpers -----------------------------------------------------

    /// Allowed collection names for GraphQL interpolation.
    const VALID_COLLECTIONS: &'static [&'static str] = &[
        "ClaspRouterConfig",
        "ClaspConnectionConfig",
        "ClaspBridgeConfig",
        "ClaspRuleConfig",
        "ClaspConfigSnapshot",
    ];

    /// Validate that `collection` is in the whitelist before interpolating
    /// into a GraphQL query.
    fn validate_collection(collection: &str) -> Result<()> {
        if !Self::VALID_COLLECTIONS.contains(&collection) {
            return Err(ConfigDefraError::InvalidConfig(format!(
                "unknown collection: {}",
                collection
            )));
        }
        Ok(())
    }

    /// Find the DefraDB _docID for a document with the given configId.
    async fn find_doc_id(&self, collection: &str, config_id: &str) -> Result<Option<String>> {
        Self::validate_collection(collection)?;
        let query = format!(
            r#"query {{
                {collection}(filter: {{configId: {{_eq: "{id}"}}}}) {{
                    _docID
                }}
            }}"#,
            collection = collection,
            id = gql_escape(config_id),
        );

        let data = self.execute_query(&query).await?;
        let docs = extract_array(&data, collection);
        Ok(docs
            .first()
            .and_then(|d| d.get("_docID"))
            .and_then(|v| v.as_str())
            .map(String::from))
    }

    /// Delete a document by configId from a collection.
    async fn delete_by_config_id(&self, collection: &str, config_id: &str) -> Result<bool> {
        let doc_id = match self.find_doc_id(collection, config_id).await? {
            Some(id) => id,
            None => return Ok(false),
        };

        let mutation = format!(
            r#"mutation {{
                delete_{collection}(docID: "{doc_id}") {{
                    _docID
                }}
            }}"#,
            collection = collection,
            doc_id = gql_escape(&doc_id),
        );

        match self.execute_mutation(&mutation).await {
            Ok(_) => {
                debug!(config_id = %config_id, collection = %collection, "Deleted config");
                Ok(true)
            }
            Err(e) => {
                warn!(config_id = %config_id, error = %e, "Failed to delete config");
                Err(e)
            }
        }
    }

    /// Execute a GraphQL query against DefraDB.
    async fn execute_query(&self, query: &str) -> Result<serde_json::Value> {
        self.client
            .graphql_with_identity(query, None, self.identity.as_deref())
            .await
            .map_err(|e| ConfigDefraError::GraphQL(e.to_string()))
    }

    /// Execute a GraphQL mutation against DefraDB.
    async fn execute_mutation(&self, mutation: &str) -> Result<serde_json::Value> {
        self.client
            .graphql_with_identity(mutation, None, self.identity.as_deref())
            .await
            .map_err(|e| ConfigDefraError::GraphQL(e.to_string()))
    }
}

// -- Field selection constants ------------------------------------------------

const ROUTER_FIELDS: &str =
    "configId name host port transports securityMode maxSessions paramTtlSecs features owner updatedAt version";

const CONNECTION_FIELDS: &str =
    "configId name routerUrl transport token reconnect features owner updatedAt version";

const BRIDGE_FIELDS: &str =
    "configId name protocol sourceAddr targetAddr mappings active owner updatedAt version";

const RULE_FIELDS: &str =
    "configId name trigger conditions actions cooldownSecs enabled owner updatedAt version";

const SNAPSHOT_FIELDS: &str =
    "snapshotId name description routers connections bridges rules owner createdAt";

/// Escape a string for inline use in a GraphQL query.
fn gql_escape(s: &str) -> String {
    s.replace('\\', "\\\\").replace('"', "\\\"")
}

/// Extract an array from a GraphQL data response.
fn extract_array(data: &serde_json::Value, key: &str) -> Vec<serde_json::Value> {
    data.get(key)
        .and_then(|v| v.as_array())
        .cloned()
        .unwrap_or_default()
}

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

    fn uid() -> String {
        uuid::Uuid::new_v4().to_string()[..8].to_string()
    }

    // -- Integration tests (require a running DefraDB instance) ---------------

    #[tokio::test]
    #[ignore]
    async fn test_save_and_get_router() {
        let store = DefraConfigStore::new("http://localhost:9181")
            .await
            .expect("DefraDB must be running");

        let id = format!("test-r-{}", uid());
        let config = RouterConfig {
            config_id: id.clone(),
            name: "Integration Test Router".into(),
            host: "127.0.0.1".into(),
            port: 9200,
            transports: vec!["websocket".into(), "quic".into()],
            security_mode: "tls".into(),
            max_sessions: 64,
            param_ttl_secs: 120,
            features: vec!["federation".into()],
            owner: "test-owner".into(),
            updated_at: 1700000000,
            version: 1,
        };

        store.save_router(&config).await.unwrap();
        let loaded = store.get_router(&id).await.unwrap();
        assert!(loaded.is_some());
        let loaded = loaded.unwrap();
        assert_eq!(loaded.name, "Integration Test Router");
        assert_eq!(loaded.port, 9200);

        // Update
        let mut updated = config.clone();
        updated.port = 9300;
        updated.version = 2;
        store.save_router(&updated).await.unwrap();
        let loaded = store.get_router(&id).await.unwrap().unwrap();
        assert_eq!(loaded.port, 9300);
        assert_eq!(loaded.version, 2);

        // Cleanup
        store.delete_router(&id).await.unwrap();
    }

    #[tokio::test]
    #[ignore]
    async fn test_list_by_owner() {
        let store = DefraConfigStore::new("http://localhost:9181")
            .await
            .expect("DefraDB must be running");

        let id_a = format!("owner-a-{}", uid());
        let id_b = format!("owner-b-{}", uid());
        let owner_a = format!("alpha-{}", uid());
        let owner_b = format!("beta-{}", uid());
        let config_a = RouterConfig::new(&id_a, "Router A1", &owner_a);
        let config_b = RouterConfig::new(&id_b, "Router B1", &owner_b);

        store.save_router(&config_a).await.unwrap();
        store.save_router(&config_b).await.unwrap();

        let alpha_routers = store.list_routers_by_owner(&owner_a).await.unwrap();
        assert!(alpha_routers.iter().any(|r| r.config_id == id_a));
        assert!(!alpha_routers.iter().any(|r| r.config_id == id_b));

        // Cleanup
        store.delete_router(&id_a).await.unwrap();
        store.delete_router(&id_b).await.unwrap();
    }

    #[tokio::test]
    #[ignore]
    async fn test_snapshot_save_and_load() {
        let store = DefraConfigStore::new("http://localhost:9181")
            .await
            .expect("DefraDB must be running");

        let snap_id = format!("snap-{}", uid());
        let snapshot = ConfigSnapshot {
            snapshot_id: snap_id.clone(),
            name: "Test Snapshot".into(),
            description: "Integration test".into(),
            routers: vec![RouterConfig::new(&format!("sr-{}", uid()), "R1", "test")],
            connections: vec![ConnectionConfig::new(
                &format!("sc-{}", uid()),
                "C1",
                "test",
            )],
            bridges: vec![],
            rules: vec![],
            owner: "test".into(),
            created_at: 1700000000,
        };

        store.save_snapshot(&snapshot).await.unwrap();
        let loaded = store.get_snapshot(&snap_id).await.unwrap();
        assert!(loaded.is_some());
        let loaded = loaded.unwrap();
        assert_eq!(loaded.name, "Test Snapshot");
        assert_eq!(loaded.routers.len(), 1);
        assert_eq!(loaded.connections.len(), 1);
    }

    #[tokio::test]
    #[ignore]
    async fn test_import_export() {
        let store = DefraConfigStore::new("http://localhost:9181")
            .await
            .expect("DefraDB must be running");

        let rid = format!("imp-r-{}", uid());
        let original = ConfigSnapshot {
            snapshot_id: format!("imp-{}", uid()),
            name: "Import Test".into(),
            description: "Testing import/export".into(),
            routers: vec![RouterConfig::new(&rid, "Imported Router", "importer")],
            connections: vec![],
            bridges: vec![],
            rules: vec![],
            owner: "importer".into(),
            created_at: 1700000000,
        };

        let json = serde_json::to_string_pretty(&original).unwrap();
        let imported = store.import_json(&json, "importer").await.unwrap();
        assert_eq!(imported.routers.len(), 1);

        // Verify the router was saved individually
        let router = store.get_router(&rid).await.unwrap();
        assert!(router.is_some());
        assert_eq!(router.unwrap().name, "Imported Router");

        // Cleanup
        store.delete_router(&rid).await.unwrap();
    }

    #[tokio::test]
    #[ignore]
    async fn test_delete() {
        let store = DefraConfigStore::new("http://localhost:9181")
            .await
            .expect("DefraDB must be running");

        let cid = format!("del-c-{}", uid());
        let config = ConnectionConfig::new(&cid, "Delete Me", "test");
        store.save_connection(&config).await.unwrap();

        let deleted = store.delete_connection(&cid).await.unwrap();
        assert!(deleted);

        let loaded = store.get_connection(&cid).await.unwrap();
        assert!(loaded.is_none());

        // Deleting again should return false
        let deleted_again = store.delete_connection(&cid).await.unwrap();
        assert!(!deleted_again);
    }
}