evenframe_core 0.1.4

Core functionality for Evenframe - TypeScript type generation and database schema synchronization
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
//! Schema comparison module - Database-agnostic core
//!
//! This module provides schema comparison functionality that works across
//! different database providers. Provider-specific implementations are in
//! submodules (surrealdb.rs, sql.rs).

pub mod filter;
#[cfg(feature = "surrealdb")]
pub mod import;
#[cfg(feature = "sql")]
pub mod sql;
#[cfg(feature = "surrealdb")]
pub mod surql;
pub mod types;

// Re-export commonly used types
pub use crate::schemasync::mockmake::MockGenerationConfig;
#[cfg(feature = "surrealdb")]
pub use import::SchemaImporter;
#[cfg(feature = "surrealdb")]
pub use surql::SurrealdbComparator;
pub use types::{
    AccessDefinition, FieldDefinition, ObjectType, PermissionSet, SchemaDefinition, SchemaType,
    TableDefinition,
};

#[cfg(feature = "surrealdb")]
use crate::schemasync::config::{PerformanceConfig, SchemasyncMockGenConfig};
use crate::{
    EvenframeError, Result,
    schemasync::TableConfig,
    types::{FieldType, TaggedUnion, VariantData},
};
#[cfg(feature = "surrealdb")]
use ::surrealdb::Surreal;
#[cfg(feature = "surrealdb")]
use ::surrealdb::engine::remote::http::Client;
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::collections::HashSet;

#[cfg(feature = "schemasync")]
use crate::schemasync::database::types::SchemaExport;

// ============================================================================
// SchemaComparator Trait - Database-agnostic interface
// ============================================================================

/// Trait for database schema comparison strategies
#[cfg(feature = "schemasync")]
#[async_trait::async_trait]
pub trait SchemaComparator: Send + Sync {
    /// Compare the current database schema with the expected schema from Rust structs
    async fn compare_schemas(
        &self,
        tables: &HashMap<String, TableConfig>,
        objects: &HashMap<String, crate::types::StructConfig>,
        enums: &HashMap<String, TaggedUnion>,
    ) -> Result<SchemaChanges>;

    /// Get the current schema from the database
    async fn get_current_schema(&self) -> Result<SchemaExport>;

    /// Check if this comparator supports embedded mode for comparison
    fn supports_embedded_comparison(&self) -> bool;
}

/// Factory function to create the appropriate comparator for a provider
#[cfg(feature = "schemasync")]
pub fn create_comparator<'a>(
    _provider: &'a dyn crate::schemasync::database::DatabaseProvider,
) -> Box<dyn SchemaComparator + 'a> {
    #[cfg(feature = "sql")]
    {
        Box::new(sql::SqlSchemaComparator::new(_provider))
    }
    #[cfg(not(feature = "sql"))]
    {
        panic!("No SQL feature enabled - use SurrealdbComparator directly for SurrealDB")
    }
}

// ============================================================================
// Comparator - Legacy struct for backward compatibility with static methods
// ============================================================================

/// Legacy Comparator struct - provides static comparison methods
/// For SurrealDB-specific comparator, use SurrealdbComparator
pub struct Comparator;

// ============================================================================
// Schema Change Types
// ============================================================================

pub use super::PreservationMode;

/// Types of changes that can occur in an access definition
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub enum AccessChangeType {
    JwtKeyChanged,
    IssuerKeyChanged,
    JwtUrlChanged,
    AuthenticateClauseChanged,
    DurationChanged,
    SigninChanged,
    SignupChanged,
    OtherChange(String),
}

impl AccessChangeType {
    /// Check if this change type is ignorable (e.g., rotating keys)
    pub fn is_ignorable(&self) -> bool {
        matches!(
            self,
            AccessChangeType::JwtKeyChanged | AccessChangeType::IssuerKeyChanged
        )
    }
}

impl std::fmt::Display for AccessChangeType {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            AccessChangeType::JwtKeyChanged => write!(f, "JWT key changed"),
            AccessChangeType::IssuerKeyChanged => write!(f, "Issuer key changed"),
            AccessChangeType::JwtUrlChanged => write!(f, "JWT URL changed"),
            AccessChangeType::AuthenticateClauseChanged => write!(f, "Authenticate clause changed"),
            AccessChangeType::DurationChanged => write!(f, "EvenframeDuration changed"),
            AccessChangeType::SigninChanged => write!(f, "Signin changed"),
            AccessChangeType::SignupChanged => write!(f, "Signup changed"),
            AccessChangeType::OtherChange(msg) => write!(f, "{}", msg),
        }
    }
}

/// Represents changes between two schemas
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SchemaChanges {
    pub new_tables: Vec<String>,
    pub removed_tables: Vec<String>,
    pub modified_tables: Vec<TableChanges>,
    pub new_accesses: Vec<String>,
    pub removed_accesses: Vec<String>,
    pub modified_accesses: Vec<AccessChange>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct AccessChange {
    pub access_name: String,
    pub changes: Vec<AccessChangeType>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TableChanges {
    pub table_name: String,
    pub new_fields: Vec<String>,
    pub removed_fields: Vec<String>,
    pub modified_fields: Vec<FieldChange>,
    pub permission_changed: bool,
    pub schema_type_changed: bool,
    pub new_events: Vec<String>,
    pub removed_events: Vec<String>,
}

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub enum ChangeType {
    Added,
    Removed,
    Modified,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct FieldChange {
    pub field_name: String,
    pub old_type: String,
    pub new_type: String,
    pub change_type: ChangeType,
    pub required_changed: bool,
    pub default_changed: bool,
}

impl SchemaChanges {
    /// Check if a specific field is unchanged
    pub fn is_field_unchanged(&self, table: &str, field: &str) -> bool {
        // If table is new or removed, field is not unchanged
        if self.new_tables.contains(&table.to_string())
            || self.removed_tables.contains(&table.to_string())
        {
            return false;
        }

        // Check modified tables
        for table_change in &self.modified_tables {
            if table_change.table_name == table {
                // If field is new or removed, it's not unchanged
                if table_change.new_fields.contains(&field.to_string())
                    || table_change.removed_fields.contains(&field.to_string())
                {
                    return false;
                }

                // If field is modified, it's not unchanged
                for field_change in &table_change.modified_fields {
                    if field_change.field_name == field {
                        return false;
                    }
                }

                // Field exists in table and is not in any change list
                return true;
            }
        }

        // Table not in modified list, so if it exists in old schema, field is unchanged
        true
    }

    /// Get all fields that need new data generation
    pub fn get_fields_needing_generation(&self, table: &str) -> Vec<String> {
        let mut fields = Vec::new();

        // If table is new, all fields need generation
        if self.new_tables.contains(&table.to_string()) {
            return vec!["*".to_string()]; // Special marker for all fields
        }

        // Find table in modified tables
        for table_change in &self.modified_tables {
            if table_change.table_name == table {
                // Add all new fields
                fields.extend(table_change.new_fields.clone());

                // Optionally add modified fields based on configuration
                // For now, we'll regenerate modified fields
                for field_change in &table_change.modified_fields {
                    fields.push(field_change.field_name.clone());
                }
            }
        }

        fields
    }

    /// Create a summary of changes
    pub fn summary(&self) -> String {
        let mut summary = Vec::new();

        if !self.new_tables.is_empty() {
            summary.push(format!("New tables: {}", self.new_tables.join(", ")));
        }

        if !self.removed_tables.is_empty() {
            summary.push(format!(
                "Removed tables: {}",
                self.removed_tables.join(", ")
            ));
        }

        if !self.modified_tables.is_empty() {
            summary.push(format!(
                "Modified tables: {}",
                self.modified_tables
                    .iter()
                    .map(|t| t.table_name.clone())
                    .collect::<Vec<_>>()
                    .join(", ")
            ));
        }

        for table in &self.modified_tables {
            if !table.new_events.is_empty() {
                summary.push(format!(
                    "New events on {}: {}",
                    table.table_name,
                    table.new_events.join(", ")
                ));
            }
            if !table.removed_events.is_empty() {
                summary.push(format!(
                    "Removed events on {}: {}",
                    table.table_name,
                    table.removed_events.join(", ")
                ));
            }
        }

        if !self.new_accesses.is_empty() {
            summary.push(format!("New accesses: {}", self.new_accesses.join(", ")));
        }

        if !self.removed_accesses.is_empty() {
            summary.push(format!(
                "Removed accesses: {}",
                self.removed_accesses.join(", ")
            ));
        }

        if !self.modified_accesses.is_empty() {
            summary.push(format!(
                "Modified accesses: {}",
                self.modified_accesses
                    .iter()
                    .map(|a| a.access_name.clone())
                    .collect::<Vec<_>>()
                    .join(", ")
            ));
        }

        if summary.is_empty() {
            "No changes detected".to_string()
        } else {
            summary.join("\n")
        }
    }
}

// ============================================================================
// Comparator Static Methods - Database-agnostic comparison logic
// ============================================================================

impl Comparator {
    /// Compare two schemas and return the differences
    pub fn compare(old: &SchemaDefinition, new: &SchemaDefinition) -> Result<SchemaChanges> {
        tracing::debug!("Starting detailed schema comparison");

        let mut changes = SchemaChanges {
            new_tables: Vec::new(),
            removed_tables: Vec::new(),
            modified_tables: Vec::new(),
            new_accesses: Vec::new(),
            removed_accesses: Vec::new(),
            modified_accesses: Vec::new(),
        };

        // Get all table names from both schemas
        let old_tables: HashSet<String> = old.tables.keys().cloned().collect();
        let new_tables: HashSet<String> = new.tables.keys().cloned().collect();

        tracing::trace!(
            old_table_count = old_tables.len(),
            new_table_count = new_tables.len(),
            "Comparing table sets"
        );

        // Find new tables
        for table in new_tables.difference(&old_tables) {
            tracing::trace!(table = %table, "Found new table");
            changes.new_tables.push(table.clone());
        }

        // Find removed tables
        for table in old_tables.difference(&new_tables) {
            tracing::trace!(table = %table, "Found removed table");
            changes.removed_tables.push(table.clone());
        }

        // Find modified tables
        for table in old_tables.intersection(&new_tables) {
            tracing::trace!(table = %table, "Comparing table");
            let (old_table, new_table) = match (old.tables.get(table), new.tables.get(table)) {
                (Some(o), Some(n)) => (o, n),
                _ => {
                    let msg =
                        format!("Table '{table}' present in key set but missing in schema maps");
                    tracing::error!(table = %table, "{}", msg);
                    return Err(EvenframeError::comparison(msg));
                }
            };
            if let Some(table_changes) = Self::compare_tables(table, old_table, new_table)? {
                tracing::trace!(
                    table = %table,
                    new_fields = table_changes.new_fields.len(),
                    removed_fields = table_changes.removed_fields.len(),
                    modified_fields = table_changes.modified_fields.len(),
                    "Table has changes"
                );
                changes.modified_tables.push(table_changes);
            }
        }

        // Also compare edges
        let old_edges: HashSet<String> = old.edges.keys().cloned().collect();
        let new_edges: HashSet<String> = new.edges.keys().cloned().collect();

        for edge in new_edges.difference(&old_edges) {
            changes.new_tables.push(edge.clone());
        }

        for edge in old_edges.difference(&new_edges) {
            changes.removed_tables.push(edge.clone());
        }

        for edge in old_edges.intersection(&new_edges) {
            let (old_edge, new_edge) = match (old.edges.get(edge), new.edges.get(edge)) {
                (Some(o), Some(n)) => (o, n),
                _ => {
                    let msg =
                        format!("Edge '{edge}' present in key set but missing in schema maps");
                    tracing::error!(edge = %edge, "{}", msg);
                    return Err(EvenframeError::comparison(msg));
                }
            };
            if let Some(edge_changes) = Self::compare_tables(edge, old_edge, new_edge)? {
                changes.modified_tables.push(edge_changes);
            }
        }

        // Compare accesses
        let old_access_names: HashSet<String> =
            old.accesses.iter().map(|a| a.name.clone()).collect();
        let new_access_names: HashSet<String> =
            new.accesses.iter().map(|a| a.name.clone()).collect();

        // Find new accesses
        for access_name in new_access_names.difference(&old_access_names) {
            changes.new_accesses.push(access_name.clone());
        }

        // Find removed accesses
        for access_name in old_access_names.difference(&new_access_names) {
            changes.removed_accesses.push(access_name.clone());
        }

        // Find modified accesses
        for access_name in old_access_names.intersection(&new_access_names) {
            let old_access = match old.accesses.iter().find(|a| &a.name == access_name) {
                Some(a) => a,
                None => {
                    let msg = format!(
                        "Access '{access_name}' present in key set but missing in old schema list"
                    );
                    tracing::error!(access = %access_name, "{}", msg);
                    return Err(EvenframeError::comparison(msg));
                }
            };
            let new_access = match new.accesses.iter().find(|a| &a.name == access_name) {
                Some(a) => a,
                None => {
                    let msg = format!(
                        "Access '{access_name}' present in key set but missing in new schema list"
                    );
                    tracing::error!(access = %access_name, "{}", msg);
                    return Err(EvenframeError::comparison(msg));
                }
            };

            if let Some(access_change) = Self::compare_accesses(old_access, new_access) {
                changes.modified_accesses.push(access_change);
            }
        }

        tracing::debug!(
            new_tables = changes.new_tables.len(),
            removed_tables = changes.removed_tables.len(),
            modified_tables = changes.modified_tables.len(),
            new_accesses = changes.new_accesses.len(),
            removed_accesses = changes.removed_accesses.len(),
            modified_accesses = changes.modified_accesses.len(),
            "Schema comparison complete"
        );

        Ok(changes)
    }

    /// Compare two table definitions
    fn compare_tables(
        table_name: &str,
        old_table: &TableDefinition,
        new_table: &TableDefinition,
    ) -> Result<Option<TableChanges>> {
        let mut table_changes = TableChanges {
            table_name: table_name.to_string(),
            new_fields: Vec::new(),
            removed_fields: Vec::new(),
            modified_fields: Vec::new(),
            permission_changed: false,
            schema_type_changed: false,
            new_events: Vec::new(),
            removed_events: Vec::new(),
        };

        // Check schema type change
        if old_table.schema_type != new_table.schema_type {
            table_changes.schema_type_changed = true;
        }

        // Check permission changes
        if old_table.permissions != new_table.permissions {
            table_changes.permission_changed = true;
        }

        // Compare regular fields
        let old_fields: HashSet<String> = old_table.fields.keys().cloned().collect();
        let new_fields: HashSet<String> = new_table.fields.keys().cloned().collect();

        // Find new fields
        for field in new_fields.difference(&old_fields) {
            table_changes.new_fields.push(field.clone());
        }

        // Find removed fields
        for field in old_fields.difference(&new_fields) {
            table_changes.removed_fields.push(field.clone());
        }

        // Find modified fields
        for field in old_fields.intersection(&new_fields) {
            let old_field = match old_table.fields.get(field) {
                Some(f) => f,
                None => {
                    let msg = format!(
                        "Field '{field}' present in old/new intersection but missing in old_table '{}'",
                        table_name
                    );
                    tracing::error!(table = %table_name, field = %field, "{}", msg);
                    return Err(EvenframeError::comparison(msg));
                }
            };
            let new_field = match new_table.fields.get(field) {
                Some(f) => f,
                None => {
                    let msg = format!(
                        "Field '{field}' present in old/new intersection but missing in new_table '{}'",
                        table_name
                    );
                    tracing::error!(table = %table_name, field = %field, "{}", msg);
                    return Err(EvenframeError::comparison(msg));
                }
            };

            // If the field types differ, record a modification without deep recursion
            if old_field.field_type != new_field.field_type {
                // Mark entire field as modified; avoid recursive descent to prevent stack overflows
                table_changes.modified_fields.push(FieldChange {
                    field_name: field.to_string(),
                    old_type: old_field.field_type.to_string(),
                    new_type: new_field.field_type.to_string(),
                    change_type: ChangeType::Modified,
                    required_changed: false,
                    default_changed: false,
                });
            } else {
                // Types are the same, check for other changes (required, default)
                if let Some(field_change) = Self::compare_fields(field, old_field, new_field) {
                    table_changes.modified_fields.push(field_change);
                }
            }
        }

        // Compare array wildcard fields
        let old_wildcard_fields: HashSet<String> =
            old_table.array_wildcard_fields.keys().cloned().collect();
        let new_wildcard_fields: HashSet<String> =
            new_table.array_wildcard_fields.keys().cloned().collect();

        // Find new wildcard fields (these represent new fields)
        for field in new_wildcard_fields.difference(&old_wildcard_fields) {
            table_changes.new_fields.push(format!("{}[*]", field));
        }

        // Find removed wildcard fields
        for field in old_wildcard_fields.difference(&new_wildcard_fields) {
            table_changes.removed_fields.push(format!("{}[*]", field));
        }

        // Find modified wildcard fields
        for field in old_wildcard_fields.intersection(&new_wildcard_fields) {
            let old_wild = match old_table.array_wildcard_fields.get(field) {
                Some(f) => f,
                None => {
                    let msg = format!(
                        "Wildcard field '{field}[*]' present in intersection but missing in old_table '{}'",
                        table_name
                    );
                    tracing::error!(table = %table_name, field = %field, "{}", msg);
                    return Err(EvenframeError::comparison(msg));
                }
            };
            let new_wild = match new_table.array_wildcard_fields.get(field) {
                Some(f) => f,
                None => {
                    let msg = format!(
                        "Wildcard field '{field}[*]' present in intersection but missing in new_table '{}'",
                        table_name
                    );
                    tracing::error!(table = %table_name, field = %field, "{}", msg);
                    return Err(EvenframeError::comparison(msg));
                }
            };
            if let Some(field_change) =
                Self::compare_fields(&format!("{}[*]", field), old_wild, new_wild)
            {
                table_changes.modified_fields.push(field_change);
            }
        }

        // Compare events by statement contents
        let old_events: HashSet<String> = old_table.events.iter().cloned().collect();
        let new_events: HashSet<String> = new_table.events.iter().cloned().collect();

        for event in new_events.difference(&old_events) {
            table_changes.new_events.push(event.clone());
        }

        for event in old_events.difference(&new_events) {
            table_changes.removed_events.push(event.clone());
        }

        // Return None if no changes detected
        if table_changes.new_fields.is_empty()
            && table_changes.removed_fields.is_empty()
            && table_changes.modified_fields.is_empty()
            && !table_changes.permission_changed
            && !table_changes.schema_type_changed
            && table_changes.new_events.is_empty()
            && table_changes.removed_events.is_empty()
        {
            Ok(None)
        } else {
            Ok(Some(table_changes))
        }
    }

    /// Compare two field definitions
    fn compare_fields(
        field_name: &str,
        old_field: &FieldDefinition,
        new_field: &FieldDefinition,
    ) -> Option<FieldChange> {
        let mut changed = false;

        // Check for basic changes first
        let mut basic_change = FieldChange {
            field_name: field_name.to_string(),
            old_type: old_field.field_type.to_string(),
            new_type: new_field.field_type.to_string(),
            change_type: ChangeType::Modified,
            required_changed: false,
            default_changed: false,
        };

        // Check required change
        if old_field.required != new_field.required {
            basic_change.required_changed = true;
            changed = true;
        }

        // Check default value change
        if old_field.default_value != new_field.default_value {
            basic_change.default_changed = true;
            changed = true;
        }

        // Check type change
        if old_field.field_type != new_field.field_type {
            changed = true;
        }

        if changed { Some(basic_change) } else { None }
    }

    /// Deep comparison of object types to find granular changes
    pub fn compare_object_types(
        prefix: &str,
        old_type: &ObjectType,
        new_type: &ObjectType,
    ) -> Vec<FieldChange> {
        let mut changes = Vec::new();

        match (old_type, new_type) {
            // Both are objects - compare fields
            (ObjectType::Object(old_fields), ObjectType::Object(new_fields)) => {
                let old_keys: HashSet<String> = old_fields.keys().cloned().collect();
                let new_keys: HashSet<String> = new_fields.keys().cloned().collect();

                // Find added fields
                for key in new_keys.difference(&old_keys) {
                    let field_path = if prefix.is_empty() {
                        key.clone()
                    } else {
                        format!("{}.{}", prefix, key)
                    };

                    changes.push(FieldChange {
                        field_name: field_path,
                        old_type: String::new(),
                        new_type: new_fields[key].to_string(),
                        change_type: ChangeType::Added,
                        required_changed: false,
                        default_changed: false,
                    });
                }

                // Find removed fields
                for key in old_keys.difference(&new_keys) {
                    let field_path = if prefix.is_empty() {
                        key.clone()
                    } else {
                        format!("{}.{}", prefix, key)
                    };

                    changes.push(FieldChange {
                        field_name: field_path,
                        old_type: old_fields[key].to_string(),
                        new_type: String::new(),
                        change_type: ChangeType::Removed,
                        required_changed: false,
                        default_changed: false,
                    });
                }

                // Compare common fields
                for key in old_keys.intersection(&new_keys) {
                    let field_path = if prefix.is_empty() {
                        key.clone()
                    } else {
                        format!("{}.{}", prefix, key)
                    };

                    let old_field_type = &old_fields[key];
                    let new_field_type = &new_fields[key];

                    if old_field_type != new_field_type {
                        // Recursively compare nested types
                        let nested_changes =
                            Self::compare_object_types(&field_path, old_field_type, new_field_type);
                        if nested_changes.is_empty() {
                            // If no nested changes, the types themselves are different
                            changes.push(FieldChange {
                                field_name: field_path,
                                old_type: old_field_type.to_string(),
                                new_type: new_field_type.to_string(),
                                change_type: ChangeType::Modified,
                                required_changed: false,
                                default_changed: false,
                            });
                        } else {
                            changes.extend(nested_changes);
                        }
                    }
                }
            }
            // Nullable types - unwrap and compare
            (ObjectType::Nullable(old_inner), ObjectType::Nullable(new_inner)) => {
                changes.extend(Self::compare_object_types(prefix, old_inner, new_inner));
            }
            // Different types entirely
            _ => {
                if old_type != new_type {
                    changes.push(FieldChange {
                        field_name: prefix.to_string(),
                        old_type: old_type.to_string(),
                        new_type: new_type.to_string(),
                        change_type: ChangeType::Modified,
                        required_changed: false,
                        default_changed: false,
                    });
                }
            }
        }

        changes
    }

    /// Compare two access definitions
    fn compare_accesses(
        old_access: &AccessDefinition,
        new_access: &AccessDefinition,
    ) -> Option<AccessChange> {
        let mut changes = Vec::new();

        // Compare access type
        if old_access.access_type != new_access.access_type {
            changes.push(AccessChangeType::OtherChange(format!(
                "Access type changed from {:?} to {:?}",
                old_access.access_type, new_access.access_type
            )));
        }

        // Compare database level
        if old_access.database_level != new_access.database_level {
            let old_level = if old_access.database_level {
                "DATABASE"
            } else {
                "NAMESPACE"
            };
            let new_level = if new_access.database_level {
                "DATABASE"
            } else {
                "NAMESPACE"
            };
            changes.push(AccessChangeType::OtherChange(format!(
                "Access level changed from {} to {}",
                old_level, new_level
            )));
        }

        // Compare signup query
        if old_access.signup_query != new_access.signup_query {
            changes.push(AccessChangeType::SignupChanged);
        }

        // Compare signin query
        if old_access.signin_query != new_access.signin_query {
            changes.push(AccessChangeType::SigninChanged);
        }

        // Compare JWT configuration
        if old_access.jwt_algorithm != new_access.jwt_algorithm {
            changes.push(AccessChangeType::OtherChange(format!(
                "JWT algorithm changed from {:?} to {:?}",
                old_access.jwt_algorithm, new_access.jwt_algorithm
            )));
        }

        if old_access.jwt_key != new_access.jwt_key {
            changes.push(AccessChangeType::JwtKeyChanged);
        }

        if old_access.jwt_url != new_access.jwt_url {
            changes.push(AccessChangeType::JwtUrlChanged);
        }

        if old_access.issuer_key != new_access.issuer_key {
            changes.push(AccessChangeType::IssuerKeyChanged);
        }

        // Compare authenticate clause
        if old_access.authenticate != new_access.authenticate {
            changes.push(AccessChangeType::AuthenticateClauseChanged);
        }

        // Compare durations
        if old_access.duration_for_token != new_access.duration_for_token {
            changes.push(AccessChangeType::DurationChanged);
        }

        if old_access.duration_for_session != new_access.duration_for_session {
            changes.push(AccessChangeType::DurationChanged);
        }

        // Compare bearer configuration
        if old_access.bearer_for != new_access.bearer_for {
            changes.push(AccessChangeType::OtherChange(format!(
                "Bearer FOR changed from {:?} to {:?}",
                old_access.bearer_for, new_access.bearer_for
            )));
        }

        if changes.is_empty() {
            None
        } else {
            Some(AccessChange {
                access_name: old_access.name.clone(),
                changes,
            })
        }
    }
}

// ============================================================================
// Helper Functions
// ============================================================================

/// Helper function to collect object type names referenced in a field type
pub fn collect_referenced_objects(
    field_type: &FieldType,
    objects_to_process: &mut Vec<String>,
    enums: &HashMap<String, TaggedUnion>,
) {
    match field_type {
        FieldType::Other(type_name) => {
            // Check if this is an enum
            if let Some(enum_def) = enums.get(type_name) {
                // For enums, we need to collect all variant data types
                for variant in &enum_def.variants {
                    if let Some(variant_data) = &variant.data {
                        match variant_data {
                            VariantData::InlineStruct(enum_struct) => {
                                objects_to_process.push(enum_struct.struct_name.clone())
                            }
                            VariantData::DataStructureRef(referenced_field_type) => {
                                if let FieldType::Other(data) = referenced_field_type {
                                    objects_to_process.push(data.clone());
                                }
                            }
                        }
                    }
                }
            } else {
                // Not an enum, just a regular object/struct
                objects_to_process.push(type_name.clone());
            }
        }
        FieldType::Option(inner) | FieldType::Vec(inner) | FieldType::RecordLink(inner) => {
            collect_referenced_objects(inner, objects_to_process, enums);
        }
        FieldType::Tuple(types) => {
            for t in types {
                collect_referenced_objects(t, objects_to_process, enums);
            }
        }
        FieldType::Struct(fields) => {
            for (_, field_type) in fields {
                collect_referenced_objects(field_type, objects_to_process, enums);
            }
        }
        FieldType::HashMap(key_type, value_type) | FieldType::BTreeMap(key_type, value_type) => {
            collect_referenced_objects(key_type, objects_to_process, enums);
            collect_referenced_objects(value_type, objects_to_process, enums);
        }
        _ => {}
    }
}

// ============================================================================
// Merger - Legacy struct for backward compatibility
// ============================================================================

/// Main entry point for Schemasync Merge functionality
#[cfg(feature = "surrealdb")]
pub struct Merger<'a> {
    pub client: &'a Surreal<Client>,
    pub default_mock_gen_config: SchemasyncMockGenConfig,
    pub performance: PerformanceConfig,
}

#[cfg(feature = "surrealdb")]
impl<'a> Merger<'a> {
    /// Create a new Merger instance
    pub async fn new(
        client: &'a Surreal<Client>,
        default_mock_gen_config: SchemasyncMockGenConfig,
        performance: PerformanceConfig,
    ) -> Result<Self> {
        Ok(Self {
            client,
            default_mock_gen_config,
            performance,
        })
    }

    /// Import schema from production database
    pub async fn import_schema_from_db(&self) -> Result<SchemaDefinition> {
        tracing::debug!("Importing schema from production database");
        let importer = SchemaImporter::new(self.client);
        let schema = importer.import_schema_only().await?;
        tracing::debug!(
            tables = schema.tables.len(),
            edges = schema.edges.len(),
            accesses = schema.accesses.len(),
            "Schema imported"
        );
        Ok(schema)
    }

    /// Generate schema from Rust structs
    pub fn generate_schema_from_structs(
        &self,
        tables: &HashMap<String, TableConfig>,
    ) -> Result<SchemaDefinition> {
        tracing::debug!(
            table_count = tables.len(),
            "Generating schema from Rust structs"
        );
        let schema = SchemaDefinition::from_table_configs(tables)?;
        tracing::debug!(
            tables = schema.tables.len(),
            edges = schema.edges.len(),
            "Schema generated from structs"
        );
        Ok(schema)
    }

    pub fn compare_schemas(
        &self,
        old: &SchemaDefinition,
        new: &SchemaDefinition,
    ) -> Result<SchemaChanges> {
        tracing::debug!("Comparing schemas using legacy method");
        Comparator::compare(old, new)
    }

    /// Export mock data to file
    pub async fn export_mock_data(&self, _file_path: &str) -> Result<()> {
        // Implementation will use the generated statements
        // and write them to the specified file
        todo!("Implement export_mock_data")
    }

    /// Generate preserved data for a specific table
    pub async fn generate_preserved_data(
        &self,
        table_name: &str,
        table_config: &TableConfig,
        mock_config: MockGenerationConfig,
        existing_records: Vec<serde_json::Value>,
        target_count: usize,
        schema_changes: Option<&SchemaChanges>,
    ) -> Vec<serde_json::Value> {
        use serde_json::Value;

        // Determine how many records to preserve vs generate
        let existing_count = existing_records.len();
        let mut result = Vec::new();

        match mock_config.preservation_mode {
            PreservationMode::None => {
                // No preservation - generate all new data
                result = self.generate_new_records(table_name, table_config, target_count);
            }
            PreservationMode::Smart => {
                // Smart preservation - keep unchanged fields, regenerate specified fields
                if existing_count > 0 {
                    // Determine which fields need regeneration
                    let mut fields_to_regenerate = mock_config.regenerate_fields.clone();

                    // If schema changes are provided, add fields that changed
                    if let Some(changes) = schema_changes {
                        // Get fields that need regeneration based on schema changes
                        let schema_fields_needing_generation =
                            changes.get_fields_needing_generation(table_name);

                        // If all fields need generation (new table), regenerate everything
                        if schema_fields_needing_generation.contains(&"*".to_string()) {
                            // Generate all new records for new tables
                            result =
                                self.generate_new_records(table_name, table_config, target_count);
                            return result;
                        }

                        // Add schema-detected fields to the regeneration list
                        for field in schema_fields_needing_generation {
                            if !fields_to_regenerate.contains(&field) {
                                fields_to_regenerate.push(field);
                            }
                        }
                    }

                    for mut record in existing_records {
                        // Regenerate specified fields
                        if let Value::Object(ref mut map) = record {
                            // First, add any new fields that don't exist in the record
                            for field in &table_config.struct_config.fields {
                                if !map.contains_key(&field.field_name) {
                                    // This is a new field, generate value
                                    let new_value = Self::generate_field_value(field, table_config);
                                    map.insert(field.field_name.clone(), new_value);
                                }
                            }

                            // Then, regenerate fields that need it
                            for field_name in &fields_to_regenerate {
                                if let Some(field) = table_config
                                    .struct_config
                                    .fields
                                    .iter()
                                    .find(|f| &f.field_name == field_name)
                                {
                                    // Generate new value for this field
                                    let new_value = Self::generate_field_value(field, table_config);
                                    map.insert(field_name.clone(), new_value);
                                }
                            }
                        }

                        result.push(record);
                    }

                    // Generate additional records if needed
                    if target_count > existing_count {
                        let additional = self.generate_new_records(
                            table_name,
                            table_config,
                            target_count - existing_count,
                        );
                        result.extend(additional);
                    }
                } else {
                    // No existing data or preservation disabled
                    result = self.generate_new_records(table_name, table_config, target_count);
                }
            }
            PreservationMode::Full => {
                // Full preservation - keep all data, only add new fields
                if existing_count > 0 {
                    // Check if target count is less than existing count
                    if target_count < existing_count {
                        eprintln!(
                            "\n  WARNING: Full preservation mode with data reduction detected!"
                        );
                        eprintln!(
                            "   Table '{}' has {} existing records but target count is set to {}",
                            table_name, existing_count, target_count
                        );
                        eprintln!(
                            "   This will DELETE {} records!",
                            existing_count - target_count
                        );
                        eprintln!("\n   Options:");
                        eprintln!(
                            "   1. Change the target count (n) to {} or higher to preserve all records",
                            existing_count
                        );
                        eprintln!("   2. Use Smart preservation mode instead of Full");
                        eprintln!(
                            "   3. Set preservation_mode to None if you want to regenerate all data"
                        );
                        eprintln!(
                            "\n   In a production environment, this would require user confirmation."
                        );
                        eprintln!(
                            "   For now, proceeding with target count of {} records.\n",
                            target_count
                        );

                        for mut record in existing_records {
                            // Only add new fields that don't exist
                            if let Value::Object(ref mut map) = record {
                                for field in &table_config.struct_config.fields {
                                    if !map.contains_key(&field.field_name) {
                                        // This is a new field, generate value
                                        let new_value =
                                            Self::generate_field_value(field, table_config);
                                        map.insert(field.field_name.clone(), new_value);
                                    }
                                }
                            }

                            result.push(record);
                        }
                    } else {
                        // Normal case: preserve all existing records
                        for mut record in existing_records {
                            // Only add new fields that don't exist
                            if let Value::Object(ref mut map) = record {
                                for field in &table_config.struct_config.fields {
                                    if !map.contains_key(&field.field_name) {
                                        // This is a new field, generate value
                                        let new_value =
                                            Self::generate_field_value(field, table_config);
                                        map.insert(field.field_name.clone(), new_value);
                                    }
                                }
                            }

                            result.push(record);
                        }

                        // Generate additional records if needed
                        if target_count > existing_count {
                            let additional = self.generate_new_records(
                                table_name,
                                table_config,
                                target_count - existing_count,
                            );
                            result.extend(additional);
                        }
                    }
                } else {
                    result = self.generate_new_records(table_name, table_config, target_count);
                }
            }
        }

        result
    }

    /// Generate new records for a table
    fn generate_new_records(
        &self,
        _table_name: &str,
        table_config: &TableConfig,
        count: usize,
    ) -> Vec<serde_json::Value> {
        use serde_json::Value;

        let mut records = Vec::new();

        for _ in 0..count {
            let mut record = serde_json::Map::new();

            // Generate values for each field
            for field in &table_config.struct_config.fields {
                let value = Self::generate_field_value(field, table_config);
                record.insert(field.field_name.clone(), value);
            }

            records.push(Value::Object(record));
        }

        records
    }

    /// Generate a value for a specific field
    fn generate_field_value(
        field: &crate::types::StructField,
        _table_config: &TableConfig,
    ) -> serde_json::Value {
        use crate::types::FieldType;
        use serde_json::json;

        // Use format if available
        if let Some(format) = &field.format {
            let value = format.generate_formatted_value();

            // Check if the format generates numeric values
            match format {
                crate::schemasync::mockmake::format::Format::Percentage
                | crate::schemasync::mockmake::format::Format::Latitude
                | crate::schemasync::mockmake::format::Format::Longitude
                | crate::schemasync::mockmake::format::Format::CurrencyAmount => {
                    // Try to parse as number
                    if let Ok(num) = value.parse::<f64>() {
                        return json!(num);
                    }
                }
                _ => {}
            }

            return json!(value);
        }

        // Generate based on field type
        match &field.field_type {
            FieldType::String => json!(crate::schemasync::Mockmaker::random_string(8)),
            FieldType::Bool => json!(rand::random::<bool>()),
            FieldType::U8
            | FieldType::U16
            | FieldType::U32
            | FieldType::U64
            | FieldType::U128
            | FieldType::Usize => json!(rand::random::<u32>() % 100),
            FieldType::I8
            | FieldType::I16
            | FieldType::I32
            | FieldType::I64
            | FieldType::I128
            | FieldType::Isize => json!(rand::random::<i32>() % 100),
            FieldType::F32 | FieldType::F64 => json!(rand::random::<f64>() * 100.0),
            FieldType::Option(inner) => {
                if rand::random::<bool>() {
                    let inner_field = crate::types::StructField {
                        field_name: field.field_name.clone(),
                        field_type: *inner.clone(),
                        format: field.format.clone(),
                        ..Default::default()
                    };
                    Self::generate_field_value(&inner_field, _table_config)
                } else {
                    json!(null)
                }
            }
            FieldType::Vec(_) => json!([]),
            FieldType::Other(type_name) => {
                // Handle common types
                if type_name.contains("DateTime") {
                    json!(chrono::Utc::now().to_rfc3339())
                } else {
                    json!(format!("{}:1", type_name.to_lowercase()))
                }
            }
            _ => json!(null),
        }
    }
}