data-modelling-core 2.4.0

Core SDK library for model operations across platforms
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
//! ODCS exporter for generating ODCS v3.1.0 YAML from data models.
//!
//! This module exports data models to ODCS (Open Data Contract Standard) v3.1.0 format only.
//! Legacy ODCL formats are no longer supported for export.

use super::{ExportError, ExportResult};
use crate::models::{Column, DataModel, Table};
use serde_yaml;
use std::collections::HashMap;

/// Extract $ref path from column relationships.
/// Returns the first foreignKey relationship as a $ref path.
fn get_ref_path_from_relationships(column: &Column) -> Option<String> {
    column.relationships.iter().find_map(|rel| {
        if rel.relationship_type == "foreignKey" {
            // Convert back to $ref format
            Some(format!("#/{}", rel.to))
        } else {
            None
        }
    })
}

/// Exporter for ODCS (Open Data Contract Standard) v3.1.0 YAML format.
pub struct ODCLExporter;

impl ODCLExporter {
    /// Export a table to ODCS v3.1.0 YAML format.
    ///
    /// Note: Only ODCS v3.1.0 format is supported. Legacy formats have been removed.
    pub fn export_table(table: &Table, _format: &str) -> String {
        // All exports use ODCS v3.1.0 format
        Self::export_odcs_v3_1_0_format(table)
    }

    /// Helper to convert serde_json::Value to serde_yaml::Value
    fn json_to_yaml_value(json: &serde_json::Value) -> serde_yaml::Value {
        match json {
            serde_json::Value::Null => serde_yaml::Value::Null,
            serde_json::Value::Bool(b) => serde_yaml::Value::Bool(*b),
            serde_json::Value::Number(n) => {
                if let Some(i) = n.as_i64() {
                    serde_yaml::Value::Number(serde_yaml::Number::from(i))
                } else if let Some(f) = n.as_f64() {
                    serde_yaml::Value::Number(serde_yaml::Number::from(f))
                } else {
                    serde_yaml::Value::String(n.to_string())
                }
            }
            serde_json::Value::String(s) => serde_yaml::Value::String(s.clone()),
            serde_json::Value::Array(arr) => {
                let yaml_arr: Vec<serde_yaml::Value> =
                    arr.iter().map(Self::json_to_yaml_value).collect();
                serde_yaml::Value::Sequence(yaml_arr)
            }
            serde_json::Value::Object(obj) => {
                let mut yaml_map = serde_yaml::Mapping::new();
                for (k, v) in obj {
                    yaml_map.insert(
                        serde_yaml::Value::String(k.clone()),
                        Self::json_to_yaml_value(v),
                    );
                }
                serde_yaml::Value::Mapping(yaml_map)
            }
        }
    }

    /// Export in ODCS v3.1.0 format (the only supported export format).
    fn export_odcs_v3_1_0_format(table: &Table) -> String {
        let mut yaml = serde_yaml::Mapping::new();

        // Required ODCS v3.1.0 fields
        yaml.insert(
            serde_yaml::Value::String("apiVersion".to_string()),
            serde_yaml::Value::String("v3.1.0".to_string()),
        );
        yaml.insert(
            serde_yaml::Value::String("kind".to_string()),
            serde_yaml::Value::String("DataContract".to_string()),
        );

        // ID - use table UUID (ODCS spec: "A unique identifier used to reduce the risk of dataset name collisions, such as a UUID.")
        yaml.insert(
            serde_yaml::Value::String("id".to_string()),
            serde_yaml::Value::String(table.id.to_string()),
        );

        // Name
        yaml.insert(
            serde_yaml::Value::String("name".to_string()),
            serde_yaml::Value::String(table.name.clone()),
        );

        // Version - from metadata or default
        let version = table
            .odcl_metadata
            .get("version")
            .and_then(|v| v.as_str())
            .map(|s| s.to_string())
            .unwrap_or_else(|| "1.0.0".to_string());
        yaml.insert(
            serde_yaml::Value::String("version".to_string()),
            serde_yaml::Value::String(version),
        );

        // Status - from metadata or default
        if let Some(status) = table.odcl_metadata.get("status")
            && !status.is_null()
        {
            yaml.insert(
                serde_yaml::Value::String("status".to_string()),
                Self::json_to_yaml_value(status),
            );
        }

        // Domain - from metadata
        if let Some(domain) = table.odcl_metadata.get("domain")
            && !domain.is_null()
        {
            yaml.insert(
                serde_yaml::Value::String("domain".to_string()),
                Self::json_to_yaml_value(domain),
            );
        }

        // Data Product - from metadata
        if let Some(data_product) = table.odcl_metadata.get("dataProduct")
            && !data_product.is_null()
        {
            yaml.insert(
                serde_yaml::Value::String("dataProduct".to_string()),
                Self::json_to_yaml_value(data_product),
            );
        }

        // Tenant - from metadata
        if let Some(tenant) = table.odcl_metadata.get("tenant")
            && !tenant.is_null()
        {
            yaml.insert(
                serde_yaml::Value::String("tenant".to_string()),
                Self::json_to_yaml_value(tenant),
            );
        }

        // Description - from metadata (can be object or string)
        if let Some(description) = table.odcl_metadata.get("description")
            && !description.is_null()
        {
            yaml.insert(
                serde_yaml::Value::String("description".to_string()),
                Self::json_to_yaml_value(description),
            );
        }

        // Tags
        if !table.tags.is_empty() {
            let tags_yaml: Vec<serde_yaml::Value> = table
                .tags
                .iter()
                .map(|t| serde_yaml::Value::String(t.to_string()))
                .collect();
            yaml.insert(
                serde_yaml::Value::String("tags".to_string()),
                serde_yaml::Value::Sequence(tags_yaml),
            );
        }

        // Team - from metadata
        if let Some(team) = table.odcl_metadata.get("team")
            && !team.is_null()
        {
            yaml.insert(
                serde_yaml::Value::String("team".to_string()),
                Self::json_to_yaml_value(team),
            );
        }

        // Roles - from metadata
        if let Some(roles) = table.odcl_metadata.get("roles")
            && !roles.is_null()
        {
            yaml.insert(
                serde_yaml::Value::String("roles".to_string()),
                Self::json_to_yaml_value(roles),
            );
        }

        // Pricing - from metadata (ODCS uses "price")
        if let Some(pricing) = table.odcl_metadata.get("pricing")
            && !pricing.is_null()
        {
            yaml.insert(
                serde_yaml::Value::String("price".to_string()),
                Self::json_to_yaml_value(pricing),
            );
        }

        // Terms - from metadata
        if let Some(terms) = table.odcl_metadata.get("terms")
            && !terms.is_null()
        {
            yaml.insert(
                serde_yaml::Value::String("terms".to_string()),
                Self::json_to_yaml_value(terms),
            );
        }

        // Servers - from metadata
        if let Some(servers) = table.odcl_metadata.get("servers")
            && !servers.is_null()
        {
            yaml.insert(
                serde_yaml::Value::String("servers".to_string()),
                Self::json_to_yaml_value(servers),
            );
        }

        // Service Levels - from metadata
        if let Some(servicelevels) = table.odcl_metadata.get("servicelevels")
            && !servicelevels.is_null()
        {
            yaml.insert(
                serde_yaml::Value::String("servicelevels".to_string()),
                Self::json_to_yaml_value(servicelevels),
            );
        }

        // Links - from metadata
        if let Some(links) = table.odcl_metadata.get("links")
            && !links.is_null()
        {
            yaml.insert(
                serde_yaml::Value::String("links".to_string()),
                Self::json_to_yaml_value(links),
            );
        }

        // Infrastructure - from metadata
        if let Some(infrastructure) = table.odcl_metadata.get("infrastructure")
            && !infrastructure.is_null()
        {
            yaml.insert(
                serde_yaml::Value::String("infrastructure".to_string()),
                Self::json_to_yaml_value(infrastructure),
            );
        }

        // Schema array (ODCS v3.1.0 uses array of SchemaObject)
        let mut schema_array = Vec::new();
        let mut schema_obj = serde_yaml::Mapping::new();

        schema_obj.insert(
            serde_yaml::Value::String("name".to_string()),
            serde_yaml::Value::String(table.name.clone()),
        );

        // Build properties from columns
        let mut properties = serde_yaml::Mapping::new();

        // Helper function to build nested properties structure
        fn build_nested_properties(
            parent_name: &str,
            all_columns: &[crate::models::Column],
            json_to_yaml_fn: &dyn Fn(&serde_json::Value) -> serde_yaml::Value,
        ) -> Option<serde_yaml::Mapping> {
            let parent_prefix = format!("{}.", parent_name);
            let nested_columns: Vec<&crate::models::Column> = all_columns
                .iter()
                .filter(|col| col.name.starts_with(&parent_prefix))
                .collect();

            if nested_columns.is_empty() {
                return None;
            }

            let mut nested_props = serde_yaml::Mapping::new();

            // Group nested columns by their immediate child name (first level only)
            let mut child_map: std::collections::HashMap<String, Vec<&crate::models::Column>> =
                std::collections::HashMap::new();

            for nested_col in &nested_columns {
                // Safety: skip columns that don't start with the expected prefix
                let Some(relative_name) = nested_col.name.strip_prefix(&parent_prefix) else {
                    continue;
                };
                if let Some(dot_pos) = relative_name.find('.') {
                    let child_name = &relative_name[..dot_pos];
                    child_map
                        .entry(child_name.to_string())
                        .or_default()
                        .push(nested_col);
                } else {
                    // Direct child - add to map
                    child_map
                        .entry(relative_name.to_string())
                        .or_default()
                        .push(nested_col);
                }
            }

            // Build properties for each child
            for (child_name, child_cols) in child_map {
                // Find the direct child column (no dots in relative name)
                let direct_child = child_cols.iter().find(|col| {
                    col.name
                        .strip_prefix(&parent_prefix)
                        .map(|rel_name| !rel_name.contains('.'))
                        .unwrap_or(false)
                });

                if let Some(child_col) = direct_child {
                    let mut child_prop = serde_yaml::Mapping::new();

                    // Check if this child has nested children
                    let child_has_nested = child_cols.iter().any(|col| {
                        col.name
                            .strip_prefix(&parent_prefix)
                            .map(|rel| {
                                rel.starts_with(&format!("{}.", child_name)) && rel != child_name
                            })
                            .unwrap_or(false)
                    });

                    // Handle ARRAY<OBJECT> or ARRAY<STRUCT> types
                    let data_type_upper = child_col.data_type.to_uppercase();
                    let is_array_object = data_type_upper.starts_with("ARRAY<")
                        && (data_type_upper.contains("OBJECT")
                            || data_type_upper.contains("STRUCT"));
                    let is_struct_or_object = data_type_upper == "STRUCT"
                        || data_type_upper == "OBJECT"
                        || data_type_upper.starts_with("STRUCT<");

                    // Try to build nested properties first (regardless of type)
                    let nested_props_map =
                        build_nested_properties(&child_col.name, all_columns, json_to_yaml_fn);

                    if is_array_object && (child_has_nested || nested_props_map.is_some()) {
                        // ARRAY<OBJECT> with nested fields
                        child_prop.insert(
                            serde_yaml::Value::String("type".to_string()),
                            serde_yaml::Value::String("array".to_string()),
                        );

                        let mut items = serde_yaml::Mapping::new();
                        items.insert(
                            serde_yaml::Value::String("type".to_string()),
                            serde_yaml::Value::String("object".to_string()),
                        );

                        // Add nested properties if they exist
                        if let Some(nested_props) = nested_props_map {
                            items.insert(
                                serde_yaml::Value::String("properties".to_string()),
                                serde_yaml::Value::Mapping(nested_props),
                            );
                        }

                        child_prop.insert(
                            serde_yaml::Value::String("items".to_string()),
                            serde_yaml::Value::Mapping(items),
                        );
                    } else if is_struct_or_object || child_has_nested || nested_props_map.is_some()
                    {
                        // OBJECT/STRUCT with nested properties, or any column with nested children
                        child_prop.insert(
                            serde_yaml::Value::String("type".to_string()),
                            serde_yaml::Value::String("object".to_string()),
                        );

                        // Add nested properties if they exist
                        if let Some(nested_props) = nested_props_map {
                            child_prop.insert(
                                serde_yaml::Value::String("properties".to_string()),
                                serde_yaml::Value::Mapping(nested_props),
                            );
                        }
                    } else {
                        // Simple field
                        child_prop.insert(
                            serde_yaml::Value::String("type".to_string()),
                            serde_yaml::Value::String(child_col.data_type.clone().to_lowercase()),
                        );
                    }

                    if !child_col.nullable {
                        child_prop.insert(
                            serde_yaml::Value::String("required".to_string()),
                            serde_yaml::Value::Bool(true),
                        );
                    }

                    if !child_col.description.is_empty() {
                        child_prop.insert(
                            serde_yaml::Value::String("description".to_string()),
                            serde_yaml::Value::String(child_col.description.clone()),
                        );
                    }

                    // Export column-level quality rules for nested columns
                    if !child_col.quality.is_empty() {
                        let quality_array: Vec<serde_yaml::Value> = child_col
                            .quality
                            .iter()
                            .map(|rule| {
                                let mut rule_map = serde_yaml::Mapping::new();
                                for (k, v) in rule {
                                    rule_map.insert(
                                        serde_yaml::Value::String(k.clone()),
                                        json_to_yaml_fn(v),
                                    );
                                }
                                serde_yaml::Value::Mapping(rule_map)
                            })
                            .collect();
                        child_prop.insert(
                            serde_yaml::Value::String("quality".to_string()),
                            serde_yaml::Value::Sequence(quality_array),
                        );
                    }

                    // Export $ref reference for nested columns if present (from relationships)
                    if let Some(ref_path) = get_ref_path_from_relationships(child_col) {
                        child_prop.insert(
                            serde_yaml::Value::String("$ref".to_string()),
                            serde_yaml::Value::String(ref_path),
                        );
                    }

                    // Export enum values for nested columns
                    if !child_col.enum_values.is_empty() {
                        let enum_yaml: Vec<serde_yaml::Value> = child_col
                            .enum_values
                            .iter()
                            .map(|e| serde_yaml::Value::String(e.clone()))
                            .collect();
                        child_prop.insert(
                            serde_yaml::Value::String("enum".to_string()),
                            serde_yaml::Value::Sequence(enum_yaml),
                        );
                    }

                    // Export constraints for nested columns
                    if !child_col.constraints.is_empty() {
                        let constraints_yaml: Vec<serde_yaml::Value> = child_col
                            .constraints
                            .iter()
                            .map(|c| serde_yaml::Value::String(c.clone()))
                            .collect();
                        child_prop.insert(
                            serde_yaml::Value::String("constraints".to_string()),
                            serde_yaml::Value::Sequence(constraints_yaml),
                        );
                    }

                    // Export foreign key for nested columns
                    if let Some(ref fk) = child_col.foreign_key {
                        let mut fk_map = serde_yaml::Mapping::new();
                        fk_map.insert(
                            serde_yaml::Value::String("table".to_string()),
                            serde_yaml::Value::String(fk.table_id.clone()),
                        );
                        fk_map.insert(
                            serde_yaml::Value::String("column".to_string()),
                            serde_yaml::Value::String(fk.column_name.clone()),
                        );
                        child_prop.insert(
                            serde_yaml::Value::String("foreignKey".to_string()),
                            serde_yaml::Value::Mapping(fk_map),
                        );
                    }

                    nested_props.insert(
                        serde_yaml::Value::String(child_name),
                        serde_yaml::Value::Mapping(child_prop),
                    );
                }
            }

            if nested_props.is_empty() {
                None
            } else {
                Some(nested_props)
            }
        }

        for column in &table.columns {
            // Skip nested columns (they're handled as part of parent columns)
            if column.name.contains('.') {
                continue;
            }

            let mut prop = serde_yaml::Mapping::new();

            // Check if this column has nested columns
            let has_nested = table.columns.iter().any(|col| {
                col.name.starts_with(&format!("{}.", column.name)) && col.name != column.name
            });

            // Determine the type - handle ARRAY<OBJECT>, STRUCT, OBJECT, etc.
            let data_type_upper = column.data_type.to_uppercase();
            let is_array_object = data_type_upper.starts_with("ARRAY<")
                && (data_type_upper.contains("OBJECT") || data_type_upper.contains("STRUCT"));
            let is_struct_or_object = data_type_upper == "STRUCT"
                || data_type_upper == "OBJECT"
                || data_type_upper.starts_with("STRUCT<");

            // Always check for nested properties if nested columns exist
            if has_nested {
                // Try to build nested properties first
                let nested_props = build_nested_properties(
                    &column.name,
                    &table.columns,
                    &Self::json_to_yaml_value,
                );

                if is_array_object {
                    // ARRAY<OBJECT> with nested fields
                    prop.insert(
                        serde_yaml::Value::String("type".to_string()),
                        serde_yaml::Value::String("array".to_string()),
                    );

                    let mut items = serde_yaml::Mapping::new();
                    items.insert(
                        serde_yaml::Value::String("type".to_string()),
                        serde_yaml::Value::String("object".to_string()),
                    );

                    // Add nested properties if they exist
                    if let Some(nested_props_map) = nested_props {
                        items.insert(
                            serde_yaml::Value::String("properties".to_string()),
                            serde_yaml::Value::Mapping(nested_props_map),
                        );
                    }

                    prop.insert(
                        serde_yaml::Value::String("items".to_string()),
                        serde_yaml::Value::Mapping(items),
                    );
                } else if is_struct_or_object || nested_props.is_some() {
                    // OBJECT/STRUCT with nested fields, or any column with nested columns
                    prop.insert(
                        serde_yaml::Value::String("type".to_string()),
                        serde_yaml::Value::String("object".to_string()),
                    );

                    // Add nested properties if they exist
                    if let Some(nested_props_map) = nested_props {
                        prop.insert(
                            serde_yaml::Value::String("properties".to_string()),
                            serde_yaml::Value::Mapping(nested_props_map),
                        );
                    }
                } else {
                    // Has nested columns but couldn't build structure - use simple type
                    prop.insert(
                        serde_yaml::Value::String("type".to_string()),
                        serde_yaml::Value::String(column.data_type.clone().to_lowercase()),
                    );
                }
            } else {
                // No nested columns - use simple type
                prop.insert(
                    serde_yaml::Value::String("type".to_string()),
                    serde_yaml::Value::String(column.data_type.clone().to_lowercase()),
                );
            }

            if !column.nullable {
                prop.insert(
                    serde_yaml::Value::String("required".to_string()),
                    serde_yaml::Value::Bool(true),
                );
            }

            if column.primary_key {
                prop.insert(
                    serde_yaml::Value::String("primaryKey".to_string()),
                    serde_yaml::Value::Bool(true),
                );
            }

            if column.secondary_key {
                prop.insert(
                    serde_yaml::Value::String("businessKey".to_string()),
                    serde_yaml::Value::Bool(true),
                );
            }

            if !column.description.is_empty() {
                prop.insert(
                    serde_yaml::Value::String("description".to_string()),
                    serde_yaml::Value::String(column.description.clone()),
                );
            }

            // Export column-level quality rules
            if !column.quality.is_empty() {
                let quality_array: Vec<serde_yaml::Value> = column
                    .quality
                    .iter()
                    .map(|rule| {
                        let mut rule_map = serde_yaml::Mapping::new();
                        for (k, v) in rule {
                            rule_map.insert(
                                serde_yaml::Value::String(k.clone()),
                                Self::json_to_yaml_value(v),
                            );
                        }
                        serde_yaml::Value::Mapping(rule_map)
                    })
                    .collect();
                prop.insert(
                    serde_yaml::Value::String("quality".to_string()),
                    serde_yaml::Value::Sequence(quality_array),
                );
            }

            // Export $ref reference if present (from relationships)
            if let Some(ref_path) = get_ref_path_from_relationships(column) {
                prop.insert(
                    serde_yaml::Value::String("$ref".to_string()),
                    serde_yaml::Value::String(ref_path),
                );
            }

            // Export enum values
            if !column.enum_values.is_empty() {
                let enum_yaml: Vec<serde_yaml::Value> = column
                    .enum_values
                    .iter()
                    .map(|e| serde_yaml::Value::String(e.clone()))
                    .collect();
                prop.insert(
                    serde_yaml::Value::String("enum".to_string()),
                    serde_yaml::Value::Sequence(enum_yaml),
                );
            }

            // Export constraints
            if !column.constraints.is_empty() {
                let constraints_yaml: Vec<serde_yaml::Value> = column
                    .constraints
                    .iter()
                    .map(|c| serde_yaml::Value::String(c.clone()))
                    .collect();
                prop.insert(
                    serde_yaml::Value::String("constraints".to_string()),
                    serde_yaml::Value::Sequence(constraints_yaml),
                );
            }

            // Export foreign key
            if let Some(ref fk) = column.foreign_key {
                let mut fk_map = serde_yaml::Mapping::new();
                fk_map.insert(
                    serde_yaml::Value::String("table".to_string()),
                    serde_yaml::Value::String(fk.table_id.clone()),
                );
                fk_map.insert(
                    serde_yaml::Value::String("column".to_string()),
                    serde_yaml::Value::String(fk.column_name.clone()),
                );
                prop.insert(
                    serde_yaml::Value::String("foreignKey".to_string()),
                    serde_yaml::Value::Mapping(fk_map),
                );
            }

            properties.insert(
                serde_yaml::Value::String(column.name.clone()),
                serde_yaml::Value::Mapping(prop),
            );
        }

        schema_obj.insert(
            serde_yaml::Value::String("properties".to_string()),
            serde_yaml::Value::Mapping(properties),
        );

        schema_array.push(serde_yaml::Value::Mapping(schema_obj));
        yaml.insert(
            serde_yaml::Value::String("schema".to_string()),
            serde_yaml::Value::Sequence(schema_array),
        );

        // Table-level quality rules
        if !table.quality.is_empty() {
            let quality_array: Vec<serde_yaml::Value> = table
                .quality
                .iter()
                .map(|rule| {
                    let mut rule_map = serde_yaml::Mapping::new();
                    for (k, v) in rule {
                        rule_map.insert(
                            serde_yaml::Value::String(k.clone()),
                            Self::json_to_yaml_value(v),
                        );
                    }
                    serde_yaml::Value::Mapping(rule_map)
                })
                .collect();
            yaml.insert(
                serde_yaml::Value::String("quality".to_string()),
                serde_yaml::Value::Sequence(quality_array),
            );
        }

        // Custom Properties from metadata (excluding already exported fields)
        let excluded_keys = [
            "id",
            "version",
            "status",
            "domain",
            "dataProduct",
            "tenant",
            "description",
            "team",
            "roles",
            "pricing",
            "terms",
            "servers",
            "servicelevels",
            "links",
            "apiVersion",
            "kind",
            "info",
            "dataContractSpecification",
        ];

        let mut custom_props = Vec::new();
        for (key, value) in &table.odcl_metadata {
            if !excluded_keys.contains(&key.as_str()) && !value.is_null() {
                let mut prop = serde_yaml::Mapping::new();
                prop.insert(
                    serde_yaml::Value::String("property".to_string()),
                    serde_yaml::Value::String(key.clone()),
                );
                prop.insert(
                    serde_yaml::Value::String("value".to_string()),
                    Self::json_to_yaml_value(value),
                );
                custom_props.push(serde_yaml::Value::Mapping(prop));
            }
        }

        // Add database type as custom property if present
        if let Some(ref db_type) = table.database_type {
            let mut prop = serde_yaml::Mapping::new();
            prop.insert(
                serde_yaml::Value::String("property".to_string()),
                serde_yaml::Value::String("databaseType".to_string()),
            );
            prop.insert(
                serde_yaml::Value::String("value".to_string()),
                serde_yaml::Value::String(format!("{:?}", db_type)),
            );
            custom_props.push(serde_yaml::Value::Mapping(prop));
        }

        // Add medallion layers as custom property if present
        if !table.medallion_layers.is_empty() {
            let layers: Vec<serde_yaml::Value> = table
                .medallion_layers
                .iter()
                .map(|l| serde_yaml::Value::String(format!("{:?}", l)))
                .collect();
            let mut prop = serde_yaml::Mapping::new();
            prop.insert(
                serde_yaml::Value::String("property".to_string()),
                serde_yaml::Value::String("medallionLayers".to_string()),
            );
            prop.insert(
                serde_yaml::Value::String("value".to_string()),
                serde_yaml::Value::Sequence(layers),
            );
            custom_props.push(serde_yaml::Value::Mapping(prop));
        }

        // Add SCD pattern as custom property if present
        if let Some(ref scd_pattern) = table.scd_pattern {
            let mut prop = serde_yaml::Mapping::new();
            prop.insert(
                serde_yaml::Value::String("property".to_string()),
                serde_yaml::Value::String("scdPattern".to_string()),
            );
            prop.insert(
                serde_yaml::Value::String("value".to_string()),
                serde_yaml::Value::String(format!("{:?}", scd_pattern)),
            );
            custom_props.push(serde_yaml::Value::Mapping(prop));
        }

        // Add Data Vault classification as custom property if present
        if let Some(ref dv_class) = table.data_vault_classification {
            let mut prop = serde_yaml::Mapping::new();
            prop.insert(
                serde_yaml::Value::String("property".to_string()),
                serde_yaml::Value::String("dataVaultClassification".to_string()),
            );
            prop.insert(
                serde_yaml::Value::String("value".to_string()),
                serde_yaml::Value::String(format!("{:?}", dv_class)),
            );
            custom_props.push(serde_yaml::Value::Mapping(prop));
        }

        // Add catalog/schema names as custom properties if present
        if let Some(ref catalog) = table.catalog_name {
            let mut prop = serde_yaml::Mapping::new();
            prop.insert(
                serde_yaml::Value::String("property".to_string()),
                serde_yaml::Value::String("catalogName".to_string()),
            );
            prop.insert(
                serde_yaml::Value::String("value".to_string()),
                serde_yaml::Value::String(catalog.clone()),
            );
            custom_props.push(serde_yaml::Value::Mapping(prop));
        }

        if let Some(ref schema) = table.schema_name {
            let mut prop = serde_yaml::Mapping::new();
            prop.insert(
                serde_yaml::Value::String("property".to_string()),
                serde_yaml::Value::String("schemaName".to_string()),
            );
            prop.insert(
                serde_yaml::Value::String("value".to_string()),
                serde_yaml::Value::String(schema.clone()),
            );
            custom_props.push(serde_yaml::Value::Mapping(prop));
        }

        if !custom_props.is_empty() {
            yaml.insert(
                serde_yaml::Value::String("customProperties".to_string()),
                serde_yaml::Value::Sequence(custom_props),
            );
        }

        // Contract created timestamp
        yaml.insert(
            serde_yaml::Value::String("contractCreatedTs".to_string()),
            serde_yaml::Value::String(table.created_at.to_rfc3339()),
        );

        serde_yaml::to_string(&yaml).unwrap_or_default()
    }

    /// Export tables to ODCS v3.1.0 YAML format (SDK interface).
    pub fn export(
        &self,
        tables: &[Table],
        _format: &str,
    ) -> Result<HashMap<String, ExportResult>, ExportError> {
        let mut exports = HashMap::new();
        for table in tables {
            // All exports use ODCS v3.1.0 format
            let yaml = Self::export_odcs_v3_1_0_format(table);
            exports.insert(
                table.name.clone(),
                ExportResult {
                    content: yaml,
                    format: "odcs_v3_1_0".to_string(),
                },
            );
        }
        Ok(exports)
    }

    /// Export a data model to ODCS v3.1.0 YAML format (legacy method for compatibility).
    pub fn export_model(
        model: &DataModel,
        table_ids: Option<&[uuid::Uuid]>,
        _format: &str,
    ) -> HashMap<String, String> {
        let tables_to_export: Vec<&Table> = if let Some(ids) = table_ids {
            model
                .tables
                .iter()
                .filter(|t| ids.contains(&t.id))
                .collect()
        } else {
            model.tables.iter().collect()
        };

        let mut exports = HashMap::new();
        for table in tables_to_export {
            // All exports use ODCS v3.1.0 format
            let yaml = Self::export_odcs_v3_1_0_format(table);
            exports.insert(table.name.clone(), yaml);
        }

        exports
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::export::odcs::ODCSExporter;
    use crate::models::{Column, Tag};

    #[test]
    fn test_export_odcs_v3_1_0_basic() {
        let table = Table {
            id: Table::generate_id("test_table", None, None, None),
            name: "test_table".to_string(),
            columns: vec![Column {
                name: "id".to_string(),
                data_type: "BIGINT".to_string(),
                nullable: false,
                primary_key: true,
                description: "Primary key".to_string(),
                ..Default::default()
            }],
            database_type: None,
            catalog_name: None,
            schema_name: None,
            medallion_layers: Vec::new(),
            scd_pattern: None,
            data_vault_classification: None,
            modeling_level: None,
            tags: vec![Tag::Simple("test".to_string())],
            odcl_metadata: HashMap::new(),
            owner: None,
            sla: None,
            contact_details: None,
            infrastructure_type: None,
            notes: None,
            position: None,
            yaml_file_path: None,
            drawio_cell_id: None,
            quality: Vec::new(),
            errors: Vec::new(),
            created_at: chrono::Utc::now(),
            updated_at: chrono::Utc::now(),
        };

        let yaml = ODCSExporter::export_table(&table, "odcs_v3_1_0");

        assert!(yaml.contains("apiVersion: v3.1.0"));
        assert!(yaml.contains("kind: DataContract"));
        assert!(yaml.contains("name: test_table"));
        assert!(yaml.contains("tags:"));
        assert!(yaml.contains("- test"));
    }
}