datafold 0.1.55

A personal database for data sovereignty with AI-powered ingestion
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
//! Simplified ingestion service that works with DataFoldNode's existing interface
//!
//! Now refactored to take &DataFoldNode references, enabling usage with both Mutex and RwLock.

use crate::datafold_node::DataFoldNode;
use crate::ingestion::config::AIProvider;
use crate::ingestion::core::IngestionRequest;
use crate::ingestion::mutation_generator::MutationGenerator;
use crate::ingestion::ollama_service::OllamaService;
use crate::ingestion::openrouter_service::OpenRouterService;
use crate::ingestion::progress::{IngestionResults, IngestionStep, ProgressService};
use crate::ingestion::{
    AISchemaResponse, IngestionConfig, IngestionError, IngestionResponse, IngestionResult,
    IngestionStatus, SimplifiedSchema, SimplifiedSchemaMap,
};
use crate::log_feature;
use crate::logging::features::LogFeature;
use crate::schema::types::Mutation;
use serde_json::Value;
use std::collections::HashMap;
use std::sync::Arc;
use std::time::{Duration, Instant};
use tokio::sync::Mutex;

/// Schema cache entry
#[derive(Debug, Clone)]
struct SchemaCacheEntry {
    schemas: SimplifiedSchemaMap,
    timestamp: Instant,
}

/// Simplified ingestion service that works with DataFoldNode
pub struct SimpleIngestionService {
    config: IngestionConfig,
    openrouter_service: Option<OpenRouterService>,
    ollama_service: Option<OllamaService>,
    mutation_generator: MutationGenerator,
    schema_cache: Arc<Mutex<Option<SchemaCacheEntry>>>,
}

impl SimpleIngestionService {
    /// Create a new simple ingestion service
    pub fn new(config: IngestionConfig) -> IngestionResult<Self> {
        let openrouter_service = if config.provider == AIProvider::OpenRouter {
            Some(OpenRouterService::new(
                config.openrouter.clone(),
                config.timeout_seconds,
                config.max_retries,
            )?)
        } else {
            None
        };

        let ollama_service = if config.provider == AIProvider::Ollama {
            Some(OllamaService::new(
                config.ollama.clone(),
                config.timeout_seconds,
                config.max_retries,
            )?)
        } else {
            None
        };

        let mutation_generator = MutationGenerator::new();

        Ok(Self {
            config,
            openrouter_service,
            ollama_service,
            mutation_generator,
            schema_cache: Arc::new(Mutex::new(None)),
        })
    }

    /// Process JSON ingestion using a DataFoldNode with progress tracking
    /// Accepts a reference to DataFoldNode, making it compatible with both Mutex and RwLock guards
    pub async fn process_json_with_node_and_progress(
        &self,
        request: IngestionRequest,
        node: &DataFoldNode,
        progress_service: &ProgressService,
        progress_id: String,
    ) -> IngestionResult<IngestionResponse> {
        log_feature!(
            LogFeature::Ingestion,
            info,
            "Starting JSON ingestion process with DataFoldNode (progress_id: {})",
            progress_id
        );

        if !self.config.is_ready() {
            progress_service
                .fail_progress(
                    &progress_id,
                    "Ingestion module is not properly configured or disabled".to_string(),
                )
                .await;
            return Ok(IngestionResponse::failure(vec![
                "Ingestion module is not properly configured or disabled".to_string(),
            ]));
        }

        // Step 1: Validate input
        progress_service
            .update_progress(
                &progress_id,
                IngestionStep::ValidatingConfig,
                "Validating input data...".to_string(),
            )
            .await;
        self.validate_input(&request.data)?;

        // Step 2: Get available schemas and strip them
        progress_service
            .update_progress(
                &progress_id,
                IngestionStep::PreparingSchemas,
                "Preparing available schemas...".to_string(),
            )
            .await;
        // Check if we can use the node directly for schemas
        let available_schemas = self.get_stripped_available_schemas_from_node(node).await?;

        // Step 2.5: Flatten data structure for AI analysis
        progress_service
            .update_progress(
                &progress_id,
                IngestionStep::FlatteningData,
                "Processing and flattening data structure...".to_string(),
            )
            .await;
        let flattened_data = self.flatten_twitter_data(&request.data);

        // Step 3: Get AI recommendation
        progress_service
            .update_progress(
                &progress_id,
                IngestionStep::GettingAIRecommendation,
                "Analyzing data with AI to determine schema...".to_string(),
            )
            .await;
        let ai_response = self
            .get_ai_recommendation(&flattened_data, &available_schemas)
            .await?;

        // Step 4: Determine schema to use
        progress_service
            .update_progress(
                &progress_id,
                IngestionStep::SettingUpSchema,
                "Setting up schema and preparing for data storage...".to_string(),
            )
            .await;
        // Check if we'll use an existing schema before calling determine_schema_to_use
        let will_use_existing = !ai_response.existing_schemas.is_empty();
        let schema_name = self
            .determine_schema_to_use(&ai_response, &request.data, node)
            .await?;
        // Only mark as new if AI suggested a new schema AND we didn't use an existing one
        let new_schema_created = ai_response.new_schemas.is_some() && !will_use_existing;

        // Step 5: Generate mutations
        progress_service
            .update_progress(
                &progress_id,
                IngestionStep::GeneratingMutations,
                "Generating database mutations...".to_string(),
            )
            .await;
        // Handle both single objects and arrays of objects
        let mutations = if let Some(array) = flattened_data.as_array() {
            // Generate a mutation for each element in the array
            let total_items = array.len();
            let mut all_mutations = Vec::new();
            for (idx, item) in array.iter().enumerate() {
                let fields_and_values = if let Some(obj) = item.as_object() {
                    obj.iter().map(|(k, v)| (k.clone(), v.clone())).collect()
                } else {
                    log_feature!(
                        LogFeature::Ingestion,
                        warn,
                        "Array item {} is not an object, skipping",
                        idx
                    );
                    continue;
                };

                let mutations = self.mutation_generator.generate_mutations(
                    &schema_name,
                    &HashMap::new(),
                    &fields_and_values,
                    &ai_response.mutation_mappers,
                    request
                        .trust_distance
                        .unwrap_or(self.config.default_trust_distance),
                    request
                        .pub_key
                        .clone()
                        .unwrap_or_else(|| "default".to_string()),
                    request.source_file_name.clone(),
                )?;

                all_mutations.extend(mutations);

                // Update progress every 10 items
                if (idx + 1) % 10 == 0 || idx + 1 == total_items {
                    let percent_of_step = ((idx + 1) as f32 / total_items as f32 * 15.0) as u8;
                    let progress_percent = 75 + percent_of_step;
                    progress_service
                        .update_progress_with_percentage(
                            &progress_id,
                            IngestionStep::GeneratingMutations,
                            format!("Generating mutations... ({}/{})", idx + 1, total_items),
                            progress_percent,
                        )
                        .await;
                }
            }

            all_mutations
        } else {
            // Handle single object
            let fields_and_values = if let Some(obj) = flattened_data.as_object() {
                obj.iter().map(|(k, v)| (k.clone(), v.clone())).collect()
            } else {
                HashMap::new()
            };

            self.mutation_generator.generate_mutations(
                &schema_name,
                &HashMap::new(),
                &fields_and_values,
                &ai_response.mutation_mappers,
                request
                    .trust_distance
                    .unwrap_or(self.config.default_trust_distance),
                request
                    .pub_key
                    .clone()
                    .unwrap_or_else(|| "default".to_string()),
                request.source_file_name.clone(),
            )?
        };

        log_feature!(
            LogFeature::Ingestion,
            info,
            "Generated {} mutations",
            mutations.len()
        );

        // Step 6: Execute mutations if requested
        progress_service
            .update_progress(
                &progress_id,
                IngestionStep::ExecutingMutations,
                "Executing mutations to store data...".to_string(),
            )
            .await;

        let mutations_len = mutations.len();

        let mutations_executed = if request
            .auto_execute
            .unwrap_or(self.config.auto_execute_mutations)
        {
            self.execute_mutations_with_node_and_progress(
                mutations,
                node,
                progress_service,
                &progress_id,
            )
            .await?
        } else {
            0
        };

        // Mark as completed
        let results = IngestionResults {
            schema_name: schema_name.clone(),
            new_schema_created,
            mutations_generated: mutations_len,
            mutations_executed,
        };
        progress_service
            .complete_progress(&progress_id, results)
            .await;

        Ok(IngestionResponse::success_with_progress(
            progress_id,
            schema_name,
            new_schema_created,
            mutations_len,
            mutations_executed,
        ))
    }

    /// Process JSON ingestion using a DataFoldNode (original method for backward compatibility)
    pub async fn process_json_with_node(
        &self,
        request: IngestionRequest,
        node: &DataFoldNode,
    ) -> IngestionResult<IngestionResponse> {
        log_feature!(
            LogFeature::Ingestion,
            info,
            "Starting JSON ingestion process with DataFoldNode"
        );

        if !self.config.is_ready() {
            return Ok(IngestionResponse::failure(vec![
                "Ingestion module is not properly configured or disabled".to_string(),
            ]));
        }

        // Step 1: Validate input
        self.validate_input(&request.data)?;

        // Step 2: Flatten Twitter data
        let flattened_data = self.flatten_twitter_data(&request.data);

        // Step 3: Get available schemas and strip them
        let available_schemas = self.get_stripped_available_schemas_from_node(node).await?;

        // Step 4: Get AI recommendation
        let ai_response = self
            .get_ai_recommendation(&flattened_data, &available_schemas)
            .await?;

        // Step 5: Determine schema to use
        // Check if we'll use an existing schema before calling determine_schema_to_use
        let will_use_existing = !ai_response.existing_schemas.is_empty();
        let schema_name = self
            .determine_schema_to_use(&ai_response, &request.data, node)
            .await?;
        // Only mark as new if AI suggested a new schema AND we didn't use an existing one
        let new_schema_created = ai_response.new_schemas.is_some() && !will_use_existing;

        // Step 6: Generate mutations
        // Handle both single objects and arrays of objects
        let mutations = if let Some(array) = flattened_data.as_array() {
            // Generate a mutation for each element in the array

            let mut all_mutations = Vec::new();
            for (idx, item) in array.iter().enumerate() {
                let fields_and_values = if let Some(obj) = item.as_object() {
                    obj.iter().map(|(k, v)| (k.clone(), v.clone())).collect()
                } else {
                    log_feature!(
                        LogFeature::Ingestion,
                        warn,
                        "Array item {} is not an object, skipping",
                        idx
                    );
                    continue;
                };

                let mutations = self.mutation_generator.generate_mutations(
                    &schema_name,
                    &HashMap::new(),
                    &fields_and_values,
                    &ai_response.mutation_mappers,
                    request
                        .trust_distance
                        .unwrap_or(self.config.default_trust_distance),
                    request
                        .pub_key
                        .clone()
                        .unwrap_or_else(|| "default".to_string()),
                    request.source_file_name.clone(),
                )?;

                all_mutations.extend(mutations);
            }

            all_mutations
        } else {
            // Handle single object
            let fields_and_values = if let Some(obj) = flattened_data.as_object() {
                obj.iter().map(|(k, v)| (k.clone(), v.clone())).collect()
            } else {
                HashMap::new()
            };

            self.mutation_generator.generate_mutations(
                &schema_name,
                &HashMap::new(),
                &fields_and_values,
                &ai_response.mutation_mappers,
                request
                    .trust_distance
                    .unwrap_or(self.config.default_trust_distance),
                request
                    .pub_key
                    .clone()
                    .unwrap_or_else(|| "default".to_string()),
                request.source_file_name.clone(),
            )?
        };

        log_feature!(
            LogFeature::Ingestion,
            info,
            "Generated {} mutations",
            mutations.len()
        );

        let mutations_len = mutations.len();

        // Step 7: Execute mutations if requested
        let mutations_executed = if request
            .auto_execute
            .unwrap_or(self.config.auto_execute_mutations)
        {
            self.execute_mutations_with_node(mutations, node).await?
        } else {
            0
        };

        Ok(IngestionResponse::success(
            schema_name,
            new_schema_created,
            mutations_len,
            mutations_executed,
        ))
    }

    /// Get AI schema recommendation
    async fn get_ai_recommendation(
        &self,
        json_data: &Value,
        available_schemas: &SimplifiedSchemaMap,
    ) -> IngestionResult<AISchemaResponse> {
        let schemas_json = available_schemas.to_json_value();
        match self.config.provider {
            AIProvider::OpenRouter => {
                self.openrouter_service
                    .as_ref()
                    .ok_or_else(|| {
                        IngestionError::configuration_error("OpenRouter service not initialized")
                    })?
                    .get_schema_recommendation(json_data, &schemas_json)
                    .await
            }
            AIProvider::Ollama => {
                self.ollama_service
                    .as_ref()
                    .ok_or_else(|| {
                        IngestionError::configuration_error("Ollama service not initialized")
                    })?
                    .get_schema_recommendation(json_data, &schemas_json)
                    .await
            }
        }
    }

    /// Validate JSON input
    pub fn validate_input(&self, data: &Value) -> IngestionResult<()> {
        if data.is_null() {
            return Err(IngestionError::invalid_input("Input data cannot be null"));
        }

        if !data.is_object() && !data.is_array() {
            return Err(IngestionError::invalid_input(
                "Input data must be a JSON object or array",
            ));
        }

        Ok(())
    }

    /// Get status information
    pub fn get_status(&self) -> IngestionResult<IngestionStatus> {
        let (provider_name, model) = match self.config.provider {
            AIProvider::OpenRouter => (
                "OpenRouter".to_string(),
                self.config.openrouter.model.clone(),
            ),
            AIProvider::Ollama => ("Ollama".to_string(), self.config.ollama.model.clone()),
        };

        Ok(IngestionStatus {
            enabled: self.config.enabled,
            configured: self.config.is_ready(),
            provider: provider_name,
            model,
            auto_execute_mutations: self.config.auto_execute_mutations,
            default_trust_distance: self.config.default_trust_distance,
        })
    }

    /// Get available schemas from the schema service via node (with caching)
    async fn get_stripped_available_schemas_from_node(
        &self,
        node: &DataFoldNode,
    ) -> IngestionResult<SimplifiedSchemaMap> {
        const CACHE_TTL: Duration = Duration::from_secs(30); // 30 second cache

        // Check cache first
        {
            let cache_guard = self.schema_cache.lock().await;
            if let Some(cache_entry) = cache_guard.as_ref() {
                if cache_entry.timestamp.elapsed() < CACHE_TTL {
                    log_feature!(
                        LogFeature::Ingestion,
                        info,
                        "Using cached schemas ({} schemas, {}s old)",
                        cache_entry.schemas.len(),
                        cache_entry.timestamp.elapsed().as_secs()
                    );
                    return Ok(cache_entry.schemas.clone());
                }
            }
        }

        log_feature!(
            LogFeature::Ingestion,
            info,
            "Cache miss or expired, fetching schemas from schema service"
        );

        // Fetch available schemas from the schema service via the node
        let schemas = {
            node.fetch_available_schemas().await.map_err(|e| {
                IngestionError::SchemaSystemError(crate::schema::SchemaError::InvalidData(format!(
                    "Failed to fetch schemas from schema service: {}",
                    e
                )))
            })?
        };

        // Create a simplified schema representation for AI analysis
        let mut schema_map = SimplifiedSchemaMap::new();

        for schema in schemas {
            let mut fields: HashMap<String, Value> = HashMap::new();

            for (field_name, topology) in &schema.field_topologies {
                if let Ok(topology_value) = serde_json::to_value(topology) {
                    fields.insert(field_name.clone(), topology_value);
                }
            }

            let simplified = SimplifiedSchema {
                name: schema.name.clone(),
                fields,
            };

            schema_map.insert(schema.name.clone(), simplified);
        }

        // Update cache
        {
            let mut cache_guard = self.schema_cache.lock().await;
            *cache_guard = Some(SchemaCacheEntry {
                schemas: schema_map.clone(),
                timestamp: Instant::now(),
            });
        }

        log_feature!(
            LogFeature::Ingestion,
            info,
            "Cached {} schemas for future requests",
            schema_map.len()
        );

        Ok(schema_map)
    }

    /// Determine which schema to use based on AI response
    async fn determine_schema_to_use(
        &self,
        ai_response: &AISchemaResponse,
        sample_data: &Value,
        node: &DataFoldNode,
    ) -> IngestionResult<String> {
        // If existing schemas were recommended, use the first one
        if !ai_response.existing_schemas.is_empty() {
            let schema_name = &ai_response.existing_schemas[0];
            log_feature!(
                LogFeature::Ingestion,
                info,
                "Using existing schema: {}",
                schema_name
            );

            // Ensure existing schema has topologies - add them if missing
            self.ensure_schema_has_topologies_with_node(
                schema_name,
                sample_data,
                &ai_response.mutation_mappers,
                node,
            )
            .await?;

            // Auto-approve existing schema (idempotent - only approves if not already approved)
            let schema_manager = {
                let db_guard = node
                    .get_fold_db()
                    .await
                    .map_err(|error| IngestionError::SchemaCreationError(error.to_string()))?;
                db_guard.schema_manager.clone()
            };

            schema_manager
                .approve(schema_name)
                .await
                .map_err(|error| IngestionError::SchemaCreationError(error.to_string()))?;

            return Ok(schema_name.clone());
        }

        // If a new schema was provided, create it
        if let Some(new_schema_def) = &ai_response.new_schemas {
            let schema_name = self
                .create_new_schema_with_node(new_schema_def, sample_data, node)
                .await?;
            return Ok(schema_name);
        }

        Err(IngestionError::ai_response_validation_error(
            "AI response contains neither existing schemas nor new schema definition",
        ))
    }

    /// Create a new schema using the DataFoldNode
    async fn create_new_schema_with_node(
        &self,
        schema_def: &Value,
        sample_data: &Value,
        node: &DataFoldNode,
    ) -> IngestionResult<String> {
        // Deserialize Value to Schema
        let mut schema: crate::schema::types::Schema = serde_json::from_value(schema_def.clone())
            .map_err(|error| {
            IngestionError::SchemaCreationError(format!(
                "Failed to deserialize schema from AI response: {}",
                error
            ))
        })?;

        log_feature!(
            LogFeature::Ingestion,
            info,
            "Deserialized schema with {} field topologies from AI",
            schema.field_topologies.len()
        );

        // Only infer if the schema is completely missing topologies
        if schema.field_topologies.is_empty() {
            log_feature!(
                LogFeature::Ingestion,
                warn,
                "AI did not provide field_topologies, inferring from sample data"
            );

            let sample_for_topology = if let Some(array) = sample_data.as_array() {
                array.first().unwrap_or(sample_data)
            } else {
                sample_data
            };

            if let Some(sample_obj) = sample_for_topology.as_object() {
                let sample_map: std::collections::HashMap<String, serde_json::Value> = sample_obj
                    .iter()
                    .map(|(k, v)| (k.clone(), v.clone()))
                    .collect();
                schema.infer_topologies_from_data(&sample_map);
                log_feature!(
                    LogFeature::Ingestion,
                    info,
                    "Inferred topologies for {} fields (no AI topologies)",
                    sample_map.len()
                );
            }
        }

        // Ensure default classifications for String fields (force indexing)
        // This must run AFTER inference to catch inferred fields
        for topology in schema.field_topologies.values_mut() {
            if let crate::schema::types::topology::TopologyNode::Primitive {
                value: crate::schema::types::topology::PrimitiveValueType::String,
                classifications,
            } = &mut topology.root
            {
                if classifications.is_none()
                    || classifications
                        .as_ref()
                        .map(|c| c.is_empty())
                        .unwrap_or(false)
                {
                    *classifications = Some(vec!["word".to_string()]);
                    crate::log_feature!(
                        crate::logging::features::LogFeature::Ingestion,
                        info,
                        "Added default 'word' classification to string field"
                    );
                }
            }
        }

        // Use topology_hash as schema name for structure-based deduplication
        schema.compute_schema_topology_hash();
        let topology_hash = schema
            .get_topology_hash()
            .ok_or_else(|| {
                IngestionError::SchemaCreationError(
                    "Schema must have topology_hash computed".to_string(),
                )
            })?
            .clone();

        schema.name = topology_hash.clone();

        // Add schema to the schema service via the node
        let schema_response = {
            node.add_schema_to_service(&schema).await.map_err(|error| {
                IngestionError::SchemaCreationError(format!(
                    "Failed to create schema via schema service: {}",
                    error
                ))
            })?
        };

        let json_str = serde_json::to_string(&schema_response).map_err(|error| {
            IngestionError::schema_parsing_error(format!(
                "Failed to serialize schema definition: {}",
                error
            ))
        })?;

        let schema_manager = {
            let db_guard = node
                .get_fold_db()
                .await
                .map_err(|error| IngestionError::SchemaCreationError(error.to_string()))?;
            let manager = db_guard.schema_manager.clone();
            drop(db_guard);
            manager
        };

        match schema_manager.load_schema_from_json(&json_str).await {
            Ok(_) => {}
            Err(error) => return Err(IngestionError::SchemaCreationError(error.to_string())),
        };

        // Auto-approve the new schema (idempotent - only approves if not already approved)
        schema_manager
            .approve(&schema_response.name)
            .await
            .map_err(|error| IngestionError::SchemaCreationError(error.to_string()))?;

        Ok(schema_response.name)
    }

    /// Ensure existing schema has topologies for all fields, adding them if missing
    async fn ensure_schema_has_topologies_with_node(
        &self,
        schema_name: &str,
        sample_data: &Value,
        mutation_mappers: &HashMap<String, String>,
        node: &DataFoldNode,
    ) -> IngestionResult<()> {
        // Get the schema from the schema manager
        let mut schema = {
            let db_guard = node
                .get_fold_db()
                .await
                .map_err(|error| IngestionError::SchemaCreationError(error.to_string()))?;
            let schema = db_guard
                .schema_manager
                .get_schema(schema_name)
                .map_err(|e| {
                    IngestionError::SchemaSystemError(crate::schema::SchemaError::InvalidData(
                        format!("Failed to fetch schema '{}': {}", schema_name, e),
                    ))
                })?
                .ok_or_else(|| {
                    IngestionError::SchemaSystemError(crate::schema::SchemaError::InvalidData(
                        format!("Schema '{}' not found", schema_name),
                    ))
                })?
                .clone();
            drop(db_guard);
            schema
        };

        // Check if schema already has all required topologies
        let fields_to_check: Vec<String> = mutation_mappers
            .values()
            .filter_map(|mapper| {
                // Extract field name from mapper (format: SchemaName.field_name)
                mapper.split('.').nth(1).map(|s| s.to_string())
            })
            .collect();

        let mut needs_update = false;
        for field_name in &fields_to_check {
            if !schema.has_field_topology(field_name) {
                needs_update = true;
                log_feature!(
                    LogFeature::Ingestion,
                    info,
                    "Schema '{}' is missing topology for field '{}'",
                    schema_name,
                    field_name
                );
                break;
            } else if let Some(topology) = schema.field_topologies.get(field_name) {
                // Check if it's a string field without "word" classification
                if let crate::schema::types::topology::TopologyNode::Primitive {
                    value: crate::schema::types::topology::PrimitiveValueType::String,
                    classifications,
                } = &topology.root
                {
                    if classifications.is_none()
                        || classifications
                            .as_ref()
                            .map(|c| c.is_empty())
                            .unwrap_or(false)
                    {
                        needs_update = true;
                        log_feature!(
                            LogFeature::Ingestion,
                            info,
                            "Schema '{}' field '{}' is missing 'word' classification",
                            schema_name,
                            field_name
                        );
                        break;
                    }
                }
            }
        }

        // If all fields have topologies, no update needed
        if !needs_update {
            log_feature!(
                LogFeature::Ingestion,
                info,
                "Schema '{}' already has topologies for all required fields",
                schema_name
            );
            return Ok(());
        }

        // Infer topologies from sample data
        let sample_for_topology = if let Some(array) = sample_data.as_array() {
            array.first().unwrap_or(sample_data)
        } else {
            sample_data
        };

        if let Some(sample_obj) = sample_for_topology.as_object() {
            let sample_map: std::collections::HashMap<String, serde_json::Value> = sample_obj
                .iter()
                .map(|(k, v)| (k.clone(), v.clone()))
                .collect();

            schema.infer_topologies_from_data(&sample_map);

            log_feature!(
                LogFeature::Ingestion,
                info,
                "Inferred topologies for {} fields in schema '{}' from sample data",
                sample_map.len(),
                schema_name
            );

            // Ensure default classifications for String fields (force indexing)
            for topology in schema.field_topologies.values_mut() {
                if let crate::schema::types::topology::TopologyNode::Primitive {
                    value: crate::schema::types::topology::PrimitiveValueType::String,
                    classifications,
                } = &mut topology.root
                {
                    if classifications.is_none()
                        || classifications
                            .as_ref()
                            .map(|c| c.is_empty())
                            .unwrap_or(false)
                    {
                        *classifications = Some(vec!["word".to_string()]);
                        crate::log_feature!(
                            crate::logging::features::LogFeature::Ingestion,
                            info,
                            "Added default 'word' classification to string field in existing schema"
                        );
                    }
                }
            }

            // Update the schema in the schema service via node
            {
                node.add_schema_to_service(&schema).await.map_err(|e| {
                    IngestionError::SchemaSystemError(crate::schema::SchemaError::InvalidData(
                        format!(
                            "Failed to update schema '{}' with topologies: {}",
                            schema_name, e
                        ),
                    ))
                })?;
            }

            // Reload the schema
            let json_str = serde_json::to_string(&schema).map_err(|error| {
                IngestionError::schema_parsing_error(format!(
                    "Failed to serialize updated schema: {}",
                    error
                ))
            })?;

            let schema_manager = {
                let db_guard = node
                    .get_fold_db()
                    .await
                    .map_err(|error| IngestionError::SchemaCreationError(error.to_string()))?;
                let manager = db_guard.schema_manager.clone();
                drop(db_guard);
                manager
            };

            match schema_manager.load_schema_from_json(&json_str).await {
                Ok(_) => {}
                Err(error) => return Err(IngestionError::SchemaCreationError(error.to_string())),
            };

            log_feature!(
                LogFeature::Ingestion,
                info,
                "Updated schema '{}' with inferred topologies",
                schema_name
            );
        }

        Ok(())
    }

    /// Execute mutations with progress tracking
    async fn execute_mutations_with_node_and_progress(
        &self,
        mutations: Vec<Mutation>,
        node: &DataFoldNode,
        progress_service: &ProgressService,
        progress_id: &str,
    ) -> IngestionResult<usize> {
        if mutations.is_empty() {
            return Ok(0);
        }

        let total_mutations = mutations.len();

        // Convert mutations to operation format for batch processing
        // Update progress for every 5 items to ensure visibility
        for (idx, _) in mutations.iter().enumerate() {
            // Update progress more frequently (every 5 items) to ensure frontend catches updates
            if (idx + 1) % 5 == 0 || idx + 1 == total_mutations {
                // Calculate progress: 90% base + up to 5% for this step (max 95%, not 100%)
                // 100% is reserved for the Completed step
                let percent_of_step = ((idx + 1) as f32 / total_mutations as f32 * 5.0) as u8;
                let progress_percent = 90 + percent_of_step;
                progress_service
                    .update_progress_with_percentage(
                        progress_id,
                        IngestionStep::ExecutingMutations,
                        format!("Executing mutations... ({}/{})", idx + 1, total_mutations),
                        progress_percent,
                    )
                    .await;
            }
        }

        // Execute all mutations in a batch using DataFoldNode directly
        // This avoids OperationProcessor recursive type conversion (Mutation -> Value -> Mutation)
        node.mutate_batch(mutations)
            .await
            .map(|mutation_ids| mutation_ids.len())
            .map_err(|e| {
                IngestionError::SchemaSystemError(crate::schema::SchemaError::InvalidData(
                    e.to_string(),
                ))
            })
    }

    /// Execute mutations without progress tracking (for backward compatibility)
    async fn execute_mutations_with_node(
        &self,
        mutations: Vec<Mutation>,
        node: &DataFoldNode,
    ) -> IngestionResult<usize> {
        if mutations.is_empty() {
            return Ok(0);
        }

        // Execute all mutations in a batch using DataFoldNode directly
        node.mutate_batch(mutations)
            .await
            .map(|mutation_ids| mutation_ids.len())
            .map_err(|e| {
                IngestionError::SchemaSystemError(crate::schema::SchemaError::InvalidData(
                    e.to_string(),
                ))
            })
    }

    /// Flatten Twitter data structure for AI analysis
    /// Converts nested structures like [{"like": {"tweetId": "...", "fullText": "..."}}]
    /// to flattened structures like [{"tweetId": "...", "fullText": "..."}]
    fn flatten_twitter_data(&self, data: &Value) -> Value {
        if let Some(array) = data.as_array() {
            let flattened_items: Vec<Value> = array
                .iter()
                .map(|item| {
                    if let Some(obj) = item.as_object() {
                        // Handle nested Twitter data structure (e.g., {"like": {...}} or {"following": {...}})
                        if obj.len() == 1 {
                            // If there's only one key, assume it's a wrapper and extract the inner object
                            let (_wrapper_key, inner_value) = obj.iter().next().unwrap();
                            if let Some(inner_obj) = inner_value.as_object() {
                                Value::Object(inner_obj.clone())
                            } else {
                                // Fallback to original structure if inner value is not an object
                                Value::Object(obj.clone())
                            }
                        } else {
                            // Multiple keys, use as-is
                            Value::Object(obj.clone())
                        }
                    } else {
                        item.clone()
                    }
                })
                .collect();

            Value::Array(flattened_items)
        } else if let Some(obj) = data.as_object() {
            // Handle single object case
            if obj.len() == 1 {
                let (wrapper_key, inner_value) = obj.iter().next().unwrap();
                if let Some(inner_obj) = inner_value.as_object() {
                    log_feature!(
                        LogFeature::Ingestion,
                        info,
                        "Flattening Twitter data: extracting inner object from wrapper '{}'",
                        wrapper_key
                    );
                    Value::Object(inner_obj.clone())
                } else {
                    Value::Object(obj.clone())
                }
            } else {
                Value::Object(obj.clone())
            }
        } else {
            data.clone()
        }
    }
}